Skip to content

Instantly share code, notes, and snippets.

@gullitmiranda
Last active September 29, 2020 18:00
Show Gist options
  • Save gullitmiranda/ddb01cdb0edf8401031ed92000f6f87e to your computer and use it in GitHub Desktop.
Save gullitmiranda/ddb01cdb0edf8401031ed92000f6f87e to your computer and use it in GitHub Desktop.
#! /bin/bash
function valid_required_envs() {
# Configs
required_envs=(KUBE_CONTEXT KUBE_SERVER)
# Securities
required_envs+=(KUBE_CERTIFICATE_AUTHORITY)
required_envs+=(KUBE_CLIENT_CERTIFICATE KUBE_CLIENT_KEY)
errors=()
for key in "${required_envs[@]}"; do
if [[ -z "${!key}" ]]; then
errors+=("The variable \$${key} can't be empty.")
fi
done
if [[ ! -z "${errors[@]}" ]]; then
echo "Errors:"
printf " %s\n" "${errors[@]}"
exit 1
fi
}
function full_path() {
echo "$( cd "$( dirname "$1" )" && pwd )"
}
valid_required_envs
KUBE_CLUSTER=${KUBE_CLUSTER:-$KUBE_CONTEXT}
KUBE_USER=${KUBE_USER:-$KUBE_CONTEXT}
KUBECONFIG=${KUBECONFIG:-$HOME/.kube/config}
KUBE_CONFIG_DIR=$(full_path "${KUBECONFIG}")
mkdir -p "${KUBE_CONFIG_DIR}"
kubectl config set-cluster ${KUBE_CLUSTER} \
--server=${KUBE_SERVER}
kubectl config set-context ${KUBE_CONTEXT} \
--cluster=${KUBE_CLUSTER} \
--user=${KUBE_USER}
kubectl config set clusters.${KUBE_CONTEXT}.certificate-authority-data ${KUBE_CERTIFICATE_AUTHORITY}
kubectl config set users.${KUBE_CONTEXT}.client-certificate-data ${KUBE_CLIENT_CERTIFICATE}
kubectl config set users.${KUBE_CONTEXT}.client-key-data ${KUBE_CLIENT_KEY}
kubectl config use-context ${KUBE_CONTEXT}
echo "Success generating context ${KUBE_CONTEXT} in \`${KUBECONFIG}\`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment