Skip to content

Instantly share code, notes, and snippets.

@yurifrl
Last active October 15, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurifrl/b1dd544e361d0021f0118752d399246b to your computer and use it in GitHub Desktop.
Save yurifrl/b1dd544e361d0021f0118752d399246b to your computer and use it in GitHub Desktop.
A bash script for kubectl port-foward, copy of https://github.com/johanhaleby/kubetail
#!/bin/bash
readonly PROGNAME=$(basename $0)
default_since="10s"
default_namespace="default"
default_line_buffered=""
default_colored_output="pod"
line_buffered="${default_line_buffered}"
colored_output="${default_colored_output}"
pod="${1}"
container=""
selector=""
since="${default_since}"
usage="${PROGNAME} [-h] [-c] [-n] [-t] [-l] [-s] -- kubectl port-foward
where:
-h, --help Show this help text
-c, --container The name of the container to tail in the pod (if multiple containers are defined in the pod). Default is none
-t, --context The k8s context. ex. int1-context. Relies on ~/.kube/config for the contexts.
-l, --selector Label selector. If used the pod name is ignored.
-n, --namespace The Kubernetes namespace where the pods are located (defaults to "default")
examples:
${PROGNAME} my-pod-v1
${PROGNAME} my-pod-v1 -c my-container
${PROGNAME} my-pod-v1 -t int1-context -c my-container
${PROGNAME} -l service=my-service
${PROGNAME} --selector service=my-service --since 10m"
if [ $# -eq 0 ]; then
echo "$usage"
exit 1
fi
if [ "$#" -ne 0 ]; then
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
echo "$usage"
exit 0
;;
-c|--container)
container="$2"
;;
-t|--context)
context="$2"
;;
-l|--selector)
selector="--selector $2"
pod=""
;;
-n|--namespace)
if [ -z "$2" ]; then
namespace="${default_namespace}"
else
namespace="$2"
fi
;;
-b|--line-buffered)
if [ "$2" = "true" ]; then
line_buffered="| grep - --line-buffered"
fi
;;
-k|--colored-output)
if [ -z "$2" ]; then
colored_output="${default_colored_output}"
else
colored_output="$2"
fi
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
fi
# Join function that supports a multi-character seperator (copied from http://stackoverflow.com/a/23673883/398441)
function join() {
# $1 is return variable name
# $2 is sep
# $3... are the elements to join
local retname=$1 sep=$2 ret=$3
shift 3 || shift $(($#))
printf -v "$retname" "%s" "$ret${@/#/$sep}"
}
# Get all pods matching the input and put them in an array. If no input then all pods are matched.
matching_pods=(`kubectl get pods --context=${context} --no-headers ${selector} --namespace=${namespace} | grep "${pod}" | sed 's/ .*//'`)
matching_pods_size=${#matching_pods[@]}
if [ ${matching_pods_size} -eq 0 ]; then
echo "No pods exists that matches ${pod}"
exit 1
else
echo "Pod found ${#matching_pods[@]} ..."
fi
color_end=$(tput sgr0)
# Wrap all pod names in the "kubectl logs <name> -f" command
pod_commands=()
for i in ${!matching_pods[@]};
do
pod=${matching_pods[$i]}
# pod_commands+=("kubectl --context=${context} logs ${pod} ${container} -f --since=${since} --namespace=${namespace} | while read line; do echo \"$colored_line\"; done");
pod_commands+=("kubectl --context=${context} port-forward ${pod} ${container} --namespace=${namespace} 8080:80");
done
# Join all log commands into one string seperated by " & "
join command_to_tail " & " "${pod_commands[@]}"
# Aggreate all logs and print to stdout
CMD="cat <( eval "${command_to_tail}" ) $line_buffered"
eval "$CMD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment