Created
July 17, 2024 18:53
-
-
Save gotwarlost/32c1933c424f2294dffa9cd6c5cdeb07 to your computer and use it in GitHub Desktop.
Tooling to convert composition implementation into a self-contained script embedded as text in the object
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
go install github.com/crossplane-contrib/function-cue/cmd/fn-cue-tools@v0.3.0 | |
mkdir -p zz_generated/scripts | |
fn-cue-tools package-script ./implementation --pkg scripts --var replicatedmap >zz_generated/scripts/file-to-avoid.cue | |
cue eval ./definition --out yaml | |
-- cue.mod/module.cue -- | |
module: "cue-functions.io/examples/simple" | |
language: version: "v0.9.0" | |
-- definition/composition.cue -- | |
package definition | |
import ( | |
scripts "cue-functions.io/examples/simple/zz_generated/scripts" | |
) | |
let version = "v1alpha1" | |
let pluralName = "xreplicatedmaps" | |
let xrdKind = "XReplicatedMap" | |
let groupName = "simple.cuefn.example.com" | |
{ | |
let fullName = "\(pluralName).\(groupName)" | |
apiVersion: "apiextensions.crossplane.io/v1" | |
kind: "Composition" | |
metadata: { | |
name: fullName | |
labels: { | |
"crossplane.io/xrd": fullName | |
} | |
} | |
spec: { | |
compositeTypeRef: { | |
apiVersion: "\(groupName)/\(version)" | |
kind: xrdKind | |
} | |
resources: [] | |
mode: "Pipeline" | |
pipeline: [ | |
{ | |
step: "run cue composition" | |
functionRef: name: "fn-cue-examples-simple" | |
input: { | |
apiVersion: "function-cue/v1" // value does not matter | |
kind: "CueFunctionInput" // ditto | |
source: "Inline" | |
script: scripts.replicatedmap | |
debugNew: true | |
} | |
}, | |
{ | |
step: "run auto ready" | |
functionRef: name: "fn-auto-ready" | |
}, | |
] | |
} | |
} | |
-- implementation/init.cue -- | |
package implementation | |
#request: {...} | |
composite: #request.observed.composite.resource | |
compName: composite.metadata.name | |
params: composite.spec.parameters | |
configMapName: [ | |
if params.name != _|_ {params.name}, | |
composite.metadata.labels["crossplane.io/claim-name"], | |
][0] | |
-- implementation/resources.cue -- | |
package implementation | |
for ns in params.namespaces { | |
response: desired: resources: "config_map_\(ns)": resource: { | |
apiVersion: "kubernetes.crossplane.io/v1alpha2" | |
kind: "Object" | |
metadata: name: "cm-\(compName)-\(ns)" | |
spec: forProvider: manifest: { | |
apiVersion: "v1" | |
kind: "ConfigMap" | |
metadata: { | |
namespace: ns | |
name: configMapName | |
labels: composite.metadata.labels | |
} | |
data: params.data | |
} | |
} | |
} |
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
package definition | |
import ( | |
scripts "cue-functions.io/examples/simple/zz_generated/scripts" | |
) | |
let version = "v1alpha1" | |
let pluralName = "xreplicatedmaps" | |
let xrdKind = "XReplicatedMap" | |
let groupName = "simple.cuefn.example.com" | |
{ | |
let fullName = "\(pluralName).\(groupName)" | |
apiVersion: "apiextensions.crossplane.io/v1" | |
kind: "Composition" | |
metadata: { | |
name: fullName | |
labels: { | |
"crossplane.io/xrd": fullName | |
} | |
} | |
spec: { | |
compositeTypeRef: { | |
apiVersion: "\(groupName)/\(version)" | |
kind: xrdKind | |
} | |
resources: [] | |
mode: "Pipeline" | |
pipeline: [ | |
{ | |
step: "run cue composition" | |
functionRef: name: "fn-cue-examples-simple" | |
input: { | |
apiVersion: "function-cue/v1" // value does not matter | |
kind: "CueFunctionInput" // ditto | |
source: "Inline" | |
script: scripts.replicatedmap | |
debugNew: true | |
} | |
}, | |
{ | |
step: "run auto ready" | |
functionRef: name: "fn-auto-ready" | |
}, | |
] | |
} | |
} |
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
apiVersion: apiextensions.crossplane.io/v1 | |
kind: Composition | |
metadata: | |
name: xreplicatedmaps.simple.cuefn.example.com | |
labels: | |
crossplane.io/xrd: xreplicatedmaps.simple.cuefn.example.com | |
spec: | |
compositeTypeRef: | |
apiVersion: simple.cuefn.example.com/v1alpha1 | |
kind: XReplicatedMap | |
resources: [] | |
mode: Pipeline | |
pipeline: | |
- step: run cue composition | |
functionRef: | |
name: fn-cue-examples-simple | |
input: | |
apiVersion: function-cue/v1 | |
kind: CueFunctionInput | |
source: Inline | |
script: | | |
package implementation | |
{ | |
for ns in params.namespaces { | |
response: desired: resources: "config_map_\(ns)": resource: { | |
apiVersion: "kubernetes.crossplane.io/v1alpha2" | |
kind: "Object" | |
metadata: name: "cm-\(compName)-\(ns)" | |
spec: forProvider: manifest: { | |
apiVersion: "v1" | |
kind: "ConfigMap" | |
metadata: { | |
namespace: ns | |
name: configMapName | |
labels: composite.metadata.labels | |
} | |
data: params.data | |
} | |
} | |
} | |
} | |
#request: { | |
... | |
} | |
composite: #request.observed.composite.resource | |
compName: composite.metadata.name | |
params: composite.spec.parameters | |
configMapName: [ | |
if params.name != _|_ // explicit error (_|_ literal) in source | |
{ | |
params.name | |
}, composite.metadata.labels["crossplane.io/claim-name"]][0] | |
debugNew: true | |
- step: run auto ready | |
functionRef: | |
name: fn-auto-ready |
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
package scripts | |
// generated by fn-cue-tools, DO NOT EDIT | |
replicatedmap: "package implementation\n\n{\n\tfor ns in params.namespaces {\n\t\tresponse: desired: resources: \"config_map_\\(ns)\": resource: {\n\t\t\tapiVersion: \"kubernetes.crossplane.io/v1alpha2\"\n\t\t\tkind: \"Object\"\n\t\t\tmetadata: name: \"cm-\\(compName)-\\(ns)\"\n\t\t\tspec: forProvider: manifest: {\n\t\t\t\tapiVersion: \"v1\"\n\t\t\t\tkind: \"ConfigMap\"\n\t\t\t\tmetadata: {\n\t\t\t\t\tnamespace: ns\n\t\t\t\t\tname: configMapName\n\t\t\t\t\tlabels: composite.metadata.labels\n\t\t\t\t}\n\t\t\t\tdata: params.data\n\t\t\t}\n\t\t}\n\t}\n}\n#request: {\n\t...\n}\ncomposite: #request.observed.composite.resource\ncompName: composite.metadata.name\nparams: composite.spec.parameters\nconfigMapName: [\n\t\tif params.name != _|_ // explicit error (_|_ literal) in source\n\t\t{\n\t\tparams.name\n\t}, composite.metadata.labels[\"crossplane.io/claim-name\"]][0]\n" | |
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
package implementation | |
#request: {...} | |
composite: #request.observed.composite.resource | |
compName: composite.metadata.name | |
params: composite.spec.parameters | |
configMapName: [ | |
if params.name != _|_ {params.name}, | |
composite.metadata.labels["crossplane.io/claim-name"], | |
][0] |
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
package implementation | |
for ns in params.namespaces { | |
response: desired: resources: "config_map_\(ns)": resource: { | |
apiVersion: "kubernetes.crossplane.io/v1alpha2" | |
kind: "Object" | |
metadata: name: "cm-\(compName)-\(ns)" | |
spec: forProvider: manifest: { | |
apiVersion: "v1" | |
kind: "ConfigMap" | |
metadata: { | |
namespace: ns | |
name: configMapName | |
labels: composite.metadata.labels | |
} | |
data: params.data | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment