diff --git a/README.md b/README.md index e8609b8..07ec974 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/kubech b/kubech index 7970430..23e919c 100644 --- a/kubech +++ b/kubech @@ -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}" # @@ -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 @@ -132,11 +139,16 @@ 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}\"" @@ -144,7 +156,11 @@ kubechn () { 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 }