Skip to content

Commit

Permalink
Start restructure pacakges
Browse files Browse the repository at this point in the history
add package to convert xrd pkl files to Classes.
Start adding a small guide to the Readme
  • Loading branch information
Avarei committed Jun 19, 2024
1 parent 2698982 commit e426080
Show file tree
Hide file tree
Showing 25 changed files with 906 additions and 90 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ BRANCH := $(shell git branch --show-current)
PKL_BASE_URI := package://pkg.pkl-lang.org

PKL_CORE_NAME := crossplane
PKL_CORE_VERSION := 0.0.29
PKL_CORE_VERSION := 1.0.0
PKL_CORE_REF := ${PKL_CORE_NAME}@${PKL_CORE_VERSION}
PKL_CORE_URI := ${PKL_BASE_URI}/${REPO}/${PKL_CORE_REF}

PKL_EXAMPLE_NAME := crossplane-example
PKL_EXAMPLE_VERSION := 0.1.19
PKL_EXAMPLE_VERSION := 1.0.0
PKL_EXAMPLE_REF := ${PKL_EXAMPLE_NAME}@${PKL_EXAMPLE_VERSION}
PKL_EXAMPLE_URI := ${PKL_BASE_URI}/${REPO}/${PKL_CORE_REF}

Expand Down
76 changes: 74 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,84 @@ spec:
spec:
type: uri
# This pkl file is at `pkl/crossplane-example/full.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@0.1.19#/full.pkl"
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@1.0.0#/full.pkl"
```
### Example
see [examples](./example/)
### Building a new Composition Function from Scratch
#### Create a XRD
If you already have an XRD skip this and the next section.
Create a new Pkl file
```pkl
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/CompositeResourceDefinition.pkl"
```
the Pkl file can be rendered as Yaml using `pkl eval <nameOfYourPklFile>.pkl`

#### Create a Module of your XRD
To Convert the XRD.pkl to Pkl Module a small helper file `xrd2module.pkl` is needed:
```pkl
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]#/generate.pkl"
crds {
import("XR.pkl")
}
k8sImportPath = "@k8s"
crossplaneImportPath = "@crossplane.contrib"
```
running `pkl eval generate.pkl -m .` will generate a new Module that can be imported and referenced in the Composition.

#### Create Managed Resources in Pkl
Import Managed Resources and Preexisting CompositeResourceDefinitions to Pkl
```bash
pkl eval "package://pkg.pkl-lang.org/pkl-pantry/[email protected]#/generate.pkl" \
-m . \
-p source="https://raw.githubusercontent.com/crossplane-contrib/provider-kubernetes/main/package/crds/kubernetes.crossplane.io_objects.yaml"
```

#### Create the Composition Function Logic
The Function must amend CompositionResponse.pkl.

This is a very minimal example. A more extensive one can be found in `pkl/crossplane.contrib.example`
```pkl
amends "crossplane.contrib/CompositionResponse.pkl"
import "crds/Object.pkl"
import "@k8s/api/core/v1/ConfigMap.pkl"
desired {
resources {
["example"] = new Resource {
resource = new Object {
metadata {
name = "example"
}
spec {
forProvider {
manifest = new ConfigMap {
metadata {
name = "example"
namespace = "crossplane-system"
}
data {
["hello"] = "world"
}
}
}
}
}
}
}
}
```
#### Create Composition
TODO.



## Building a Pkl Package
A Pkl Package can be built in the following steps:
1. Create Pkl files in a directory
Expand All @@ -59,7 +131,7 @@ A Pkl Package can be built in the following steps:

## Basic Pkl File
Pkl Files used in this Function **must** amend CompositionRequest.pkl.
see [here](example/inline/composition.yaml) and [here](pkl/crossplane-example/full.pkl)
see [here](example/inline/composition.yaml) and [here](pkl/crossplane.contrib.example/full.pkl)

### Generating Pkl Files and Modules from Manifests
There are some package to make it easier to convert existing CRDs or Manifests into the Pkl format.
Expand Down
2 changes: 1 addition & 1 deletion example/full/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ spec:
spec:
type: uri
# This pkl file is at `pkl/crossplane-example/full.pkl` in this repo
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@0.1.19#/full.pkl"
uri: "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@1.0.0#/full.pkl"
10 changes: 5 additions & 5 deletions example/inline/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ spec:
type: inline
# This pkl file is at `pkl/crossplane-example/full.pkl` in this repo
inline: |
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@0.0.29#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@0.0.29#/Resource.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@0.0.29#/Crossplane.pkl"
amends "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@1.0.0#/CompositionResponse.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@1.0.0#/Resource.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane@1.0.0#/Crossplane.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@0.1.19#/crds/XR.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@0.1.19#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@1.0.0#/crds/XR.pkl"
import "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@1.0.0#/crds/Object.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/ConfigMap.pkl"
Expand Down
2 changes: 1 addition & 1 deletion internal/function/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

var (
pklPackage = "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@0.1.19"
pklPackage = "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane-example@1.0.0"
)

func TestRunFunction(t *testing.T) {
Expand Down
33 changes: 0 additions & 33 deletions pkl/crossplane-example/PklProject.deps.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
amends "pkl:Project"

package {
name = "crossplane-example"
name = "crossplane.contrib.example"
authors {
"Tim Geimer <[email protected]>"
}
version = "0.1.19"
version = "1.0.0"
baseUri = "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/\(name)"
packageZipUrl = "https://github.com/crossplane-contrib/function-pkl/releases/download/\(name)@\(version)/\(name)@\(version).zip"
sourceCode = "https://github.com/crossplane-contrib/function-pkl"
Expand All @@ -35,7 +35,6 @@ dependencies {
["k8s"] {
uri = "package://pkg.pkl-lang.org/pkl-k8s/[email protected]"
}
["crossplane"] {
uri = "package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]"
}
["crossplane.contrib"] = import("../crossplane.contrib/PklProject")
["crossplane.contrib.xrd"] = import("../crossplane.contrib.xrd/PklProject")
}
64 changes: 64 additions & 0 deletions pkl/crossplane.contrib.example/PklProject.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"schemaVersion": 1,
"resolvedDependencies": {
"package://pkg.pkl-lang.org/pkl-k8s/k8s@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-k8s/[email protected]",
"checksums": {
"sha256": "75c6d66d94c335417a3c502e107aaeadf7a60b4e1d34b2c979afe11193205a1a"
}
},
"package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crospslane.contrib.xrd@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]",
"path": "../crossplane.contrib.xrd"
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.deepToTyped@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "0746fe618d59187b15ca3e589caf359a285c74efadd8c796da86aaa08202d542"
}
},
"package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "b76f2ea3fd7252b7d6ec4ac7331dea197b4ce9846adc86c3853c13c48b358d13"
}
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.syntax@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "83ad123a9de2e1e559dbf72370ec8d0353b8d017f37823cdd4ab71f0ce04eb18"
}
},
"package://pkg.pkl-lang.org/pkl-pantry/pkl.experimental.uri@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "0b1db5755fa0c7651d5c62e0d5ef8a9ed4ed6411fe31769d06714600162e1589"
}
},
"package://pkg.pkl-lang.org/pkl-pantry/org.json_schema@1": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "7a11e61df5248c81b9256cae11ae333c13aa5a05101aff1d7b26d7a2e825df96"
}
},
"package://pkg.pkl-lang.org/pkl-pantry/org.openapis.v3@2": {
"type": "remote",
"uri": "projectpackage://pkg.pkl-lang.org/pkl-pantry/[email protected]",
"checksums": {
"sha256": "a3ce491d0e504c14d456d599f988bbb4a2b77915696450dfd65b8e4cef7c9522"
}
},
"package://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/crossplane.contrib@1": {
"type": "local",
"uri": "projectpackage://pkg.pkl-lang.org/github.com/crossplane-contrib/function-pkl/[email protected]",
"path": "../crossplane.contrib"
}
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
amends "@crossplane/CompositionResponse.pkl"
import "@crossplane/Resource.pkl"
import "@crossplane/Crossplane.pkl"
amends "crossplane.contrib/CompositionResponse.pkl"
import "crossplane.contrib/Resource.pkl"
import "crossplane.contrib/crossplane.pkl"

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

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

local request = new Crossplane {
local request = new crossplane {
customResourceTemplates = new {
["XR"] {
["example.crossplane.io/v1"] = XR
Expand Down
1 change: 1 addition & 0 deletions pkl/crossplane.contrib.example/xrds/ExampleXR.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
amends "@crossplane.contrib.xrd/C"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module io.crossplane.apiextensions.v1.CompositeResourceDefinition
extends "@k8s/K8sResource.pkl"

import "@k8s/apimachinery/pkg/apis/meta/v1/ObjectMeta.pkl"
import "@openapiv3/Schema.pkl" as Openapiv3Schema
import "@k8s/apiextensions-apiserver/pkg/apis/apiextensions/v1/CustomResourceDefinition.pkl"

fixed apiVersion: "apiextensions.crossplane.io/v1"

Expand Down Expand Up @@ -320,7 +320,7 @@ class AdditionalPrinterColumn {
/// schema results in a schema that contains only the fields required by all composite resources.
class Schema {
/// OpenAPIV3Schema is the OpenAPI v3 schema to use for validation and pruning.
openAPIV3Schema: Openapiv3Schema?
openAPIV3Schema: CustomResourceDefinition.JSONSchemaProps?
}

/// CompositeResourceDefinitionStatus shows the observed state of the definition.
Expand Down
47 changes: 47 additions & 0 deletions pkl/crossplane.contrib.xrd/PklProject
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
// 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 = "github.com/crossplane-contrib/function-pkl"

package {
name = "crospslane.contrib.xrd"
authors {
"Tim Geimer <[email protected]>"
}
version = "1.0.0"
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"] = import("../crossplane.contrib/PklProject")

["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]" }

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

0 comments on commit e426080

Please sign in to comment.