Skip to content

Commit ee7919c

Browse files
authored
Add script to create kind cluster for e2e use (#117)
Signed-off-by: lubronzhan <[email protected]>
1 parent f3d0353 commit ee7919c

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

Makefile

+32
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
2020
DOCKERTAG ?= $(VERSION)
2121
REPOSITORY ?= kubevip
2222

23+
KUBEVIPCLOUDPROVIDER_E2E_IMAGE ?= ghcr.io/kube-vip/kube-vip-cloud-provider:main
24+
# Optional variables
25+
# Run specific test specs (matched by regex)
26+
KUBEVIPCLOUDPROVIDER_E2E_PACKAGE_FOCUS ?=
27+
2328
.PHONY: all build clean install uninstall fmt simplify check run
2429

2530
all: check install
@@ -70,3 +75,30 @@ run: install
7075

7176
test:
7277
go test ./...
78+
79+
.PHONY: setup-kind-cluster
80+
setup-kind-cluster: ## Make a kind cluster for testing
81+
./test/scripts/make-kind-cluster.sh
82+
83+
## Loads kube-vip-cloud-provider image into kind cluster specified by CLUSTERNAME (default
84+
## kvcp-e2e). By default for local development will build the current
85+
## working kube-vip-cloud-provider source and load into the cluster. If LOAD_PREBUILT_IMAGE
86+
## is specified and set to true, it will load a pre-build image. This requires
87+
## the multiarch-build target to have been run which puts the Kube-vip-cloud-provider docker
88+
## image at <repo>/image/kube-vip-cloud-provider-version.tar.gz. This second option is chosen
89+
## in CI to speed up builds.
90+
.PHONY: load-kvcp-image-kind
91+
load-kvcp-image-kind: ## Load Kube-vip-cloud-provider image from building working source or pre-built image into Kind.
92+
./test/scripts/kind-load-kvcp-image.sh
93+
94+
.PHONY: cleanup-kind
95+
cleanup-kind:
96+
./test/scripts/cleanup.sh
97+
98+
.PHONY: e2e
99+
e2e: | setup-kind-cluster load-kvcp-image-kind run-e2e cleanup-kind ## Run E2E tests against a real k8s cluster
100+
101+
.PHONY: run-e2e
102+
run-e2e:
103+
KUBEVIPCLOUDPROVIDER_E2E_IMAGE=$(KUBEVIPCLOUDPROVIDER_E2E_IMAGE) \
104+
go run github.com/onsi/ginkgo/v2/ginkgo -tags=e2e -mod=readonly -keep-going -randomize-suites -randomize-all -poll-progress-after=120s --focus '$(KUBEVIPCLOUDPROVIDER_E2E_TEST_FOCUS)' -r $(KUBEVIPCLOUDPROVIDER_E2E_PACKAGE_FOCUS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#! /usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
readonly KUBECTL_VERS="v1.29.2"
8+
readonly KIND_VERS="v0.22.0"
9+
10+
readonly PROGNAME=$(basename $0)
11+
readonly CURL=${CURL:-curl}
12+
13+
# Google storage is case sensitive, so we we need to lowercase the OS.
14+
readonly OS=$(uname | tr '[:upper:]' '[:lower:]')
15+
16+
usage() {
17+
echo "Usage: $PROGNAME INSTALLDIR"
18+
}
19+
20+
download() {
21+
local -r url="$1"
22+
local -r target="$2"
23+
24+
echo Downloading "$target" from "$url"
25+
${CURL} --progress-bar --location --output "$target" "$url"
26+
}
27+
28+
case "$#" in
29+
"1")
30+
mkdir -p "$1"
31+
readonly DESTDIR=$(cd "$1" && pwd)
32+
;;
33+
*)
34+
usage
35+
exit 64
36+
;;
37+
esac
38+
39+
echo "Installing Kubernetes toolchain..."
40+
41+
download \
42+
"https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERS}/kind-${OS}-amd64" \
43+
"${DESTDIR}/kind"
44+
45+
chmod +x "${DESTDIR}/kind"
46+
47+
download \
48+
"https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERS}/bin/${OS}/amd64/kubectl" \
49+
"${DESTDIR}/kubectl"
50+
51+
chmod +x "${DESTDIR}/kubectl"

test/scripts/cleanup.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env bash
2+
3+
set -o pipefail
4+
set -o errexit
5+
set -o nounset
6+
7+
readonly KIND=${KIND:-kind}
8+
readonly CLUSTERNAME=${CLUSTERNAME:-kvcp-e2e}
9+
10+
kind::cluster::delete() {
11+
${KIND} delete cluster --name "${CLUSTERNAME}"
12+
}
13+
14+
# Delete existing kind cluster.
15+
kind::cluster::delete

test/scripts/kind-load-kvcp-image.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
readonly KIND=${KIND:-kind}
8+
9+
readonly LOAD_PREBUILT_IMAGE=${LOAD_PREBUILT_IMAGE:-false}
10+
readonly CLUSTERNAME=${CLUSTERNAME:-kvcp-e2e}
11+
12+
readonly HERE=$(cd $(dirname $0) && pwd)
13+
readonly REPO=$(cd ${HERE}/../.. && pwd)
14+
15+
kind::cluster::exists() {
16+
${KIND} get clusters | grep -q "$1"
17+
}
18+
19+
kind::cluster::load::archive() {
20+
${KIND} load image-archive \
21+
--name "${CLUSTERNAME}" \
22+
"$@"
23+
}
24+
25+
kind::cluster::load::docker() {
26+
${KIND} load docker-image \
27+
--name "${CLUSTERNAME}" \
28+
"$@"
29+
}
30+
31+
if ! kind::cluster::exists "$CLUSTERNAME" ; then
32+
echo "cluster $CLUSTERNAME does not exist"
33+
exit 2
34+
fi
35+
36+
if [ "${LOAD_PREBUILT_IMAGE}" = "true" ]; then
37+
kind::cluster::load::archive "$(ls ${REPO}/image/kube-vip-cloud-provider-*.tar)"
38+
else
39+
# Build the current version of Kube-vip-cloud-provider.
40+
VERSION="v$$"
41+
make -C ${REPO} container IMAGE=ghcr.io/kube-vip/kube-vip-cloud-provider VERSION=$VERSION
42+
43+
# Also tag as main since test suites will use this tag unless overridden.
44+
docker tag ghcr.io/kube-vip/kube-vip-cloud-provider:${VERSION} ghcr.io/kube-vip/kube-vip-cloud-provider:main
45+
46+
# Push the Kube-vip-cloud-provider build image into the cluster.
47+
kind::cluster::load::docker ghcr.io/kube-vip/kube-vip-cloud-provider:${VERSION}
48+
kind::cluster::load::docker ghcr.io/kube-vip/kube-vip-cloud-provider:main
49+
fi

test/scripts/make-kind-cluster.sh

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#! /usr/bin/env bash
2+
3+
# make-kind-cluster.sh: build a kind cluster.
4+
5+
set -o pipefail
6+
set -o errexit
7+
set -o nounset
8+
9+
readonly KIND=${KIND:-kind}
10+
readonly KUBECTL=${KUBECTL:-kubectl}
11+
12+
readonly NODEIMAGE=${NODEIMAGE:-"kindest/node:v1.29.2@sha256:51a1434a5397193442f0be2a297b488b6c919ce8a3931be0ce822606ea5ca245"}
13+
readonly CLUSTERNAME=${CLUSTERNAME:-kvcp-e2e}
14+
readonly WAITTIME=${WAITTIME:-5m}
15+
16+
readonly HERE=$(cd "$(dirname "$0")" && pwd)
17+
readonly REPO=$(cd "${HERE}/../.." && pwd)
18+
19+
kind::cluster::exists() {
20+
${KIND} get clusters | grep -q "$1"
21+
}
22+
23+
kind::cluster::create() {
24+
${KIND} create cluster \
25+
--name "${CLUSTERNAME}" \
26+
--image "${NODEIMAGE}" \
27+
--wait "${WAITTIME}"
28+
}
29+
30+
kind::cluster::load() {
31+
${KIND} load docker-image \
32+
--name "${CLUSTERNAME}" \
33+
"$@"
34+
}
35+
36+
if kind::cluster::exists "$CLUSTERNAME" ; then
37+
echo "cluster $CLUSTERNAME already exists"
38+
echo exit 2
39+
fi
40+
41+
# Create a fresh kind cluster.
42+
if ! kind::cluster::exists "$CLUSTERNAME" ; then
43+
kind::cluster::create
44+
45+
# Print the k8s version for verification.
46+
${KUBECTL} version
47+
fi

0 commit comments

Comments
 (0)