Skip to content

Commit 348c595

Browse files
committed
Address review comments
* Don't pass log through - get it from the context * Use ContextEval - need to set a timeout on the context that's passed Signed-off-by: Kevin McDermott <[email protected]>
1 parent 9d7f593 commit 348c595

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

docs/spec/v1/receivers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,15 @@ If the body of the incoming hook looks like this:
757757

758758
This simple example would match `ImageRepositories` containing the name `hello-world`.
759759

760-
If you want do do more complex processing:
760+
If you want to do more complex processing:
761761

762762
```yaml
763763
resourceFilter: has(resource.metadata.annotations) && request.body.tag.split('/').last().split(":").first() == resource.metadata.annotations['update-image']
764764
```
765765

766766
This would look for an annotation "update-image" on the resource, and match it to the `hello-world` part of the tag name.
767767

768-
**NOTE**: Currently the `resource` value in the CEL expression only provides the object metadata, this means you can access things like `resource.metadata.labels` and `resource.metadata.annotations` and `resource.metadata.name`.
768+
**Note:** Currently the `resource` value in the CEL expression only provides the object metadata, this means you can access things like `resource.metadata.labels` and `resource.metadata.annotations` and `resource.metadata.name`.
769769

770770
There are a number of functions available to the CEL expressions beyond the basic CEL functionality.
771771

internal/controller/receiver_controller.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
helper "github.com/fluxcd/pkg/runtime/controller"
4141
"github.com/fluxcd/pkg/runtime/patch"
4242
"github.com/fluxcd/pkg/runtime/predicates"
43-
"github.com/go-logr/logr"
4443

4544
apiv1 "github.com/fluxcd/notification-controller/api/v1"
4645
"github.com/fluxcd/notification-controller/internal/server"
@@ -153,12 +152,14 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
153152
return ctrl.Result{}, nil
154153
}
155154

156-
return r.reconcile(ctx, obj, log)
155+
return r.reconcile(ctx, obj)
157156
}
158157

159158
// reconcile steps through the actual reconciliation tasks for the object, it returns early on the first step that
160159
// produces an error.
161-
func (r *ReceiverReconciler) reconcile(ctx context.Context, obj *apiv1.Receiver, logger logr.Logger) (ctrl.Result, error) {
160+
func (r *ReceiverReconciler) reconcile(ctx context.Context, obj *apiv1.Receiver) (ctrl.Result, error) {
161+
log := ctrl.LoggerFrom(ctx)
162+
162163
// Mark the resource as under reconciliation.
163164
conditions.MarkReconciling(obj, meta.ProgressingReason, "Reconciliation in progress")
164165

internal/server/cel.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package server
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223
"mime"
@@ -54,7 +55,7 @@ func newCELProgram(expr string) (cel.Program, error) {
5455
return nil, fmt.Errorf("expression %v check failed: %w", expr, issues.Err())
5556
}
5657

57-
prg, err := env.Program(checked, cel.EvalOptions(cel.OptOptimize))
58+
prg, err := env.Program(checked, cel.EvalOptions(cel.OptOptimize), cel.InterruptCheckFrequency(100))
5859
if err != nil {
5960
return nil, fmt.Errorf("expression %v failed to create a Program: %w", expr, err)
6061
}
@@ -77,13 +78,13 @@ func newCELEvaluator(expr string, req *http.Request) (resourcePredicate, error)
7778
}
7879
}
7980

80-
return func(obj client.Object) (*bool, error) {
81+
return func(ctx context.Context, obj client.Object) (*bool, error) {
8182
data, err := clientObjectToMap(obj)
8283
if err != nil {
8384
return nil, err
8485
}
8586

86-
out, _, err := prg.Eval(map[string]any{
87+
out, _, err := prg.ContextEval(ctx, map[string]any{
8788
"resource": data,
8889
"request": map[string]any{
8990
"body": body,

internal/server/receiver_handlers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (s *ReceiverServer) handlePayload(w http.ResponseWriter, r *http.Request) {
115115
return
116116
}
117117

118-
var evaluator func(client.Object) (*bool, error)
118+
var evaluator func(context.Context, client.Object) (*bool, error)
119119
if receiver.Spec.ResourceFilter != "" {
120120
evaluator, err = newCELEvaluator(receiver.Spec.ResourceFilter, r)
121121
if err != nil {
@@ -150,12 +150,12 @@ func (s *ReceiverServer) notifySingleResource(ctx context.Context, logger logr.L
150150

151151
func (s *ReceiverServer) notifyResource(ctx context.Context, logger logr.Logger, resource *metav1.PartialObjectMetadata, predicate resourcePredicate) error {
152152
if predicate != nil {
153-
accept, err := predicate(resource)
153+
accept, err := predicate(ctx, resource)
154154
if err != nil {
155155
return err
156156
}
157157
if !*accept {
158-
logger.Info(fmt.Sprintf("resource '%s/%s.%s' NOT annotated because CEL expression returned false", resource.Kind, resource.Name, resource.Namespace))
158+
logger.V(1).Info(fmt.Sprintf("resource '%s/%s.%s' NOT annotated because CEL expression returned false", resource.Kind, resource.Name, resource.Namespace))
159159
return nil
160160
}
161161
}
@@ -514,7 +514,7 @@ func (s *ReceiverServer) token(ctx context.Context, receiver apiv1.Receiver) (st
514514
return token, nil
515515
}
516516

517-
type resourcePredicate func(client.Object) (*bool, error)
517+
type resourcePredicate func(context.Context, client.Object) (*bool, error)
518518

519519
// requestReconciliation requests reconciliation of all the resources matching the given CrossNamespaceObjectReference by annotating them accordingly.
520520
func (s *ReceiverServer) requestReconciliation(ctx context.Context, logger logr.Logger, resource apiv1.CrossNamespaceObjectReference, defaultNamespace string, resourcePredicate resourcePredicate) error {

0 commit comments

Comments
 (0)