Skip to content

Commit

Permalink
Add request.body in CEL notification filtering
Browse files Browse the repository at this point in the history
This moves the JSON body to request.body from request to allow for
future expansion with the headers.

Add documentation

Signed-off-by: Kevin McDermott <[email protected]>
  • Loading branch information
bigkevmcd committed Oct 13, 2024
1 parent b1c4e7e commit 1e6929c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
49 changes: 49 additions & 0 deletions docs/spec/v1/receivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,55 @@ resources:
**Note:** Cross-namespace references [can be disabled for security
reasons](#disabling-cross-namespace-selectors).

#### Filtering reconciled objects with CEL

To filter the resources that are reconciled you can use [Common Expression Language (CEL)](https://cel.dev/).

For example to trigger `ImageRepositories` on notifications from [Google Artifact Regisry](https://cloud.google.com/artifact-registry/docs/configure-notifications#examples) you can define a receiver.

```yaml
apiVersion: notification.toolkit.fluxcd.io/v1
kind: Receiver
metadata:
name: gar-receiver
namespace: apps
spec:
type: gcr
secretRef:
name: flux-gar-token
resources:
- apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageRepository
name: "*"
matchLabels:
registry: gar
```

This will trigger the reconciliation of all `ImageRepositories` with matching labels `registry: gar`, but if you want to only notify `ImageRepository` resources that are referenced from the incoming hook you can use CEL to filter the resources.

```yaml
apiVersion: notification.toolkit.fluxcd.io/v1
kind: Receiver
metadata:
name: gar-receiver
namespace: apps
spec:
type: gcr
secretRef:
name: flux-gar-token
resources:
- apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageRepository
name: "*"
matchLabels:
registry: gar
resourceFilter: 'request.tag.contains(resource.metadata.name)'
```

To reduce the number of `ImageRepositories` that are reconciled, you can filter them with a CEL expression.

```

### Secret reference

`.spec.secretRef.name` is a required field to specify a name reference to a
Expand Down
5 changes: 3 additions & 2 deletions internal/server/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func newCELEvaluator(expr string, req *http.Request) (resourcePredicate, error)

out, _, err := prg.Eval(map[string]any{
"resource": data,
"request": body,
"request": map[string]any{
"body": body,
},
})
if err != nil {
return nil, fmt.Errorf("expression %v failed to evaluate: %w", expr, err)
Expand All @@ -74,7 +76,6 @@ func makeCELEnv() (*cel.Env, error) {
mapStrDyn := decls.NewMapType(decls.String, decls.Dyn)
return cel.NewEnv(
celext.Strings(),
celext.Encoders(),
notifications(),
cel.Declarations(
decls.NewVar("resource", mapStrDyn),
Expand Down
4 changes: 2 additions & 2 deletions internal/server/receiver_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ func Test_handlePayload(t *testing.T) {
},
},
},
ResourceFilter: `has(resource.metadata.annotations) && request.tag.split('/').last().split(":").first() == resource.metadata.annotations['update-image']`,
ResourceFilter: `has(resource.metadata.annotations) && request.body.tag.split('/').last().split(":").first() == resource.metadata.annotations['update-image']`,
},
Status: apiv1.ReceiverStatus{
WebhookPath: apiv1.ReceiverWebhookPath,
Expand Down Expand Up @@ -878,7 +878,7 @@ func Test_handlePayload(t *testing.T) {
Name: "test-resource",
},
},
ResourceFilter: `has(resource.metadata.annotations) && request.tag.split('/').last().split(":").first() == resource.metadata.annotations['update-image']`,
ResourceFilter: `has(resource.metadata.annotations) && request.body.tag.split('/').last().split(":").first() == resource.metadata.annotations['update-image']`,
},
Status: apiv1.ReceiverStatus{
WebhookPath: apiv1.ReceiverWebhookPath,
Expand Down

0 comments on commit 1e6929c

Please sign in to comment.