Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active December 9, 2025 17:04
Show Gist options
  • Select an option

  • Save wdhowe/726bc72ce086ffeb041f08b4d5964eb6 to your computer and use it in GitHub Desktop.

Select an option

Save wdhowe/726bc72ce086ffeb041f08b4d5964eb6 to your computer and use it in GitHub Desktop.
bashrc/profile
##-- Aliases --##
alias ll='ls -l'
alias k='kubectl'
alias ke='kubectl exec -it'
alias kc='kubectx'
alias kn='kubens'
##-- Completions --##
# bash-completion@2 (installed via brew install bash-completion@2)
[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"
# task completion (go-task / taskfile)
eval "$(task --completion bash)"
# direnv load/unload environments automatically (installed via brew install direnv)
eval "$(direnv hook bash)"
##-- General Functions --##
function nth() {
# Get the nth line from the input.
# Usage: some_command | nth 3
sed -n "${1}p"
}
##-- Git Functions and Prompt --##
# bash-git-prompt (installed via brew install bash-git-prompt)
if [ -f "/opt/homebrew/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR="/opt/homebrew/opt/bash-git-prompt/share"
source "/opt/homebrew/opt/bash-git-prompt/share/gitprompt.sh"
fi
function git-rm-local-merged-branches() {
echo "Removing branches with deleted upstreams..."
git branch -vv | grep ': gone]' | grep -v "^\*" | awk '{print $1}' | xargs -r git branch -D
echo "Removing traditionally merged branches (exclude main,master)..."
git branch --merged | grep -v "^\*" | grep -v "main" | grep -v "master" | xargs git branch --delete
}
function git-clean() {
echo "Fetching latest changes and pruning deleted branches..."
git fetch --prune
git-rm-local-merged-branches
}
function git-fresh-main() {
echo "Checkout a fresh main branch..."
git checkout main &&\
git pull &&\
git-clean
}
function git-undo-last-commit() {
echo "Undoing last commit, keeping changes staged..."
git reset HEAD~1
}
function k8s-cached-images() {
# List all unique container images cached on all nodes in the current Kubernetes cluster
kubectl get nodes -o jsonpath="{.items[*].status.images[*].names}" | tr -s '[[:space:]],' '\n' | tr -d '"' | sort -u
}
function k8s-cached-images-tags() {
# List unique container image tags cached on nodes (excludes digest references)
kubectl get nodes -o jsonpath="{.items[*].status.images[*].names}" |
tr -s '[[:space:]],' '\n' |
tr -d '"' |
grep -v '@sha256:' |
sort -u
}
function k8s-pod-images() {
# List all unique container images in the current Kubernetes cluster
kubectl get pods --all-namespaces -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort -u
}
function k8s-unused-images() {
# List container images cached on nodes but not used by any running pod
comm -23 <(k8s-cached-images-tags) <(k8s-pod-images)
}
function k8s-clear-cached-images() {
echo "Clear unused container images from all nodes (for kind clusters)"
echo "WARNING: This will delete ALL images not currently used by pods"
echo "Sample of unused images (tag references only):"
k8s-unused-images
echo ""
echo "Note: The cleanup will remove all unused images, including digest references."
echo ""
read -p "Continue with deletion? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
for node in $(kubectl get nodes -o jsonpath='{.items[*].metadata.name}'); do
echo "Cleaning images on node: $node"
docker exec "$node" crictl rmi --prune 2>/dev/null ||
docker exec "$node" ctr -n k8s.io images prune 2>/dev/null ||
echo " Could not prune images on $node (unsupported runtime or not a kind cluster)"
done
echo "Done!"
else
echo "Cancelled."
fi
}
### MANAGED BY RANCHER DESKTOP START (DO NOT EDIT)
export PATH="/Users/bhowe/.rd/bin:$PATH"
### MANAGED BY RANCHER DESKTOP END (DO NOT EDIT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment