Skip to content

Instantly share code, notes, and snippets.

@jondlm
Last active February 20, 2019 09:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondlm/35cbf0363eb925e2eff6ff86c0a30992 to your computer and use it in GitHub Desktop.
Save jondlm/35cbf0363eb925e2eff6ff86c0a30992 to your computer and use it in GitHub Desktop.
Kubernetes Fuzzy Helper Scripts
#!/usr/bin/env bash
# vim: set syntax=sh:
set -e
if ! hash kubectl 2>/dev/null; then echo "Please install \`kubectl\` before running this command (https://kubernetes.io/docs/tasks/tools/install-kubectl/)"; exit 1; fi
if ! hash fzf 2>/dev/null; then echo "Please install \`fzf\` before running this command (https://github.com/junegunn/fzf#installation)"; exit 1; fi
selection=`kubectl get pods --all-namespaces | grep -v 'NAMESPACE' | fzf --header "Select a pod..."`
namespace=`echo "$selection" | awk '{ print $1 }'`
pod=`echo "$selection" | awk '{ print $2 }'`
containers=`kubectl -n $namespace get pods $pod -o jsonpath='{range .spec.containers[*]}{@.name}{"\n"}{end}'`
container_count=$((`echo "$containers" | wc -l`))
if [ "$container_count" -gt "1" ]; then
container=`echo "$containers" | fzf --header "Select a container..."`
else
container=$containers
fi
kubectl -n $namespace exec -it $pod -c $container ash ||\
kubectl -n $namespace exec -it $pod -c $container bash ||\
kubectl -n $namespace exec -it $pod -c $container sh
#!/usr/bin/env bash
# vim: set syntax=sh:
set -e
if ! hash kubectl 2>/dev/null; then echo "Please install \`kubectl\` before running this command (https://kubernetes.io/docs/tasks/tools/install-kubectl/)"; exit 1; fi
if ! hash fzf 2>/dev/null; then echo "Please install \`fzf\` before running this command (https://github.com/junegunn/fzf#installation)"; exit 1; fi
selection=`kubectl get pods --all-namespaces | grep -v 'NAMESPACE' | fzf --header "Select a pod to delete..."`
namespace=`echo "$selection" | awk '{ print $1 }'`
pod=`echo "$selection" | awk '{ print $2 }'`
kubectl -n $namespace delete pod $pod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment