Skip to content

Instantly share code, notes, and snippets.

@dgoade
Last active October 11, 2022 17:22
Show Gist options
  • Save dgoade/4004314e410da312a1102b646c86229e to your computer and use it in GitHub Desktop.
Save dgoade/4004314e410da312a1102b646c86229e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##==================================
## Common K8s Functions and Aliases
##==================================
##---------------------------------
## Recommended usage
##---------------------------------
# Add the following to your zsh aliases:
# source "${THIS_REPO}/k8s_aliases.zsh"
# alias kcedit="vim ${THIS_REPO}/k8s_aliases.zsh"
# alias kcreload="source ${THIS_REPO}/k8s_aliases.zsh"
##---------------------------------
## Semantic version for these aliases
##---------------------------------
K8S_ALIASES_VERSION=1.0.2
export K8S_ALIASES_VERSION
# top level wrapper to get semver for these aliases
function k8sv() {
echo "ONE Twilio K8S Aliases v${K8S_ALIASES_VERSION}"
}
##------------------------------------
## K8s command-line completion for zsh
## ref: https://tinyurl.com/ys5vu3ka
##------------------------------------
# shellcheck disable=SC1090
source <(kubectl completion zsh)
complete -F __start_kubectl k
##---------------------------------
## owl aliases
##---------------------------------
# unset the env vars from 'owl aws-login'
function aws-login-unset() {
unset AWS_ACCESS_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_ACCOUNT_ID
unset AWS_DEFAULT_REGION
unset AWS_REGION
unset AWS_SECRET_ACCESS_KEY
unset AWS_SECRET_KEY
unset AWS_SECURITY_TOKEN
unset AWS_SESSION_TOKEN
unset AWS_VPC_ID
unset EC2_URL
unset TWILIO_VPC_ID
}
# execute 'owl aws-login' and set the env vars
function aws-login() {
# first, we unset them
aws-login-unset
# now, we execute the aws-login subcommand
# and set the env vars we'll need
eval "$( "${OWL}/bin/owl" aws-login "${@}" )"
}
##---------------------------------
## K8s aliases
##---------------------------------
alias kc="kubectl"
function ecr-login() {
declare -i result=0
declare region
declare registry
region=us-east-1
registry=433117149852.dkr.ecr.us-east-1.amazonaws.com
aws ecr get-login-password \
--region "${region}" | helm registry login \
--username AWS \
--password-stdin "${registry}"
result="${?}"
return "${result}"
}
# list all images for pods running in the current context
# ref: https://tinyurl.com/y7f6vkox
function kcimages() {
kubectl get pods --field-selector=status.phase=Running \
-o jsonpath="{.items[*].spec.containers[*].image}" \
|tr -s "[:space:]" "\n" |sort |uniq -c
}
# manage contexts
alias uc="kubectl config use-context"
alias cc="kubectl config current-context"
alias gc="kubectl config get-contexts"
alias lc="kubectl config get-contexts"
# manage secrets
## get a base64-encoded secret and decode it
function kcgsecret() {
declare -i result
declare secret_key="${1}"
declare secret
secret=$(kubectl get secret "${secret_key}" -o json)
result=${?}
if [[ ${result} = 0 ]]; then
echo "${secret}" | jq -r '.data | map_values(@base64d)'
fi
}
# manage deployments
alias kcgdeps="kubectl get deployments -o wide"
alias kcddeps="kubectl describe deployments"
# manage pods
alias kcgpods="kubectl get pods -o wide"
alias kcdpods="kubectl describe pods"
##---------------------------------
## ArgoCD aliases
##---------------------------------
## port-forward to connect to the api server
## ref: https://tinyurl.com/yc459wjs
function argo-server-pf() {
declare namespace="${1:-argocd}"
echo "Port-forwarding to the ArgoCD instance in ${namespace}..."
kubectl port-forward svc/argocd-server -n "${namespace}" 8080:443
}
## port-forward to the argo-metrics server
## ref: https://tinyurl.com/2p83bfsj
function argo-metrics-pf() {
declare namespace="${1:-argocd}"
echo "Port-forwarding to the argo-metrics instance in ${namespace}..."
kubectl port-forward svc/argocd-metrics -n argocd-client 8082:8082
}
## get the admin password from secrets
## ref: https://tinyurl.com/yc6wbhmy
function argopass() {
declare namespace="${1:-argocd}"
echo "Getting admin password for the ArgoCD instance in ${namespace}..."
kubectl -n "${namespace}" get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment