- Load the functions by appending
source functions.zsh
at the end of your.zshrc
file. - Log in to gcloud:
gcloud auth login
- Set the default region:
gcloud config set compute/region us-east1
- Run the function:
genter -f FILTER_CONTEXT_AND_PODS -c CUSTOM_COMMAND
. By default the command executed in the pod is bash.
Last active
April 5, 2022 19:36
-
-
Save kucho/b9abc77b4c6d9dc059f9504757859fa0 to your computer and use it in GitHub Desktop.
gcloud + kubectl + fzf
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
function myip { | |
dig +short txt ch whoami.cloudflare @1.0.0.1 | tr -d '"' | |
} | |
function gconf { | |
local -r g=("gcloud" "config" "configurations") | |
local -r header='Select the configuration' | |
${g} activate ${1:-$(${g} list | fzf --header "$header" --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1)} | |
} | |
function genv { | |
local -r g=("gcloud" "projects") | |
local -r header='Select the project' | |
gcloud config set project ${1:-$(${g} list | fzf --header "$header" --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1)} | |
} | |
function gcluster { | |
local -r g=("gcloud" "container" "clusters") | |
local -r header='Select the cluster' | |
${g} list | fzf --header "$header" --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1 | |
} | |
function gcred { | |
local -r g=("gcloud" "container" "clusters") | |
${g} get-credentials ${1:-{gcluster}} | |
} | |
function gname { | |
local -r g=("kubectl" "get" "namespace") | |
local -r header='Select the namespace' | |
local -r namespace=$(${g} | fzf --header ${header} --query "$1" --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1) | |
kubectl config set-context --current --namespace="$namespace" | |
} | |
function gpod { | |
local -r header='Select the pod' | |
local -r podList=$(kubectl get pods -o custom-columns=NAME:metadata.name --no-headers) | |
if [ $(wc -l <<< "$podList") -eq 1 ]; then | |
echo "$podList" | |
else | |
kubectl get pods | fzf --header ${header} --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1 | |
fi | |
} | |
function gcont { | |
local -r pod=${1:?"The pod must be specified."} | |
kubectl get pods ${pod} -o jsonpath='{.spec.containers[*].name}' | |
} | |
function gexec { | |
local -r pod=$(gpod) | |
local container=$(gcont ${pod}) | |
if [ ! $(wc -l <<< "$container") -eq 1 ]; then | |
container=$(gcont ${pod} | fzf --header ${header} --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1) | |
fi | |
local command="$1" | |
if [ -z "$1" -a "$1" != " " ]; then | |
command='bash' | |
fi | |
kubectl exec -it ${pod} -c ${container} -- ${command} | |
} | |
function gips { | |
local -r g=("gcloud" "container" "clusters") | |
gcloud container clusters describe ${1:-$(${g} list | fzf --height 50% --header-lines 1 --reverse | cut -d ' ' -f 1)} \ | |
--flatten="masterAuthorizedNetworksConfig.cidrBlocks[]" \ | |
--format="value(masterAuthorizedNetworksConfig.cidrBlocks.displayName:label='Name', masterAuthorizedNetworksConfig.cidrBlocks.cidrBlock:label='IP')" | |
} | |
function gipcheck { | |
local userName=${GCLOUD_NAME} | |
local -r ipTable=$(gips "$1") | |
local selectedUser | |
if [[ $userName ]]; then | |
echo "Verifying IP as $userName" | |
selectedUser=$(echo ${ipTable} | fzf --height 50% --reverse --filter "$userName" | cut -f 1,2) | |
read userName userIp <<< ${selectedUser} | |
else | |
echo 'Huh, a man with no name...' | |
local -r header='Select your name from the list' | |
selectedUser=$(echo ${ipTable} | fzf --height 50% --header ${header} --reverse | cut -f 1,2) | |
read userName userIp <<< ${selectedUser} | |
export GCLOUD_NAME="$userName" | |
fi | |
local -r currentIp=$(myip) | |
local userIp=$(echo "$userIp" | sed 's/\/32$//') | |
if [[ $userIp != $currentIp ]]; then | |
echo "Your current IP $currentIp is not registered or changed from last time ($userIp). Please update your cluster configuration." | |
return 1 | |
else | |
echo 'Your IP is still valid' | |
return 0 | |
fi | |
} | |
function genter { | |
# Check por options | |
while test $# -gt 0; do | |
case "$1" in | |
-f) | |
shift | |
filter=$1 | |
shift | |
;; | |
-c) | |
shift | |
command=$1 | |
shift | |
;; | |
*) | |
echo "$1 is not a recognized flag!" | |
return 1; | |
;; | |
esac | |
done | |
genv | |
local -r clusterName=$(gcluster) | |
gcred "$clusterName" | |
# Check ip | |
gipcheck "$clusterName" | |
local -r isIpValid=$? # Check status code from last command | |
if [[ ! isIpValid ]]; then | |
return 0 | |
fi | |
gname "$filter" | |
gexec "$command" "$filter" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment