Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"maps"
"reflect"
"sort"

Expand Down Expand Up @@ -85,6 +86,15 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
response.Fatal(rsp, errors.Wrapf(err, "cannot create new Struct from extra resources output"))
return rsp, nil
}

if in.Spec.Context.GetPolicy() == v1beta1.ContextPolicyMerge {
v, _ := request.GetContextKey(req, in.Spec.Context.GetKey())
if fields := v.GetStructValue().GetFields(); fields != nil {
maps.Copy(fields, s.GetFields())
s.Fields = fields
}
}

response.SetContextKey(rsp, in.Spec.Context.GetKey(), structpb.NewStructValue(s))

return rsp, nil
Expand Down
127 changes: 126 additions & 1 deletion fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ func TestRunFunction(t *testing.T) {
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "hello"},
Context: &structpb.Struct{
Fields: map[string]*structpb.Value{
v1beta1.FunctionContextKeyExtraResources: structpb.NewStructValue(resource.MustStructJSON(`{
"previous": [
{
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"kind": "EnvironmentConfig",
"metadata": {
"name": "previous"
}
}
]
}`)),
},
},
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{
Expand Down Expand Up @@ -646,7 +661,7 @@ func TestRunFunction(t *testing.T) {
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"type": "Reference",
"into": "obj-0",
"ref": {
"ref": {
"name": "my-env-config"
}
}
Expand Down Expand Up @@ -688,6 +703,116 @@ func TestRunFunction(t *testing.T) {
},
},
},
"MergeContext": {
reason: "The Function should put resolved extra resources into existing context value when policy is Merge.",
args: args{
req: &fnv1.RunFunctionRequest{
Meta: &fnv1.RequestMeta{Tag: "hello"},
Context: &structpb.Struct{
Fields: map[string]*structpb.Value{
v1beta1.FunctionContextKeyExtraResources: structpb.NewStructValue(resource.MustStructJSON(`{
"previous": [
{
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"kind": "EnvironmentConfig",
"metadata": {
"name": "previous"
}
}
]
}`)),
},
},
Observed: &fnv1.State{
Composite: &fnv1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "test.crossplane.io/v1alpha1",
"kind": "XR",
"metadata": {
"name": "my-xr"
}
}`),
},
},
RequiredResources: map[string]*fnv1.Resources{
"obj-0": {
Items: []*fnv1.Resource{
{
Resource: resource.MustStructJSON(`{
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"kind": "EnvironmentConfig",
"metadata": {
"name": "my-env-config"
}
}`),
},
},
},
},
Input: resource.MustStructJSON(`{
"apiVersion": "extra-resources.fn.crossplane.io/v1beta1",
"kind": "Input",
"spec": {
"context": {
"policy": "Merge"
},
"extraResources": [
{
"kind": "EnvironmentConfig",
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"type": "Reference",
"into": "obj-0",
"ref": {
"name": "my-env-config"
}
}
]
}
}`),
},
},
want: want{
rsp: &fnv1.RunFunctionResponse{
Meta: &fnv1.ResponseMeta{Tag: "hello", Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1.Result{},
Requirements: &fnv1.Requirements{
Resources: map[string]*fnv1.ResourceSelector{
"obj-0": {
ApiVersion: "apiextensions.crossplane.io/v1beta1",
Kind: "EnvironmentConfig",
Match: &fnv1.ResourceSelector_MatchName{
MatchName: "my-env-config",
},
},
},
},
Context: &structpb.Struct{
Fields: map[string]*structpb.Value{
v1beta1.FunctionContextKeyExtraResources: structpb.NewStructValue(resource.MustStructJSON(`{
"previous": [
{
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"kind": "EnvironmentConfig",
"metadata": {
"name": "previous"
}
}
],
"obj-0": [
{
"apiVersion": "apiextensions.crossplane.io/v1beta1",
"kind": "EnvironmentConfig",
"metadata": {
"name": "my-env-config"
}
}
]
}`)),
},
},
},
},
},
}

for name, tc := range cases {
Expand Down
26 changes: 26 additions & 0 deletions input/v1beta1/resource_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ type Context struct {
// standard functions such as Function Patch and Transform.
// +kubebuilder:default=apiextensions.crossplane.io/extra-resources
Key *string `json:"key,omitempty"`

// Policy specifies how to handle the context's potentially existing value.
// Replace replaces the existing context key with the new extra resources.
// Merge merges the extra resources into the context key's existing value.
// +kubebuilder:default=Replace
// +kubebuilder:validation:Enum=Replace;Merge
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// +kubebuilder:validation:Enum=Replace;Merge
// +kubebuilder:validation:Enum=Replace;MergeObjects

So that it matches patch and trasform?
https://github.com/crossplane-contrib/function-patch-and-transform/blob/b12238e354e01df2db426f6347b7850029279773/input/v1beta1/resources_patches.go#L41C70-L42

And potentially add MergeObjectsAppendArrays in a future PR.

Policy *ContextPolicy `json:"policy,omitempty"`
}

// GetKey returns the key of the context, defaulting to
Expand All @@ -59,6 +66,25 @@ func (i *Context) GetKey() string {
return *i.Key
}

// ContextPolicy specifies how to handle the context's potentially existing value.
type ContextPolicy string

const (
// ContextPolicyReplace replaces the context key.
ContextPolicyReplace ContextPolicy = "Replace"
// ContextPolicyMerge merges into the context key.
ContextPolicyMerge ContextPolicy = "Merge"
)

// GetPolicy returns the policy of the context, defaulting to Replace if not
// specified.
func (i *Context) GetPolicy() ContextPolicy {
if i == nil || i.Policy == nil {
return ContextPolicyReplace
}
return *i.Policy
}

// Policy represents the Resolution policy of Reference instance.
type Policy struct {
// Resolution specifies whether resolution of this reference is required.
Expand Down
5 changes: 5 additions & 0 deletions input/v1beta1/zz_generated.deepcopy.go

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

10 changes: 10 additions & 0 deletions package/input/extra-resources.fn.crossplane.io_inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ spec:
E.g. 'apiextensions.crossplane.io/environment', the environment used in
standard functions such as Function Patch and Transform.
type: string
policy:
default: Replace
description: |-
Policy specifies how to handle the context's potentially existing value.
Replace replaces the existing context key with the new extra resources.
Merge merges the extra resources into the context key's existing value.
enum:
- Replace
- Merge
type: string
type: object
extraResources:
description: |-
Expand Down
Loading