Skip to content

Commit 61e3d22

Browse files
committed
Add example for transformer configurations: crd
1 parent 16add04 commit 61e3d22

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ go get sigs.k8s.io/kustomize
4242
* [remote target](remoteBuild.md) - Building a kustomization from a github URL
4343

4444
* [json patch](jsonpatch.md) - Apply a json patch in a kustomization
45+
46+
* [transformer configs](transformerconfigs/README.md) - Customize transformer configurations
47+

examples/transformerconfigs/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Transformer Configurations
2+
3+
Kustomize computes the resources by applying a series of transformers:
4+
- namespace transformer
5+
- prefix transformer
6+
- label transformer
7+
- annotation transformer
8+
- name reference transformer
9+
- variable reference transformer
10+
11+
Each transformer takes a list of resources and modifies certain fields. The modification is based on the transformer's rule.
12+
The fields to update is represented by fieldSpec. Take prefix transformer as an example. It will add the prefix to `.metadata.name`
13+
field in each resources (except CustomResourceDefinition). Here `.metadata.name` is included by the prefix transformer configurations.
14+
15+
Kustomize has a default set of configurations. They can be saved to local directory through `kustomize config save -d`. kusotmize allows modifying those configuration files and using them in `kustomize build` through `-t`. This tutorial shows how to customize those configurations to
16+
- [support a crd type](crd/README.md)
17+
- exclude some label selectors from commonLabels
18+
- add extra fields for variable substitution
19+
- add extra fields for name reference
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
## Transformer Configurations - CRD
2+
3+
This tutorial shows how to add transformer configurations to support a CRD type.
4+
5+
### Get Default Config
6+
Get the default transformer configurations by
7+
8+
<!-- @saveConfig @test -->
9+
```
10+
kustomize config save -d ~/.kustomize/config
11+
```
12+
The default configurations are save in directory `~/.kustomize/config` as several files
13+
14+
> ```
15+
> commonannotations.yaml commonlabels.yaml nameprefix.yaml namereference.yaml namespace.yaml varreference.yaml
16+
> ```
17+
18+
### Add Config for a CRD
19+
All transformers will be involved for a CRD type. The default configurations already include some common fieldSpec for all types:
20+
21+
- nameprefix is added to `.metadata.name`
22+
- namespace is added to `.metadata.namespace`
23+
- labels is added to `.metadata.labels`
24+
- annotations is added to `.metadata.annotations`
25+
26+
Thus those fieldSpec don't need to be added to support a CRD type.
27+
Consider a CRD type `MyKind` with fields
28+
- `.spec.secretRef.name` reference a Secret
29+
- `.spec.beeRef.name` reference an instance of CRD `Bee`
30+
- `.spec.containers.commands` as the list of container commands
31+
- `.spec.selectors` as the label selectors
32+
33+
Add following file to configure the transformers for the above fields
34+
<!-- @addConfig @test -->
35+
```
36+
cat > ~/.kustomize/config/mykind.yaml << EOF
37+
38+
commonLabels:
39+
- path: spec/selectors
40+
create: true
41+
kind: MyKind
42+
43+
nameReference:
44+
- kind: Bee
45+
fieldSpecs:
46+
- path: spec/beeRef/name
47+
kind: MyKind
48+
- kind: Secret
49+
fieldSpecs:
50+
- path: spec/secretRef/name
51+
kind: MyKind
52+
53+
varReference:
54+
- path: spec/containers/commands
55+
kind: MyKind
56+
57+
EOF
58+
```
59+
60+
### Apply config
61+
Create a kustomization with a `MyKind` instance.
62+
63+
<!-- @createKustomization @test -->
64+
```
65+
DEMO_HOME=$(mktemp -d)
66+
67+
cat > $DEMO_HOME/kustomization.yaml << EOF
68+
resources:
69+
- resources.yaml
70+
71+
namePrefix: test-
72+
EOF
73+
74+
cat > $DEMO_HOME/resources.yaml << EOF
75+
apiVersion: v1
76+
kind: Secret
77+
metadata:
78+
name: crdsecret
79+
data:
80+
PATH: YmJiYmJiYmIK
81+
---
82+
apiVersion: v1beta1
83+
kind: Bee
84+
metadata:
85+
name: bee
86+
spec:
87+
action: fly
88+
---
89+
apiVersion: jingfang.example.com/v1beta1
90+
kind: MyKind
91+
metadata:
92+
name: mykind
93+
spec:
94+
secretRef:
95+
name: crdsecret
96+
beeRef:
97+
name: bee
98+
EOF
99+
```
100+
101+
Run `kustomize build` with customized transformer configurations and verify that
102+
the namereference is correctly resolved.
103+
104+
<!-- @build @test -->
105+
```
106+
test 2 == \
107+
$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep -A 1 ".*Ref" | grep "test-" | wc -l); \
108+
echo $?
109+
```
110+
111+
To understand this better, compare the output using default transformer configurations.
112+
113+
<!-- @compareOutput -->
114+
```
115+
diff \
116+
<(kustomize build $DEMO_HOME) \
117+
<(kustomize build $DEMO_HOME -t ~/.kustomize/config ) |\
118+
more
119+
```
120+
121+
The difference output should look something like
122+
> ```
123+
> 14c14
124+
> < name: bee
125+
> ---
126+
> > name: test-bee
127+
> 16c16
128+
> < name: crdsecret
129+
> ---
130+
> > name: test-crdsecret
131+
> ```
132+
133+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resources:
2+
- resources.yaml
3+
4+
namePrefix: test-
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: crdsecret
5+
data:
6+
PATH: YmJiYmJiYmIK
7+
---
8+
apiVersion: v1beta1
9+
kind: Bee
10+
metadata:
11+
name: bee
12+
spec:
13+
action: fly
14+
---
15+
apiVersion: jingfang.example.com/v1beta1
16+
kind: MyKind
17+
metadata:
18+
name: mykind
19+
spec:
20+
secretRef:
21+
name: crdsecret
22+
beeRef:
23+
name: bee

0 commit comments

Comments
 (0)