1
0
Fork 0

Initial commit

This commit is contained in:
Sergio Talens-Oliag 2023-07-17 23:34:46 +02:00
commit a267b572a3
Signed by: sto
GPG key ID: 821AEE0FD167FBDF
23 changed files with 2173 additions and 0 deletions

362
sbin/cilium-install.sh Executable file
View file

@ -0,0 +1,362 @@
#!/bin/sh
# ----
# File: cilium-install.sh
# Description: Tool to install k8s cilium test clusters using k3d or kind
# Author: Sergio Talens-Oliag <sto@mixinet.net>
# Copyright: (c) 2023 Sergio Talens-Oliag <sto@mixinet.net>
# ----
set -e
# Compute WORK_DIR
SCRIPT="$(readlink -f "$0")"
SCRIPT_DIR="$(dirname "$SCRIPT")"
WORK_DIR_RELPATH=".."
WORK_DIR="$(readlink -f "$SCRIPT_DIR/$WORK_DIR_RELPATH")"
TMPL_DIR="$WORK_DIR/tmpl"
YAML_DIR="$WORK_DIR/yaml"
# ---------
# VARIABLES
# ---------
GATEWAY_API_ENABLED="${GATEWAY_API_ENABLED:-false}"
INGRESS_CONTROLLER_DEFAULT="${INGRESS_CONTROLLER_DEFAULT:-false}"
INGRESS_CONTROLLER_ENABLED="${INGRESS_CONTROLLER_ENABLED:-false}"
LOADBALANCER_MODE="shared"
TUNNEL="vxlan"
K3D_NETWORK_NAME="cilium"
K3D_NET_PREFIX="172.30"
K3D_CLUSTER_SUBNET_PREFIX="10.1"
K3D_SERVICE_SUBNET_PREFIX="10.10"
KIND_NETWORK_NAME="kind"
KIND_NET_PREFIX="172.31"
KIND_CLUSTER_SUBNET_PREFIX="10.2"
KIND_SERVICE_SUBNET_PREFIX="10.20"
NETWORK_TYPE="bridge"
METALLB_ENABLED="true"
METALLB_BASE_URL="https://raw.githubusercontent.com/metallb/metallb"
METALLB_VERSION="v0.13.9"
METALLB_DEPLOY_YAML="config/manifests/metallb-native.yaml"
METALLB_YAML_URL="$METALLB_BASE_URL/$METALLB_VERSION/$METALLB_DEPLOY_YAML"
METALLB_YAML="$YAML_DIR/metallb-native.yaml"
NGINX_IC_ENABLED="true"
NGINX_IC_BASE_URL="https://raw.githubusercontent.com/kubernetes/ingress-nginx"
NGINX_IC_VERSION="controller-v1.7.0"
NGINX_IC_DEPLOY_YAML="deploy/static/provider/cloud/deploy.yaml"
NGINX_IC_YAML_URL="$NGINX_IC_BASE_URL/$NGINX_IC_VERSION/$NGINX_IC_DEPLOY_YAML"
NGINX_IC_YAML="$YAML_DIR/ingress-nginx-deploy.yaml"
# GOTMPLs
TMPL_K3D_CONFIG_YAML="$TMPL_DIR/k3d-config.yaml"
TMPL_KIND_CONFIG_YAML="$TMPL_DIR/kind-config.yaml"
TMPL_IPPOOLS_YAML="$TMPL_DIR/ippools.yaml"
TMPL_CILIUM_YAML="$TMPL_DIR/cilium.yaml"
TMPL_METALLB_CRDS_YAML="$TMPL_DIR/metallb-crds.yaml"
# Adjust variables based on other variables
if [ "$METALLB_ENABLED" = "true" ]; then
BGP_CONTROL_PLANE_ENABLED="false"
else
BGP_CONTROL_PLANE_ENABLED="true"
fi
# ---------
# FUNCTIONS
# ---------
create_network() {
NETWORK_ID="$(
docker network inspect "$NETWORK_NAME" --format "{{.Id}}" 2>/dev/null
)" || true
if [ "$NETWORK_ID" ]; then
echo "Using existing network '$NETWORK_NAME' with id '$NETWORK_ID'"
else
echo "Creating network '$NETWORK_NAME' in docker"
docker network create \
--driver "$NETWORK_TYPE" \
--subnet "$NETWORK_SUBNET" \
--gateway "$NETWORK_GATEWAY" \
--ip-range "$NETWORK_IP_RANGE" \
"$NETWORK_NAME"
fi
}
create_cluster() {
echo "Creating $CTOOL cluster '$CNAME'"
case "$CTOOL" in
k3d)
tmpl \
-v "cnum=$CNUM" \
-v "cname=$CNAME" \
-v "host_ip=$HOST_IP" \
-v "cluster_subnet=$CLUSTER_SUBNET" \
-v "service_subnet=$SERVICE_SUBNET" \
-v "work_dir=$WORK_DIR" \
"$TMPL_K3D_CONFIG_YAML" |
k3d cluster create -c -
;;
kind)
tmpl \
-v "cnum=$CNUM" \
-v "cname=$CNAME" \
-v "host_ip=$HOST_IP" \
-v "cluster_subnet=$CLUSTER_SUBNET" \
-v "service_subnet=$SERVICE_SUBNET" \
-v "work_dir=$WORK_DIR" \
"$TMPL_KIND_CONFIG_YAML" |
kind create cluster --config="-"
;;
esac
echo "Cluster '$CNAME' info"
kubectl --context "$CTX" cluster-info
}
install_gateway_api_crds() {
BASE_URL="https://raw.githubusercontent.com/kubernetes-sigs/gateway-api"
BASE_URL="$BASE_URL/v0.5.1/config/crd"
echo "Installing GatewayAPI CRDs"
for crd_yaml in standard/gateway.networking.k8s.io_gatewayclasses.yaml \
standard/gateway.networking.k8s.io_gateways.yaml \
standard/gateway.networking.k8s.io_httproutes.yaml \
experimental/gateway.networking.k8s.io_referencegrants.yaml; do
kubectl --context "$CTX" apply -f "$BASE_URL/$crd_yaml"
done
}
cilium_status() {
echo "Checking cilium status"
cilium status --wait --context "$CTX"
}
master_node_ip() {
# If we are not running kube-proxy the cilium Pods can't reach the api server
# because the in-cluster service can't be reached, to fix the issue we use an
# internal IP that the pods can reach, in this case we get the internal IP of
# the master node container
case "$CTOOL" in
k3d) MASTER_NODE="node/$CTX-server-0";;
kind) MASTER_NODE="node/$CNAME-control-plane";;
*) echo "Unknown master node"; exit 1;;
esac
kubectl --context "$CTX" get "$MASTER_NODE" -o wide --no-headers |
awk '{ print $6 }'
}
cilium_cli_install() {
if [ "$GATEWAY_API_ENABLED" = "true" ]; then
install_gateway_api_crds
fi
_xtra_args=""
if [ "$CNUM" = "2" ]; then
_xtra_args="--inherit-ca kind-cilium1"
fi
MASTER_NODE_IP="$(master_node_ip)"
# shellcheck disable=SC2086
tmpl \
-v "master_node_ip=$MASTER_NODE_IP" \
-v "cnum=$CNUM" \
-v "cname=$CNAME" \
-v "bgp_control_plane_enabled=$BGP_CONTROL_PLANE_ENABLED" \
-v "gateway_api_enabled=$GATEWAY_API_ENABLED" \
-v "ingress_controller_default=$INGRESS_CONTROLLER_DEFAULT" \
-v "ingress_controller_enabled=$INGRESS_CONTROLLER_ENABLED" \
-v "loadbalancer_mode=$LOADBALANCER_MODE" \
-v "tunnel=$TUNNEL" \
"$TMPL_CILIUM_YAML" |
cilium install --context "$CTX" --helm-values - $_xtra_args
# Wait for the deployment
cilium_status
echo "Enabling hubble"
cilium hubble enable --ui --context "$CTX"
}
cilium_helm_install() {
if [ "$GATEWAY_API_ENABLED" = "true" ]; then
install_gateway_api_crds
fi
helm repo add cilium https://helm.cilium.io/ >/dev/null || true
# Copy the cilium-ca to the second cluster
if [ "$CNUM" = "2" ]; then
echo "Copying the 'cilium-ca' from '$CTOOL-cilium1' to '$CTX'"
kubectl --context "$CTOOL-cilium1" -n kube-system get secrets/cilium-ca \
-o yaml | kubectl apply --context "$CTX" -f -
fi
MASTER_NODE_IP="$(master_node_ip)"
# shellcheck disable=SC2086
tmpl \
-v "master_node_ip=$MASTER_NODE_IP" \
-v "cnum=$CNUM" \
-v "cname=$CNAME" \
-v "bgp_control_plane_enabled=$BGP_CONTROL_PLANE_ENABLED" \
-v "gateway_api_enabled=$GATEWAY_API_ENABLED" \
-v "ingress_controller_default=$INGRESS_CONTROLLER_DEFAULT" \
-v "ingress_controller_enabled=$INGRESS_CONTROLLER_ENABLED" \
-v "loadbalancer_mode=$LOADBALANCER_MODE" \
-v "tunnel=$TUNNEL" \
"$TMPL_CILIUM_YAML" |
helm upgrade --install cilium cilium/cilium --version 1.13.1 \
--kube-context "$CTX" --namespace=kube-system --values=-
}
cilium_install(){
echo "Installing cilium in cluster '$CNAME'"
cilium_helm_install
cilium_status
}
lb_download_yaml() {
[ -d "$YAML_DIR" ] || mkdir "$YAML_DIR"
curl -fsSL -o "$METALLB_YAML" "$METALLB_YAML_URL"
}
lb_install() {
if [ "$METALLB_ENABLED" = "true" ]; then
if [ ! -f "$METALLB_YAML" ]; then
lb_download_yaml
fi
echo "Installing metallb on kind cluster '$CNAME'"
kubectl --context "$CTX" apply -f "$METALLB_YAML"
echo "Waiting for metallb to be ready"
kubectl --context "$CTX" rollout status deployment --timeout="120s" \
-n "metallb-system" "controller"
echo "Configuring metallb"
tmpl -v "lb_pool_range=$LB_POOL_RANGE" "$TMPL_METALLB_CRDS_YAML" |
kubectl --context "$CTX" apply -f -
elif [ "$BGP_CONTROL_PLANE_ENABLED" = "true" ]; then
echo "Adding LB IPAM Pools"
tmpl -v "lb_pool_cdir=$LB_POOL_CDIR" "$TMPL_IPPOOLS_YAML" |
kubectl --context "$CTX" apply -f -
fi
}
ingress_download_yaml() {
[ -d "$YAML_DIR" ] || mkdir "$YAML_DIR"
curl -fsSL -o "$NGINX_IC_YAML" "$NGINX_IC_YAML_URL"
}
ingress_install() {
if [ "$NGINX_IC_ENABLED" = "true" ]; then
if [ ! -f "$NGINX_IC_YAML" ]; then
ingress_download_yaml
fi
echo "Installing nginx ingress controller on kind cluster '$CNAME'"
kubectl --context "$CTX" apply -f "$NGINX_IC_YAML"
echo "Waiting for the nginx controller to be ready"
kubectl --context "$CTX" wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=120s
fi
}
mesh_install() {
echo "Enabling cluster-mesh on cluster '$CNAME'"
cilium clustermesh enable --context "$CTX" --service-type LoadBalancer
echo "Checking cilium status on cluster '$CNAME'"
cilium status --context "$CTX" --wait
if [ "$CNUM" -eq "2" ]; then
echo "Connecting cluster"
cilium clustermesh connect --context "$CTOOL-cilium1" \
--destination-context "$CTOOL-cilium2"
echo "Checking cilium status on cluster '$CNAME'"
cilium status --context "$CTX" --wait
fi
}
usage() {
cat <<EOF
Usage: $0 CTOOL CLUSTER [OPERATION]
- CTOOL is 'k3d' or 'kind'
- CLUSTER is '1' or '2'
- OPERATION is one of:
- 'base' (== 'network,cluster,cilium,lb,ingress')
- 'full' (== 'base,mesh')
- 'network'
- 'cluster'
- 'cilium'
- 'lb'
- 'lb-yaml'
- 'ingress'
- 'ingress-yaml'
- 'mesh'
- 'status'
If missing the default OPERATION is 'base'
EOF
exit "$1"
}
# ====
# MAIN
# ====
CTOOL="$1"
CNUM="$2"
ACTION="$3"
case "$CTOOL" in
k3d)
NETWORK_NAME="$K3D_NETWORK_NAME"
NET_PREFIX="$K3D_NET_PREFIX"
CLUSTER_SUBNET_PREFIX="$K3D_CLUSTER_SUBNET_PREFIX"
SERVICE_SUBNET_PREFIX="$K3D_SERVICE_SUBNET_PREFIX"
;;
kind)
NETWORK_NAME="$KIND_NETWORK_NAME"
NET_PREFIX="$KIND_NET_PREFIX"
CLUSTER_SUBNET_PREFIX="$KIND_CLUSTER_SUBNET_PREFIX"
SERVICE_SUBNET_PREFIX="$KIND_SERVICE_SUBNET_PREFIX"
;;
*) usage 1;;
esac
case "$CNUM" in
1|2) ;;
*) usage 1 ;;
esac
# Adjust variables based on the input arguments
CNAME="cilium$CNUM"
CTX="$CTOOL-$CNAME"
HOST_IP="127.$NET_PREFIX.$CNUM"
CLUSTER_SUBNET="$CLUSTER_SUBNET_PREFIX$CNUM.0.0/16"
SERVICE_SUBNET="$SERVICE_SUBNET_PREFIX$CNUM.0.0/16"
NETWORK_SUBNET="$NET_PREFIX.0.0/16"
NETWORK_GATEWAY="$NET_PREFIX.0.1"
NETWORK_IP_RANGE="$NET_PREFIX.0.0/17"
LB_POOL_CDIR="$NET_PREFIX.20$CNUM.0/24"
LB_POOL_RANGE="$NET_PREFIX.20$CNUM.1-$NET_PREFIX.20$CNUM.254"
case "$ACTION" in
base|"")
create_network
create_cluster
cilium_install
lb_install
ingress_install
;;
full)
create_network
create_cluster
cilium_install
lb_install
ingress_install
mesh_install
;;
network) create_network ;;
cluster) create_cluster ;;
cilium) cilium_install;;
lb) lb_install ;;
lb-yaml) lb_download_yaml ;;
ingress) ingress_install;;
ingress-yaml) ingress_download_yaml;;
status) cilium_status;;
mesh) mesh_install;;
*) usage 1;;
esac

91
sbin/cilium-remove.sh Executable file
View file

@ -0,0 +1,91 @@
#!/bin/sh
# ----
# File: cilium-remove.sh
# Description: Tool to remove k8s cilium test clusters using k3d or kind
# Author: Sergio Talens-Oliag <sto@mixinet.net>
# Copyright: (c) 2023 Sergio Talens-Oliag <sto@mixinet.net>
# ----
set -e
# ---------
# VARIABLES
# ---------
K3D_NETWORK_NAME="cilium"
KIND_NETWORK_NAME="kind"
# ---------
# FUNCTIONS
# ---------
delete_network() {
NETWORK_ID="$(
docker network inspect "$NETWORK_NAME" --format "{{.Id}}" 2>/dev/null
)" || true
if [ "$NETWORK_ID" ]; then
echo "Removing network '$NETWORK_NAME' with id '$NETWORK_ID'"
docker network rm "$NETWORK_NAME"
else
echo "Network '$NETWORK_NAME' not found in docker"
fi
}
delete_cluster() {
case "$CTOOL" in
k3d)
echo "Deleting k3d cluster '$CNAME'"
k3d cluster delete "$CNAME"
;;
kind)
echo "Deleting kind cluster '$CNAME'"
kind delete cluster -n "$CNAME"
;;
esac
}
usage() {
cat <<EOF
Usage: $0 CTOOL CLUSTER [OPERATION]
Where:
- CTOOL is 'k3d' or 'kind'
- CLUSTER is '1' or '2'
- OPERATION is one of:
- 'all'
- 'network'
- 'cluster'
If missing the default OPERATION is 'all'
EOF
exit "$1"
}
# ====
# MAIN
# ====
CTOOL="$1"
CNUM="$2"
ACTION="${3:-all}"
case "$CTOOL" in
k3d) NETWORK_NAME="$K3D_NETWORK_NAME" ;;
kind) NETWORK_NAME="$KIND_NETWORK_NAME" ;;
*) usage 1;;
esac
case "$CNUM" in
1|2) ;;
*) usage 1 ;;
esac
CNAME="cilium$CNUM"
case "$ACTION" in
all|"")
delete_cluster
delete_network
;;
cluster) delete_cluster ;;
network) delete_network ;;
*) usage 1;;
esac

318
sbin/tools.sh Executable file
View file

@ -0,0 +1,318 @@
#!/bin/sh
# ----
# File: tools.sh
# Description: Tool to check and install tools used on this repo
# Author: Sergio Talens-Oliag <sto@mixinet.net>
# Copyright: (c) 2023 Sergio Talens-Oliag <sto@mixinet.net>
# ----
set -e
# ---------
# Variables
# ---------
# System dirs
BASH_COMPLETION="/etc/bash_completion.d"
ZSH_COMPLETIONS="/usr/share/zsh/vendor-completions"
# Terminal related variables
if [ "$TERM" ] && type tput >/dev/null; then
bold="$(tput bold)"
normal="$(tput sgr0)"
else
bold=""
normal=""
fi
export yes_no="(${bold}Y${normal}es/${bold}N${normal}o)"
# Versions
CILIUM_CLI_VERSION="${CILIUM_CLI_VERSION:-v0.13.2}"
# Uncomment to get the latest helm version
# GET_HELM_URL="https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3"
HELM_VERSION="${HELM_VERSION:-3.11.2}"
HUBBLE_VERSION="${HUBBLE_VERSION:-v0.11.3}"
K3D_VERSION="${K3D_VERSION:-v5.4.9}"
KIND_VERSION="${KIND_VERSION:-v0.18.0}"
KUBECTL_VERSION="${KUBECTL_VERSION:-1.26.3}"
TMPL_VERSION="${TMPL_VERSION:-v0.4.0}"
# ---------
# Functions
# ---------
# Auxiliary function to read a boolean value. $1 text to show - $2 default value
read_bool() {
case "${2}" in
y | Y | yes | Yes | YES | true | True | TRUE) _yn="Yes" ;;
*) _yn="No" ;;
esac
printf "%s ${yes_no} [%s]: " "${1}" "${bold}${_yn}${normal}"
read -r READ_VALUE
case "${READ_VALUE}" in
'') [ "$_yn" = "Yes" ] && READ_VALUE="true" || READ_VALUE="false" ;;
y | Y | yes | Yes | YES | true | True | TRUE) READ_VALUE="true" ;;
*) READ_VALUE="false" ;;
esac
}
# Auxiliary function to check if a boolean value is set to yes/true or not
is_selected() {
case "${1}" in
y | Y | yes | Yes | YES | true | True | TRUE) return 0 ;;
*) return 1 ;;
esac
}
# Auxiliary function to check if an application is installed
tools_app_installed() {
_app="$1"
type "$_app" >/dev/null 2>&1 || return 1
}
# Function to check if all the tools are installed
tools_check_apps_installed() {
_missing=""
for _app in "$@"; do
tools_app_installed "$_app" || _missing="$_missing $_app"
done
if [ "$_missing" ]; then
echo "The following apps could not be found:"
for _app in $_missing; do
echo "- $_app"
done
exit 1
fi
}
# Auxiliary function to check if we want to install an app
tools_install_app() {
_app="$1"
if tools_app_installed "$_app"; then
echo "$_app found ($(type "$_app"))."
MSG="Re-install in /usr/local/bin?"
OPT="false"
else
echo "$_app could not be found."
MSG="Install it in /usr/local/bin?"
OPT="true"
fi
# Export NONINTERACTIVE as 'true' to use default values
if [ "$NONINTERACTIVE" = "true" ]; then
READ_VALUE="$OPT"
else
read_bool "$MSG" "$OPT"
fi
is_selected "${READ_VALUE}" && return 0 || return 1
}
tools_check_cilium() {
if tools_install_app "cilium"; then
tmp_dir="$(mktemp -d)"
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://github.com/cilium/cilium-cli/releases/download"
url="$url/${CILIUM_CLI_VERSION}/cilium-linux-$arch.tar.gz"
curl -fsSL -o "$tmp_dir/cilium.tar.gz" "$url"
tar xzf "$tmp_dir/cilium.tar.gz" -C "$tmp_dir" "cilium"
sudo install "$tmp_dir/cilium" /usr/local/bin
rm -rf "$tmp_dir"
cilium version
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "cilium completion bash > $BASH_COMPLETION/cilium"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "cilium completion zsh > $ZSH_COMPLETIONS/_cilium"
fi
fi
}
tools_check_docker() {
if tools_install_app "docker"; then
tmp_dir="$(mktemp -d)"
curl -fsSL -o "$tmp_dir/install-docker.sh" "https://get.docker.com"
sh "$tmp_dir/install-docker.sh"
rm -rf "$tmp_dir"
sudo usermod -aG docker "$(id -un)"
docker --version
fi
}
tools_check_helm() {
if tools_install_app "helm"; then
tmp_dir="$(mktemp -d)"
if [ "$GET_HELM" ]; then
curl -fsSL -o "$tmp_dir/get_helm.sh" "$GET_HELM"
bash "$tmp_dir/get_helm.sh"
else
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://get.helm.sh/helm-v$HELM_VERSION-$os-$arch.tar.gz"
curl -fsSL -o "$tmp_dir/helm.tar.gz" "$url"
tar xzf "$tmp_dir/helm.tar.gz" -C "$tmp_dir" "$os-$arch/helm"
sudo install "$tmp_dir/$os-$arch/helm" /usr/local/bin
fi
rm -rf "$tmp_dir"
helm version
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "helm completion bash > $BASH_COMPLETION/helm"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "helm completion zsh > $ZSH_COMPLETIONS/_helm"
fi
fi
}
tools_check_hubble() {
if tools_install_app "hubble"; then
tmp_dir="$(mktemp -d)"
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://github.com/cilium/hubble/releases/download"
url="$url/${HUBBLE_VERSION}/hubble-linux-$arch.tar.gz"
curl -fsSL -o "$tmp_dir/hubble.tar.gz" "$url"
tar xzf "$tmp_dir/hubble.tar.gz" -C "$tmp_dir" "hubble"
sudo install "$tmp_dir/hubble" /usr/local/bin
rm -rf "$tmp_dir"
hubble version
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "hubble completion bash > $BASH_COMPLETION/hubble"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "hubble completion zsh > $ZSH_COMPLETIONS/_hubble"
fi
fi
}
tools_check_k3d() {
if tools_install_app "k3d"; then
[ -d /usr/local/bin ] || sudo mkdir /usr/local/bin
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh |
TAG="$K3D_VERSION" bash
k3d version
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "k3d completion bash > $BASH_COMPLETION/k3d"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "k3d completion zsh > $ZSH_COMPLETIONS/_k3d"
fi
fi
}
tools_check_kind() {
if tools_install_app "kind"; then
os="$(uname | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://github.com/kubernetes-sigs/kind/releases/download"
url="$url/$KIND_VERSION/kind-$os-$arch"
[ -d /usr/local/bin ] || sudo mkdir /usr/local/bin
tmp_dir="$(mktemp -d)"
curl -fsSL -o "$tmp_dir/kind" "$url"
sudo install "$tmp_dir/kind" /usr/local/bin/
rm -rf "$tmp_dir"
kind version
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "kind completion bash > $BASH_COMPLETION/kind"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "kind completion zsh > $ZSH_COMPLETIONS/_kind"
fi
fi
}
tools_check_kubectl() {
if tools_install_app "kubectl"; then
os="$(uname | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://dl.k8s.io/release/v$KUBECTL_VERSION/bin/$os/$arch/kubectl"
[ -d /usr/local/bin ] || sudo mkdir /usr/local/bin
tmp_dir="$(mktemp -d)"
curl -fsSL -o "$tmp_dir/kubectl" "$url"
sudo install "$tmp_dir/kubectl" /usr/local/bin/
rm -rf "$tmp_dir"
kubectl version --client --output=yaml
if [ -d "$BASH_COMPLETION" ]; then
sudo sh -c "kubectl completion bash > $BASH_COMPLETION/kubectl"
fi
if [ -d "$ZSH_COMPLETIONS" ]; then
sudo sh -c "kubectl completion zsh > $ZSH_COMPLETIONS/_kubectl"
fi
fi
}
tools_check_tmpl() {
if tools_install_app "tmpl"; then
tmp_dir="$(mktemp -d)"
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch="amd64" ;;
esac
url="https://github.com/krakozaure/tmpl/releases/download"
url="$url/${TMPL_VERSION}/tmpl-linux_$arch"
curl -fsSL -o "$tmp_dir/tmpl" "$url"
sudo install "$tmp_dir/tmpl" /usr/local/bin
rm -rf "$tmp_dir"
fi
}
tools_check() {
for _app in "$@"; do
case "$_app" in
cilium) tools_check_cilium;;
docker) tools_check_docker ;;
helm) tools_check_helm ;;
k3d) tools_check_k3d ;;
kind) tools_check_kind ;;
kubectl) tools_check_kubectl ;;
hubble) tools_check_hubble;;
tmpl) tools_check_tmpl ;;
*) echo "Unknown application '$_app'" ;;
esac
done
}
tools_apps_list() {
tools="cilium docker helm k3d kind kubectl hubble tmpl"
echo "$tools"
}
# Usage function
usage() {
cat <<EOF
Command to check and install tools used by our scripts.
Usage:
$(basename "$0") apps|SPACE_SEPARATED_LIST_OF_TOOLS
Where the SPACE_SEPARATED_LIST_OF_TOOLS can include the following apps:
$(for tool in $(tools_apps_list); do echo "- $tool"; done)
EOF
exit "$1"
}
# ----
# MAIN
# ----
# shellcheck disable=SC2046
case "$1" in
"") usage 0 ;;
apps) tools_check $(tools_apps_list) ;;
*) tools_check "$@" ;;
esac
# ----
# vim: ts=2:sw=2:et:ai:sts=2