Skip to content

Commit

Permalink
Merge pull request #18 from Avarei/pkl-compositions
Browse files Browse the repository at this point in the history
Pkl compositions
  • Loading branch information
Avarei authored Jul 3, 2024
2 parents e477f50 + 8f94e30 commit b8d8be8
Show file tree
Hide file tree
Showing 25 changed files with 1,422 additions and 239 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ go.work
.idea/

# Temporary Debug Binaries
__debug_bin*
__debug_bin*

# Pkl
*PklProject.deps.json
30 changes: 18 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ TARGET =? $(shell git branch --show-current)
LATEST_CORE := $(shell git tag -l "crossplane.contrib@*.*.*" --sort=-v:refname | head -n 1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
LATEST_EXAMPLE := $(shell git tag -l "crossplane.contrib.example@*.*.*" --sort=-v:refname | head -n 1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
LATEST_XRD := $(shell git tag -l "crossplane.contrib.xrd@*.*.*" --sort=-v:refname | head -n 1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
REPO_PARAM := $(if $(REPO),-e REPOSITORY="$(REPO)")
CORE_PARAM := $(if $(LATEST_CORE),-e CROSSPLANE_CONTRIB_VERSION="$(LATEST_CORE)")
EXAMPLE_PARAM := $(if $(LATEST_EXAMPLE),-e CROSSPLANE_CONTRIB_EXAMPLE_VERSION="$(LATEST_EXAMPLE)")
XRD_PARAM := $(if $(LATEST_XRD),-e CROSSPLANE_CONTRIB_XRD_VERSION="$(LATEST_XRD)")

# This Resolves the Dependencies and sets the versions of our packages to the Latest ones for the package in Git
.PHONY: pkl-resolve
pkl-resolve:
pkl project resolve \
-e REPOSITORY="$(REPO)" \
-e CROSSPLANE_CONTRIB_VERSION="$(LATEST_CORE)" \
-e CROSSPLANE_CONTRIB_EXAMPLE_VERSION="$(LATEST_EXAMPLE)" \
-e CROSSPLANE_CONTRIB_XRD_VERSION="$(LATEST_XRD)" \
./pkl/*/
pkl project resolve $(REPO_PARAM) $(CORE_PARAM) $(EXAMPLE_PARAM) $(XRD_PARAM) ./pkl/*/

.PHONY: pkl-resolve-hack
pkl-resolve-hack:
pkl project resolve ./hack/pklcrd/

.PHONY: pkl-package
pkl-package: pkl-resolve
$(eval PACKAGE_FILES := $(shell \
pkl project package \
-e REPOSITORY="$(REPO)" \
-e CROSSPLANE_CONTRIB_VERSION="$(LATEST_CORE)" \
-e CROSSPLANE_CONTRIB_EXAMPLE_VERSION="$(LATEST_EXAMPLE)" \
-e CROSSPLANE_CONTRIB_XRD_VERSION="$(LATEST_XRD)" \
./pkl/*/ ))
pkl project package $(REPO_PARAM) $(CORE_PARAM) $(EXAMPLE_PARAM) $(XRD_PARAM) ./pkl/*/ ))

# Ensures the TAG Variable is set.
.PHONY: check-tag
Expand All @@ -53,6 +51,14 @@ pkl-release: check-tag pkl-package
--target ${TARGET} \
$(RELEASE_FILES)

PROJECT_DIR := $(dir $(firstword $(MAKEFILE_LIST)))
.PHONY: generate
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:
docker build --build-arg -t runtime .
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ spec:
functionRef:
name: function-pkl
input:
apiVersion: template.fn.crossplane.io/v1beta1
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/crossplane.contrib.example@1.0.0#/full.pkl"
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected].1#/full.pkl"
```
### Example
Expand Down
4 changes: 2 additions & 2 deletions example/full/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ spec:
functionRef:
name: function-pkl
input:
apiVersion: template.fn.crossplane.io/v1beta1
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/crossplane.contrib.example@1.0.0#/full.pkl"
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected].1#/full.pkl"
80 changes: 11 additions & 69 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: template.fn.crossplane.io/v1beta1
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/[email protected]#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/Resource.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/Crossplane.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/crds/XR.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/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/[email protected]#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/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: 2 additions & 2 deletions example/minimal/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ spec:
functionRef:
name: function-pkl
input:
apiVersion: template.fn.crossplane.io/v1beta1
apiVersion: pkl.fn.crossplane.io/v1beta1
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/crossplane-example@1.0.0#/minimal.pkl"
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected].1#/minimal.pkl"
55 changes: 55 additions & 0 deletions hack/pklcrd/PklProject
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

/// Templates for configuring [Kubernetes](https://kubernetes.io).
amends "pkl:Project"

local repo = read?("env:REPOSITORY") ?? "github.com/crossplane-contrib/function-pkl"
local packageVersion: String = "0.0.0"

package {
name = "pklcrd"
authors {
"Tim Geimer <[email protected]>"
}
version = packageVersion
baseUri = "package://pkg.pkl-lang.org/\(repo)/\(name)"
packageZipUrl = "https://\(repo)/releases/download/\(name)@\(version)/\(name)@\(version).zip"
sourceCode = "https://\(repo)"

license = "Apache-2.0"
description = """
Convert CompositeResourceDefitions to Modules that can be amended
"""
}
dependencies {
["crossplane.contrib.xrd"] = import("../../pkl/crossplane.contrib.xrd/PklProject")

["k8s.contrib.crd"] {
uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]"
}
["k8s"] {
uri = "package://pkg.pkl-lang.org/pkl-k8s/[email protected]"
}

//workaround
["jsonschema"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
["jsonschema.contrib"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
["deepToTyped"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
["uri"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
["syntax"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
["openapiv3"] { uri = "package://pkg.pkl-lang.org/pkl-pantry/[email protected]" }
}
6 changes: 6 additions & 0 deletions hack/pklcrd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generate the Pkl.pkl and Composition.pkl

```shell
pkl eval -m ../../pkl/crossplane-contrib crd2module.pkl
pkl eval -m ../../pkl/crossplane-contrib crd2module-composition-fix.pkl
```
15 changes: 15 additions & 0 deletions hack/pklcrd/crd2module-composition-fix.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
amends "generate.pkl"

import "@k8s/K8sResource.pkl"

source = "https://raw.githubusercontent.com/crossplane/crossplane/v1.16.0/cluster/crds/apiextensions.crossplane.io_compositions.yaml"

// These Converters Provide Type Safety on the Object resource. Without them manifest would be of type "Dynamic"
converters {
["compositions.apiextensions.crossplane.io"] {
[List("spec", "pipeline", "pipeline", "input")] = K8sResource
}
}

// The Package references to be used within the new Module
k8sImportPath = "@k8s" // package://pkg.pkl-lang.org/pkl-k8s/k8s@<version>#
6 changes: 6 additions & 0 deletions hack/pklcrd/crd2module.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
amends "@k8s.contrib.crd/generate.pkl"

source = "package/input/pkl.fn.crossplane.io_pkls.yaml"

// The Package references to be used within the new Module
k8sImportPath = "@k8s" // package://pkg.pkl-lang.org/pkl-k8s/k8s@<version>#
Loading

0 comments on commit b8d8be8

Please sign in to comment.