generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from ytsarev/write-to-context
Implement writing to Context
- Loading branch information
Showing
10 changed files
with
304 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
run: | ||
crossplane render --verbose xr.yaml composition.yaml functions.yaml -rc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Example Manifests | ||
|
||
You can run your function locally and test it using `crossplane render` | ||
with these example manifests. | ||
|
||
```shell | ||
# Run the function locally | ||
$ go run . --insecure --debug | ||
``` | ||
|
||
```shell | ||
# Then, in another terminal, call it with these example manifests | ||
$ crossplane render --verbose xr.yaml composition.yaml functions.yaml -rc | ||
--- | ||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: XR | ||
metadata: | ||
name: example | ||
status: | ||
conditions: | ||
- lastTransitionTime: "2024-01-01T00:00:00Z" | ||
reason: Available | ||
status: "True" | ||
type: Ready | ||
--- | ||
apiVersion: render.crossplane.io/v1beta1 | ||
fields: | ||
contextField: contextValue | ||
moreComplexField: | ||
test: field | ||
kind: Context | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
apiVersion: apiextensions.crossplane.io/v1 | ||
kind: Composition | ||
metadata: | ||
name: function-template-go | ||
spec: | ||
compositeTypeRef: | ||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: XR | ||
mode: Pipeline | ||
pipeline: | ||
- step: normal | ||
functionRef: | ||
name: kcl-function | ||
input: | ||
apiVersion: krm.kcl.dev/v1alpha1 | ||
kind: KCLInput | ||
metadata: | ||
annotations: | ||
"krm.kcl.dev/default_ready": "True" | ||
name: basic | ||
spec: | ||
source: | | ||
oxr = option("params").oxr | ||
dxr = { | ||
**oxr | ||
} | ||
context = { | ||
apiVersion: "meta.krm.kcl.dev/v1alpha1" | ||
kind: "Context" | ||
data = { | ||
contextField = "contextValue" | ||
moreComplexField = { | ||
test: "field" | ||
} | ||
} | ||
} | ||
items = [ | ||
context | ||
dxr | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: pkg.crossplane.io/v1beta1 | ||
kind: Function | ||
metadata: | ||
name: kcl-function | ||
annotations: | ||
# This tells crossplane render to connect to the function locally. | ||
render.crossplane.io/runtime: Development | ||
spec: | ||
package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: example.crossplane.io/v1beta1 | ||
kind: XR | ||
metadata: | ||
name: example | ||
spec: | ||
count: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package resource | ||
|
||
import ( | ||
"dario.cat/mergo" | ||
"github.com/crossplane/function-sdk-go/errors" | ||
fnv1 "github.com/crossplane/function-sdk-go/proto/v1" | ||
) | ||
|
||
// MergeContext merges existing Context with new values provided | ||
func MergeContext(req *fnv1.RunFunctionRequest, val map[string]interface{}) (map[string]interface{}, error) { | ||
mergedContext := req.GetContext().AsMap() | ||
if len(val) == 0 { | ||
return mergedContext, nil | ||
} | ||
if err := mergo.Merge(&mergedContext, val, mergo.WithOverride); err != nil { | ||
return mergedContext, errors.Wrapf(err, "cannot merge data %T", req) | ||
} | ||
return mergedContext, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package resource | ||
|
||
import ( | ||
"testing" | ||
|
||
fnv1 "github.com/crossplane/function-sdk-go/proto/v1" | ||
"github.com/crossplane/function-sdk-go/resource" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
) | ||
|
||
func TestMergeContext(t *testing.T) { | ||
type args struct { | ||
val map[string]interface{} | ||
req *fnv1.RunFunctionRequest | ||
} | ||
type want struct { | ||
us map[string]any | ||
err error | ||
} | ||
|
||
cases := map[string]struct { | ||
reason string | ||
args args | ||
want want | ||
}{ | ||
"NoContextAtKey": { | ||
reason: "When there is no existing context data at the key to merge, return the value", | ||
args: args{ | ||
req: &fnv1.RunFunctionRequest{ | ||
Context: nil, | ||
}, | ||
val: map[string]interface{}{"hello": "world"}, | ||
}, | ||
want: want{ | ||
us: map[string]interface{}{"hello": "world"}, | ||
err: nil, | ||
}, | ||
}, | ||
"SuccessfulMerge": { | ||
reason: "Confirm that keys are merged with source overwriting destination", | ||
args: args{ | ||
req: &fnv1.RunFunctionRequest{ | ||
Context: resource.MustStructJSON(`{"apiextensions.crossplane.io/environment":{"complex":{"a":"b","c":{"d":"e","f":"1","overWrite": "fromContext"}}}}`), | ||
}, | ||
val: map[string]interface{}{ | ||
"newKey": "newValue", | ||
"apiextensions.crossplane.io/environment": map[string]any{ | ||
"complex": map[string]any{ | ||
"c": map[string]any{ | ||
"overWrite": "fromFunction", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
us: map[string]interface{}{ | ||
"apiextensions.crossplane.io/environment": map[string]any{ | ||
"complex": map[string]any{ | ||
"a": "b", | ||
"c": map[string]any{ | ||
"d": "e", | ||
"f": "1", | ||
"overWrite": "fromFunction", | ||
}, | ||
}, | ||
}, | ||
"newKey": "newValue"}, | ||
err: nil, | ||
}, | ||
}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
rsp, err := MergeContext(tc.args.req, tc.args.val) | ||
|
||
if diff := cmp.Diff(tc.want.us, rsp, protocmp.Transform()); diff != "" { | ||
t.Errorf("%s\nf.MergeContext(...): -want rsp, +got rsp:\n%s", tc.reason, diff) | ||
} | ||
|
||
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" { | ||
t.Errorf("%s\nf.RunFunction(...): -want err, +got err:\n%s", tc.reason, diff) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters