From 1163c8b48929efcefcc22a5faac7096406a8f8f3 Mon Sep 17 00:00:00 2001 From: peefy Date: Tue, 2 Jan 2024 22:31:35 +0800 Subject: [PATCH] feat: add more examples for crossplane kcl functions Signed-off-by: peefy --- README.md | 13 ++- examples/patch_desired/patching/Makefile | 2 + examples/patch_desired/patching/README.md | 31 ++++++ .../patch_desired/patching/composition.yaml | 29 +++++ .../patch_desired/patching/functions.yaml | 9 ++ examples/patch_desired/patching/xr.yaml | 5 + examples/patch_resources/patching/Makefile | 2 + examples/patch_resources/patching/README.md | 47 ++++++++ .../patch_resources/patching/composition.yaml | 36 ++++++ .../patch_resources/patching/functions.yaml | 9 ++ examples/patch_resources/patching/xr.yaml | 5 + .../patching_multiple/Makefile | 2 + .../patching_multiple/README.md | 104 ++++++++++++++++++ .../patching_multiple/composition.yaml | 69 ++++++++++++ .../patching_multiple/functions.yaml | 9 ++ .../patch_resources/patching_multiple/xr.yaml | 5 + examples/resources/basic/Makefile | 2 + examples/resources/basic/README.md | 14 ++- examples/resources/basic/functions.yaml | 2 +- examples/resources/condition/Makefile | 2 + examples/resources/condition/README.md | 48 ++++++++ examples/resources/condition/composition.yaml | 41 +++++++ examples/resources/condition/functions.yaml | 9 ++ examples/resources/condition/xr.yaml | 8 ++ examples/resources/format/Makefile | 2 + examples/resources/format/README.md | 49 +++++++++ examples/resources/format/composition.yaml | 40 +++++++ examples/resources/format/functions.yaml | 9 ++ examples/resources/format/xr.yaml | 8 ++ examples/resources/loop/Makefile | 2 + examples/resources/loop/README.md | 75 +++++++++++++ examples/resources/loop/composition.yaml | 34 ++++++ examples/resources/loop/functions.yaml | 9 ++ examples/resources/loop/xr.yaml | 6 + examples/resources/options/Makefile | 2 + examples/resources/options/README.md | 47 ++++++++ examples/resources/options/composition.yaml | 33 ++++++ examples/resources/options/functions.yaml | 9 ++ examples/resources/options/xr.yaml | 6 + examples/resources/options_oxr/Makefile | 2 + examples/resources/options_oxr/README.md | 47 ++++++++ .../resources/options_oxr/composition.yaml | 35 ++++++ examples/resources/options_oxr/functions.yaml | 9 ++ examples/resources/options_oxr/xr.yaml | 6 + examples/resources/regex/Makefile | 2 + examples/resources/regex/README.md | 49 +++++++++ examples/resources/regex/composition.yaml | 42 +++++++ examples/resources/regex/functions.yaml | 9 ++ examples/resources/regex/xr.yaml | 8 ++ examples/xr/patching/Makefile | 2 + examples/xr/patching/README.md | 31 ++++++ examples/xr/patching/composition.yaml | 28 +++++ examples/xr/patching/functions.yaml | 9 ++ examples/xr/patching/xr.yaml | 5 + manifests/kcl-functions.yaml | 6 + pkg/resource/res.go | 2 +- 56 files changed, 1119 insertions(+), 7 deletions(-) create mode 100644 examples/patch_desired/patching/Makefile create mode 100644 examples/patch_desired/patching/README.md create mode 100644 examples/patch_desired/patching/composition.yaml create mode 100644 examples/patch_desired/patching/functions.yaml create mode 100644 examples/patch_desired/patching/xr.yaml create mode 100644 examples/patch_resources/patching/Makefile create mode 100644 examples/patch_resources/patching/README.md create mode 100644 examples/patch_resources/patching/composition.yaml create mode 100644 examples/patch_resources/patching/functions.yaml create mode 100644 examples/patch_resources/patching/xr.yaml create mode 100644 examples/patch_resources/patching_multiple/Makefile create mode 100644 examples/patch_resources/patching_multiple/README.md create mode 100644 examples/patch_resources/patching_multiple/composition.yaml create mode 100644 examples/patch_resources/patching_multiple/functions.yaml create mode 100644 examples/patch_resources/patching_multiple/xr.yaml create mode 100644 examples/resources/basic/Makefile create mode 100644 examples/resources/condition/Makefile create mode 100644 examples/resources/condition/README.md create mode 100644 examples/resources/condition/composition.yaml create mode 100644 examples/resources/condition/functions.yaml create mode 100644 examples/resources/condition/xr.yaml create mode 100644 examples/resources/format/Makefile create mode 100644 examples/resources/format/README.md create mode 100644 examples/resources/format/composition.yaml create mode 100644 examples/resources/format/functions.yaml create mode 100644 examples/resources/format/xr.yaml create mode 100644 examples/resources/loop/Makefile create mode 100644 examples/resources/loop/README.md create mode 100644 examples/resources/loop/composition.yaml create mode 100644 examples/resources/loop/functions.yaml create mode 100644 examples/resources/loop/xr.yaml create mode 100644 examples/resources/options/Makefile create mode 100644 examples/resources/options/README.md create mode 100644 examples/resources/options/composition.yaml create mode 100644 examples/resources/options/functions.yaml create mode 100644 examples/resources/options/xr.yaml create mode 100644 examples/resources/options_oxr/Makefile create mode 100644 examples/resources/options_oxr/README.md create mode 100644 examples/resources/options_oxr/composition.yaml create mode 100644 examples/resources/options_oxr/functions.yaml create mode 100644 examples/resources/options_oxr/xr.yaml create mode 100644 examples/resources/regex/Makefile create mode 100644 examples/resources/regex/README.md create mode 100644 examples/resources/regex/composition.yaml create mode 100644 examples/resources/regex/functions.yaml create mode 100644 examples/resources/regex/xr.yaml create mode 100644 examples/xr/patching/Makefile create mode 100644 examples/xr/patching/README.md create mode 100644 examples/xr/patching/composition.yaml create mode 100644 examples/xr/patching/functions.yaml create mode 100644 examples/xr/patching/xr.yaml create mode 100644 manifests/kcl-functions.yaml diff --git a/README.md b/README.md index 5eebc89..96865cc 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,21 @@ $ docker build . --tag=kcllang/crossplane-kcl $ crossplane xpkg build -f package --embed-runtime-image=kcllang/crossplane-kcl ``` -## Quick Start Examples +## Quick Start Examples and Debug Locally See [here](./examples/resources/basic/) +## Installing + +```yaml +apiVersion: pkg.crossplane.io/v1beta1 +kind: Function +metadata: + name: kcl-function +spec: + package: kcllang/crossplane-kcl +``` + ## Expected Output A KRM YAML list which means that each document must have an `apiVersion`, `kind` diff --git a/examples/patch_desired/patching/Makefile b/examples/patch_desired/patching/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/patch_desired/patching/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/patch_desired/patching/README.md b/examples/patch_desired/patching/README.md new file mode 100644 index 0000000..30e9fda --- /dev/null +++ b/examples/patch_desired/patching/README.md @@ -0,0 +1,31 @@ +# 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: nopexample.org/v1 +kind: XSubnetwork +metadata: + annotations: + nobu.dev/app: someapp + nobu.dev/cueified: "true" + name: test-xrender +spec: + forProvider: + network: somenetwork +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: updated xr ":XSubnetwork" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/patch_desired/patching/composition.yaml b/examples/patch_desired/patching/composition.yaml new file mode 100644 index 0000000..ff39e87 --- /dev/null +++ b/examples/patch_desired/patching/composition.yaml @@ -0,0 +1,29 @@ +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: + target: PatchDesired + source: | + { + metadata.name = "test-desired" + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.network: "somenetwork" + } diff --git a/examples/patch_desired/patching/functions.yaml b/examples/patch_desired/patching/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/patch_desired/patching/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/patch_desired/patching/xr.yaml b/examples/patch_desired/patching/xr.yaml new file mode 100644 index 0000000..6085111 --- /dev/null +++ b/examples/patch_desired/patching/xr.yaml @@ -0,0 +1,5 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender diff --git a/examples/patch_resources/patching/Makefile b/examples/patch_resources/patching/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/patch_resources/patching/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/patch_resources/patching/README.md b/examples/patch_resources/patching/README.md new file mode 100644 index 0000000..99f8dad --- /dev/null +++ b/examples/patch_resources/patching/README.md @@ -0,0 +1,47 @@ +# 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: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender +--- +apiVersion: nobu.dev/v1 +kind: Bucket +metadata: + annotations: + crossplane.io/composition-resource-name: bucket + nobu.dev/app: someapp + nobu.dev/cueified: "true" + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: bucket + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XSubnetwork + name: test-xrender + uid: "" +spec: + forProvider: + network: somenetwork +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "bucket:" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/patch_resources/patching/composition.yaml b/examples/patch_resources/patching/composition.yaml new file mode 100644 index 0000000..8ec316c --- /dev/null +++ b/examples/patch_resources/patching/composition.yaml @@ -0,0 +1,36 @@ +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: + target: PatchResources + resources: + - name: bucket + base: + apiVersion: nobu.dev/v1 + kind: Bucket + metadata: + name: bucket + source: | + { + metadata.name = "bucket" + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.network: "somenetwork" + } diff --git a/examples/patch_resources/patching/functions.yaml b/examples/patch_resources/patching/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/patch_resources/patching/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/patch_resources/patching/xr.yaml b/examples/patch_resources/patching/xr.yaml new file mode 100644 index 0000000..6085111 --- /dev/null +++ b/examples/patch_resources/patching/xr.yaml @@ -0,0 +1,5 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender diff --git a/examples/patch_resources/patching_multiple/Makefile b/examples/patch_resources/patching_multiple/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/patch_resources/patching_multiple/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/patch_resources/patching_multiple/README.md b/examples/patch_resources/patching_multiple/README.md new file mode 100644 index 0000000..5c4fc16 --- /dev/null +++ b/examples/patch_resources/patching_multiple/README.md @@ -0,0 +1,104 @@ +# 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 +crossplane beta render xr.yaml composition.yaml functions.yaml -r +--- +apiVersion: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender +--- +apiVersion: nobu.dev/v1 +kind: Bucket +metadata: + annotations: + crossplane.io/composition-resource-name: bucket + nobu.dev/app: someapp + nobu.dev/cueified: "true" + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: bucket + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XSubnetwork + name: test-xrender + uid: "" +spec: + forProvider: + policy: some-bucket-policy +--- +apiVersion: nobu.dev/v1 +kind: User +metadata: + annotations: + crossplane.io/composition-resource-name: iam-user + nobu.dev/app: someapp + nobu.dev/cueified: "true" + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: iam-user + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XSubnetwork + name: test-xrender + uid: "" +spec: + forProvider: + name: somename +--- +apiVersion: nobu.dev/v1 +kind: Role +metadata: + annotations: + crossplane.io/composition-resource-name: iam-role + nobu.dev/app: someapp + nobu.dev/cueified: "true" + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: iam-role + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XSubnetwork + name: test-xrender + uid: "" +spec: + forProvider: + policy: some-role-policy +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "bucket:" +severity: SEVERITY_NORMAL +step: normal +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "iam-role:" +severity: SEVERITY_NORMAL +step: normal +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "iam-user:" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/patch_resources/patching_multiple/composition.yaml b/examples/patch_resources/patching_multiple/composition.yaml new file mode 100644 index 0000000..78697d0 --- /dev/null +++ b/examples/patch_resources/patching_multiple/composition.yaml @@ -0,0 +1,69 @@ +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: + target: PatchResources + resources: + - name: bucket + base: + apiVersion: nobu.dev/v1 + kind: Bucket + metadata: + name: bucket + - name: iam-user + base: + apiVersion: nobu.dev/v1 + kind: User + metadata: + name: iam-user + - name: iam-role + base: + apiVersion: nobu.dev/v1 + kind: Role + metadata: + name: iam-role + source: | + items = [ + { + # Target the bucket by name + metadata.name = "bucket" + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.policy: "some-bucket-policy" + } + { + # Target the user by name + metadata.name = "iam-user" + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.name: "somename" + } + { + # Target the role by name + metadata.name = "iam-role" + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.policy: "some-role-policy" + } + ] diff --git a/examples/patch_resources/patching_multiple/functions.yaml b/examples/patch_resources/patching_multiple/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/patch_resources/patching_multiple/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/patch_resources/patching_multiple/xr.yaml b/examples/patch_resources/patching_multiple/xr.yaml new file mode 100644 index 0000000..6085111 --- /dev/null +++ b/examples/patch_resources/patching_multiple/xr.yaml @@ -0,0 +1,5 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender diff --git a/examples/resources/basic/Makefile b/examples/resources/basic/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/basic/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/basic/README.md b/examples/resources/basic/README.md index 3cd029e..d596b6e 100644 --- a/examples/resources/basic/README.md +++ b/examples/resources/basic/README.md @@ -17,14 +17,15 @@ kind: XR metadata: name: example-xr --- -apiVersion: example.org/v1 -kind: Generated +apiVersion: ec2.aws.upbound.io/v1beta1 +kind: Instance metadata: annotations: - crossplane.io/composition-resource-name: "" + crossplane.io/composition-resource-name: instance generateName: example-xr- labels: crossplane.io/composite: example-xr + name: instance ownerReferences: - apiVersion: example.crossplane.io/v1 blockOwnerDeletion: true @@ -32,10 +33,15 @@ metadata: kind: XR name: example-xr uid: "" +spec: + forProvider: + ami: ami-0d9858aa3c6322f73 + instanceType: t2.micro + region: us-east-2 --- apiVersion: render.crossplane.io/v1beta1 kind: Result -message: created resource ":Generated" +message: created resource "instance:Instance" severity: SEVERITY_NORMAL step: normal ``` diff --git a/examples/resources/basic/functions.yaml b/examples/resources/basic/functions.yaml index 8d105c8..3218fdd 100644 --- a/examples/resources/basic/functions.yaml +++ b/examples/resources/basic/functions.yaml @@ -6,4 +6,4 @@ metadata: # This tells crossplane beta render to connect to the function locally. render.crossplane.io/runtime: Development spec: - package: kcllang/crossplane-kcl:v0.1.0 + package: kcllang/crossplane-kcl diff --git a/examples/resources/condition/Makefile b/examples/resources/condition/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/condition/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/condition/README.md b/examples/resources/condition/README.md new file mode 100644 index 0000000..3b91d28 --- /dev/null +++ b/examples/resources/condition/README.md @@ -0,0 +1,48 @@ +# 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: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +--- +apiVersion: eks.nobu.dev/v1beta +kind: XNodepool +metadata: + annotations: + crossplane.io/composition-resource-name: "" + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XNopResource + name: test-xrender + uid: "" +spec: + parameters: + autoscaling: + - maxNodeCount: 1 + minNodeCount: 1 + clusterName: example-injections + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource ":XNodepool" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/condition/composition.yaml b/examples/resources/condition/composition.yaml new file mode 100644 index 0000000..329c29d --- /dev/null +++ b/examples/resources/condition/composition.yaml @@ -0,0 +1,41 @@ +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: + target: Resources + params: + name: "input-instance" + source: | + env: str = option("params").oxr.spec.provider + items = [ + { + if env == "aws": + apiVersion = "eks.nobu.dev/v1beta" + if env == "gcp": + apiVersion = "gke.nobu.dev/v1beta1" + kind = "XNodepool" + spec.parameters: { + autoscaling: [{ + maxNodeCount: 1 + minNodeCount: 1 + }] + clusterName: "example-injections" + region: "us-east-2" + } + } + ] diff --git a/examples/resources/condition/functions.yaml b/examples/resources/condition/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/condition/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/condition/xr.yaml b/examples/resources/condition/xr.yaml new file mode 100644 index 0000000..214af84 --- /dev/null +++ b/examples/resources/condition/xr.yaml @@ -0,0 +1,8 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +spec: + provider: aws + diff --git a/examples/resources/format/Makefile b/examples/resources/format/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/format/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/format/README.md b/examples/resources/format/README.md new file mode 100644 index 0000000..37c1081 --- /dev/null +++ b/examples/resources/format/README.md @@ -0,0 +1,49 @@ +# 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: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +--- +apiVersion: eks.nobu.dev/v1beta +kind: XNodepool +metadata: + annotations: + crossplane.io/composition-resource-name: pool-1 + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: pool-1 + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XNopResource + name: test-xrender + uid: "" +spec: + parameters: + autoscaling: + - maxNodeCount: 1 + minNodeCount: 1 + clusterName: example-injections + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "pool-1:XNodepool" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/format/composition.yaml b/examples/resources/format/composition.yaml new file mode 100644 index 0000000..ccdcc36 --- /dev/null +++ b/examples/resources/format/composition.yaml @@ -0,0 +1,40 @@ +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: + target: Resources + params: + name_suffix: 1 + source: | + name_suffix: str = str(option("params").name_suffix) or "" + name_suffix_with_line: str = "-{}".format(name_suffix) if name_suffix else "" + items = [ + { + apiVersion = "eks.nobu.dev/v1beta" + kind = "XNodepool" + metadata.name = "pool" + name_suffix_with_line + spec.parameters: { + autoscaling: [{ + maxNodeCount: 1 + minNodeCount: 1 + }] + clusterName: "example-injections" + region: "us-east-2" + } + } + ] diff --git a/examples/resources/format/functions.yaml b/examples/resources/format/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/format/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/format/xr.yaml b/examples/resources/format/xr.yaml new file mode 100644 index 0000000..214af84 --- /dev/null +++ b/examples/resources/format/xr.yaml @@ -0,0 +1,8 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +spec: + provider: aws + diff --git a/examples/resources/loop/Makefile b/examples/resources/loop/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/loop/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/loop/README.md b/examples/resources/loop/README.md new file mode 100644 index 0000000..219eebe --- /dev/null +++ b/examples/resources/loop/README.md @@ -0,0 +1,75 @@ +# 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/v1 +kind: XR +metadata: + name: example-xr +--- +apiVersion: ec2.aws.upbound.io/v1beta1 +kind: Instance +metadata: + annotations: + crossplane.io/composition-resource-name: basic-instance-us-east-1 + generateName: example-xr- + labels: + crossplane.io/composite: example-xr + name: instance-us-east-1 + ownerReferences: + - apiVersion: example.crossplane.io/v1 + blockOwnerDeletion: true + controller: true + kind: XR + name: example-xr + uid: "" +spec: + forProvider: + ami: ami-0d9858aa3c6322f73 + instanceType: t2.micro + region: us-east-1 +--- +apiVersion: ec2.aws.upbound.io/v1beta1 +kind: Instance +metadata: + annotations: + crossplane.io/composition-resource-name: basic-instance-us-east-2 + generateName: example-xr- + labels: + crossplane.io/composite: example-xr + name: instance-us-east-2 + ownerReferences: + - apiVersion: example.crossplane.io/v1 + blockOwnerDeletion: true + controller: true + kind: XR + name: example-xr + uid: "" +spec: + forProvider: + ami: ami-0d9858aa3c6322f73 + instanceType: t2.micro + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "instance-us-east-1:Instance" +severity: SEVERITY_NORMAL +step: normal +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "instance-us-east-2:Instance" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/loop/composition.yaml b/examples/resources/loop/composition.yaml new file mode 100644 index 0000000..f3e2f8a --- /dev/null +++ b/examples/resources/loop/composition.yaml @@ -0,0 +1,34 @@ +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: + target: Resources + params: + name: "input-instance" + source: | + regions = ["us-east-1", "us-east-2"] + items = [{ + apiVersion: "ec2.aws.upbound.io/v1beta1" + kind: "Instance" + metadata.name = "instance-" + r + spec.forProvider: { + ami: "ami-0d9858aa3c6322f73" + instanceType: "t2.micro" + region: r + } + } for r in regions] diff --git a/examples/resources/loop/functions.yaml b/examples/resources/loop/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/loop/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/loop/xr.yaml b/examples/resources/loop/xr.yaml new file mode 100644 index 0000000..25472b9 --- /dev/null +++ b/examples/resources/loop/xr.yaml @@ -0,0 +1,6 @@ +# Replace this with your XR! +apiVersion: example.crossplane.io/v1 +kind: XR +metadata: + name: example-xr +spec: {} diff --git a/examples/resources/options/Makefile b/examples/resources/options/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/options/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/options/README.md b/examples/resources/options/README.md new file mode 100644 index 0000000..464de73 --- /dev/null +++ b/examples/resources/options/README.md @@ -0,0 +1,47 @@ +# 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/v1 +kind: XR +metadata: + name: example-xr +--- +apiVersion: ec2.aws.upbound.io/v1beta1 +kind: Instance +metadata: + annotations: + crossplane.io/composition-resource-name: input-instance + generateName: example-xr- + labels: + crossplane.io/composite: example-xr + name: input-instance + ownerReferences: + - apiVersion: example.crossplane.io/v1 + blockOwnerDeletion: true + controller: true + kind: XR + name: example-xr + uid: "" +spec: + forProvider: + ami: ami-0d9858aa3c6322f73 + instanceType: t2.micro + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "input-instance:Instance" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/options/composition.yaml b/examples/resources/options/composition.yaml new file mode 100644 index 0000000..f94ff0c --- /dev/null +++ b/examples/resources/options/composition.yaml @@ -0,0 +1,33 @@ +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: + target: Resources + params: + name: "input-instance" + source: | + { + apiVersion: "ec2.aws.upbound.io/v1beta1" + kind: "Instance" + metadata.name = option("params").name + spec.forProvider: { + ami: "ami-0d9858aa3c6322f73" + instanceType: "t2.micro" + region: "us-east-2" + } + } diff --git a/examples/resources/options/functions.yaml b/examples/resources/options/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/options/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/options/xr.yaml b/examples/resources/options/xr.yaml new file mode 100644 index 0000000..25472b9 --- /dev/null +++ b/examples/resources/options/xr.yaml @@ -0,0 +1,6 @@ +# Replace this with your XR! +apiVersion: example.crossplane.io/v1 +kind: XR +metadata: + name: example-xr +spec: {} diff --git a/examples/resources/options_oxr/Makefile b/examples/resources/options_oxr/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/options_oxr/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/options_oxr/README.md b/examples/resources/options_oxr/README.md new file mode 100644 index 0000000..987b72e --- /dev/null +++ b/examples/resources/options_oxr/README.md @@ -0,0 +1,47 @@ +# 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/v1 +kind: XR +metadata: + name: example-xr +--- +apiVersion: ec2.aws.upbound.io/v1beta1 +kind: Instance +metadata: + annotations: + crossplane.io/composition-resource-name: example-xr + generateName: example-xr- + labels: + crossplane.io/composite: example-xr + name: example-xr + ownerReferences: + - apiVersion: example.crossplane.io/v1 + blockOwnerDeletion: true + controller: true + kind: XR + name: example-xr + uid: "" +spec: + forProvider: + ami: ami-0d9858aa3c6322f73 + instanceType: t2.micro + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "example-xr:Instance" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/options_oxr/composition.yaml b/examples/resources/options_oxr/composition.yaml new file mode 100644 index 0000000..29734c7 --- /dev/null +++ b/examples/resources/options_oxr/composition.yaml @@ -0,0 +1,35 @@ +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: + target: Resources + params: + name: "input-instance" + source: | + oxr = option("params").oxr + # Return YAML list and append to dxr. + items = [{ + apiVersion: "ec2.aws.upbound.io/v1beta1" + kind: "Instance" + metadata.name = oxr.metadata.name + spec.forProvider: { + ami: "ami-0d9858aa3c6322f73" + instanceType: "t2.micro" + region: "us-east-2" + } + }] diff --git a/examples/resources/options_oxr/functions.yaml b/examples/resources/options_oxr/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/options_oxr/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/options_oxr/xr.yaml b/examples/resources/options_oxr/xr.yaml new file mode 100644 index 0000000..25472b9 --- /dev/null +++ b/examples/resources/options_oxr/xr.yaml @@ -0,0 +1,6 @@ +# Replace this with your XR! +apiVersion: example.crossplane.io/v1 +kind: XR +metadata: + name: example-xr +spec: {} diff --git a/examples/resources/regex/Makefile b/examples/resources/regex/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/resources/regex/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/resources/regex/README.md b/examples/resources/regex/README.md new file mode 100644 index 0000000..8c85869 --- /dev/null +++ b/examples/resources/regex/README.md @@ -0,0 +1,49 @@ +# 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: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +--- +apiVersion: eks.nobu.dev/v1beta +kind: XNodepool +metadata: + annotations: + crossplane.io/composition-resource-name: name + generateName: test-xrender- + labels: + crossplane.io/composite: test-xrender + name: name + ownerReferences: + - apiVersion: nopexample.org/v1 + blockOwnerDeletion: true + controller: true + kind: XNopResource + name: test-xrender + uid: "" +spec: + parameters: + autoscaling: + - maxNodeCount: 1 + minNodeCount: 1 + clusterName: example-injections + region: us-east-2 +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: created resource "name:XNodepool" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/resources/regex/composition.yaml b/examples/resources/regex/composition.yaml new file mode 100644 index 0000000..0ba6463 --- /dev/null +++ b/examples/resources/regex/composition.yaml @@ -0,0 +1,42 @@ +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: + target: Resources + params: + name: name + source: | + import regex + + name: str = option("params").name or "" + assert regex.match(name, r"[A-Za-z_][A-Za-z0-9_]*"), "invalid name: ${name}, expected the regex [A-Za-z_][A-Za-z0-9_]*" + items = [ + { + apiVersion = "eks.nobu.dev/v1beta" + kind = "XNodepool" + metadata.name = name + spec.parameters: { + autoscaling: [{ + maxNodeCount: 1 + minNodeCount: 1 + }] + clusterName: "example-injections" + region: "us-east-2" + } + } + ] diff --git a/examples/resources/regex/functions.yaml b/examples/resources/regex/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/resources/regex/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/resources/regex/xr.yaml b/examples/resources/regex/xr.yaml new file mode 100644 index 0000000..214af84 --- /dev/null +++ b/examples/resources/regex/xr.yaml @@ -0,0 +1,8 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XNopResource +metadata: + name: test-xrender +spec: + provider: aws + diff --git a/examples/xr/patching/Makefile b/examples/xr/patching/Makefile new file mode 100644 index 0000000..f9a7429 --- /dev/null +++ b/examples/xr/patching/Makefile @@ -0,0 +1,2 @@ +run: + crossplane beta render xr.yaml composition.yaml functions.yaml -r diff --git a/examples/xr/patching/README.md b/examples/xr/patching/README.md new file mode 100644 index 0000000..30e9fda --- /dev/null +++ b/examples/xr/patching/README.md @@ -0,0 +1,31 @@ +# 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: nopexample.org/v1 +kind: XSubnetwork +metadata: + annotations: + nobu.dev/app: someapp + nobu.dev/cueified: "true" + name: test-xrender +spec: + forProvider: + network: somenetwork +--- +apiVersion: render.crossplane.io/v1beta1 +kind: Result +message: updated xr ":XSubnetwork" +severity: SEVERITY_NORMAL +step: normal +``` diff --git a/examples/xr/patching/composition.yaml b/examples/xr/patching/composition.yaml new file mode 100644 index 0000000..f61a5fc --- /dev/null +++ b/examples/xr/patching/composition.yaml @@ -0,0 +1,28 @@ +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: + target: XR + source: | + { + metadata.annotations: { + "nobu.dev/cueified": "true", + "nobu.dev/app": "someapp", + } + spec.forProvider.network: "somenetwork" + } diff --git a/examples/xr/patching/functions.yaml b/examples/xr/patching/functions.yaml new file mode 100644 index 0000000..3218fdd --- /dev/null +++ b/examples/xr/patching/functions.yaml @@ -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: kcllang/crossplane-kcl diff --git a/examples/xr/patching/xr.yaml b/examples/xr/patching/xr.yaml new file mode 100644 index 0000000..6085111 --- /dev/null +++ b/examples/xr/patching/xr.yaml @@ -0,0 +1,5 @@ +# Replace this with your XR! +apiVersion: nopexample.org/v1 +kind: XSubnetwork +metadata: + name: test-xrender diff --git a/manifests/kcl-functions.yaml b/manifests/kcl-functions.yaml new file mode 100644 index 0000000..623539c --- /dev/null +++ b/manifests/kcl-functions.yaml @@ -0,0 +1,6 @@ +apiVersion: pkg.crossplane.io/v1beta1 +kind: Function +metadata: + name: kcl-function +spec: + package: kcllang/crossplane-kcl diff --git a/pkg/resource/res.go b/pkg/resource/res.go index 64e06c7..0b7d8e5 100644 --- a/pkg/resource/res.go +++ b/pkg/resource/res.go @@ -129,7 +129,7 @@ func DataResourcesFromYaml(in []byte) (result []unstructured.Unstructured, err e // This is used when targeting PatchDesired resources type DesiredMatch map[*resource.DesiredComposed][]map[string]interface{} -// matchResources finds and associates the data to the desired resource +// MatchResources finds and associates the data to the desired resource // The length of the passed data should match the total count of desired match data func MatchResources(desired map[resource.Name]*resource.DesiredComposed, data []unstructured.Unstructured) (DesiredMatch, error) { // Iterate over the data patches and match them to desired resources