Skip to content

Commit

Permalink
feat: add function pipeline ctx
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Apr 11, 2024
1 parent d64d4d0 commit 6b98f33
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Here's what you can do in the KCL script:
+ Read the `ObservedComposedResources` from `option("params").ocds`.
+ Read the `DesiredCompositeResource` from `option("params").dxr`.
+ Read the `DesiredComposedResources` from `option("params").dcds`.
+ Read the function pipeline's context from `option("params").ctx`.

## Library

Expand Down
2 changes: 2 additions & 0 deletions examples/default/function_ctx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
crossplane beta render xr.yaml composition.yaml functions.yaml -r
113 changes: 113 additions & 0 deletions examples/default/function_ctx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Example Manifests

You can run your function locally and test it using `crossplane beta 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 beta render xr.yaml composition.yaml functions.yaml -r
---
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
status:
dummy: cool-status
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: AccessKey
metadata:
annotations:
crossplane.io/composition-resource-name: sample-access-key-1
generateName: example-
labels:
crossplane.io/composite: example
name: sample-access-key-1
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
spec:
forProvider:
userSelector:
matchLabels:
testing.upbound.io/example-name: test-user-1
writeConnectionSecretToRef:
name: sample-access-key-secret-1
namespace: crossplane-system
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: User
metadata:
annotations:
crossplane.io/composition-resource-name: test-user-0
generateName: example-
labels:
crossplane.io/composite: example
dummy: foo
testing.upbound.io/example-name: test-user-0
name: test-user-0
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
spec:
forProvider: {}
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: AccessKey
metadata:
annotations:
crossplane.io/composition-resource-name: sample-access-key-0
generateName: example-
labels:
crossplane.io/composite: example
name: sample-access-key-0
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
spec:
forProvider:
userSelector:
matchLabels:
testing.upbound.io/example-name: test-user-0
writeConnectionSecretToRef:
name: sample-access-key-secret-0
namespace: crossplane-system
---
apiVersion: iam.aws.upbound.io/v1beta1
kind: User
metadata:
annotations:
crossplane.io/composition-resource-name: test-user-1
generateName: example-
labels:
crossplane.io/composite: example
dummy: foo
testing.upbound.io/example-name: test-user-1
name: test-user-1
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
spec:
forProvider: {}
```
23 changes: 23 additions & 0 deletions examples/default/function_ctx/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-template-go
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: normal
functionRef:
name: kcl-function
input:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
metadata:
name: basic
spec:
source: |
ctx = option("params").ctx
# Show function pipeline's context.
items = [{ctx = ctx}]
9 changes: 9 additions & 0 deletions examples/default/function_ctx/functions.yaml
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 beta render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest
6 changes: 6 additions & 0 deletions examples/default/function_ctx/xr.yaml
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: 2
12 changes: 12 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Fatal(rsp, err)
return rsp, nil
}
// Set function context
ctxByte, err := req.Context.MarshalJSON()
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
ctxObj, err := pkgresource.JsonByteToRawExtension(ctxByte)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
in.Spec.Params["ctx"] = ctxObj
// Input Example: https://github.com/kcl-lang/krm-kcl/blob/main/examples/mutation/set-annotations/suite/good.yaml
inputBytes, outputBytes := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
kclRunBytes, err := yaml.Marshal(in)
Expand Down
8 changes: 8 additions & 0 deletions pkg/resource/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ type Resource struct {
Base unstructured.Unstructured `json:"base,omitempty"`
}

func JsonByteToRawExtension(jsonByte []byte) (runtime.RawExtension, error) {
o, err := JsonByteToUnstructured(jsonByte)
if err != nil {
return runtime.RawExtension{}, err
}
return UnstructuredToRawExtension(o)
}

func JsonByteToUnstructured(jsonByte []byte) (*unstructured.Unstructured, error) {
var data map[string]interface{}
err := json.Unmarshal(jsonByte, &data)
Expand Down

0 comments on commit 6b98f33

Please sign in to comment.