|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# The name of the kind cluster |
| 6 | +CLUSTER_NAME="${CLUSTER_NAME:-"kind"}" |
| 7 | + |
| 8 | +# Function to check if a command exists |
| 9 | +command_exists() { |
| 10 | + command -v "$1" >/dev/null 2>&1 |
| 11 | +} |
| 12 | + |
| 13 | +# Function to display usage information |
| 14 | +usage() { |
| 15 | + echo "Usage: $0 [create|delete]" |
| 16 | + exit 1 |
| 17 | +} |
| 18 | + |
| 19 | +# Check if kubectl is installed |
| 20 | +if ! command_exists kubectl; then |
| 21 | + echo "kubectl is not installed. Please install kubectl before running this script." |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# Check if kind is installed |
| 26 | +if ! command_exists kind; then |
| 27 | + echo "kind is not installed. Please install kind before running this script." |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# Check if helm is installed |
| 32 | +if ! command_exists helm; then |
| 33 | + echo "helm is not installed. Please install helm before running this script." |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# Check the argument |
| 38 | +if [ $# -ne 1 ]; then |
| 39 | + usage |
| 40 | +fi |
| 41 | + |
| 42 | +ACTION=$1 |
| 43 | + |
| 44 | +# Validate input argument |
| 45 | +if [ "$ACTION" != "create" ] && [ "$ACTION" != "delete" ]; then |
| 46 | + echo "Invalid argument. Please use 'create' or 'delete'." |
| 47 | + usage |
| 48 | +fi |
| 49 | + |
| 50 | +# Check if the cluster already exists |
| 51 | +CLUSTER_EXISTS=$(kind get clusters) |
| 52 | + |
| 53 | +# Handle the case where no clusters are found |
| 54 | +if [[ "$CLUSTER_EXISTS" == "No kind clusters found." ]]; then |
| 55 | + CLUSTER_EXISTS="" |
| 56 | +fi |
| 57 | + |
| 58 | +if [ "$ACTION" == "create" ]; then |
| 59 | + if echo "$CLUSTER_EXISTS" | grep -qw "$CLUSTER_NAME"; then |
| 60 | + echo "Cluster '$CLUSTER_NAME' already exists. Exiting." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + # Cluster configuration YAML |
| 65 | + cat <<EOF > kind-config.yaml |
| 66 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 67 | +kind: Cluster |
| 68 | +nodes: |
| 69 | +- role: control-plane |
| 70 | + image: kindest/node:v1.27.3@sha256:3966ac761ae0136263ffdb6cfd4db23ef8a83cba8a463690e98317add2c9ba72 |
| 71 | + # required for GPU workaround (https://github.com/NVIDIA/nvidia-docker/issues/614#issuecomment-423991632) |
| 72 | + extraMounts: |
| 73 | + - hostPath: /dev/null |
| 74 | + containerPath: /var/run/nvidia-container-devices/all |
| 75 | +EOF |
| 76 | + |
| 77 | + # Create the kind cluster |
| 78 | + echo "Creating kind cluster..." |
| 79 | + kind create cluster --name "$CLUSTER_NAME" --config kind-config.yaml |
| 80 | + |
| 81 | + # Cleanup |
| 82 | + rm kind-config.yaml |
| 83 | + |
| 84 | + echo "Kind cluster created successfully." |
| 85 | + |
| 86 | + echo "Implementing nvidia-docker/issues/614 workaround..." |
| 87 | + docker exec -ti $CLUSTER_NAME-control-plane ln -s /sbin/ldconfig /sbin/ldconfig.real |
| 88 | + |
| 89 | + echo "Installing Nvidia GPU operator..." |
| 90 | + helm repo add nvidia https://helm.ngc.nvidia.com/nvidia || true |
| 91 | + helm repo update |
| 92 | + helm install --wait --generate-name \ |
| 93 | + -n gpu-operator --create-namespace \ |
| 94 | + nvidia/gpu-operator --set driver.enabled=false |
| 95 | + |
| 96 | + echo "Installation completed successfully." |
| 97 | + |
| 98 | +elif [ "$ACTION" == "delete" ]; then |
| 99 | + if [ -z "$CLUSTER_EXISTS" ]; then |
| 100 | + echo "Cluster '$CLUSTER_NAME' does not exist. Exiting." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + # Delete the kind cluster |
| 105 | + echo "Deleting kind cluster..." |
| 106 | + kind delete cluster --name "$CLUSTER_NAME" |
| 107 | + |
| 108 | + echo "Kind cluster deleted successfully." |
| 109 | +fi |
0 commit comments