You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DevOps-Bash-tools/.envrc-kubernetes

41 lines
1.3 KiB
Plaintext

#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2021-02-25 10:10:53 +0000 (Thu, 25 Feb 2021)
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
tmpdir="/tmp/.kube"
mkdir -pv "$tmpdir"
default_kubeconfig="${HOME:-$(cd ~ && pwd)}/.kube/config"
original_kubeconfig="${KUBECONFIG:-$default_kubeconfig}"
# reload safety - do not source from new tmpdir - not necessary for direnv but useful for local sourcing tests
#if [[ "$original_kubeconfig" =~ $tmpdir ]]; then
# echo "ignoring \$KUBECONFIG=$original_kubeconfig, using default home location $default_kubeconfig"
# original_kubeconfig="${HOME:-$(cd ~ && pwd)}/.kube/config"
#fi
# isolate the kubernetes context to avoid a race condition affecting any other shells or scripts
export KUBECONFIG="$tmpdir/config.${EUID:-${UID:-$(id -u)}}.$$"
# load your real
#cp ~/.kube/config "$KUBECONFIG"
cp "$original_kubeconfig" "$KUBECONFIG"
# XXX: replace MYCONTEXT
kubectl config use-context MYCONTEXT