bin/fzf-browse-pods
#!/usr/bin/env bash
#
# Based on https://github.com/junegunn/fzf/blob/master/ADVANCED.md#using-fzf-as-the-secondary-filter
set -o pipefail
if [[ -n "$DEBUG" ]]; then
# shellcheck disable=SC2086
if [[ "$(echo $DEBUG | tr '[:upper:]' '[:lower:]')" == "verbose" ]]; then
set -x
fi
fi
function debug() {
if [[ -n "$DEBUG" ]]; then
echo "$@"
fi
}
function echo-stderr() {
echo "$@" 1>&2 ## Send message to stderr.
}
function fail() {
printf '%s\n' "$1" >&2 ## Send message to stderr. Exclude >&2 if you don't want it that way.
exit "${2-1}" ## Return a code specified by $2 or 1 by default.
}
function has() {
# Check if a command is in $PATH
which "$@" > /dev/null 2>&1
}
function get-settings() {
SETTING_ENV_NAME=${SETTING_ENV_NAME:-'default-value'}
}
function check-dependencies() {
debug "Checking dependencies..."
# shellcheck disable=SC2041
# Placeholders for whatever programs you really need
for p in 'fzf' 'kubectl' 'sed'
do
if ! has $p; then
fail "Can't find $p in your $PATH"
else
debug "- Found $p"
fi
done
}
function my-name() {
basename "$0"
}
function usage() {
echo "Usage: $(my-name)"
echo
echo "Use fzf to browse running k8s pods"
}
pods() {
: | command='kubectl get pods --all-namespaces' fzf \
--info=inline --layout=reverse --header-lines=1 \
--prompt "$(kubectl config current-context | sed 's/-context$//')> " \
--header $'╱ Enter (kubectl exec) ╱ CTRL-O (open log in editor) ╱ CTRL-R (reload) ╱\n\n' \
--bind 'start:reload:$command' \
--bind 'ctrl-r:reload:$command' \
--bind 'ctrl-/:change-preview-window(80%,border-bottom|hidden|)' \
--bind 'enter:execute:kubectl exec -it --namespace {1} {2} -- bash > /dev/tty' \
--bind 'ctrl-o:execute:${EDITOR:-vim} <(kubectl logs --all-containers --namespace {1} {2}) > /dev/tty' \
--preview-window up:follow \
--preview 'kubectl logs --follow --all-containers --tail=10000 --namespace {1} {2}' "$@"
}
check-dependencies
pods