Git Product home page Git Product logo

function-cel-filter's Introduction

function-cel-filter

CI

A composition function that filters matching composed resources using CEL expressions.

Each filter:

  • Matches composed resources by name using a regular expression.
  • Specifies whether resources should be included using a CEL expression.

If a filter's CEL expression evaluates to true, Crossplane creates the matching composed resources.

Filters only apply to matching composed resources. The function doesn't filter composed resources that don't match a filter.

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: function-template-go
spec:
  compositeTypeRef:
    apiVersion: example.crossplane.io/v1
    kind: NoSQL
  mode: Pipeline
  pipeline:
  - step: patch-and-transform
    functionRef:
      name: function-patch-and-transform
    input:
      apiVersion: pt.fn.crossplane.io/v1beta1
      kind: Resources
      resources:
      - name: table
        base:
          apiVersion: dynamodb.aws.upbound.io/v1beta1
          kind: Table
          metadata:
            name: crossplane-quickstart-database
          spec:
            forProvider:
              region: "us-east-2"
              writeCapacity: 1
              readCapacity: 1
              attribute:
                - name: S3ID
                  type: S
              hashKey: S3ID
      - name: bucket
        base:
          apiVersion: s3.aws.upbound.io/v1beta1
          kind: Bucket
          spec:
            forProvider:
              region: us-east-2
  - step: filter-composed-resources
    functionRef:
      name: function-cel-filter
    input:
      apiVersion: cel.fn.crossplane.io/v1beta1
      kind: Filters
      filters:
      # Only create the bucket if the XR's spec.export field is set to "S3".
      - name: bucket
        expression: observed.composite.resource.spec.export == "S3"

The following top-level variables are available to the CEL expression:

  • observed
  • desired
  • context

Example expressions:

  • observed.composite.resource.spec.widgets == 42
  • observed.resources['bucket'].connection_details['user'] == b'admin'
  • desired.resources['bucket'].resource.spec.widgets == 42
  • has(desired.resources.bucket)

Expressions must evaluate to true for the composed resource to be included.

See the RunFunctionRequest protobuf message for schema details. The introduction to CEL documentation shows more example expressions.

function-cel-filter's People

Contributors

negz avatar renovate[bot] avatar

Stargazers

J.M. Fernández avatar

Watchers

Steven Borrelli avatar  avatar  avatar

Forkers

negz aerfio

function-cel-filter's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

crossplane
example/functions.yaml
  • xpkg.upbound.io/crossplane-contrib/function-patch-and-transform v0.5.0
dockerfile
Dockerfile
  • docker/dockerfile 1
  • golang 1
github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-go v5
  • golangci/golangci-lint-action v6
  • actions/checkout v4
  • actions/setup-go v5
  • docker/setup-qemu-action v3
  • docker/setup-buildx-action v3
  • actions/checkout v4
  • docker/build-push-action v5
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/download-artifact v4
  • docker/login-action v3
  • ubuntu 22.04
  • ubuntu 22.04
  • ubuntu 22.04
  • ubuntu 22.04
gomod
go.mod
  • go 1.21
  • go 1.22.3
  • github.com/alecthomas/kong v0.9.0
  • github.com/crossplane/crossplane-runtime v1.15.1
  • github.com/crossplane/function-sdk-go v0.2.0
  • github.com/google/cel-go v0.20.1
  • github.com/google/go-cmp v0.6.0
  • google.golang.org/protobuf v1.34.1
  • k8s.io/apimachinery v0.29.3
  • sigs.k8s.io/controller-tools v0.14.0

  • Check this box to trigger a request for Renovate to run again on this repository

Cannot filter on desired resources selected by square brackets

What happened?

I tried to filter resources based on existence of another resource. It seems to be supported, as this scenario is documented in root README.md as has(desired.resources.bucket). This filter works, but once you switch to the one using [ and ], for example when the name has a - in it it fails with:

crossplane: error: cannot render composite resource: pipeline step "filter-composed-resources" returned a fatal result: cannot evaluate CEL expression "has(desired.resources['table'])" for filter 0: cannot parse expression: ERROR: <input>:1:22: invalid argument to has() macro
                    | has(desired.resources['table'])
                    | .....................^

How can we reproduce it?

diff --git a/example/composition.yaml b/example/composition.yaml
index 660c65d..209ed7b 100644
--- a/example/composition.yaml
+++ b/example/composition.yaml
@@ -46,4 +46,4 @@ spec:
         filters:
           # Only create the bucket if the XR's spec.export field is set to "S3".
           - name: bucket
-            expression: observed.composite.resource.spec.export == "S3"
+            expression: has(desired.resources['table'])

Apply this patch and run the command from ./example/README.md. Then change it like that

- desired.resources['table']
+ desired.resources.table

And see it pass through

What environment did it happen in?

Function version:
Tested on both v0.1.1 and also current main (58e86dc), exact command used to run Function:

go run . --insecure --debug

Exact package when using v0.1.1: xpkg.upbound.io/crossplane-contrib/function-cel-filter:v0.1.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.