diff --git a/fn.go b/fn.go index ec83cd8..bb14adf 100644 --- a/fn.go +++ b/fn.go @@ -31,6 +31,21 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest) 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 + fctx.Fields = make(map[string]*structpb.Value) + } + // Default to ignore. + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(true) + rsp.Context = fctx + in := &v1alpha1.Input{} if err := request.GetInput(req, in); err != nil { // You can set a custom status condition on the claim. This allows you to @@ -78,18 +93,7 @@ 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) { - 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()})) return rsp, nil @@ -100,9 +104,28 @@ 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("shouldn't filter", "event", fmt.Sprintf("%+v", e)) + + fctx.Fields["ops.upbound.io/ignored-resource"] = structpb.NewBoolValue(false) + rsp.Context = fctx + 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 { 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=