diff --git a/README.md b/README.md index 9068df13d..6af3335e3 100644 --- a/README.md +++ b/README.md @@ -23,38 +23,36 @@ 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-quick-start/capd-cluster-class.yaml +``` + +To create a cluster, update `clusterConfig` variable and run: + +```shell +kubectl apply --server-side -f examples/capi-quick-start/capd-cluster.yaml ``` Wait until control plane is ready: ```shell -kubectl wait clusters/capi-quickstart --for=condition=ControlPlaneInitialized --timeout=5m +kubectl wait clusters/capd-quick-start --for=condition=ControlPlaneInitialized --timeout=5m ``` To get the kubeconfig for the new cluster, run: ```shell -clusterctl get kubeconfig capi-quickstart > capd-kubeconfig +clusterctl get kubeconfig capd-quick-start > capd-kubeconfig ``` If you are not on Linux, you will also need to fix the generated kubeconfig's `server`, run: ```shell -kubectl config set-cluster capi-quickstart \ +kubectl config set-cluster capd-quick-start \ --kubeconfig capd-kubeconfig \ - --server=https://$(docker port capi-quickstart-lb 6443/tcp) + --server=https://$(docker port capd-quick-start-lb 6443/tcp) ``` 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 To delete the workload cluster, run: ```shell -kubectl delete cluster capi-quickstart +kubectl delete cluster capd-quick-start ``` Notice that the traefik service is deleted before the cluster is actually finally deleted. diff --git a/examples/capi-quick-start/capd-cluster-class.yaml b/examples/capi-quick-start/capd-cluster-class.yaml new file mode 100644 index 000000000..1029323e9 --- /dev/null +++ b/examples/capi-quick-start/capd-cluster-class.yaml @@ -0,0 +1,129 @@ +# 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 + - definitions: + - jsonPatches: + - op: add + path: /spec/template/spec/customImage + valueFrom: + template: ghcr.io/mesosphere/kind-node:{{ .builtin.machineDeployment.version }} + selector: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: DockerMachineTemplate + matchResources: + machineDeploymentClass: + names: + - '*' + - jsonPatches: + - op: add + path: /spec/template/spec/customImage + valueFrom: + template: ghcr.io/mesosphere/kind-node:{{ .builtin.controlPlane.version }} + selector: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: DockerMachineTemplate + matchResources: + controlPlane: true + name: customKinDNodeImageForWorkers + 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: {} diff --git a/examples/capi-quick-start/capd-cluster.yaml b/examples/capi-quick-start/capd-cluster.yaml new file mode 100644 index 000000000..7c5243bbd --- /dev/null +++ b/examples/capi-quick-start/capd-cluster.yaml @@ -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.5 + workers: + machineDeployments: + - class: default-worker + name: md-0 + replicas: 1 diff --git a/hack/examples/bases/capd/kustomization.yaml.tmpl b/hack/examples/bases/capd/kustomization.yaml.tmpl new file mode 100644 index 000000000..57a5111c3 --- /dev/null +++ b/hack/examples/bases/capd/kustomization.yaml.tmpl @@ -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/${CLUSTERCTL_VERSION}/clusterclass-quick-start.yaml +- https://github.com/kubernetes-sigs/cluster-api/releases/download/${CLUSTERCTL_VERSION}/cluster-template-development.yaml + +namePrefix: capd- + +patches: +- target: + group: cluster.x-k8s.io + kind: Cluster + patch: |- + - op: "add" + path: "/metadata/name" + value: "quick-start" diff --git a/hack/examples/bases/capd/kustomizeconfig.yaml b/hack/examples/bases/capd/kustomizeconfig.yaml new file mode 100644 index 000000000..fcfe205f7 --- /dev/null +++ b/hack/examples/bases/capd/kustomizeconfig.yaml @@ -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 diff --git a/hack/examples/kustomization.yaml.tmpl b/hack/examples/kustomization.yaml.tmpl new file mode 100644 index 000000000..457ee2415 --- /dev/null +++ b/hack/examples/kustomization.yaml.tmpl @@ -0,0 +1,93 @@ +# 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: ClusterClass + name: capd-quick-start + patch: |- + - op: "add" + path: "/spec/patches/-" + value: + name: customKinDNodeImageForWorkers + definitions: + - selector: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: DockerMachineTemplate + matchResources: + machineDeploymentClass: + names: + - "*" + jsonPatches: + - op: add + path: /spec/template/spec/customImage + valueFrom: + template: ghcr.io/mesosphere/kind-node:{{ .builtin.machineDeployment.version }} + - selector: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: DockerMachineTemplate + matchResources: + controlPlane: true + jsonPatches: + - op: add + path: /spec/template/spec/customImage + valueFrom: + template: ghcr.io/mesosphere/kind-node:{{ .builtin.controlPlane.version }} +- target: + group: cluster.x-k8s.io + kind: Cluster + patch: |- + - op: "remove" + path: "/metadata/namespace" + - op: "add" + path: "/spec/topology/version" + value: "${KUBERNETES_VERSION}" + - 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 diff --git a/hack/examples/sync.sh b/hack/examples/sync.sh new file mode 100755 index 000000000..e0603b39b --- /dev/null +++ b/hack/examples/sync.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# Copyright 2023 D2iQ, Inc. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail +IFS=$'\n\t' + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +readonly SCRIPT_DIR + +readonly EXAMPLES_KUSTOMIZATION_FILE="${SCRIPT_DIR}/kustomization.yaml" +readonly CAPD_KUSTOMIZATION_FILE="${SCRIPT_DIR}/bases/capd/kustomization.yaml" + +trap 'rm -rf ${CAPD_KUSTOMIZATION_FILE} ${EXAMPLES_KUSTOMIZATION_FILE}' EXIT +# download the quick-start files that match the clusterctl version +CLUSTERCTL_VERSION=$(clusterctl version -o short 2>/dev/null) envsubst \ + <"${CAPD_KUSTOMIZATION_FILE}.tmpl" >"${CAPD_KUSTOMIZATION_FILE}" +# replace the kubernetes version +envsubst <"${EXAMPLES_KUSTOMIZATION_FILE}.tmpl" >"${EXAMPLES_KUSTOMIZATION_FILE}" + +mkdir -p examples/capi-quick-start +# Sync ClusterClass and all Templates +kustomize build ./hack/examples | + gojq --yaml-input --yaml-output '. | select(.kind != "Cluster")' >examples/capi-quick-start/capd-cluster-class.yaml +# Sync Cluster +kustomize build ./hack/examples | + gojq --yaml-input --yaml-output '. | select(.kind == "Cluster")' >examples/capi-quick-start/capd-cluster.yaml diff --git a/make/all.mk b/make/all.mk index 042bcdcc9..8ae539bee 100644 --- a/make/all.mk +++ b/make/all.mk @@ -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 diff --git a/make/examples.mk b/make/examples.mk new file mode 100644 index 000000000..d59316755 --- /dev/null +++ b/make/examples.mk @@ -0,0 +1,9 @@ +# Copyright 2023 D2iQ, Inc. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +KUBERNETES_VERSION := v1.27.5 + +.PHONY: examples.sync +examples.sync: export KUBERNETES_VERSION := $(KUBERNETES_VERSION) +examples.sync: ## Syncs the examples by fetching upstream examples and applying kustomize patches + hack/examples/sync.sh