Skip to content

Commit

Permalink
Update Kind scripts to support KubeVirt
Browse files Browse the repository at this point in the history
Signed-off-by: Johanan Liebermann <[email protected]>
  • Loading branch information
johananl committed Jan 29, 2025
1 parent 776ca83 commit ae472e5
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 15 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ TEST_EXTENSION_IMG ?= $(REGISTRY)/$(TEST_EXTENSION_IMAGE_NAME)

# kind
CAPI_KIND_CLUSTER_NAME ?= capi-test
CAPI_KIND_REGISTRY_NAME ?= kind-registry

# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971

Expand Down Expand Up @@ -996,6 +997,10 @@ test-e2e: $(GINKGO) generate-e2e-templates ## Run the end-to-end tests
kind-cluster: ## Create a new kind cluster designed for development with Tilt
hack/kind-install-for-capd.sh

.PHONY: kind-cluster-kubevirt
kind-cluster-kubevirt: ## Create a new kind cluster with KubeVirt designed for development with Tilt
hack/kind-install-for-kubevirt.sh

.PHONY: tilt-e2e-prerequisites
tilt-e2e-prerequisites: ## Build the corresponding kindest/node images required for e2e testing and generate the e2e templates
scripts/build-kind.sh
Expand Down Expand Up @@ -1335,8 +1340,9 @@ clean: ## Remove generated binaries, GitBook files, Helm charts, and Tilt build
$(MAKE) clean-tilt

.PHONY: clean-kind
clean-kind: ## Cleans up the kind cluster with the name $CAPI_KIND_CLUSTER_NAME
clean-kind: ## Cleans up the kind cluster with the name $CAPI_KIND_CLUSTER_NAME and its local registry named $CAPI_KIND_REGISTRY_NAME
kind delete cluster --name="$(CAPI_KIND_CLUSTER_NAME)" || true
docker rm -f "$(CAPI_KIND_REGISTRY_NAME)"

.PHONY: clean-bin
clean-bin: ## Remove all generated binaries
Expand Down
49 changes: 35 additions & 14 deletions hack/kind-install-for-capd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ if [[ "${TRACE-0}" == "1" ]]; then
fi

KIND_CLUSTER_NAME=${CAPI_KIND_CLUSTER_NAME:-"capi-test"}
KIND_INFRA_PROVIDER=${CAPI_KIND_INFRA_PROVIDER:-""}
KIND_DOCKER_CONFIG_PATH=${KIND_DOCKER_CONFIG_PATH:-"$HOME/.docker/config.json"}
# See: https://kind.sigs.k8s.io/docs/user/configuration/#ip-family
KIND_NETWORK_IPFAMILY=${KIND_NETWORK_IPFAMILY:-"dual"}

Expand All @@ -56,21 +58,40 @@ fi
# https://github.com/kubernetes-sigs/kind/issues/2875
# https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
# See: https://github.com/containerd/containerd/blob/main/docs/hosts.md
cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: ${KIND_NETWORK_IPFAMILY}
nodes:
- role: control-plane
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"

if [[ "$KIND_INFRA_PROVIDER" == "kubevirt" ]]; then
cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
disableDefaultCNI: true
nodes:
- role: control-plane
extraMounts:
- containerPath: /var/lib/kubelet/config.json
hostPath: ${KIND_DOCKER_CONFIG_PATH}
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
EOF
else
cat <<EOF | kind create cluster --name="$KIND_CLUSTER_NAME" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: ${KIND_NETWORK_IPFAMILY}
nodes:
- role: control-plane
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
EOF
fi

# 4. Add the registry config to the nodes
#
Expand Down
105 changes: 105 additions & 0 deletions hack/kind-install-for-kubevirt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bash

# Copyright 2025 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# This script installs a local Kind cluster with a local container registry and the correct files
# mounted for using CAPK to test Cluster API.
# The default Kind CNI is disabled because it doesn't work CAPK. Instead, Calico is used.
# MetalLB is used as a local layer 2 load balancer to support exposing the API servers of workload
# clusters on the local machine that's running this script.

set -o errexit
set -o nounset
set -o pipefail

CALICO_VERSION=${CAPI_CALICO_VERSION:-"v3.29.1"}
METALLB_VERSION=${CAPI_METALLB_VERSION:-""}
# Set manually for non-Docker runtimes - example: "172.20"
METALLB_IP_PREFIX=${CAPI_METALLB_IP_PREFIX:-""}
KUBEVIRT_VERSION=${CAPI_KUBEVIRT_VERSION:-""}

# Deploy Kind cluster
CAPI_INFRA_PROVIDER=kubevirt "$(dirname "${BASH_SOURCE[0]}")/kind-install-for-capd.sh"

# Deploy Calico
kubectl apply -f \
"https://raw.githubusercontent.com/projectcalico/calico/${CALICO_VERSION}/manifests/calico.yaml"

# Deploy MetalLB
if [[ -z "$METALLB_VERSION" ]]; then
METALLB_VERSION=$(curl "https://api.github.com/repos/metallb/metallb/releases/latest" \
| jq -r ".tag_name")
fi

kubectl apply -f \
"https://raw.githubusercontent.com/metallb/metallb/${METALLB_VERSION}/config/manifests/metallb-native.yaml"

echo "Waiting for controller pod to be created..."
while [[ -z $(kubectl -n metallb-system get pods \
-l app=metallb,component=controller -o jsonpath="{.items[0].metadata.name}" 2>/dev/null) ]]; do
sleep 2
done
echo "Pod created!"

echo "Waiting for speaker pod to be created..."
while [[ -z $(kubectl -n metallb-system get pods \
-l app=metallb,component=speaker -o jsonpath="{.items[0].metadata.name}" 2>/dev/null) ]]; do
sleep 2
done
echo "Pod created!"

echo "Waiting for pods to become ready..."
kubectl wait pods -n metallb-system \
-l app=metallb,component=controller --for=condition=Ready --timeout=10m
kubectl wait pods -n metallb-system \
-l app=metallb,component=speaker --for=condition=Ready --timeout=2m
echo "Pods ready!"

if [[ -z "$METALLB_IP_PREFIX" ]]; then
SUBNET=$(docker network inspect \
-f '{{range .IPAM.Config}}{{if .Gateway}}{{.Subnet}}{{end}}{{end}}' kind)
METALLB_IP_PREFIX=$(echo $SUBNET | sed -E 's|^([0-9]+\.[0-9]+)\..*$|\1|g')
fi

cat <<EOF | kubectl apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: capi-ip-pool
namespace: metallb-system
spec:
addresses:
- ${METALLB_IP_PREFIX}.255.200-${METALLB_IP_PREFIX}.255.250
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: empty
namespace: metallb-system
EOF

# Deploy KubeVirt
if [[ -z "$KUBEVIRT_VERSION" ]]; then
KUBEVIRT_VERSION=$(curl "https://api.github.com/repos/kubevirt/kubevirt/releases/latest" \
| jq -r ".tag_name")
fi
kubectl apply -f \
"https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-operator.yaml"
kubectl apply -f \
"https://github.com/kubevirt/kubevirt/releases/download/${KUBEVIRT_VERSION}/kubevirt-cr.yaml"
echo "Waiting for KubeVirt to become ready..."
kubectl wait -n kubevirt kv kubevirt --for=condition=Available --timeout=10m
echo "KubeVirt ready!"

0 comments on commit ae472e5

Please sign in to comment.