Skip to content

build: add tooling to generate examples files #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ example), run:
make SKIP_BUILD=true dev.run-on-kind
```

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

```shell
clusterctl generate cluster capi-quickstart \
--flavor development \
--kubernetes-version v1.27.2 \
--control-plane-machine-count=1 \
--worker-machine-count=1 | \
gojq --yaml-input --yaml-output \
'. | (select(.kind=="Cluster").metadata.labels["capiext.labs.d2iq.io/cni"]|="calico")' | \
kubectl apply --server-side -f -
kubectl apply --server-side -f examples/capi-quickstart/cluster-class.yaml
```

To create a cluster, update `clusterConfig` variable and run:

```shell
kubectl apply --server-side -f examples/capi-quickstart/cluster.yaml
```

Wait until control plane is ready:
Expand Down
105 changes: 105 additions & 0 deletions examples/capi-quickstart/capd-cluster-class.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: cluster.x-k8s.io/v1beta1
kind: ClusterClass
metadata:
name: capd-quick-start
spec:
controlPlane:
machineInfrastructure:
ref:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: capd-quick-start-control-plane
ref:
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
kind: KubeadmControlPlaneTemplate
name: capd-quick-start-control-plane
infrastructure:
ref:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerClusterTemplate
name: capd-quick-start-cluster
patches:
- external:
discoverVariablesExtension: clusterconfigvars.capi-runtime-extensions
generateExtension: clusterconfigpatch.capi-runtime-extensions
name: cluster-config
workers:
machineDeployments:
- class: default-worker
template:
bootstrap:
ref:
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
name: capd-quick-start-default-worker-bootstraptemplate
infrastructure:
ref:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: capd-quick-start-default-worker-machinetemplate
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerClusterTemplate
metadata:
name: capd-quick-start-cluster
spec:
template:
spec: {}
---
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
kind: KubeadmControlPlaneTemplate
metadata:
name: capd-quick-start-control-plane
spec:
template:
spec:
kubeadmConfigSpec:
clusterConfiguration:
apiServer:
certSANs:
- localhost
- 127.0.0.1
- 0.0.0.0
- host.docker.internal
controllerManager:
extraArgs:
enable-hostpath-provisioner: "true"
initConfiguration:
nodeRegistration: {}
joinConfiguration:
nodeRegistration: {}
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
metadata:
name: capd-quick-start-control-plane
spec:
template:
spec:
extraMounts:
- containerPath: /var/run/docker.sock
hostPath: /var/run/docker.sock
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
metadata:
name: capd-quick-start-default-worker-machinetemplate
spec:
template:
spec:
extraMounts:
- containerPath: /var/run/docker.sock
hostPath: /var/run/docker.sock
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
metadata:
name: capd-quick-start-default-worker-bootstraptemplate
spec:
template:
spec:
joinConfiguration:
nodeRegistration: {}
32 changes: 32 additions & 0 deletions examples/capi-quickstart/capd-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
labels:
capiext.labs.d2iq.io/cni: calico
name: capd-quick-start
spec:
clusterNetwork:
pods:
cidrBlocks:
- 192.168.0.0/16
serviceDomain: cluster.local
services:
cidrBlocks:
- 10.128.0.0/12
topology:
class: capd-quick-start
controlPlane:
metadata: {}
replicas: 1
variables:
- name: clusterConfig
value: {}
version: v1.27.2
workers:
machineDeployments:
- class: default-worker
name: md-0
replicas: 1
23 changes: 23 additions & 0 deletions hack/examples/bases/capd/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

configurations:
- kustomizeconfig.yaml

resources:
- https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.5.1/clusterclass-quick-start.yaml
- https://github.com/kubernetes-sigs/cluster-api/releases/download/v1.5.1/cluster-template-development.yaml

namePrefix: capd-

patches:
- target:
group: cluster.x-k8s.io
kind: Cluster
patch: |-
- op: "add"
path: "/metadata/name"
value: "quick-start"
42 changes: 42 additions & 0 deletions hack/examples/bases/capd/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

nameReference:
- group: infrastructure.cluster.x-k8s.io
kind: DockerMachineTemplate
name: quick-start-control-plane
fieldSpecs:
- group: cluster.x-k8s.io
kind: ClusterClass
path: spec/controlPlane/machineInfrastructure/ref/name
- group: controlplane.cluster.x-k8s.io
kind: KubeadmControlPlaneTemplate
fieldSpecs:
- group: cluster.x-k8s.io
kind: ClusterClass
path: spec/controlPlane/ref/name
- group: infrastructure.cluster.x-k8s.io
kind: DockerClusterTemplate
fieldSpecs:
- group: cluster.x-k8s.io
kind: ClusterClass
path: spec/infrastructure/ref/name
- group: bootstrap.cluster.x-k8s.io
kind: KubeadmConfigTemplate
fieldSpecs:
- group: cluster.x-k8s.io
kind: ClusterClass
path: spec/workers/machineDeployments/template/bootstrap/ref/name
- group: infrastructure.cluster.x-k8s.io
kind: DockerMachineTemplate
name: quick-start-default-worker-machinetemplate
fieldSpecs:
- group: cluster.x-k8s.io
kind: ClusterClass
path: spec/workers/machineDeployments/template/infrastructure/ref/name
- group: cluster.x-k8s.io
kind: ClusterClass
fieldSpecs:
- group: cluster.x-k8s.io
kind: Cluster
path: spec/topology/class
61 changes: 61 additions & 0 deletions hack/examples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

sortOptions:
order: fifo

resources:
- ./bases/capd

patches:
- target:
group: cluster.x-k8s.io
kind: ClusterClass
patch: |-
- op: "add"
path: "/spec/patches"
value:
- name: "cluster-config"
external:
generateExtension: "clusterconfigpatch.capi-runtime-extensions"
discoverVariablesExtension: "clusterconfigvars.capi-runtime-extensions"
- op: "remove"
path: "/spec/variables"
- target:
group: cluster.x-k8s.io
kind: Cluster
patch: |-
- op: "remove"
path: "/metadata/namespace"
- op: "add"
path: "/spec/topology/version"
value: "v1.27.2"
- op: "add"
path: "/spec/clusterNetwork/pods/cidrBlocks"
value:
- "192.168.0.0/16"
- op: "add"
path: "/spec/clusterNetwork/services/cidrBlocks"
value:
- "10.128.0.0/12"
- op: "add"
path: "/spec/clusterNetwork/serviceDomain"
value: "cluster.local"
- op: "add"
path: "/spec/topology/controlPlane/replicas"
value: 1
- op: "add"
path: "/spec/topology/workers/machineDeployments/0/replicas"
value: 1
- op: "add"
path: "/spec/topology/variables"
value:
- name: "clusterConfig"
value: {}
- op: "add"
path: "/metadata/labels"
value:
capiext.labs.d2iq.io/cni: calico
1 change: 1 addition & 0 deletions make/all.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ include $(INCLUDE_DIR)clusterctl.mk
include $(INCLUDE_DIR)flux.mk
include $(INCLUDE_DIR)dev.mk
include $(INCLUDE_DIR)helm.mk
include $(INCLUDE_DIR)examples.mk
19 changes: 19 additions & 0 deletions make/examples.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 D2iQ, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

KUBERNETES_VERSION ?= v1.27.5

# gojq mutations:
# - ClusterClass: Add cluster-config external patch
#
# - Cluster: Add CNI label
# - Cluster: Add an empty clusterConfig variable
.PHONY: examples.sync
examples.sync: ## Syncs the examples by fetching upstream examples using clusterclt and applying gojq mutations
mkdir -p examples/capi-quickstart
# Sync ClusterClass and all Templates
kustomize build ./hack/examples | \
gojq --yaml-input --yaml-output '. | select(.kind != "Cluster")' > examples/capi-quickstart/capd-cluster-class.yaml
# Sync Cluster
kustomize build ./hack/examples | \
gojq --yaml-input --yaml-output '. | select(.kind == "Cluster")' > examples/capi-quickstart/capd-cluster.yaml