Skip to content

Commit

Permalink
Merge pull request #222 from Wompipomp/add-conditions-and-events
Browse files Browse the repository at this point in the history
Add conditions and events
  • Loading branch information
Peefy authored Jan 3, 2025
2 parents 2b186b5 + a7bc48b commit 813f284
Show file tree
Hide file tree
Showing 19 changed files with 697 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ vendor/
.kclvm
.DS_store
package/*.xpkg

.idea
.vscode
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ bar:
# Omitted for brevity
```
You can access the retrieved resources in your code like this:
> Note that Crossplane performs an additional reconciliation pass for extra resources.
> Consequently, during the initial execution, these resources may be uninitialized. It is essential to implement checks to handle this scenario.
```yaml
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
Expand All @@ -522,7 +524,8 @@ spec:
source: |
er = option("params")?.extraResources
name = er?.bar[0]?.Resource?.metadata?.name or ""
if er?.bar:
name = er?.bar[0]?.Resource?.metadata?.name or ""
# Omit other logic
```

Expand All @@ -544,6 +547,89 @@ spec:
items = [dxr] # Omit other resources
```

### Settings conditions and events

> This feature requires Crossplane v1.17 or newer.

You can set conditions and events directly from KCL, either in the composite resource or both the composite and claim resources.
To set one or more conditions, use the following approach:
```yaml
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
metadata:
annotations:
"krm.kcl.dev/default_ready": "True"
name: basic
spec:
source: |
oxr = option("params").oxr
dxr = {
**oxr
}
conditions = {
apiVersion: "meta.krm.kcl.dev/v1alpha1"
kind: "Conditions"
conditions = [
{
target: "CompositeAndClaim"
force: False
condition = {
type: "DatabaseReady"
status: "False"
reason: "FailedToCreate"
message: "Encountered an error creating the database"
}
}
]
}
items = [
conditions
dxr
]
```

- **target**: Specifies whether the condition should be present in the composite resource or both the composite and claim resources. Possible values are `CompositeAndClaim` and `Composite`
- **force**: Forces the overwrite of existing conditions. If a condition with the same `type` already exists, it will not be overwritten by default. Setting force to `True` will overwrite the first condition.

You can also set events as follows:
```yaml
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
metadata:
annotations:
"krm.kcl.dev/default_ready": "True"
name: basic
spec:
source: |
oxr = option("params").oxr
dxr = {
**oxr
}
events = {
apiVersion: "meta.krm.kcl.dev/v1alpha1"
kind: "Events"
events = [
{
target: "CompositeAndClaim"
event = {
type: "Warning"
reason: "ResourceLimitExceeded"
message: "The resource limit has been exceeded"
}
}
]
}
items = [
events
dxr
]
```

## Library

You can directly use [KCL standard libraries](https://kcl-lang.io/docs/reference/model/overview) such as `regex.match`, `math.log`.
Expand Down
2 changes: 2 additions & 0 deletions examples/default/conditions/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
crossplane render --verbose xr.yaml composition.yaml functions.yaml -r
62 changes: 62 additions & 0 deletions examples/default/conditions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Example Manifests

You can run your function locally and test it using `crossplane render`
with these example manifests.

```shell
# Run the function locally
$ go run . --insecure --debug
```

```shell
# Then, in another terminal, call it with these example manifests
$ crossplane render --verbose xr.yaml composition.yaml functions.yaml -r --extra-resources extra_resources.yaml
---
---
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
status:
conditions:
- lastTransitionTime: "2024-01-01T00:00:00Z"
message: 'Unready resources: another-awesome-dev-bucket, my-awesome-dev-bucket'
reason: Creating
status: "False"
type: Ready
---
apiVersion: example/v1alpha1
kind: Foo
metadata:
annotations:
crossplane.io/composition-resource-name: another-awesome-dev-bucket
generateName: example-
labels:
crossplane.io/composite: example
name: another-awesome-dev-bucket
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
---
apiVersion: example/v1alpha1
kind: Foo
metadata:
annotations:
crossplane.io/composition-resource-name: my-awesome-dev-bucket
generateName: example-
labels:
crossplane.io/composite: example
name: my-awesome-dev-bucket
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""

```
49 changes: 49 additions & 0 deletions examples/default/conditions/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-template-go
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: normal
functionRef:
name: kcl-function
input:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
metadata:
annotations:
"krm.kcl.dev/default_ready": "True"
name: basic
spec:
source: |
oxr = option("params").oxr
dxr = {
**oxr
}
conditions = {
apiVersion: "meta.krm.kcl.dev/v1alpha1"
kind: "Conditions"
conditions = [
{
target: "CompositeAndClaim"
force: False
condition = {
type: "DatabaseReady"
status: "False"
reason: "FailedToCreate"
message: "Encountered an error creating the database"
}
}
]
}
items = [
conditions
dxr
]
9 changes: 9 additions & 0 deletions examples/default/conditions/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: kcl-function
annotations:
# This tells crossplane render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest
6 changes: 6 additions & 0 deletions examples/default/conditions/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
count: 1
2 changes: 2 additions & 0 deletions examples/default/events/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
crossplane render --verbose xr.yaml composition.yaml functions.yaml -r
62 changes: 62 additions & 0 deletions examples/default/events/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Example Manifests

You can run your function locally and test it using `crossplane render`
with these example manifests.

```shell
# Run the function locally
$ go run . --insecure --debug
```

```shell
# Then, in another terminal, call it with these example manifests
$ crossplane render --verbose xr.yaml composition.yaml functions.yaml -r --extra-resources extra_resources.yaml
---
---
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
status:
conditions:
- lastTransitionTime: "2024-01-01T00:00:00Z"
message: 'Unready resources: another-awesome-dev-bucket, my-awesome-dev-bucket'
reason: Creating
status: "False"
type: Ready
---
apiVersion: example/v1alpha1
kind: Foo
metadata:
annotations:
crossplane.io/composition-resource-name: another-awesome-dev-bucket
generateName: example-
labels:
crossplane.io/composite: example
name: another-awesome-dev-bucket
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""
---
apiVersion: example/v1alpha1
kind: Foo
metadata:
annotations:
crossplane.io/composition-resource-name: my-awesome-dev-bucket
generateName: example-
labels:
crossplane.io/composite: example
name: my-awesome-dev-bucket
ownerReferences:
- apiVersion: example.crossplane.io/v1beta1
blockOwnerDeletion: true
controller: true
kind: XR
name: example
uid: ""

```
46 changes: 46 additions & 0 deletions examples/default/events/composition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-template-go
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1beta1
kind: XR
mode: Pipeline
pipeline:
- step: normal
functionRef:
name: kcl-function
input:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLInput
metadata:
annotations:
"krm.kcl.dev/default_ready": "True"
name: basic
spec:
source: |
oxr = option("params").oxr
dxr = {
**oxr
}
events = {
apiVersion: "meta.krm.kcl.dev/v1alpha1"
kind: "Events"
events = [
{
target: "CompositeAndClaim"
event = {
type: "Warning"
reason: "ResourceLimitExceeded"
message: "The resource limit has been exceeded"
}
}
]
}
items = [
events
dxr
]
9 changes: 9 additions & 0 deletions examples/default/events/functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: kcl-function
annotations:
# This tells crossplane render to connect to the function locally.
render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/crossplane-contrib/function-kcl:latest
6 changes: 6 additions & 0 deletions examples/default/events/xr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: example.crossplane.io/v1beta1
kind: XR
metadata:
name: example
spec:
count: 1
Loading

0 comments on commit 813f284

Please sign in to comment.