Skip to content

Commit 8209935

Browse files
chore(docs): add optionals example documentation (#605)
* chore(docs): add optionals example documentation * chore(docs): update RGD documentation headers and examples
1 parent 2c9b0d4 commit 8209935

File tree

2 files changed

+118
-7
lines changed

2 files changed

+118
-7
lines changed

website/docs/docs/concepts/00-resource-group-definitions.md

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ kro automatically:
123123
- Validates that referenced resources exist
124124
- Updates these fields as your resources change
125125
126-
## ResourceGraphDefinition Processing
126+
## Processing
127127
128128
When you create a **ResourceGraphDefinition**, kro processes it in several steps to ensure
129129
correctness and set up the necessary components:
@@ -164,7 +164,7 @@ etc.) automatically.
164164
kro continuously monitors your ResourceGraphDefinition for changes, updating the API and
165165
its behavior accordingly.
166166

167-
## ResourceGraphDefinition Instance Example
167+
## Instance Example
168168

169169
After the **ResourceGraphDefinition** is validated and registered in the cluster, users
170170
can create instances of it. Here's an example of how an instance for the
@@ -181,20 +181,26 @@ spec:
181181
replicas: 3
182182
```
183183

184-
## ResourceGraphDefinition More about Resources
184+
## More about Resources
185185

186186
Users can specify more controls in resources in `.spec.resources[]`
187187

188188
```yaml
189189
spec:
190190
resources:
191-
- id:
192-
template || externalRef:
191+
- id: my-resource
192+
template || externalRef: {} # users can either template resources or reference objects outside the graph
193193
readyWhen:
194+
# users can specify CEL expressions to determine when a resource is ready
195+
- ${deployment.status.conditions.exists(x, x.type == 'Available' && x.status == "True")}
194196
includeWhen:
197+
# users can specify CEL expressions to determine when a resource should be included in the graph
198+
- ${schema.spec.value.enabled}
195199
```
196200

197-
Using `externalRef` An user can specify if the object is something that is created out-of-band and needs to be referenced in the RGD.
201+
### Using `externalRef` to reference Objects outside the ResourceGraphDefinition.
202+
203+
Users can specify if the object is something that is created out-of-band and needs to be referenced in the RGD.
198204
An external reference could be specified like this:
199205
```
200206
resources:
@@ -209,7 +215,52 @@ resources:
209215

210216
As part of processing the Resource Graph, the instance reconciler waits for the `externalRef` object to be present and reads the object from the cluster as a node in the graph. Subsequent resources can use data from this node.
211217

212-
## ResourceGraphDefinition Status Reporting
218+
219+
### Using Conditional CEL Expressions (`?`)
220+
221+
KRO can make use of CEL Expressions (see [this proposal for details](https://github.com/google/cel-spec/wiki/proposal-246) or look at the [CEL Implementation Reference](https://pkg.go.dev/github.com/google/cel-go/cel#hdr-Syntax_Changes-OptionalTypes)) to define optional runtime conditions for resources based on the conditional operator `?`.
222+
223+
This allows you to optionally define values that have no predefined schema or are not hard dependencies in the Graph.
224+
225+
#### Using `?` for referencing schema-less objects like `ConfigMap` or `Secret`
226+
227+
You can use the `optional` operator to reference objects that do not have a predefined schema in the ResourceGraphDefinition. This is useful for referencing objects that may or may not exist at runtime.
228+
229+
> :warning: `?` removes the ability of KRO to introspect the schema of the referenced object. Thus, it cannot wait for fields after the `?` to be present. It is recommended to use conditional expressions only for objects that are not critical to the ResourceGraphDefinition's operation or when the schema cannot be known at design time.
230+
231+
A config map can be referenced like this:
232+
233+
```yaml title="config-map.yaml"
234+
apiVersion: v1
235+
kind: ConfigMap
236+
metadata:
237+
name: demo
238+
data:
239+
VALUE: "foobar"
240+
```
241+
242+
```yaml title="external reference in ResourceGraphDefinition"
243+
- id: external
244+
externalRef:
245+
apiVersion: v1
246+
kind: ConfigMap
247+
metadata:
248+
name: demo
249+
namespace: default
250+
```
251+
252+
With this reference, you can access the data in your schema:
253+
254+
```text title="CEL Expression"
255+
${external.data.?VALUE}
256+
```
257+
258+
> :warning: KRO will only wait for the external reference to be present in the cluster, but it will not validate the schema of the referenced config. If the config map does not have the `VALUE` field, the expression will evaluate to `null` and might result in unexpected behavior in your application if not handled properly.
259+
260+
261+
_For a more detailed example, see the [Optional Values & External References](../../examples/basic/optionals.md) documentation._
262+
263+
## Status Reporting
213264

214265
The `status` section of a `ResourceGraphDefinition` provides information about the state of the graph and it's generated `CustomResourceDefinition` and controller.
215266

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
sidebar_position: 104
3+
---
4+
5+
# Optional Values & External References
6+
7+
8+
```yaml title="config-map.yaml"
9+
apiVersion: v1
10+
kind: ConfigMap
11+
metadata:
12+
name: demo
13+
data:
14+
ECHO_VALUE: "Hello, World!"
15+
```
16+
17+
18+
```yaml title="deploymentservice-rg.yaml"
19+
apiVersion: kro.run/v1alpha1
20+
kind: ResourceGraphDefinition
21+
metadata:
22+
name: deploymentservice
23+
spec:
24+
schema:
25+
apiVersion: v1alpha1
26+
kind: DeploymentService
27+
spec:
28+
name: string
29+
resources:
30+
- id: input
31+
externalRef:
32+
apiVersion: v1
33+
kind: ConfigMap
34+
metadata:
35+
name: demo
36+
namespace: default
37+
- id: deployment
38+
template:
39+
apiVersion: apps/v1
40+
kind: Deployment
41+
metadata:
42+
name: ${schema.spec.name}
43+
spec:
44+
replicas: 1
45+
selector:
46+
matchLabels:
47+
app: deployment
48+
template:
49+
metadata:
50+
labels:
51+
app: deployment
52+
spec:
53+
containers:
54+
- name: ${schema.spec.name}-busybox
55+
image: busybox
56+
command: ["sh", "-c", "echo $MY_VALUE && sleep 3600"]
57+
env:
58+
- name: MY_VALUE
59+
value: ${input.data.?ECHO_VALUE}
60+
```

0 commit comments

Comments
 (0)