Last active
May 22, 2024 14:02
-
-
Save bradleyfrank/7a2e3d445e4fc2d3af459a9c5f9232c0 to your computer and use it in GitHub Desktop.
Create entries in kubectl interactively
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
| main() { | |
| local OPTIND projects project cluster append_config temp; append_config=1 temp="$(mktemp)" | |
| while getopts ':rp:' flag; do | |
| case "$flag" in | |
| r) unset append_config ;; | |
| p) projects="$OPTARG" ;; | |
| :) echo "Must supply an argument to -${OPTARG}." >&2; exit 1 ;; | |
| \?) echo "Invalid option: -${OPTARG}" >&2; exit 1 ;; | |
| esac | |
| done | |
| if [[ -n $projects ]]; then | |
| projects="$(tr ',' '\n' <<< "$projects")" | |
| else | |
| projects="$(gcloud projects list \ | |
| --sort-by 'projectId' \ | |
| --filter 'projectId !~ "^(sys|quickstart|test|gam)-.*" AND lifecycleState=ACTIVE' \ | |
| --format 'value[separator=";"](projectId,name)' \ | |
| | awk -F ';' '{print $1" ("$2")"}' \ | |
| | fzf --multi | |
| )" | |
| fi | |
| KUBECONFIG="${KUBECONFIG:-"${HOME}/.kube/config"}" | |
| [[ -e $KUBECONFIG ]] && cp "$KUBECONFIG" "${KUBECONFIG}.$(date --iso-8601=seconds | tr -d ':-')" | |
| [[ -z $append_config ]] && rm -f "$KUBECONFIG" | |
| while read -r project; do | |
| projectId="$(awk -F ' ' '{print $1}' <<< "$project")" | |
| gcloud container clusters list \ | |
| --project "$projectId" \ | |
| --format="value[separator=' '](name,zone)" \ | |
| | sed "s/^/${projectId} /g" \ | |
| | awk '{print "("$1") " $2" "$1" "$3}' >> "$temp" | |
| done <<< "$projects" | |
| while read -r cluster; do | |
| name="$(awk '{print $2}' <<< "$cluster")" | |
| project="$(awk '{print $3}' <<< "$cluster")" | |
| location="$(awk '{print $4}' <<< "$cluster")" | |
| gcloud container clusters get-credentials "$name" --project "$project" --region "$location" | |
| done < <(fzf --multi --with-nth 2,1 < "$temp") | |
| rm "$temp" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment