Last active
April 17, 2026 13:39
-
-
Save beli-sk/d37935ff3c1a8dcc49b7021fbac9e7ca to your computer and use it in GitHub Desktop.
create temporary kubeconfig for bash session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-------------------------------------------------------------------------- | |
| # | |
| # kubetmp - create temporary kubeconfig for bash session | |
| # | |
| # - to be sourced at start of interactive shell | |
| # | |
| #-------------------------------------------------------------------------- | |
| # Copyright 2021,2026 Michal Belica <https://beli.sk> | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| #-------------------------------------------------------------------------- | |
| KUBETMP_PATH="${HOME}/.kube/tmp" | |
| __kubetmp_ns() { | |
| kubectl config view --minify -o jsonpath="{.contexts[0].context.namespace}" | |
| echo | |
| } | |
| _kubetmp_cleanup() { | |
| local tty=$(tty | tr / _) | |
| local configfile="${KUBETMP_PATH}/config.${tty}" | |
| unset KUBECONFIG | |
| [[ -f "$configfile" ]] && rm "$configfile" 2>/dev/null | |
| PS1=$( echo "$PS1" | sed -r 's/(.*)\(ns:[^)]*\)\)(.*)/\1\2/' ) | |
| } | |
| kubetmp() { | |
| local version="0.0.5" | |
| local context | |
| local namespace | |
| local tty=$(tty | tr / _) | |
| local configfile="${KUBETMP_PATH}/config.${tty}" | |
| [[ ! -d "$KUBETMP_PATH" ]] && mkdir -p "$KUBETMP_PATH" | |
| if [[ -z "$1" ]] ; then | |
| context="" | |
| namespace="" | |
| elif [[ "$1" == "c" || "$1" == "ctx" || "$1" == "context" ]] ; then | |
| context="$2" | |
| namespace="$3" | |
| elif [[ "$1" == "n" || "$1" == "ns" || "$1" == "namespace" ]] ; then | |
| context="" | |
| namespace="$2" | |
| elif [[ "$1" == "x" || "$1" == "exit" ]] ; then | |
| context="" | |
| namespace="" | |
| _kubetmp_cleanup | |
| return | |
| elif [[ "$1" == "version" ]] ; then | |
| echo "kubetmp version $version" | |
| echo '' | |
| echo 'Copyright 2021,2026 Michal Belica <https://beli.sk>' | |
| echo '' | |
| echo 'Licensed under the Apache License, Version 2.0 (the "License");' | |
| echo 'you may not use this file except in compliance with the License.' | |
| echo 'You may obtain a copy of the License at' | |
| echo '' | |
| echo ' http://www.apache.org/licenses/LICENSE-2.0' | |
| echo '' | |
| echo 'Unless required by applicable law or agreed to in writing, software' | |
| echo 'distributed under the License is distributed on an "AS IS" BASIS,' | |
| echo 'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.' | |
| echo 'See the License for the specific language governing permissions and' | |
| echo 'limitations under the License.' | |
| echo '' | |
| return | |
| else | |
| echo 'use: kubetmp [command] [args...]' | |
| echo | |
| echo 'commands:' | |
| echo ' create temporary kubeconfig with specified context and/or namespace:' | |
| echo ' context|ctx|c <context> [namespace]' | |
| echo ' namespace|ns|n <namespace>' | |
| echo ' (no command or args uses current context and namespace)' | |
| echo ' special commands:' | |
| echo ' exit -- reset and delete temporary config' | |
| echo ' version -- show version information' | |
| echo ' help -- show this text' | |
| echo | |
| return | |
| fi | |
| PS1=$( echo "$PS1" | sed -r 's/(.*)\(ns:[^)]*\)\)(.*)/\1\2/' ) | |
| if [[ "$KUBECONFIG" != "$configfile" || -n "$context" ]] ; then | |
| unset KUBECONFIG | |
| [[ -n "$context" ]] && kubectl config use-context "$context" | |
| touch "$configfile" | |
| chmod 0600 "$configfile" | |
| kubectl config view --flatten --minify > "$configfile" | |
| export KUBECONFIG="$configfile" | |
| fi | |
| [[ -n "$namespace" ]] && kubectl config set-context --current --namespace="$namespace" | |
| PS1='(ns:$(__kubetmp_ns))'"${PS1}" | |
| } | |
| _kubetmp_completions() { | |
| local kcbackup="$KUBECONFIG" | |
| local words cword | |
| cword="${COMP_CWORD}" | |
| words=("${COMP_WORDS[@]:0:$cword+1}") | |
| if [[ "${#words[@]}" == "2" ]] ; then | |
| COMPREPLY=($(compgen -W "context namespace exit version help" -- "${words[1]}")) | |
| elif [[ "${#words[@]}" == "3" ]] ; then | |
| if [[ "${words[1]}" == "n" || "${words[1]}" == "ns" || "${words[1]}" == "namespace" ]] ; then | |
| COMPREPLY=($(compgen -W "`kubectl get ns -o jsonpath='{.items[*].metadata.name}'`" -- "${words[2]}")) | |
| elif [[ "${words[1]}" == "c" || "${words[1]}" == "ctx" || "${words[1]}" == "context" ]] ; then | |
| COMPREPLY=($(compgen -W "`KUBECONFIG= kubectl config view -o jsonpath='{.contexts[*].name}'`" -- "${words[2]}")) | |
| fi | |
| elif [[ "${#words[@]}" == "4" ]] ; then | |
| if [[ "${words[1]}" == "c" || "${words[1]}" == "ctx" || "${words[1]}" == "context" ]] ; then | |
| unset KUBECONFIG | |
| COMPREPLY=($(compgen -W "`kubectl --context=${words[2]} get ns -o jsonpath='{.items[*].metadata.name}'`" -- "${words[3]}")) | |
| export KUBECONFIG="$kcbackup" | |
| fi | |
| fi | |
| } | |
| complete -F _kubetmp_completions kubetmp | |
| alias ktmp=kubetmp | |
| complete -F _kubetmp_completions ktmp | |
| _kubetmp_cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment