Skip to content

Commit

Permalink
Merge pull request #78 from dalton-hill-0/adding-condition-example
Browse files Browse the repository at this point in the history
Add Status Condition Example
  • Loading branch information
jbw976 authored Sep 20, 2024
2 parents c09b736 + 8adddeb commit 09d3659
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 606 deletions.
22 changes: 22 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ

in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
// You can set a custom status condition on the claim. This allows you to
// communicate with the user. See the link below for status condition
// guidance.
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
response.ConditionFalse(rsp, "FunctionSuccess", "InternalError").
WithMessage("Something went wrong.").
TargetCompositeAndClaim()

// You can emit an event regarding the claim. This allows you to communicate
// with the user. Note that events should be used sparingly and are subject
// to throttling; see the issue below for more information.
// https://github.com/crossplane/crossplane/issues/5802
response.Warning(rsp, errors.New("something went wrong")).
TargetCompositeAndClaim()

response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
return rsp, nil
}
Expand All @@ -34,5 +49,12 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Normalf(rsp, "I was run with input %q!", in.Example)
f.log.Info("I was run!", "input", in.Example)

// You can set a custom status condition on the claim. This allows you to
// communicate with the user. See the link below for status condition
// guidance.
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
response.ConditionTrue(rsp, "FunctionSuccess", "Success").
TargetCompositeAndClaim()

return rsp, nil
}
9 changes: 9 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func TestRunFunction(t *testing.T) {
{
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
Message: "I was run with input \"Hello, world\"!",
Target: fnv1beta1.Target_TARGET_COMPOSITE.Enum(),
},
},
Conditions: []*fnv1beta1.Condition{
{
Type: "FunctionSuccess",
Status: fnv1beta1.Status_STATUS_CONDITION_TRUE,
Reason: "Success",
Target: fnv1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(),
},
},
},
Expand Down
27 changes: 13 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ toolchain go1.22.3

require (
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/crossplane/crossplane-runtime v1.16.0
github.com/crossplane/function-sdk-go v0.3.0-rc.0.0.20240816202013-2da86c5eb7d1
github.com/google/go-cmp v0.6.0
google.golang.org/protobuf v1.34.1
k8s.io/apimachinery v0.29.3
Expand All @@ -20,7 +20,7 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-json-experiment/json v0.0.0-20231013223334-54c864be5b8d // indirect
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -31,7 +31,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -45,20 +45,19 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304212257-790db918fca8 // indirect
google.golang.org/grpc v1.62.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 09d3659

Please sign in to comment.