From 64f6df884a53f3a11fbfd57cec905de6b965eb6f Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 14:59:54 -0700 Subject: [PATCH 1/6] debugging --- fn.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fn.go b/fn.go index ec83cd8..0919450 100644 --- a/fn.go +++ b/fn.go @@ -78,10 +78,8 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) return rsp, nil } - fmt.Println("receieved resource:") - fmt.Printf("%+v\n", e) - if !match(in, e) { + f.log.Info("doesn't match", "event", fmt.Sprintf("%+v", e)) fctx := req.GetContext() if len(fctx.AsMap()) == 0 { // init map @@ -100,6 +98,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() response.Normal(rsp, "passing along given event") + f.log.Info("matches", "event", fmt.Sprintf("%+v", e)) return rsp, nil } From 1da20505a4c92863fb3e3154a0e6dec4469c314f Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:09:34 -0700 Subject: [PATCH 2/6] default to ignore --- fn.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fn.go b/fn.go index 0919450..2d1a1a8 100644 --- a/fn.go +++ b/fn.go @@ -30,6 +30,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) f.log.Info("Running function", "tag", req.GetMeta().GetTag()) rsp := response.To(req, response.DefaultTTL) + fctx := req.GetContext() + if len(fctx.AsMap()) == 0 { + // init map + fctx.Fields = make(map[string]*structpb.Value) + } + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) + rsp.Context = fctx in := &v1alpha1.Input{} if err := request.GetInput(req, in); err != nil { @@ -80,13 +87,6 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) if !match(in, e) { f.log.Info("doesn't match", "event", fmt.Sprintf("%+v", e)) - fctx := req.GetContext() - if len(fctx.AsMap()) == 0 { - // init map - fctx.Fields = make(map[string]*structpb.Value) - } - fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) - rsp.Context = fctx response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() response.Normal(rsp, fmt.Sprintf("not interested in the given event %s, skipping", types.NamespacedName{Name: e.GetName(), Namespace: e.GetNamespace()})) @@ -99,6 +99,10 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() response.Normal(rsp, "passing along given event") f.log.Info("matches", "event", fmt.Sprintf("%+v", e)) + + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(false) + rsp.Context = fctx + return rsp, nil } From b08f47359fe9003fbc100c948965921a0390723b Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:12:03 -0700 Subject: [PATCH 3/6] update sdk for function-event-filter --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 515ff73..7af2ba6 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.24.4 require ( github.com/alecthomas/kong v0.9.0 - github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250715215746-ca27889cd196 + github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250805171053-2910b68d255d github.com/google/go-cmp v0.6.0 google.golang.org/protobuf v1.34.3-0.20240816073751-94ecbc261689 k8s.io/api v0.31.0 diff --git a/go.sum b/go.sum index 62c66a0..fa834ec 100644 --- a/go.sum +++ b/go.sum @@ -24,8 +24,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/crossplane/crossplane-runtime v1.18.0 h1:aAQIMNOgPbbXaqj9CUSv+gPl3QnVbn33YlzSe145//0= github.com/crossplane/crossplane-runtime v1.18.0/go.mod h1:p7nVVsLn0CWjsLvLCtr7T40ErbTgNWKRxmYnwFdfXb4= -github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250715215746-ca27889cd196 h1:wsaHsx3jIuJzEB+9O8+AegjfwaVM0j7IIlOegUTVtDE= -github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250715215746-ca27889cd196/go.mod h1:fEwSBgMH6+kicaBeOWz6PZRwhjLg4tu9QEDeP/9O2yE= +github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250805171053-2910b68d255d h1:bzt8qEg9I2GrLc216IuuTn4x+GECxc+DoGlDZ4PMuJY= +github.com/crossplane/function-sdk-go v0.5.0-rc.0.0.20250805171053-2910b68d255d/go.mod h1:fEwSBgMH6+kicaBeOWz6PZRwhjLg4tu9QEDeP/9O2yE= github.com/crossplane/upjet v1.4.1-0.20240911184956-3afbb7796d46 h1:2IH1YPTBrNmBj0Z1OCjEBTrQCuRaLutZbWLaswFeCFQ= github.com/crossplane/upjet v1.4.1-0.20240911184956-3afbb7796d46/go.mod h1:wkdZf/Cvhr6PI30VdHIOjg4dX39Z5uijqnLWFk5PbGM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= From 0dd690adea21bc7c4081d0906ff3b656450a795a Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:49:32 -0700 Subject: [PATCH 4/6] make sure we don't overwrite the passed in context --- fn.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fn.go b/fn.go index 2d1a1a8..895bb24 100644 --- a/fn.go +++ b/fn.go @@ -35,7 +35,12 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) // init map fctx.Fields = make(map[string]*structpb.Value) } - fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) + _, ok := fctx.GetFields()["ops.upbound.io/ignored-resource"] + if !ok { + // If we don't yet have a value here, default to ignore. Otherwise + // keep whatever value was passed in. + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) + } rsp.Context = fctx in := &v1alpha1.Input{} @@ -86,8 +91,6 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) } if !match(in, e) { - f.log.Info("doesn't match", "event", fmt.Sprintf("%+v", e)) - response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() response.Normal(rsp, fmt.Sprintf("not interested in the given event %s, skipping", types.NamespacedName{Name: e.GetName(), Namespace: e.GetNamespace()})) return rsp, nil @@ -98,7 +101,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() response.Normal(rsp, "passing along given event") - f.log.Info("matches", "event", fmt.Sprintf("%+v", e)) + f.log.Info("shouldn't filter", "event", fmt.Sprintf("%+v", e)) fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(false) rsp.Context = fctx From 9c50049955b5d148de4c9ec53dea70f0ae7291aa Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:07:32 -0700 Subject: [PATCH 5/6] add shouldIgnore --- fn.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/fn.go b/fn.go index 895bb24..66cfc1a 100644 --- a/fn.go +++ b/fn.go @@ -30,6 +30,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) f.log.Info("Running function", "tag", req.GetMeta().GetTag()) rsp := response.To(req, response.DefaultTTL) + + if f.shouldIgnore(req) { + response.ConditionTrue(rsp, "FunctionSuccess", "Success").TargetCompositeAndClaim() + response.Normal(rsp, "received an ignored resource, skipping") + return rsp, nil + } + fctx := req.GetContext() if len(fctx.AsMap()) == 0 { // init map @@ -109,6 +116,20 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) return rsp, nil } +// shouldIgnore returns true if the caller has communicated that the resource +// is an ignored resource. False otherwise. +func (f *Function) shouldIgnore(req *fnv1.RunFunctionRequest) bool { + fctx := req.GetContext() + ignored, ok := fctx.AsMap()["ops.upbound.io/ignored-resource"] + if ok { + i, ok := ignored.(bool) + if ok && i { + return true + } + } + return false +} + func match(in *v1alpha1.Input, e corev1.Event) bool { //nolint:gocyclo // This method is above our cyclomatic complexity level. Be wary of adding additional complexity. ib, err := json.Marshal(in.InvolvedObject) if err != nil { From 6849bec0584a591f767c6bfad03f39271fb7dabb Mon Sep 17 00:00:00 2001 From: tnthornton <2375126+tnthornton@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:36:09 -0700 Subject: [PATCH 6/6] default to ignore --- fn.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/fn.go b/fn.go index 66cfc1a..bb14adf 100644 --- a/fn.go +++ b/fn.go @@ -42,12 +42,8 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) // init map fctx.Fields = make(map[string]*structpb.Value) } - _, ok := fctx.GetFields()["ops.upbound.io/ignored-resource"] - if !ok { - // If we don't yet have a value here, default to ignore. Otherwise - // keep whatever value was passed in. - fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) - } + // Default to ignore. + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) rsp.Context = fctx in := &v1alpha1.Input{}