Skip to content

Instantly share code, notes, and snippets.

@stuart-warren
Last active February 4, 2022 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuart-warren/e5d22f51ee0affd17cbd459ddf2f67c9 to your computer and use it in GitHub Desktop.
Save stuart-warren/e5d22f51ee0affd17cbd459ddf2f67c9 to your computer and use it in GitHub Desktop.
kubectx/kubens multi context magic
# BSD 2-Clause License
# Copyright (c) 2019, Stuart Warren
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# located at $ZSH_CUSTOM/kubectx.zsh
# specify export KUBECONFIG="/some/file" before sourcing oh-my-zsh
export KUBECTXTTYCONFIG="${HOME}/.kube/tty/$(basename $(tty) 2>/dev/null || echo 'notty')"
mkdir -p "$(dirname $KUBECTXTTYCONFIG)"
export KUBECONFIG="${KUBECTXTTYCONFIG}:${KUBECONFIG}"
cat <<EOF >${KUBECTXTTYCONFIG}
apiVersion: v1
kind: Config
current-context: ""
EOF
ct() {
kubectx $*
kubectl --context="$(kubectl --kubeconfig $KUBECTXTTYCONFIG config current-context)" config view --raw --minify -o json | jq 'del(.users,.clusters)' >$KUBECTXTTYCONFIG
}
_fzf_complete_ct() {
_fzf_complete "+m --ansi --no-preview" "$@" < <(
KUBECTX_IGNORE_FZF=true kubectx
)
}
alias ns="kubens"
_fzf_complete_ns() {
_fzf_complete "+m --ansi --no-preview" "$@" < <(
KUBECTX_IGNORE_FZF=true kubens
)
}
@jack4it
Copy link

jack4it commented Nov 23, 2019

Got an error like error: current-context is not set

[edit] see ahmetb/kubectx#12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment