|
| 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" |
0 commit comments