Skip to content

Commit b75f632

Browse files
committed
[testing] Refactor kube cluster deployment for reuse
In anticipation of using kube for more than bootstrap monitor testing, the functionality to deploy a kind cluster is factored out of the bootstrap monitor test script for reuse. The new script has also been updated to only deploy a cluster if one is not already running.
1 parent d31320e commit b75f632

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

scripts/ensure_kube_cluster.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This script ensures that a kubernetes cluster is available with the
6+
# default kubeconfig. If a cluster is not already running, kind will
7+
# be used to start one.
8+
9+
if ! [[ "$0" =~ scripts/ensure_kube_cluster.sh ]]; then
10+
echo "must be run from repository root"
11+
exit 255
12+
fi
13+
14+
function ensure_command {
15+
local cmd=$1
16+
local install_uri=$2
17+
18+
echo "Ensuring ${cmd} is available"
19+
if ! command -v "${cmd}" &> /dev/null; then
20+
local local_cmd="${PWD}/bin/${cmd}"
21+
mkdir -p "${PWD}/bin"
22+
echo "${cmd} not found, attempting to install..."
23+
curl -L -o "${local_cmd}" "${install_uri}"
24+
# TODO(marun) Optionally validate the binary against published checksum
25+
chmod +x "${local_cmd}"
26+
fi
27+
}
28+
29+
# Enables using a context other than the current default
30+
KUBE_CONTEXT="${KUBE_CONTEXT:-}"
31+
32+
# Ensure locally-installed binaries are in the path
33+
PATH="${PWD}/bin:$PATH"
34+
35+
# Determine the platform to download binaries for
36+
GOOS="$(go env GOOS)"
37+
GOARCH="$(go env GOARCH)"
38+
39+
KUBECTL_VERSION=v1.30.2
40+
ensure_command kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/${GOOS}/${GOARCH}/kubectl"
41+
42+
# Compose the kubectl command
43+
KUBECTL_CMD="kubectl"
44+
if [[ -n "${KUBE_CONTEXT}" ]]; then
45+
KUBECTL_CMD="${KUBECTL_CMD} --context=${KUBE_CONTEXT}"
46+
fi
47+
48+
# Check if a cluster is already running
49+
if ${KUBECTL_CMD} cluster-info &> /dev/null; then
50+
echo "A kube cluster is already accessible"
51+
exit 0
52+
fi
53+
54+
KIND_VERSION=v0.23.0
55+
ensure_command kind "https://kind.sigs.k8s.io/dl/${KIND_VERSION}/kind-${GOOS}-${GOARCH}"
56+
57+
SCRIPT_SHA=7cb9e6be25b48a0e248097eef29d496ab1a044d0
58+
ensure_command "kind-with-registry.sh" \
59+
"https://raw.githubusercontent.com/kubernetes-sigs/kind/${SCRIPT_SHA}/site/static/examples/kind-with-registry.sh"
60+
61+
echo "Deploying a new kind cluster with a local registry"
62+
bash -x "${PWD}/bin/kind-with-registry.sh"

scripts/tests.e2e.bootstrap_monitor.sh

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,6 @@ if ! [[ "$0" =~ scripts/tests.e2e.bootstrap_monitor.sh ]]; then
99
exit 255
1010
fi
1111

12-
GOOS="$(go env GOOS)"
13-
GOARCH="$(go env GOARCH)"
14-
15-
function ensure_command {
16-
local cmd=$1
17-
local install_uri=$2
18-
19-
if ! command -v "${cmd}" &> /dev/null; then
20-
# Try to use a local version
21-
local local_cmd="${PWD}/bin/${cmd}"
22-
mkdir -p "${PWD}/bin"
23-
if ! command -v "${local_cmd}" &> /dev/null; then
24-
echo "${cmd} not found, attempting to install..."
25-
curl -L -o "${local_cmd}" "${install_uri}"
26-
# TODO(marun) Optionally validate the binary against published checksum
27-
chmod +x "${local_cmd}"
28-
fi
29-
fi
30-
}
31-
32-
# Ensure the kubectl command is available
33-
KUBECTL_VERSION=v1.30.2
34-
ensure_command kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/${GOOS}/${GOARCH}/kubectl"
35-
36-
# Ensure the kind command is available
37-
KIND_VERSION=v0.23.0
38-
ensure_command kind "https://kind.sigs.k8s.io/dl/${KIND_VERSION}/kind-${GOOS}-${GOARCH}"
39-
40-
# Ensure the kind-with-registry command is available
41-
ensure_command "kind-with-registry.sh" "https://raw.githubusercontent.com/kubernetes-sigs/kind/7cb9e6be25b48a0e248097eef29d496ab1a044d0/site/static/examples/kind-with-registry.sh"
42-
43-
# Deploy a kind cluster with a local registry. Include the local bin in the path to
44-
# ensure locally installed kind and kubectl are available since the script expects to
45-
# call them without a qualifying path.
46-
PATH="${PWD}/bin:$PATH" bash -x "${PWD}/bin/kind-with-registry.sh"
12+
./scripts/ensure_kube_cluster.sh
4713

4814
KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./scripts/ginkgo.sh -v ./tests/fixture/bootstrapmonitor/e2e

0 commit comments

Comments
 (0)