Skip to content

Commit

Permalink
update and autogenerate example/inline/composition
Browse files Browse the repository at this point in the history
Signed-off-by: Tim <32556895+Avarei@users.noreply.github.com>
  • Loading branch information
Avarei committed Jul 3, 2024
1 parent 99c897e commit 8f94e30
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ generate: pkl-resolve pkl-resolve-hack
go generate ./...
pkl eval --working-dir $(PROJECT_DIR)hack/pklcrd -m ../../pkl/crossplane.contrib crd2module.pkl
pkl eval --working-dir $(PROJECT_DIR)hack/pklcrd -m ../../pkl/crossplane.contrib crd2module-composition-fix.pkl

pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example composition-inline.pkl > $(PROJECT_DIR)example/inline/composition.yaml

.PHONY: build-image
build-image:
Expand Down
78 changes: 10 additions & 68 deletions example/inline/composition.yaml
Original file line number Diff line number Diff line change
@@ -1,68 +1,27 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-pkl
name: inline-example
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: run-the-template
functionRef:
- functionRef:
name: function-pkl
input:
apiVersion: pkl.fn.crossplane.io/v1beta1
kind: Pkl
spec:
type: inline
# This pkl file is at `pkl/crossplane.contrib.example/full.pkl` in this repo
inline: |
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@1.0.0#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@1.0.0#/Resource.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@1.0.0#/Crossplane.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib.example@1.0.0#/crds/XR.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib.example@1.0.0#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/ConfigMap.pkl"
local request = new Crossplane {
customResourceTemplates = new {
["XR"] {
["example.crossplane.io/v1"] = XR
}
["Object"] {
["kubernetes.crossplane.io/v1alpha2"] = Object
}
}
}.Request
local observedCompositeResource: XR = request.observed.composite.resource as XR
local extraResource: Object? = request.getExtraResource("ineed", 0)?.resource as Object?
requirements {
extraResources {
["ineed"] {
apiVersion = Object.apiVersion
kind = Object.kind
match {
matchName = "required"
}
}
}
}
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@0.0.1#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib.example@0.0.1#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.1.0#/api/core/v1/ConfigMap.pkl"
desired {
composite {
resource = new XR {
status {
someStatus = "pretty status"
}
}
}
resources {
["cm-one"] = new Resource {
["cm-minimal"] = new {
resource = new Object {
metadata {
name = "cm-one"
Expand All @@ -71,35 +30,18 @@ spec:
forProvider {
manifest = new ConfigMap {
metadata {
name = "cm-one"
name = "cm-minimal"
namespace = "crossplane-system"
}
data {
["foo"] = observedCompositeResource?.metadata?.name ?? throw("Composite could not find observed composite name")
["required"] = extraResource?.metadata?.name ?? "i could not find what I needed..."
["foo"] = "bar"
}
}
}
}
}
ready = Ready_READY_TRUE
}
}
}
results {
new {
severity = Severity_SEVERITY_NORMAL
message = "welcome"
}
}
context {
["greetings"] = "with <3 from function-pkl"
when (request.context.containsKey("apiextensions.crossplane.io/environment")) {
["apiextensions.crossplane.io/environment"] = request.context.getOrNull("apiextensions.crossplane.io/environment")
}
}
meta = if (request.meta != null) new ResponseMeta {
ttl = 60.s
} else null
type: inline
step: pkl-template
4 changes: 4 additions & 0 deletions pkl/crossplane.contrib.example/DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ local request = new crossplane {
```
To Parse the Request automatically into Pkl Language all K8sResources used must be declared in customResourceTemplates in the specified form.

## Create a Composition
The Composition itself can be created in [yaml](../../example/full/composition.yaml) or in [pkl](composition-inline.pkl). The latter is especially useful, if the the function is used with an inline Pkl file.


## Create PklProject
[PklProject](PklProject) contains the dependencies of the Project as well as Metadata on where it will be Published, version and name.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ amends "@crossplane.contrib/Composition.pkl"

import "crds/XR.pkl"
import "@crossplane.contrib/Pkl.pkl"
import "@crossplane.contrib/CompositionResponse.pkl"

local renderer = new PcfRenderer{
omitNullProperties = true
}

metadata {
name = "inline-example"
Expand All @@ -26,13 +21,7 @@ spec {
input = new Pkl {
spec {
type = "inline"
inline = new CompositionResponse {
results {
new {
message = "hi"
}
}
}.toPrettyString()
inline = read("minimal-no-project.pkl").text
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions pkl/crossplane.contrib.example/minimal-no-project.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@0.0.1#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib.example@0.0.1#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.1.0#/api/core/v1/ConfigMap.pkl"

desired {
resources {
["cm-minimal"] = new {
resource = new Object {
metadata {
name = "cm-one"
}
spec {
forProvider {
manifest = new ConfigMap {
metadata {
name = "cm-minimal"
namespace = "crossplane-system"
}
data {
["foo"] = "bar"
}
}
}
}
}
}
}
}

0 comments on commit 8f94e30

Please sign in to comment.