Skip to content

Instantly share code, notes, and snippets.

@Sekiphp
Last active January 18, 2024 22:21
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 Sekiphp/4f862d701132cbee4326e510f9a114e4 to your computer and use it in GitHub Desktop.
Save Sekiphp/4f862d701132cbee4326e510f9a114e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Load variables
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$DIR/variables.sh"
# Read pod name into variable (allowed only valid pod names)
read_pod_name() {
while read -p "Open shell for pod name: " POD_NAME </dev/tty
do
if [[ ! " ${POD_NAMES[@]} " =~ " ${POD_NAME} " ]]; then
echo "Error: Pod name not found or invalid!"
else
break
fi
done
}
# Read namespace into variable
read_namespace() {
# I have namespace from script argument
if [[ ! -z "$1" ]]; then
NAMESPACE="$1"
return
fi
while read -p "Namespace: " NAMESPACE </dev/tty
do
if [[ ! "$NAMESPACE" =~ ^[0-9]+$ ]]; then
echo "Error: Namespace must contain only numbers!"
else
break
fi
done
}
read_namespace "$1"
echo "================================================================="
az account set --subscription="$SUBSCRIPTION_ID"
az aks get-credentials --name "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP" --overwrite >> /dev/null 2>&1
kubectl get pods -n "$NAMESPACE"
POD_NAMES=($(kubectl get pods -n "$NAMESPACE" | awk 'NR>1 {print $1}'))
# Check if there are any pods in this namespace
if [[ "${#POD_NAMES[@]}" -eq 0 ]]; then
echo "Not found any pods - terminating!"
exit 101
fi
echo "================================================================="
read_pod_name
# Open shell for specified pod
kubectl exec -it -n "$NAMESPACE" "$POD_NAME" -- "/bin/sh"
@Sekiphp
Copy link
Author

Sekiphp commented Jan 13, 2024

variables.sh

#!/bin/bash

SUBSCRIPTION_ID=""
CLUSTER_NAME=""
RESOURCE_GROUP=""

@Sekiphp
Copy link
Author

Sekiphp commented Jan 18, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment