bekind (3880B)
1 #!/usr/bin/env bash 2 bekind=$(basename $0) 3 bekind_dir=$(dirname "$(readlink -f "$0")") 4 profile_dir="./profiles" 5 6 bekind_help(){ 7 echo "Usage: $ProgName <subcommand> [options]\n" 8 echo "Subcommands:" 9 echo " clean clean all kind clusters" 10 echo " create create a kind cluster" 11 echo " delete delete a kind cluster" 12 echo " list list kind clusters" 13 echo "" 14 echo "For help with each subcommand run:" 15 echo "$ProgName <subcommand> -h|--help" 16 echo "" 17 } 18 19 bekind_create(){ 20 profile="" 21 host="" 22 port="8443" 23 while [[ $# -gt 1 ]] 24 do 25 case $1 in 26 "--profile" | "-p") 27 shift 28 profile=$1 29 shift 30 ;; 31 "--port") 32 shift 33 port=$1 34 shift 35 ;; 36 "--host") 37 shift 38 host=$1 39 shift 40 ;; 41 esac 42 done 43 name="$1" 44 shift 45 if [[ -z "${name}" ]]; then 46 name="kind" 47 fi 48 env="" 49 configfile="config.${name}" 50 if [[ -n "${host}" ]]; then 51 env="DOCKER_HOST=ssh://${host}" 52 configfile="${configfile}.${host}" 53 fi 54 args="" 55 if [[ -n "${profile}" ]]; then 56 if [[ -z "${host}" ]]; then 57 echo "Cannot use profile ${profile} without an host" 1>&2 58 exit 1 59 fi 60 # does the profile exists 61 profile_file="${profile_dir}/${profile}.yaml" 62 if [[ -f "${profile_file}" ]]; then 63 tmpfile=$(mktemp /tmp/bekind.XXXXXX) 64 hostip=$(getent hosts ${host} | awk '{ print $1 }') 65 sed -e "s/HOST/${hostip}/g" -e "s/PORT/${port}/g" "${profile_file}" > ${tmpfile} 66 args="${args} --config ${tmpfile}" 67 else 68 echo "Profile ${profile} doesn't exists" 1>&2 69 exit 1 70 fi 71 # replace hosts in there 72 fi 73 tmpkubeconfig=$(mktemp /tmp/bekind.kubeconfig.XXXXXX) 74 export KUBECONFIG=${tmpkubeconfig} 75 echo "> Create the cluster (name: ${name}, args: ${args})" 76 env $env kind create cluster --name ${name} ${args} 77 echo "> Write the kubeconfig in ~/.kube/${configfile}" 78 env $env kind get kubeconfig --name ${name} > ~/.kube/${configfile} 79 rm ${tmpkubeconfig} 80 unset KUBECONFIG 81 } 82 83 bekind_delete() { 84 while [[ $# -gt 1 ]] 85 do 86 case $1 in 87 "--host") 88 shift 89 host=$1 90 shift 91 ;; 92 esac 93 done 94 name="$1" 95 shift 96 env="" 97 if [[ -z "${name}" ]]; then 98 name="kind" 99 fi 100 if [[ -n "${host}" ]]; then 101 env="DOCKER_HOST=ssh://${host}" 102 fi 103 echo "> Delete kind cluster ${name} from ${host}" 104 env $env kind delete cluster --name ${name} 105 } 106 107 bekind_list() { 108 while [[ $# -gt 0 ]] 109 do 110 case $1 in 111 "--host") 112 shift 113 host=$1 114 shift 115 ;; 116 esac 117 done 118 env="" 119 if [[ -n "$host" ]]; then 120 env="DOCKER_HOST=ssh://${host}" 121 fi 122 env $env kind get clusters 123 } 124 125 bekind_clean() { 126 while [[ $# -gt 1 ]] 127 do 128 case $1 in 129 "--host") 130 shift 131 host=$1 132 shift 133 ;; 134 esac 135 done 136 env="" 137 if [[ -n "$host" ]]; then 138 env="DOCKER_HOST=ssh://${host}" 139 fi 140 echo "> Clean kind clusters from ${host}" 141 env $env kind delete clusters --all 142 } 143 144 subcommand=$1 145 case $subcommand in 146 "" | "-h" | "--help") 147 bekind_help 148 ;; 149 *) 150 shift 151 bekind_${subcommand} $@ 152 if [ $? = 127 ]; then 153 echo "Error: '$subcommand' is not a known subcommand." >&2 154 echo " Run '$ProgName --help' for a list of known subcommands." >&2 155 exit 1 156 fi 157 ;; 158 esac