Last active
September 13, 2018 20:26
-
-
Save chancez/5658276a06330dd4bc65346eb9e3ed89 to your computer and use it in GitHub Desktop.
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 kpod() { | |
local CMD=(kubectl get pods --no-headers) | |
local NAMESPACE="$1" | |
if [ -n "$NAMESPACE" ]; then | |
CMD+=(--namespace "$NAMESPACE") | |
fi | |
"${CMD[@]}" | fzf --select-1 | awk '{print $1}' | |
} | |
function kcontainer() { | |
local POD="$1" | |
local NAMESPACE="$2" | |
if [ -z "$POD" ]; then | |
echo "Must specify pod!" | |
return 1 | |
fi | |
local CMD=(kubectl get pod "$POD" -o json) | |
if [ -n "$NAMESPACE" ]; then | |
CMD+=(--namespace "$2") | |
fi | |
local CONTAINERS="$(${CMD[@]} | jq -r '.spec.containers[].name')" | |
local CONTAINER="$(echo "$CONTAINERS" | fzf --select-1)" | |
if [ "$CONTAINER" == "" ]; then | |
echo "Must specify container for pod $POD. Choices: ${$(echo $CONTAINERS | paste -sd ', ' -)}" | |
return 1 | |
fi | |
echo "$CONTAINER" | |
} | |
function kexec() { | |
local POD | |
local CONTAINER | |
local NAMESPACE | |
local PARAMS=() | |
while [ $# -gt 0 ]; do | |
local key="$1" | |
local value="$2" | |
case "$1" in | |
-p|--pod) | |
POD=$value | |
shift 2 | |
;; | |
-c|--container) | |
CONTAINER=$value | |
shift 2 | |
;; | |
-n|--namespace) | |
NAMESPACE=$value | |
shift 2 | |
;; | |
--) # end argument parsing and pass the rest as arguments to the logs command | |
shift | |
PARAMS+=($@) | |
break | |
;; | |
-*|--*=) # unsupported flags | |
echo "Error: Unsupported flag $key" >&2 | |
return 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS+=("$key") | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
set -- "${PARAMS[@]}" | |
if [ -z "$POD" ]; then | |
POD="$(kpod "$NAMESPACE")" | |
local RET=$? | |
if [ $RET -ne 0 ]; then | |
echo "$POD" | |
return $CODE | |
fi | |
fi | |
if [ -z "$POD" ]; then | |
echo "Must specify pod!" | |
return 1 | |
fi | |
if [ -z "$CONTAINER" ]; then | |
CONTAINER="$(kcontainer "$POD" "$NAMESPACE")" | |
local RET=$? | |
if [ $RET -ne 0 ]; then | |
echo "$CONTAINER" | |
return $CODE | |
fi | |
fi | |
if [ -z "$CONTAINER" ]; then | |
echo "Must specify container!" | |
return 1 | |
fi | |
local COLUMNS=$(tput cols) | |
local LINES=$(tput lines) | |
local TERM=xterm | |
local EXEC_CMD=(kubectl exec -i -t "$POD" -c "$CONTAINER") | |
if [ -n "$NAMESPACE" ]; then | |
EXEC_CMD+=(--namespace "$NAMESPACE") | |
fi | |
# test if bash exists, if it does, we'll use bash, otherwise use sh | |
"${EXEC_CMD[@]}" -- test -e /bin/bash 2> /dev/null | |
if [ $? -eq 0 ] ; then | |
local KUBE_SHELL=${KUBE_SHELL:-/bin/bash} | |
else | |
local KUBE_SHELL=${KUBE_SHELL:-/bin/sh} | |
fi | |
EXEC_CMD+=(-- "$KUBE_SHELL") | |
# if arguments are passed, then invoke our shell with -c, otherwise just | |
# start an interactive login shell | |
if [ $# -gt 0 ]; then | |
EXEC_CMD+=(-c "$*") | |
else | |
EXEC_CMD+=(-il) | |
fi | |
echo "${EXEC_CMD[@]}" | |
"${EXEC_CMD[@]}" | |
} | |
function klogs() { | |
local POD | |
local CONTAINER | |
local NAMESPACE | |
local PARAMS=() | |
while [ $# -gt 0 ]; do | |
local key="$1" | |
local value="$2" | |
case "$1" in | |
-p|--pod) | |
POD=$value | |
shift 2 | |
;; | |
-c|--container) | |
CONTAINER=$value | |
shift 2 | |
;; | |
-n|--namespace) | |
NAMESPACE=$value | |
shift 2 | |
;; | |
--) # end argument parsing and pass the rest as arguments to the logs command | |
shift | |
PARAMS+=($@) | |
break | |
;; | |
-*|--*=) # unsupported flags | |
echo "Error: Unsupported flag $key" >&2 | |
return 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS+=("$key") | |
shift | |
;; | |
esac | |
done | |
# set positional arguments in their proper place | |
set -- "${PARAMS[@]}" | |
if [ -z "$POD" ]; then | |
POD="$(kpod "$NAMESPACE")" | |
local RET=$? | |
if [ $RET -ne 0 ]; then | |
echo "$POD" | |
return $CODE | |
fi | |
fi | |
if [ -z "$POD" ]; then | |
echo "Must specify pod!" | |
return 1 | |
fi | |
if [ -z "$CONTAINER" ]; then | |
CONTAINER="$(kcontainer "$POD" "$NAMESPACE")" | |
local RET=$? | |
if [ $RET -ne 0 ]; then | |
echo "$CONTAINER" | |
return $CODE | |
fi | |
fi | |
if [ -z "$CONTAINER" ]; then | |
echo "Must specify container!" | |
return 1 | |
fi | |
local LOGS_CMD=(kubectl logs "$POD" -c "$CONTAINER") | |
if [ -n "$NAMESPACE" ]; then | |
LOGS_CMD+=(--namespace "$NAMESPACE") | |
fi | |
LOGS_CMD+=("$@") | |
echo "${LOGS_CMD[@]}" | |
"${LOGS_CMD[@]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment