Skip to content

Commit db1bde1

Browse files
dkoshkinjimmidyson
andauthored
build: add tooling to generate examples files (#148)
Co-authored-by: Jimmi Dyson <[email protected]>
1 parent 80df72f commit db1bde1

File tree

9 files changed

+370
-15
lines changed

9 files changed

+370
-15
lines changed

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,36 @@ 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-quick-start/capd-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-quick-start/capd-cluster.yaml
3836
```
3937

4038
Wait until control plane is ready:
4139

4240
```shell
43-
kubectl wait clusters/capi-quickstart --for=condition=ControlPlaneInitialized --timeout=5m
41+
kubectl wait clusters/capd-quick-start --for=condition=ControlPlaneInitialized --timeout=5m
4442
```
4543

4644
To get the kubeconfig for the new cluster, run:
4745

4846
```shell
49-
clusterctl get kubeconfig capi-quickstart > capd-kubeconfig
47+
clusterctl get kubeconfig capd-quick-start > capd-kubeconfig
5048
```
5149

5250
If you are not on Linux, you will also need to fix the generated kubeconfig's `server`, run:
5351

5452
```shell
55-
kubectl config set-cluster capi-quickstart \
53+
kubectl config set-cluster capd-quick-start \
5654
--kubeconfig capd-kubeconfig \
57-
--server=https://$(docker port capi-quickstart-lb 6443/tcp)
55+
--server=https://$(docker port capd-quick-start-lb 6443/tcp)
5856
```
5957

6058
Wait until all nodes are ready (this indicates that CNI has been deployed successfully):
@@ -119,7 +117,7 @@ watch -n 0.5 kubectl --kubeconfig capd-kubeconfig get service/traefik
119117
To delete the workload cluster, run:
120118

121119
```shell
122-
kubectl delete cluster capi-quickstart
120+
kubectl delete cluster capd-quick-start
123121
```
124122

125123
Notice that the traefik service is deleted before the cluster is actually finally deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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: capd-quick-start
8+
spec:
9+
controlPlane:
10+
machineInfrastructure:
11+
ref:
12+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
13+
kind: DockerMachineTemplate
14+
name: capd-quick-start-control-plane
15+
ref:
16+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
17+
kind: KubeadmControlPlaneTemplate
18+
name: capd-quick-start-control-plane
19+
infrastructure:
20+
ref:
21+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
22+
kind: DockerClusterTemplate
23+
name: capd-quick-start-cluster
24+
patches:
25+
- external:
26+
discoverVariablesExtension: clusterconfigvars.capi-runtime-extensions
27+
generateExtension: clusterconfigpatch.capi-runtime-extensions
28+
name: cluster-config
29+
- definitions:
30+
- jsonPatches:
31+
- op: add
32+
path: /spec/template/spec/customImage
33+
valueFrom:
34+
template: ghcr.io/mesosphere/kind-node:{{ .builtin.machineDeployment.version }}
35+
selector:
36+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
37+
kind: DockerMachineTemplate
38+
matchResources:
39+
machineDeploymentClass:
40+
names:
41+
- '*'
42+
- jsonPatches:
43+
- op: add
44+
path: /spec/template/spec/customImage
45+
valueFrom:
46+
template: ghcr.io/mesosphere/kind-node:{{ .builtin.controlPlane.version }}
47+
selector:
48+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
49+
kind: DockerMachineTemplate
50+
matchResources:
51+
controlPlane: true
52+
name: customKinDNodeImageForWorkers
53+
workers:
54+
machineDeployments:
55+
- class: default-worker
56+
template:
57+
bootstrap:
58+
ref:
59+
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
60+
kind: KubeadmConfigTemplate
61+
name: capd-quick-start-default-worker-bootstraptemplate
62+
infrastructure:
63+
ref:
64+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
65+
kind: DockerMachineTemplate
66+
name: capd-quick-start-default-worker-machinetemplate
67+
---
68+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
69+
kind: DockerClusterTemplate
70+
metadata:
71+
name: capd-quick-start-cluster
72+
spec:
73+
template:
74+
spec: {}
75+
---
76+
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
77+
kind: KubeadmControlPlaneTemplate
78+
metadata:
79+
name: capd-quick-start-control-plane
80+
spec:
81+
template:
82+
spec:
83+
kubeadmConfigSpec:
84+
clusterConfiguration:
85+
apiServer:
86+
certSANs:
87+
- localhost
88+
- 127.0.0.1
89+
- 0.0.0.0
90+
- host.docker.internal
91+
controllerManager:
92+
extraArgs:
93+
enable-hostpath-provisioner: "true"
94+
initConfiguration:
95+
nodeRegistration: {}
96+
joinConfiguration:
97+
nodeRegistration: {}
98+
---
99+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
100+
kind: DockerMachineTemplate
101+
metadata:
102+
name: capd-quick-start-control-plane
103+
spec:
104+
template:
105+
spec:
106+
extraMounts:
107+
- containerPath: /var/run/docker.sock
108+
hostPath: /var/run/docker.sock
109+
---
110+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
111+
kind: DockerMachineTemplate
112+
metadata:
113+
name: capd-quick-start-default-worker-machinetemplate
114+
spec:
115+
template:
116+
spec:
117+
extraMounts:
118+
- containerPath: /var/run/docker.sock
119+
hostPath: /var/run/docker.sock
120+
---
121+
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
122+
kind: KubeadmConfigTemplate
123+
metadata:
124+
name: capd-quick-start-default-worker-bootstraptemplate
125+
spec:
126+
template:
127+
spec:
128+
joinConfiguration:
129+
nodeRegistration: {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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: capd-quick-start
10+
spec:
11+
clusterNetwork:
12+
pods:
13+
cidrBlocks:
14+
- 192.168.0.0/16
15+
serviceDomain: cluster.local
16+
services:
17+
cidrBlocks:
18+
- 10.128.0.0/12
19+
topology:
20+
class: capd-quick-start
21+
controlPlane:
22+
metadata: {}
23+
replicas: 1
24+
variables:
25+
- name: clusterConfig
26+
value: {}
27+
version: v1.27.5
28+
workers:
29+
machineDeployments:
30+
- class: default-worker
31+
name: md-0
32+
replicas: 1
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: kustomize.config.k8s.io/v1beta1
5+
kind: Kustomization
6+
7+
configurations:
8+
- kustomizeconfig.yaml
9+
10+
resources:
11+
- https://github.com/kubernetes-sigs/cluster-api/releases/download/${CLUSTERCTL_VERSION}/clusterclass-quick-start.yaml
12+
- https://github.com/kubernetes-sigs/cluster-api/releases/download/${CLUSTERCTL_VERSION}/cluster-template-development.yaml
13+
14+
namePrefix: capd-
15+
16+
patches:
17+
- target:
18+
group: cluster.x-k8s.io
19+
kind: Cluster
20+
patch: |-
21+
- op: "add"
22+
path: "/metadata/name"
23+
value: "quick-start"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
nameReference:
5+
- group: infrastructure.cluster.x-k8s.io
6+
kind: DockerMachineTemplate
7+
name: quick-start-control-plane
8+
fieldSpecs:
9+
- group: cluster.x-k8s.io
10+
kind: ClusterClass
11+
path: spec/controlPlane/machineInfrastructure/ref/name
12+
- group: controlplane.cluster.x-k8s.io
13+
kind: KubeadmControlPlaneTemplate
14+
fieldSpecs:
15+
- group: cluster.x-k8s.io
16+
kind: ClusterClass
17+
path: spec/controlPlane/ref/name
18+
- group: infrastructure.cluster.x-k8s.io
19+
kind: DockerClusterTemplate
20+
fieldSpecs:
21+
- group: cluster.x-k8s.io
22+
kind: ClusterClass
23+
path: spec/infrastructure/ref/name
24+
- group: bootstrap.cluster.x-k8s.io
25+
kind: KubeadmConfigTemplate
26+
fieldSpecs:
27+
- group: cluster.x-k8s.io
28+
kind: ClusterClass
29+
path: spec/workers/machineDeployments/template/bootstrap/ref/name
30+
- group: infrastructure.cluster.x-k8s.io
31+
kind: DockerMachineTemplate
32+
name: quick-start-default-worker-machinetemplate
33+
fieldSpecs:
34+
- group: cluster.x-k8s.io
35+
kind: ClusterClass
36+
path: spec/workers/machineDeployments/template/infrastructure/ref/name
37+
- group: cluster.x-k8s.io
38+
kind: ClusterClass
39+
fieldSpecs:
40+
- group: cluster.x-k8s.io
41+
kind: Cluster
42+
path: spec/topology/class

hack/examples/kustomization.yaml.tmpl

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2023 D2iQ, Inc. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
apiVersion: kustomize.config.k8s.io/v1beta1
5+
kind: Kustomization
6+
7+
sortOptions:
8+
order: fifo
9+
10+
resources:
11+
- ./bases/capd
12+
13+
patches:
14+
- target:
15+
group: cluster.x-k8s.io
16+
kind: ClusterClass
17+
patch: |-
18+
- op: "add"
19+
path: "/spec/patches"
20+
value:
21+
- name: "cluster-config"
22+
external:
23+
generateExtension: "clusterconfigpatch.capi-runtime-extensions"
24+
discoverVariablesExtension: "clusterconfigvars.capi-runtime-extensions"
25+
- op: "remove"
26+
path: "/spec/variables"
27+
- target:
28+
group: cluster.x-k8s.io
29+
kind: ClusterClass
30+
name: capd-quick-start
31+
patch: |-
32+
- op: "add"
33+
path: "/spec/patches/-"
34+
value:
35+
name: customKinDNodeImageForWorkers
36+
definitions:
37+
- selector:
38+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
39+
kind: DockerMachineTemplate
40+
matchResources:
41+
machineDeploymentClass:
42+
names:
43+
- "*"
44+
jsonPatches:
45+
- op: add
46+
path: /spec/template/spec/customImage
47+
valueFrom:
48+
template: ghcr.io/mesosphere/kind-node:{{ .builtin.machineDeployment.version }}
49+
- selector:
50+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
51+
kind: DockerMachineTemplate
52+
matchResources:
53+
controlPlane: true
54+
jsonPatches:
55+
- op: add
56+
path: /spec/template/spec/customImage
57+
valueFrom:
58+
template: ghcr.io/mesosphere/kind-node:{{ .builtin.controlPlane.version }}
59+
- target:
60+
group: cluster.x-k8s.io
61+
kind: Cluster
62+
patch: |-
63+
- op: "remove"
64+
path: "/metadata/namespace"
65+
- op: "add"
66+
path: "/spec/topology/version"
67+
value: "${KUBERNETES_VERSION}"
68+
- op: "add"
69+
path: "/spec/clusterNetwork/pods/cidrBlocks"
70+
value:
71+
- "192.168.0.0/16"
72+
- op: "add"
73+
path: "/spec/clusterNetwork/services/cidrBlocks"
74+
value:
75+
- "10.128.0.0/12"
76+
- op: "add"
77+
path: "/spec/clusterNetwork/serviceDomain"
78+
value: "cluster.local"
79+
- op: "add"
80+
path: "/spec/topology/controlPlane/replicas"
81+
value: 1
82+
- op: "add"
83+
path: "/spec/topology/workers/machineDeployments/0/replicas"
84+
value: 1
85+
- op: "add"
86+
path: "/spec/topology/variables"
87+
value:
88+
- name: "clusterConfig"
89+
value: {}
90+
- op: "add"
91+
path: "/metadata/labels"
92+
value:
93+
capiext.labs.d2iq.io/cni: calico

0 commit comments

Comments
 (0)