-
Notifications
You must be signed in to change notification settings - Fork 0
/
.kshell.sh
64 lines (54 loc) · 2.21 KB
/
.kshell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Copy and paste this function into your .zshrc or .bashrc to use.
# This function assumes the following:
# 1. You have fzf (https://github.com/junegunn/fzf) installed
# 2. You have kubectl installed
# 3. Your helm managed deployments contain the label 'app.kubernetes.io/instance'
# where the value is the name of the helm release
# 4. Your active kubeconfig context is already pointing to the cluster
# and namespace where your helm releases are installed
# Usage:
# $ kshell (reset|status)
#
# Passing "reset" as an argument will prompt you to choose a new helm release,
# deploy, etc. Otherwise, kshell will remember your previous choices and
# immediately open up a new shell prompt in your desired Kubernetes workload.
#######################################
# Opens interactive bash shell in a kubernetes deployment managed by Helm.
# Globals:
# KSHELL_RELEASE
# KSHELL_DEPLOY
# KSHELL_CONTAINER
# Arguments:
# None
#######################################
function kshell {
if [[ (-z "$KSHELL_RELEASE" && -z "$KSHELL_DEPLOY" && -z "$KSHELL_CONTAINER") ]]; then
configured=false
else
configured=true
fi
if [[ "$1" == "status" ]]; then
if [[ "$configured" == true ]]; then
echo "Helm Release: $KSHELL_RELEASE"
echo "Deployment: $KSHELL_DEPLOY"
echo "Container: $KSHELL_CONTAINER"
else
echo "kshell not configured yet."
fi
return
fi
if [[ "$configured" == false || ("$1" == "reset") ]]; then
echo "Choose Helm release"
release=$(helm list -q | fzf --height=30% --layout=reverse --border --margin=1 --padding=1)
export KSHELL_RELEASE=$release
echo "Choose deployment"
deploy=$(kubectl get deploy -l "app.kubernetes.io/instance=$release" --sort-by=.metadata.creationTimestamp -o name | fzf --height=30% --layout=reverse --border --margin=1 --padding=1)
export KSHELL_DEPLOY=$deploy
echo "Choose container"
containers=$(kubectl get "$deploy" -o jsonpath='{range .spec.template.spec.containers[*]}{.name}{"\n"}{end}')
container=$(echo "$containers" | fzf --height=30% --layout=reverse --border --margin=1 --padding=1)
export KSHELL_CONTAINER=$container
fi
kubectl exec -it "$deploy" -c "$container" -- bash
}