-
Notifications
You must be signed in to change notification settings - Fork 1
/
.kube_profile
159 lines (141 loc) · 4.7 KB
/
.kube_profile
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Prints the currently selected k8s user, cluster, and namespace as
# user@cluster:namespace.
function kube_info {
KUBE_INFO='
{{- $userName := "(none)" }}
{{- $clusterName := "localhost" }}
{{- $namespaceName := "default" }}
{{- range .contexts }}
{{- if eq .name (index $ "current-context") }}
{{- if .context.user }}
{{- $userName = .context.user }}
{{- end }}
{{- if .context.cluster }}
{{- $clusterName = .context.cluster }}
{{- end }}
{{- if .context.namespace }}
{{- $namespaceName = .context.namespace }}
{{- end }}
{{- $userName }}@{{ $clusterName }}:{{ $namespaceName }}
{{- break }}
{{- end }}
{{- end }}'
kubectl config view -o go-template="$KUBE_INFO"
}
# Manages the KUBECONFIG environment variable by apending the given config file
# to the end of the list. If no argument is given, the default KUBECONFIG
# setting is reinstated.
function kcnf {
KUBECONFIG=$(printf '%s:%s:%s' "$SESSION_KUBECONFIG" "${ORIG_KUBECONFIG:-$HOME/.kube/config}" "${1:+$HOME/.kube/$1.yaml}" | sed 's/:::*/:/g; s/^://; s/:$//')
export KUBECONFIG
}
function _kube_profile_configs {
COMPREPLY=()
[ $COMP_CWORD -gt 1 ] && return
local word="${COMP_WORDS[COMP_CWORD]}"
completions=$(cd $HOME/.kube && ls *.yaml | sed 's|\.yaml$||')
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _kube_profile_configs kcnf
# Records the current setting of the KUBECONFIG environment variable.
function kcnf_save {
ORIG_KUBECONFIG=$KUBECONFIG
ORIG_KUBECONFIG_SET=1
}
# Reinstates the original setting of the KUBECONFIG environment variable.
function kcnf_restore {
[ -z "$ORIG_KUBECONFIG_SET" ] || KUBECONFIG=$ORIG_KUBECONFIG
unset ORIG_KUBECONFIG_SET ORIG_KUBECONFIG
}
# Sets the name of the current context to use.
function kco {
if [ -z "$1" ]; then
echo 'kco: context not provided' >&2
return 1
fi
kubectl config use-context "$1"
}
function _kube_profile_contexts {
COMPREPLY=()
[ $COMP_CWORD -gt 1 ] && return
local word="${COMP_WORDS[COMP_CWORD]}"
completions=$(kubectl config get-contexts --no-headers -o name)
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _kube_profile_contexts kco
# Sets the name of the cluster to use in the currently selected context.
function kcl {
if [ -z "$1" ]; then
echo 'kcl: cluster not provided' >&2
return 1
fi
kubectl config set-context --current --cluster "$1"
}
function _kube_profile_clusters {
COMPREPLY=()
[ $COMP_CWORD -gt 1 ] && return
local word="${COMP_WORDS[COMP_CWORD]}"
completions=$(kubectl config get-clusters | tail -n +2)
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _kube_profile_clusters kcl
# Sets the name of the namespace to use in the currently selected context.
function kns {
if [ -z "$1" ]; then
echo 'kns: namespace not provided' >&2
return 1
fi
kubectl config set-context --current --namespace "$1"
}
function _kube_profile_namespaces {
COMPREPLY=()
[ $COMP_CWORD -gt 1 ] && return
local word="${COMP_WORDS[COMP_CWORD]}"
completions=$(kubectl get ns --no-headers -o custom-columns=NAME:.metadata.name)
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _kube_profile_namespaces kns
# Sets the name of the user to use in the currently selected context.
function ku {
if [ -z "$1" ]; then
echo 'ku: user not provided' >&2
return 1
fi
kubectl config set-context --current --user "$1"
}
function _kube_profile_users {
COMPREPLY=()
[ $COMP_CWORD -gt 1 ] && return
local word="${COMP_WORDS[COMP_CWORD]}"
completions=$(kubectl config get-users | tail -n +2)
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _kube_profile_users ku
# Initializes a new kubeconfig session by creating a temporary file to hold
# session specific settings for context and prepending that file to the
# KUBECONFIG environnment variable value.
function init_kube_session {
if [ -z "$SESSION_KUBECONFIG" ]; then
trap '[ -z "$SESSION_KUBECONFIG" ] || rm -f "$SESSION_KUBECONFIG"' EXIT
SESSION_KUBECONFIG=$(mktemp -t kube-session.XXXXXXXXXX)
fi
# Initialize the session config from its template file if available.
[ -f ~/.kube/session_config ] &&
cp ~/.kube/session_config "$SESSION_KUBECONFIG"
# Initialize the KUBECONFIG environment variable to include the session
# config file.
kcnf_save
kcnf
if ! kubectl --kubeconfig "$SESSION_KUBECONFIG" config current-context >/dev/null 2>&1; then
# Ensure that a context named "session" exists and use it.
kubectl --kubeconfig "$SESSION_KUBECONFIG" config set-context session
kubectl --kubeconfig "$SESSION_KUBECONFIG" config use-context session
fi
}
function exit_kube_session {
[ -z "$SESSION_KUBECONFIG" ] && return
rm -f "$SESSION_KUBECONFIG"
unset SESSION_KUBECONFIG
kcnf_restore
}
# vim: set ft=sh: