From db07d307a35bc9b9dfd1fab666b93c81a9757404 Mon Sep 17 00:00:00 2001 From: Banana Date: Fri, 15 Nov 2019 15:21:54 +0100 Subject: [PATCH] some small kubectl helper script to get a shell in a pod by searching for it by given string --- README | 2 +- kubernetes/README | 10 ++++++++++ kubernetes/kubeconnect | 44 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 kubernetes/README create mode 100755 kubernetes/kubeconnect diff --git a/README b/README index b02e938..821a174 100644 --- a/README +++ b/README @@ -5,7 +5,7 @@ # COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 # along with this program. If not, see http://www.sun.com/cddl/cddl.html -# 2018 http://www.bananas-playground.net +# 2011-2019 http://www.bananas-playground.net A bag full of things diff --git a/kubernetes/README b/kubernetes/README new file mode 100644 index 0000000..4b52eec --- /dev/null +++ b/kubernetes/README @@ -0,0 +1,10 @@ +# This program is free software: you can redistribute it and/or modify +# it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE + +# You should have received a copy of the +# COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 +# along with this program. If not, see http://www.sun.com/cddl/cddl.html + +# 2019 http://www.bananas-playground.net + +Stuff while using kubernetes and some of its cli. Nothing special. diff --git a/kubernetes/kubeconnect b/kubernetes/kubeconnect new file mode 100755 index 0000000..d205804 --- /dev/null +++ b/kubernetes/kubeconnect @@ -0,0 +1,44 @@ +#!/bin/bash + +# get a shell in a pod +# kubeconect podname context +# +# podname = the string to find +# context = the context to use for kubectl +# +# uses the default container and /bin/bash as a shell + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE +# +# You should have received a copy of the +# COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 +# along with this program. If not, see http://www.sun.com/cddl/cddl.html + +# 2019 http://www.bananas-playground.net + +if [ $# -lt 2 ]; then + echo "You need to provide pod name and context"; + echo "kubeconnect pod-name context"; + exit 126; +fi; + +PODNAME="$1" +CONTEXT="$2" + +if kubectl config use-context "$CONTEXT" &> /dev/null +then + echo "Using context: "`kubectl config current-context`; + AMOUNT=$(kubectl get pods | grep $PODNAME | awk '{print $1}' | wc -l); + if [[ $AMOUNT -eq 1 ]]; + then + REALPODNAME=$(kubectl get pods | grep $PODNAME | awk '{print $1}'); + echo "Pod found: "$REALPODNAME; + kubectl exec -it $REALPODNAME -- /bin/bash + else + echo "ERROR: No such pod found. '$PODNAME'"; + fi +else + echo "Could not set context: '$CONTEXT'" +fi + -- 2.39.5