Skip to content

Commit 5bc38f5

Browse files
committed
feat: Testing support for Kustomize projects
1 parent 04fe71e commit 5bc38f5

File tree

6 files changed

+378
-1
lines changed

6 files changed

+378
-1
lines changed

.github/workflows/pr-status-checks-workflow-call.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ permissions:
1919

2020
jobs:
2121
pr-status-check:
22-
uses: "openmcp-project/blueprint-workflows/.github/workflows/git-pr-status-checks.yml@main"
22+
uses: "openmcp-project/blueprint-workflows/.github/workflows/git-pr-status-checks.yml@feat/kustomize"
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
[base]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#base
2+
[config]: https://github.com/kubernetes-sigs/kustomize/tree/master/examples/helloWorld
3+
[gitops]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#gitops
4+
[hello]: https://github.com/monopole/hello
5+
[kustomization]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization
6+
[original]: https://github.com/kubernetes-sigs/kustomize/tree/master/examples/helloWorld
7+
[overlay]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay
8+
[overlays]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay
9+
[patch]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#patch
10+
[variant]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#variant
11+
[variants]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#variant
12+
13+
# Demo: hello world with variants
14+
15+
Steps:
16+
17+
1. Clone an existing configuration as a [base].
18+
1. Customize it.
19+
1. Create two different [overlays] (_staging_ and _production_)
20+
from the customized base.
21+
1. Run kustomize and kubectl to deploy staging and production.
22+
23+
First define a place to work:
24+
25+
<!-- @makeWorkplace @testAgainstLatestRelease -->
26+
```
27+
DEMO_HOME=$(mktemp -d)
28+
```
29+
30+
Alternatively, use
31+
32+
> ```
33+
> DEMO_HOME=~/hello
34+
> ```
35+
36+
## Establish the base
37+
38+
Let's run the [hello] service.
39+
40+
To use [overlays] to create [variants], we must
41+
first establish a common [base].
42+
43+
To keep this document shorter, the base resources are
44+
off in a supplemental data directory rather than
45+
declared here as HERE documents. Download them:
46+
47+
<!-- @downloadBase @testAgainstLatestRelease -->
48+
```
49+
BASE=$DEMO_HOME/base
50+
mkdir -p $BASE
51+
52+
curl -s -o "$BASE/#1.yaml" "https://raw.githubusercontent.com\
53+
/kubernetes-sigs/kustomize\
54+
/master/examples/helloWorld\
55+
/{configMap,deployment,kustomization,service}.yaml"
56+
```
57+
58+
Look at the directory:
59+
60+
<!-- @runTree -->
61+
```
62+
tree $DEMO_HOME
63+
```
64+
65+
Expect something like:
66+
67+
> ```
68+
> /tmp/tmp.IyYQQlHaJP
69+
> └── base
70+
> ├── configMap.yaml
71+
> ├── deployment.yaml
72+
> ├── kustomization.yaml
73+
> └── service.yaml
74+
> ```
75+
76+
77+
One could immediately apply these resources to a
78+
cluster:
79+
80+
> ```
81+
> kubectl apply -k $DEMO_HOME/base
82+
> ```
83+
84+
to instantiate the _hello_ service. `kubectl`
85+
would only recognize the resource files.
86+
87+
### The Base Kustomization
88+
89+
The `base` directory has a [kustomization] file:
90+
91+
<!-- @showKustomization @testAgainstLatestRelease -->
92+
```
93+
more $BASE/kustomization.yaml
94+
```
95+
96+
Optionally, run `kustomize` on the base to emit
97+
customized resources to `stdout`:
98+
99+
<!-- @buildBase @testAgainstLatestRelease -->
100+
```
101+
kustomize build $BASE
102+
```
103+
104+
### Customize the base
105+
106+
A first customization step could be to change the _app
107+
label_ applied to all resources:
108+
109+
<!-- @addLabel @testAgainstLatestRelease -->
110+
```
111+
sed -i.bak 's/app: hello/app: my-hello/' \
112+
$BASE/kustomization.yaml
113+
```
114+
115+
See the effect:
116+
<!-- @checkLabel @testAgainstLatestRelease -->
117+
```
118+
kustomize build $BASE | grep -C 3 app:
119+
```
120+
121+
## Create Overlays
122+
123+
Create a _staging_ and _production_ [overlay]:
124+
125+
* _Staging_ enables a risky feature not enabled in production.
126+
* _Production_ has a higher replica count.
127+
* Web server greetings from these cluster
128+
[variants] will differ from each other.
129+
130+
<!-- @overlayDirectories @testAgainstLatestRelease -->
131+
```
132+
OVERLAYS=$DEMO_HOME/overlays
133+
mkdir -p $OVERLAYS/staging
134+
mkdir -p $OVERLAYS/production
135+
```
136+
137+
#### Staging Kustomization
138+
139+
In the `staging` directory, make a kustomization
140+
defining a new name prefix, and some different labels.
141+
142+
<!-- @makeStagingKustomization @testAgainstLatestRelease -->
143+
```
144+
cat <<'EOF' >$OVERLAYS/staging/kustomization.yaml
145+
namePrefix: staging-
146+
commonLabels:
147+
variant: staging
148+
org: acmeCorporation
149+
commonAnnotations:
150+
note: Hello, I am staging!
151+
resources:
152+
- ../../base
153+
patches:
154+
- path: map.yaml
155+
EOF
156+
```
157+
158+
#### Staging Patch
159+
160+
Add a configMap customization to change the server
161+
greeting from _Good Morning!_ to _Have a pineapple!_
162+
163+
Also, enable the _risky_ flag.
164+
165+
<!-- @stagingMap @testAgainstLatestRelease -->
166+
```
167+
cat <<EOF >$OVERLAYS/staging/map.yaml
168+
apiVersion: v1
169+
kind: ConfigMap
170+
metadata:
171+
name: the-map
172+
data:
173+
altGreeting: "Have a pineapple!"
174+
enableRisky: "true"
175+
EOF
176+
```
177+
178+
#### Production Kustomization
179+
180+
In the production directory, make a kustomization
181+
with a different name prefix and labels.
182+
183+
<!-- @makeProductionKustomization @testAgainstLatestRelease -->
184+
```
185+
cat <<EOF >$OVERLAYS/production/kustomization.yaml
186+
namePrefix: production-
187+
commonLabels:
188+
variant: production
189+
org: acmeCorporation
190+
commonAnnotations:
191+
note: Hello, I am production!
192+
resources:
193+
- ../../base
194+
patches:
195+
- path: deployment.yaml
196+
EOF
197+
```
198+
199+
200+
#### Production Patch
201+
202+
Make a production patch that increases the replica
203+
count (because production takes more traffic).
204+
205+
<!-- @productionDeployment @testAgainstLatestRelease -->
206+
```
207+
cat <<EOF >$OVERLAYS/production/deployment.yaml
208+
apiVersion: apps/v1
209+
kind: Deployment
210+
metadata:
211+
name: the-deployment
212+
spec:
213+
replicas: 10
214+
EOF
215+
```
216+
217+
## Compare overlays
218+
219+
220+
`DEMO_HOME` now contains:
221+
222+
- a _base_ directory - a slightly customized clone
223+
of the original configuration, and
224+
225+
- an _overlays_ directory, containing the kustomizations
226+
and patches required to create distinct _staging_
227+
and _production_ [variants] in a cluster.
228+
229+
Review the directory structure and differences:
230+
231+
<!-- @listFiles -->
232+
```
233+
tree $DEMO_HOME
234+
```
235+
236+
Expecting something like:
237+
238+
> ```
239+
> /tmp/tmp.IyYQQlHaJP1
240+
> ├── base
241+
> │   ├── configMap.yaml
242+
> │   ├── deployment.yaml
243+
> │   ├── kustomization.yaml
244+
> │   └── service.yaml
245+
> └── overlays
246+
> ├── production
247+
> │   ├── deployment.yaml
248+
> │   └── kustomization.yaml
249+
> └── staging
250+
> ├── kustomization.yaml
251+
> └── map.yaml
252+
> ```
253+
254+
Compare the output directly
255+
to see how _staging_ and _production_ differ:
256+
257+
<!-- @compareOutput -->
258+
```
259+
diff \
260+
<(kustomize build $OVERLAYS/staging) \
261+
<(kustomize build $OVERLAYS/production) |\
262+
more
263+
```
264+
265+
The first part of the difference output should look
266+
something like
267+
268+
> ```diff
269+
> < altGreeting: Have a pineapple!
270+
> < enableRisky: "true"
271+
> ---
272+
> > altGreeting: Good Morning!
273+
> > enableRisky: "false"
274+
> 8c8
275+
> < note: Hello, I am staging!
276+
> ---
277+
> > note: Hello, I am production!
278+
> 11c11
279+
> < variant: staging
280+
> ---
281+
> > variant: production
282+
> 13c13
283+
> (...truncated)
284+
> ```
285+
286+
287+
## Deploy
288+
289+
The individual resource sets are:
290+
291+
<!-- @buildStaging @testAgainstLatestRelease -->
292+
```
293+
kustomize build $OVERLAYS/staging
294+
```
295+
296+
<!-- @buildProduction @testAgainstLatestRelease -->
297+
```
298+
kustomize build $OVERLAYS/production
299+
```
300+
301+
To deploy, pipe the above commands to kubectl apply:
302+
303+
> ```
304+
> kustomize build $OVERLAYS/staging |\
305+
> kubectl apply -f -
306+
> ```
307+
308+
> ```
309+
> kustomize build $OVERLAYS/production |\
310+
> kubectl apply -f -
311+
> ```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: the-map
5+
data:
6+
altGreeting: "Good Morning!"
7+
enableRisky: "false"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: the-deployment
5+
spec:
6+
replicas: 3
7+
selector:
8+
matchLabels:
9+
deployment: hello
10+
template:
11+
metadata:
12+
labels:
13+
deployment: hello
14+
spec:
15+
containers:
16+
- name: the-container
17+
image: monopole/hello:1
18+
command: ["/hello",
19+
"--port=8080",
20+
"--enableRiskyFeature=$(ENABLE_RISKY)"]
21+
ports:
22+
- containerPort: 8080
23+
env:
24+
- name: ALT_GREETING
25+
valueFrom:
26+
configMapKeyRef:
27+
name: the-map
28+
key: altGreeting
29+
- name: ENABLE_RISKY
30+
valueFrom:
31+
configMapKeyRef:
32+
name: the-map
33+
key: enableRisky
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
metadata:
4+
name: arbitrary
5+
6+
# Example configuration for the webserver
7+
# at https://github.com/monopole/hello
8+
commonLabels:
9+
app: hello
10+
11+
resources:
12+
- deployment.yaml
13+
- service.yaml
14+
- configMap.yaml
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: the-service
5+
spec:
6+
selector:
7+
deployment: hello
8+
type: LoadBalancer
9+
ports:
10+
- protocol: TCP
11+
port: 8666
12+
targetPort: 8080

0 commit comments

Comments
 (0)