Skip to content

Commit 0cc9e28

Browse files
committed
build: add tooling to generate examples files
1 parent 14347de commit 0cc9e28

File tree

5 files changed

+368
-10
lines changed

5 files changed

+368
-10
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@ example), run:
2323
make SKIP_BUILD=true dev.run-on-kind
2424
```
2525

26-
To create a cluster with [clusterctl](https://cluster-api.sigs.k8s.io/user/quick-start.html), and label it for Calico
27-
CNI at the same time, run:
26+
To create the ClusterClass and it's Templates with this runtime extension enabled, run:
2827

2928
```shell
30-
clusterctl generate cluster capi-quickstart \
31-
--flavor development \
32-
--kubernetes-version v1.27.2 \
33-
--control-plane-machine-count=1 \
34-
--worker-machine-count=1 | \
35-
gojq --yaml-input --yaml-output \
36-
'. | (select(.kind=="Cluster").metadata.labels["capiext.labs.d2iq.io/cni"]|="calico")' | \
37-
kubectl apply --server-side -f -
29+
kubectl apply --server-side -f examples/capi-quickstart/cluster-class.yaml
30+
```
31+
32+
To create a cluster, update `clusterConfig` variable and run:
33+
34+
```shell
35+
kubectl apply --server-side -f examples/capi-quickstart/cluster.yaml
3836
```
3937

4038
Wait until control plane is ready:
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: cluster.x-k8s.io/v1beta1
5+
kind: ClusterClass
6+
metadata:
7+
name: quick-start
8+
namespace: default
9+
spec:
10+
controlPlane:
11+
machineInfrastructure:
12+
ref:
13+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
14+
kind: DockerMachineTemplate
15+
name: quick-start-control-plane
16+
ref:
17+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
18+
kind: KubeadmControlPlaneTemplate
19+
name: quick-start-control-plane
20+
infrastructure:
21+
ref:
22+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
23+
kind: DockerClusterTemplate
24+
name: quick-start-cluster
25+
patches:
26+
- definitions:
27+
- jsonPatches:
28+
- op: add
29+
path: /spec/template/spec/kubeadmConfigSpec/clusterConfiguration/imageRepository
30+
valueFrom:
31+
variable: imageRepository
32+
selector:
33+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
34+
kind: KubeadmControlPlaneTemplate
35+
matchResources:
36+
controlPlane: true
37+
description: Sets the imageRepository used for the KubeadmControlPlane.
38+
enabledIf: '{{ ne .imageRepository "" }}'
39+
name: imageRepository
40+
- definitions:
41+
- jsonPatches:
42+
- op: add
43+
path: /spec/template/spec/kubeadmConfigSpec/clusterConfiguration/etcd
44+
valueFrom:
45+
template: |
46+
local:
47+
imageTag: {{ .etcdImageTag }}
48+
selector:
49+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
50+
kind: KubeadmControlPlaneTemplate
51+
matchResources:
52+
controlPlane: true
53+
description: Sets tag to use for the etcd image in the KubeadmControlPlane.
54+
name: etcdImageTag
55+
- definitions:
56+
- jsonPatches:
57+
- op: add
58+
path: /spec/template/spec/kubeadmConfigSpec/clusterConfiguration/dns
59+
valueFrom:
60+
template: |
61+
imageTag: {{ .coreDNSImageTag }}
62+
selector:
63+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
64+
kind: KubeadmControlPlaneTemplate
65+
matchResources:
66+
controlPlane: true
67+
description: Sets tag to use for the etcd image in the KubeadmControlPlane.
68+
name: coreDNSImageTag
69+
- definitions:
70+
- jsonPatches:
71+
- op: add
72+
path: /spec/template/spec/customImage
73+
valueFrom:
74+
template: |
75+
kindest/node:{{ .builtin.machineDeployment.version | replace "+" "_" }}
76+
selector:
77+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
78+
kind: DockerMachineTemplate
79+
matchResources:
80+
machineDeploymentClass:
81+
names:
82+
- default-worker
83+
- jsonPatches:
84+
- op: add
85+
path: /spec/template/spec/customImage
86+
valueFrom:
87+
template: |
88+
kindest/node:{{ .builtin.controlPlane.version | replace "+" "_" }}
89+
selector:
90+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
91+
kind: DockerMachineTemplate
92+
matchResources:
93+
controlPlane: true
94+
description: Sets the container image that is used for running dockerMachines for the controlPlane and default-worker machineDeployments.
95+
name: customImage
96+
- definitions:
97+
- jsonPatches:
98+
- op: add
99+
path: /spec/template/spec/kubeadmConfigSpec/clusterConfiguration/apiServer/extraArgs
100+
value:
101+
admission-control-config-file: /etc/kubernetes/kube-apiserver-admission-pss.yaml
102+
- op: add
103+
path: /spec/template/spec/kubeadmConfigSpec/clusterConfiguration/apiServer/extraVolumes
104+
value:
105+
- hostPath: /etc/kubernetes/kube-apiserver-admission-pss.yaml
106+
mountPath: /etc/kubernetes/kube-apiserver-admission-pss.yaml
107+
name: admission-pss
108+
pathType: File
109+
readOnly: true
110+
- op: add
111+
path: /spec/template/spec/kubeadmConfigSpec/files
112+
valueFrom:
113+
template: |
114+
- content: |
115+
apiVersion: apiserver.config.k8s.io/v1
116+
kind: AdmissionConfiguration
117+
plugins:
118+
- name: PodSecurity
119+
configuration:
120+
apiVersion: pod-security.admission.config.k8s.io/v1{{ if semverCompare "< v1.25" .builtin.controlPlane.version }}beta1{{ end }}
121+
kind: PodSecurityConfiguration
122+
defaults:
123+
enforce: "{{ .podSecurityStandard.enforce }}"
124+
enforce-version: "latest"
125+
audit: "{{ .podSecurityStandard.audit }}"
126+
audit-version: "latest"
127+
warn: "{{ .podSecurityStandard.warn }}"
128+
warn-version: "latest"
129+
exemptions:
130+
usernames: []
131+
runtimeClasses: []
132+
namespaces: [kube-system]
133+
path: /etc/kubernetes/kube-apiserver-admission-pss.yaml
134+
selector:
135+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
136+
kind: KubeadmControlPlaneTemplate
137+
matchResources:
138+
controlPlane: true
139+
description: Adds an admission configuration for PodSecurity to the kube-apiserver.
140+
enabledIf: '{{ .podSecurityStandard.enabled }}'
141+
name: podSecurityStandard
142+
- external:
143+
discoverVariablesExtension: clusterconfigvars.capi-runtime-extensions
144+
generateExtension: clusterconfigpatch.capi-runtime-extensions
145+
name: cluster-config
146+
variables:
147+
- name: imageRepository
148+
required: true
149+
schema:
150+
openAPIV3Schema:
151+
default: ""
152+
description: imageRepository sets the container registry to pull images from. If empty, nothing will be set and the from of kubeadm will be used.
153+
example: registry.k8s.io
154+
type: string
155+
- name: etcdImageTag
156+
required: true
157+
schema:
158+
openAPIV3Schema:
159+
default: ""
160+
description: etcdImageTag sets the tag for the etcd image.
161+
example: 3.5.3-0
162+
type: string
163+
- name: coreDNSImageTag
164+
required: true
165+
schema:
166+
openAPIV3Schema:
167+
default: ""
168+
description: coreDNSImageTag sets the tag for the coreDNS image.
169+
example: v1.8.5
170+
type: string
171+
- name: podSecurityStandard
172+
required: false
173+
schema:
174+
openAPIV3Schema:
175+
properties:
176+
audit:
177+
default: restricted
178+
description: audit sets the level for the audit PodSecurityConfiguration mode. One of privileged, baseline, restricted.
179+
type: string
180+
enabled:
181+
default: true
182+
description: enabled enables the patches to enable Pod Security Standard via AdmissionConfiguration.
183+
type: boolean
184+
enforce:
185+
default: baseline
186+
description: enforce sets the level for the enforce PodSecurityConfiguration mode. One of privileged, baseline, restricted.
187+
type: string
188+
warn:
189+
default: restricted
190+
description: warn sets the level for the warn PodSecurityConfiguration mode. One of privileged, baseline, restricted.
191+
type: string
192+
type: object
193+
workers:
194+
machineDeployments:
195+
- class: default-worker
196+
template:
197+
bootstrap:
198+
ref:
199+
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
200+
kind: KubeadmConfigTemplate
201+
name: quick-start-default-worker-bootstraptemplate
202+
infrastructure:
203+
ref:
204+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
205+
kind: DockerMachineTemplate
206+
name: quick-start-default-worker-machinetemplate
207+
---
208+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
209+
kind: DockerClusterTemplate
210+
metadata:
211+
name: quick-start-cluster
212+
namespace: default
213+
spec:
214+
template:
215+
spec: {}
216+
---
217+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
218+
kind: KubeadmControlPlaneTemplate
219+
metadata:
220+
name: quick-start-control-plane
221+
namespace: default
222+
spec:
223+
template:
224+
spec:
225+
kubeadmConfigSpec:
226+
clusterConfiguration:
227+
apiServer:
228+
certSANs:
229+
- localhost
230+
- 127.0.0.1
231+
- 0.0.0.0
232+
- host.docker.internal
233+
controllerManager:
234+
extraArgs:
235+
enable-hostpath-provisioner: "true"
236+
initConfiguration:
237+
nodeRegistration: {}
238+
joinConfiguration:
239+
nodeRegistration: {}
240+
---
241+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
242+
kind: DockerMachineTemplate
243+
metadata:
244+
name: quick-start-control-plane
245+
namespace: default
246+
spec:
247+
template:
248+
spec:
249+
extraMounts:
250+
- containerPath: /var/run/docker.sock
251+
hostPath: /var/run/docker.sock
252+
---
253+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
254+
kind: DockerMachineTemplate
255+
metadata:
256+
name: quick-start-default-worker-machinetemplate
257+
namespace: default
258+
spec:
259+
template:
260+
spec:
261+
extraMounts:
262+
- containerPath: /var/run/docker.sock
263+
hostPath: /var/run/docker.sock
264+
---
265+
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
266+
kind: KubeadmConfigTemplate
267+
metadata:
268+
name: quick-start-default-worker-bootstraptemplate
269+
namespace: default
270+
spec:
271+
template:
272+
spec:
273+
joinConfiguration:
274+
nodeRegistration: {}

examples/capi-quickstart/cluster.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: cluster.x-k8s.io/v1beta1
5+
kind: Cluster
6+
metadata:
7+
labels:
8+
capiext.labs.d2iq.io/cni: calico
9+
name: capi-quickstart
10+
namespace: default
11+
spec:
12+
clusterNetwork:
13+
pods:
14+
cidrBlocks:
15+
- 192.168.0.0/16
16+
serviceDomain: cluster.local
17+
services:
18+
cidrBlocks:
19+
- 10.128.0.0/12
20+
topology:
21+
class: quick-start
22+
controlPlane:
23+
metadata: {}
24+
replicas: 1
25+
variables:
26+
- name: imageRepository
27+
value: ""
28+
- name: etcdImageTag
29+
value: ""
30+
- name: coreDNSImageTag
31+
value: ""
32+
- name: podSecurityStandard
33+
value:
34+
audit: restricted
35+
enabled: true
36+
enforce: baseline
37+
warn: restricted
38+
- name: clusterConfig
39+
value: {}
40+
version: v1.27.5
41+
workers:
42+
machineDeployments:
43+
- class: default-worker
44+
name: md-0
45+
replicas: 1

make/all.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ include $(INCLUDE_DIR)clusterctl.mk
2020
include $(INCLUDE_DIR)flux.mk
2121
include $(INCLUDE_DIR)dev.mk
2222
include $(INCLUDE_DIR)helm.mk
23+
include $(INCLUDE_DIR)examples.mk

make/examples.mk

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
5+
KUBERNETES_VERSION ?= v1.27.5
6+
7+
# gojq mutations:
8+
# - ClusterClass: Add cluster-config external patch
9+
#
10+
# - Cluster: Add CNI label
11+
# - Cluster: Add an empty clusterConfig variable
12+
.PHONY: examples.sync
13+
examples.sync: ## Syncs the examples by fetching upstream examples using clusterclt and applying gojq mutations
14+
examples.sync: export KUBECONFIG := $(KIND_KUBECONFIG)
15+
examples.sync: kind.create clusterctl.init
16+
mkdir -p examples/capi-quickstart
17+
# Sync ClusterClass and all Templates
18+
clusterctl generate cluster capi-quickstart \
19+
--flavor development \
20+
--kubernetes-version $(KUBERNETES_VERSION) \
21+
--control-plane-machine-count=1 \
22+
--worker-machine-count=1 | \
23+
gojq --yaml-input --yaml-output \
24+
'. | (select(.kind=="ClusterClass").spec.patches|= .+ [{"name": "cluster-config", "external": {"generateExtension": "clusterconfigpatch.capi-runtime-extensions", "discoverVariablesExtension": "clusterconfigvars.capi-runtime-extensions"}}])' | \
25+
gojq --yaml-input --yaml-output \
26+
'. | select(.kind != "Cluster")' | \
27+
gojq --yaml-input --yaml-output '.' > examples/capi-quickstart/cluster-class.yaml
28+
# Sync Cluster
29+
clusterctl generate cluster capi-quickstart \
30+
--flavor development \
31+
--kubernetes-version $(KUBERNETES_VERSION) \
32+
--control-plane-machine-count=1 \
33+
--worker-machine-count=1 | \
34+
gojq --yaml-input --yaml-output \
35+
'. | (select(.kind=="Cluster").metadata.labels["capiext.labs.d2iq.io/cni"]|="calico")' | \
36+
gojq --yaml-input --yaml-output \
37+
'. | (select(.kind=="Cluster").spec.topology.variables|= .+ [{"name": "clusterConfig", "value": {}}])' | \
38+
gojq --yaml-input --yaml-output \
39+
'. | select(.kind == "Cluster")' | \
40+
gojq --yaml-input --yaml-output '.' > examples/capi-quickstart/cluster.yaml

0 commit comments

Comments
 (0)