Skip to content

chore: init function enriching context #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: init function enriching context
  • Loading branch information
mariatsji committed May 7, 2025
commit 69d7ea58f71b4fe16fb52a8922d3c6a666b93486
30 changes: 25 additions & 5 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package main

import (
"context"
"encoding/json"

"github.com/crossplane/function-sdk-go/errors"
"github.com/crossplane/function-sdk-go/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
"github.com/crossplane/user-s3-arn/input/v1beta1"
"github.com/crossplane/user-s3-arn/input/v1alpha1"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
)

// Key to retrieve extras at.
const (
FunctionContextKeyS3UserARN = "s3-user-arn.fn.crossplane.io"
)

// Function returns whatever response you ask it to.
Expand All @@ -24,7 +32,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)

rsp := response.To(req, response.DefaultTTL)

in := &v1beta1.Input{}
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
// communicate with the user. See the link below for status condition
Expand All @@ -45,9 +53,21 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
return rsp, nil
}

// TODO: Add your Function logic here!
response.Normalf(rsp, "I was run with input %q!", in.Example)
f.log.Info("I was run!", "input", in.Example)
data := struct{}{}
b, err := json.Marshal(data)
if err != nil {
response.Fatal(rsp, errors.Errorf("cannot marshal %T: %w", data, err))
return rsp, nil
}
s := &structpb.Struct{}
err = protojson.Unmarshal(b, s)
if err != nil {
response.Fatal(rsp, errors.Errorf("cannot unmarshal %T into %T: %w", b, s, err))
return rsp, nil
}
response.SetContextKey(rsp, FunctionContextKeyS3UserARN, structpb.NewStructValue(s))

response.Normalf(rsp, "")

// 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
Expand Down
10 changes: 6 additions & 4 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"

"github.com/crossplane/function-sdk-go/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
Expand All @@ -25,6 +26,7 @@ func TestRunFunction(t *testing.T) {
rsp *fnv1.RunFunctionResponse
err error
}
ctx, _ := structpb.NewStruct(map[string]any{FunctionContextKeyS3UserARN: map[string]any{}})

cases := map[string]struct {
reason string
Expand All @@ -37,19 +39,19 @@ func TestRunFunction(t *testing.T) {
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "hello"},
Input: resource.MustStructJSON(`{
"apiVersion": "template.fn.crossplane.io/v1beta1",
"kind": "Input",
"example": "Hello, world"
"apiVersion": "s3-user-arn.fn.crossplane.io/v1alpha1",
"kind": "Input"
}`),
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)},
Context: ctx,
Results: []*fnv1.Result{
{
Severity: fnv1.Severity_SEVERITY_NORMAL,
Message: "I was run with input \"Hello, world\"!",
Message: "",
Target: fnv1.Target_TARGET_COMPOSITE.Enum(),
},
},
Expand Down
14 changes: 4 additions & 10 deletions input/v1beta1/input.go → input/v1alpha1/input.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Package v1beta1 contains the input type for this Function
// Package v1alpha1 contains the input type for this Function
// +kubebuilder:object:generate=true
// +groupName=template.fn.crossplane.io
// +versionName=v1beta1
package v1beta1
// +groupName=s3-user-arn.fn.crossplane.io
// +versionName=v1alpha1
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -11,17 +11,11 @@ import (
// This isn't a custom resource, in the sense that we never install its CRD.
// It is a KRM-like object, so we generate a CRD to describe its schema.

// TODO: Add your input type here! It doesn't need to be called 'Input', you can
// rename it to anything you like.

// Input can be used to provide input to this Function.
// +kubebuilder:object:root=true
// +kubebuilder:storageversion
// +kubebuilder:resource:categories=crossplane
type Input struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Example is an example field. Replace it with whatever input you need. :)
Example string `json:"example"`
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading