Skip to content

Commit

Permalink
Merge pull request #25 from Avarei/refactor/examples
Browse files Browse the repository at this point in the history
Refactor/examples
  • Loading branch information
Avarei authored Jul 6, 2024
2 parents 41a1a95 + 9cc3192 commit a43614b
Show file tree
Hide file tree
Showing 25 changed files with 309 additions and 168 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ pkl-release: check-tag pkl-package

PROJECT_DIR := $(dir $(firstword $(MAKEFILE_LIST)))
.PHONY: generate
generate: pkl-resolve pkl-resolve-hack
generate: 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
pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example -m crds xrds/ExampleXR.pkl
pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example compositions/inline.pkl > $(PROJECT_DIR)example/inline/composition.yaml
pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example compositions/uri.pkl > $(PROJECT_DIR)example/full/composition.yaml
pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example xrs/inline.pkl > $(PROJECT_DIR)example/inline/xr.yaml
pkl eval --working-dir $(PROJECT_DIR)pkl/crossplane.contrib.example xrs/uri.pkl > $(PROJECT_DIR)example/full/xr.yaml

.PHONY: build-image
build-image:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ spec:
kind: Pkl
spec:
type: uri
# This pkl file is at `pkl/crossplane.contrib.example/full.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/full.pkl"
# This pkl file is at `pkl/crossplane.contrib.example/compositions/steps/full.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/compositions/steps/full.pkl"
```
### Example
Expand Down
9 changes: 4 additions & 5 deletions example/full/composition.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-pkl
name: pkl-uri-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: uri
# This pkl file is at `pkl/crossplane.contrib.example/full.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/full.pkl"
uri: package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib.example@0.0.0#/compositions/steps/full.pkl
step: pkl-template
7 changes: 4 additions & 3 deletions example/full/xr.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
spec:
compositionRef:
name: pkl-uri-example
metadata:
name: example-xr
spec: {}
name: uri-example
7 changes: 4 additions & 3 deletions example/inline/xr.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
spec:
compositionRef:
name: inline-example
metadata:
name: example-xr
spec: {}
name: inline-example
4 changes: 2 additions & 2 deletions example/minimal/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ spec:
kind: Pkl
spec:
type: uri
# This pkl file is at `pkl/crossplane-example/minimal.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/minimal.pkl"
# This pkl file is at `pkl/crossplane-example/compositions/steps/minimal.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/compositions/steps/minimal.pkl"
2 changes: 1 addition & 1 deletion internal/function/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func convertExtraResources(extraResources map[string]*helper.ResourceSelector) m
ApiVersion: fixedrs.APIVersion,
Kind: fixedrs.Kind,
}
if fixedrs.Match.MatchLabels != nil && len(fixedrs.Match.MatchLabels.GetLabels()) > 0 {
if fixedrs.Match.MatchLabels != nil {
rs.Match = &fnv1beta1.ResourceSelector_MatchLabels{
MatchLabels: &fnv1beta1.MatchLabels{
Labels: fixedrs.Match.MatchLabels.GetLabels(),
Expand Down
58 changes: 56 additions & 2 deletions internal/function/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestRunFunction(t *testing.T) {
Type: "local",
Local: &v1beta1.Local{
ProjectDir: pklPackage,
File: pklPackage + "/minimal.pkl",
File: pklPackage + "/compositions/steps/minimal.pkl",
},
},
}),
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestRunFunction(t *testing.T) {
Type: "local",
Local: &v1beta1.Local{
ProjectDir: pklPackage,
File: pklPackage + "/full.pkl",
File: pklPackage + "/compositions/steps/full.pkl",
},
},
}),
Expand Down Expand Up @@ -242,6 +242,60 @@ func TestRunFunction(t *testing.T) {
},
},
},
"Event": {
reason: "Testing correct paththrough of req.desired to res.desired",
args: args{
ctx: context.TODO(),
req: &fnv1beta1.RunFunctionRequest{
Input: resource.MustStructObject(&v1beta1.Pkl{
Spec: v1beta1.PklSpec{
Type: "local",
Local: &v1beta1.Local{
ProjectDir: pklPackage,
File: pklPackage + "/compositions/steps/event.pkl",
},
},
}),
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "example.crossplane.io/v1",
"kind": "XR",
"status": {
"someStatus": "pretty status"
}
}`),
},
},
},
},
want: want{
rsp: &fnv1beta1.RunFunctionResponse{
Desired: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "example.crossplane.io/v1",
"kind": "XR",
"status": {
"someStatus": "pretty status"
}
}`),
},
},
Results: []*fnv1beta1.Result{
{
Severity: fnv1beta1.Severity_SEVERITY_WARNING,
Message: "I am an Event from Pkl!",
},
},
Meta: &fnv1beta1.ResponseMeta{
Ttl: &durationpb.Duration{
Seconds: 60,
},
},
},
},
},
}

for name, tc := range cases {
Expand Down
1 change: 0 additions & 1 deletion internal/pkl/reader/crossplane_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (f *CrossplaneReader) BaseRead(url url.URL) ([]byte, error) {
if err != nil {
return nil, err
}
fmt.Println(string(requestYaml))

return requestYaml, nil
default:
Expand Down
2 changes: 1 addition & 1 deletion pkl/crossplane.contrib.example/DEPLOY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Deploying this Example

## Prerequirements
* pkl cli
* [pkl cli](https://pkl-lang.org/main/current/pkl-cli/index.html#installation)
* crossplane in a cluster
* function-pkl deployed (see [here](../../README.md))
* provider-kubernetes deployed
Expand Down
4 changes: 2 additions & 2 deletions pkl/crossplane.contrib.example/DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pkl eval xrds/crd2module.pkl -m crds
## Create Pkl file for the Pipeline Step
The Pkl file referenced in the Composition must amend `CompositionResponse.pkl`.

The example at [full.pkl](full.pkl) gives an overview of what can be done.
The example at [full.pkl](compositions/steps/full.pkl) gives an overview of what can be done.

[CompositionResponse.pkl](../crossplane.contrib/CompositionResponse.pkl) contains documentation on what the individual fields do.

Expand All @@ -53,7 +53,7 @@ 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.
The Composition itself can be created in [yaml](../../example/full/composition.yaml) or in [pkl](compositions/inline.pkl). The latter is especially useful, if the the function is used with an inline Pkl file.


## Create PklProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
amends "@crossplane.contrib/Composition.pkl"

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

metadata {
Expand All @@ -21,7 +21,7 @@ spec {
input = new Pkl {
spec {
type = "inline"
inline = read("minimal-no-project.pkl").text
inline = read("steps/minimal-no-project.pkl").text
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions pkl/crossplane.contrib.example/compositions/steps/event.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
amends "@crossplane.contrib/CompositionResponse.pkl"
import "@crossplane.contrib/crossplane.pkl"

import ".../crds/XR.pkl"
import ".../crds/Object.pkl"

local request = new crossplane {
customResourceTemplates = new {
["XR"] {
["example.crossplane.io/v1"] = XR
}
["Object"] {
["kubernetes.crossplane.io/v1alpha2"] = Object
}
}
}.Request

// Pass through the Desired state from previous pipeline steps
desired = request.desired

results {
new {
severity = SEVERITY_WARNING
message = "I am an Event from Pkl!"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
amends "@crossplane.contrib/CompositionResponse.pkl"
import "@crossplane.contrib/crossplane.pkl"

import "crds/XR.pkl"
import "crds/Object.pkl"
import ".../crds/XR.pkl"
import ".../crds/Object.pkl"

import "@k8s/api/core/v1/ConfigMap.pkl"

Expand Down Expand Up @@ -78,6 +78,6 @@ context {
}
}

meta = if (request.meta != null) new ResponseMeta {
meta = if (request.meta != null) new {
ttl = 60.s
} else null
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
amends "@crossplane.contrib/CompositionResponse.pkl"
import "crds/Object.pkl"
import ".../crds/Object.pkl"
import "@k8s/api/core/v1/ConfigMap.pkl"

desired {
Expand Down
30 changes: 30 additions & 0 deletions pkl/crossplane.contrib.example/compositions/uri.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
amends "@crossplane.contrib/Composition.pkl"
import "@crossplane.contrib/Pkl.pkl"

import "../crds/XR.pkl"
import ".../PklProject"

metadata {
name = "pkl-uri-example"
}
spec {
compositeTypeRef {
apiVersion = XR.apiVersion
kind = XR.kind
}
mode = "Pipeline"
pipeline {
new {
step = "pkl-template"
functionRef {
name = "function-pkl"
}
input = new Pkl {
spec {
type = "uri"
uri = PklProject.package.baseUri + "@" + PklProject.package.version + "#/compositions/steps/full.pkl"
}
}
}
}
}
3 changes: 3 additions & 0 deletions pkl/crossplane.contrib.example/xrds/ExampleXR.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ spec {
kind = "XR"
plural = "xrs"
}
defaultCompositionRef {
name = import("../compositions/uri.pkl").metadata.name!!
}
versions {
new {
name = "v1"
Expand Down
11 changes: 11 additions & 0 deletions pkl/crossplane.contrib.example/xrs/inline.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
amends "../crds/XR.pkl"

metadata {
name = "inline-example"
}

spec {
compositionRef {
name = import("../compositions/inline.pkl").metadata.name!!
}
}
11 changes: 11 additions & 0 deletions pkl/crossplane.contrib.example/xrs/uri.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
amends "../crds/XR.pkl"

metadata {
name = "uri-example"
}

spec {
compositionRef {
name = import("../compositions/uri.pkl").metadata.name!!
}
}
Loading

0 comments on commit a43614b

Please sign in to comment.