Skip to content

Commit

Permalink
feat: support checking ns existence by label (#11)
Browse files Browse the repository at this point in the history
that will speed up namespace switching in big clusters
  • Loading branch information
imbstack committed Jun 16, 2023
1 parent 9f11a54 commit c978cf1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ NOTE:
- Also short names like "kchc/kchn/kchu" are available.
VARS:
KUBECONFIG_SRC_DIR : Set directory with extra kubectl config files to read in kubech commands.
This allow to have multiple config files in addition to
the one at "$HOME/.kube/config".
Default: "$HOME/.kube/config.src.d"
KUBECONFIG_DEST_DIR : Set directory for temporary kubectl config files. The files
in this directory are auto-generated by kubech commands
and you don't need to interact with them or even change that var.
Default: "$HOME/.kube/config.dest.d"
KUBECONFIG_SRC_DIR : Set directory with extra kubectl config files to read in kubech commands.
This allow to have multiple config files in addition to
the one at "\$HOME/.kube/config".
Default: "$HOME/.kube/config.src.d"
KUBECONFIG_DEST_DIR : Set directory for temporary kubectl config files. The files
in this directory are auto-generated by kubech commands
and you don't need to interact with them or even change that var.
Default: "$HOME/.kube/config.dest.d"
KUBECH_NAMESPACE_CHECK : Method used to check if namespace exists before switching to it.
The default lists all namespaces which can be slow in large
clusters. After kubernetes 1.22+ all namespaces have a label
that can be used to speed this up. Set to "label" to enable this.
Available options: "list", "label".
Default: "list"
USAGE:
kubechc : List all contexts
Expand Down
38 changes: 27 additions & 11 deletions kubech
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ KUBECONFIG_ORIG="${KUBECONFIG:-$HOME/.kube/config}"
KUBECONFIG_SRC_DIR="${KUBECONFIG_SRC_DIR:-$HOME/.kube/config.src.d}"
KUBECONFIG_DEST_DIR="${KUBECONFIG_DEST_DIR:-$HOME/.kube/config.dest.d}"
KUBECONFIG_ACTIVE=""
KUBECH_NAMESPACE_CHECK="${KUBECH_NAMESPACE_CHECK:-list}"


#
Expand All @@ -22,14 +23,20 @@ NOTE:
- Also short names like "kchc/kchn/kchu" are available.

VARS:
KUBECONFIG_SRC_DIR : Set directory with extra kubectl config files to read in kubech commands.
This allow to have multiple config files in addition to
the one at "\$HOME/.kube/config".
Default: "${KUBECONFIG_SRC_DIR}"
KUBECONFIG_DEST_DIR : Set directory for temporary kubectl config files. The files
in this directory are auto-generated by kubech commands
and you don't need to interact with them or even change that var.
Default: "${KUBECONFIG_DEST_DIR}"
KUBECONFIG_SRC_DIR : Set directory with extra kubectl config files to read in kubech commands.
This allow to have multiple config files in addition to
the one at "\$HOME/.kube/config".
Default: "${KUBECONFIG_SRC_DIR}"
KUBECONFIG_DEST_DIR : Set directory for temporary kubectl config files. The files
in this directory are auto-generated by kubech commands
and you don't need to interact with them or even change that var.
Default: "${KUBECONFIG_DEST_DIR}"
KUBECH_NAMESPACE_CHECK : Method used to check if namespace exists before switching to it.
The default lists all namespaces which can be slow in large
clusters. After kubernetes 1.22+ all namespaces have a label
that can be used to speed this up. Set to "label" to enable this.
Available options: "list", "label".
Default: "${KUBECH_NAMESPACE_CHECK}"

USAGE:
kubechc : List all contexts
Expand Down Expand Up @@ -132,19 +139,28 @@ alias kchc='kubechc'
# Change kubectl namespace
kubechn () {
kube_namespace="${1:-default}"
kube_namespace_all=$(kubectl get namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
if [[ "${KUBECH_NAMESPACE_CHECK}" == "label" ]]; then
kube_namespace_exists=$(kubectl get namespaces -l "kubernetes.io/metadata.name=${kube_namespace}" -o name)
else
kube_namespace_all=$(kubectl get namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
kube_namespace_exists=$(echo "${kube_namespace_all}" | grep -E "^${kube_namespace}$")
fi

if [[ -n ${1} ]]; then
# Only switch namespace if it exists.
if echo "${kube_namespace_all}" | grep -qE "^${kube_namespace}$"; then
if [[ -n "${kube_namespace_exists}" ]]; then
kubechc "$(kubectl config current-context)" "${kube_namespace}"
kubectl config set-context --current --namespace="${kube_namespace}"
echo "Switched to namespace \"${kube_namespace}\""
else
echo "The namespace \"${kube_namespace}\" doesn't exist"
fi
else
echo "${kube_namespace_all}"
if [[ -n "${kube_namespace_all}" ]]; then
echo "${kube_namespace_all}"
else
kubectl get namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
fi
fi
}

Expand Down

0 comments on commit c978cf1

Please sign in to comment.