Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: labeler logic #1

Merged
merged 16 commits into from
Feb 21, 2024
Merged

feat: labeler logic #1

merged 16 commits into from
Feb 21, 2024

Conversation

niklastreml
Copy link
Contributor

@niklastreml niklastreml commented Feb 8, 2024

Description

This pr implements a crossplane function, which allows any user to create a project scoped logging resource. It transforms a namespaced logging.caas.telekom.de resource into a clusterlevel logging.banzaicloud.io resource and ensure the created logging instance is scoped to the project in which the caas logging resource was created. For this two things need to be ensured:

  1. spec.controlNamespace needs to be set to the namespace in which the namespaced caas logging resource was created. This ensure the FluentD deployment gets created in the namespace of the crd
  2. spec.WatchNamespaces needs to point only to namespace inside that rancher project. This ensures that only logs from a users project are collected.

Implementation

  • Reads the namespace from a logging.caas.telekom.de CRD.
  • Retrieves the value of the label field.cattle.io/projectId from that namespace.
  • Creates a new banzaicloud logging resource.
  • Sets its spec.controlNamespace to match the namespace of the logging.caas.telekom.de.
  • Configures spec.watchNamespaceSelector.MatchLabels to {"field.cattle.io/projectId":""}.

Testing locally

Install the crossplane cli

In one bash session start the function:

kubectl config use-context <your-rancher-cluster>
go run . --insecure --debug --out-of-cluster

This runs the function locally, using your current kubectl context to lookup the namespaces.

Invoke the function with a logging.caas.telekom.de Composite Resource:

cd examples/
crossplane beta render xr.yaml composition.yaml functions.yaml
---
apiVersion: caas.telekom.de/v1alpha1
kind: Logging
metadata:
  name: example-xr
---
apiVersion: logging.banzaicloud.io/v1beta1
kind: Logging
metadata:
  annotations:
    crossplane.io/composition-resource-name: logging
  generateName: example-xr-
  labels:
    crossplane.io/composite: example-xr
  ownerReferences:
  - apiVersion: caas.telekom.de/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Logging
    name: example-xr
    uid: ""
spec:
  allowClusterResourcesFromAllNamespaces: false
  configCheck:
    timeoutSeconds: 0
  controlNamespace: microservices
  enableRecreateWorkloadOnImmutableFieldChange: false
  flowConfigCheckDisabled: false
  skipInvalidResources: false
  watchNamespaceSelector:
    matchLabels:
      field.cattle.io/projectId: caas-services
status:
  problemsCount: 0

This does not create any resources, it only invokes the function in the same way crossplane would, when actually deployed.

Running Tests

go test ./...

Example

I've deployed the function on my kind cluster. You can find an example that a user would create and its result below:

# Input
apiVersion: caas.telekom.de/v1alpha1
kind: Logging
metadata:
  name: my-logging
  namespace: xrd-test
spec: {}
# Output
apiVersion: logging.banzaicloud.io/v1beta1
kind: Logging
metadata:
  annotations:
    crossplane.io/composition-resource-name: logging
  creationTimestamp: "2024-02-08T15:21:25Z"
  generateName: my-logging-q66zj-
  generation: 1
  labels:
    crossplane.io/claim-name: my-logging
    crossplane.io/claim-namespace: xrd-test
    crossplane.io/composite: my-logging-q66zj
  name: my-logging-q66zj-5zssn
  ownerReferences:
  - apiVersion: caas.telekom.de/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: XLogging
    name: my-logging-q66zj
    uid: 0349b57d-79b3-4b08-8963-5a2eda90a5fd
  resourceVersion: "1406511"
  uid: fce5cc7d-9128-4f88-99ed-16b516bceb3e
spec:
  allowClusterResourcesFromAllNamespaces: false
  configCheck:
    timeoutSeconds: 0
  controlNamespace: xrd-test
  enableRecreateWorkloadOnImmutableFieldChange: false
  flowConfigCheckDisabled: false
  skipInvalidResources: false
  watchNamespaceSelector:
    matchLabels:
      field.cattle.io/projectId: p-12345
status:
  problems:
  - 'Deprecated behaviour! Other logging resources exist with the same loggingRef:
    hiller,kunde1,kunde2. This is going to be an error with the next major release.'
  problemsCount: 1
  watchNamespaces:
  - customer-1-ns-a
  - customer-1-ns-b
  - xrd-test

External Resources

Function Docs
Go example

@eumel8 eumel8 assigned eumel8 and unassigned eumel8 Feb 8, 2024
@eumel8 eumel8 self-requested a review February 8, 2024 10:48
@eumel8
Copy link
Member

eumel8 commented Feb 8, 2024

LGTM, nice!

@niklastreml niklastreml requested review from puffitos, lvlcn-t and eumel8 and removed request for eumel8, puffitos and lvlcn-t February 8, 2024 10:59
@niklastreml
Copy link
Contributor Author

Ready for review

@CerRegulus
Copy link

LGTM. Thank you very much 🥳 ❤️

Copy link
Member

@lvlcn-t lvlcn-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, you could move the logic into dedicated functions but for now it's readable.

fn_test.go Outdated Show resolved Hide resolved
Copy link
Member

@puffitos puffitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few remarks.

fn.go Outdated Show resolved Hide resolved
package/input/template.fn.crossplane.io_inputs.yaml Outdated Show resolved Hide resolved
fn.go Outdated Show resolved Hide resolved
@niklastreml niklastreml merged commit 042a302 into main Feb 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants