-
Notifications
You must be signed in to change notification settings - Fork 10
/
kubectlrc
executable file
·134 lines (120 loc) · 5.28 KB
/
kubectlrc
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
#!/usr/bin/env bash
ANYRC_ENV='kubectlrc'
init() {
K8S_SUBCMD="$1"; shift # expect 'exec'
K8S_GLOBAL_OPTS=''
K8S_CONTAINER_OPTS=''
K8S_EXEC_OPTS=''
K8S_UNKNOWN_OPTS=''
case "${K8S_SUBCMD}" in
exec )
while [[ -n "$1" ]]; do
case "$1" in
--help )
kubectl "${K8S_SUBCMD}" "$1"
exit 0 ;;
--add-dir-header | --alsologtostderr | --insecure-skip-tls-verify | --logtostderr | --match-server-version | --skip-headers | --skip-log-header )
K8S_GLOBAL_OPTS="${K8S_GLOBAL_OPTS} $1" ;;
--add-dir-header=* | --alsologtostderr=* | --insecure-skip-tls-verify=* | --logtostderr=* | --match-server-version=* | --skip-headers=* | --skip-log-header )
K8S_GLOBAL_OPTS="${K8S_GLOBAL_OPTS} $1" ;;
--as | --as-group | --cache-dir | --certificate-authority | --client-certificate | --client-key | --cluster | --context | --kubeconfig | --log-backtrace-at | --log-dir | --log-file | --log-file-max-size | --log-flush-frequency | -n | --namespace | --password | --profile | --profile-output | --request-timeout | -s | --server | --stderrthreshold | --tls-server-name | --token | --user | --username | -v | --v | --vmodule )
K8S_GLOBAL_OPTS="${K8S_GLOBAL_OPTS} $1 $2"
shift ;;
--as=* | --as-group=* | --cache-dir=* | --certificate-authority=* | --client-certificate=* | --client-key=* | --cluster=* | --context=* | --kubeconfig=* | --log-backtrace-at=* | --log-dir=* | --log-file=* | --log-file-max-size=* | --log-flush-frequency=* | -n=* | --namespace=* | --password=* | --profile=* | --profile-output=* | --request-timeout=* | -s=* | --server=* | --stderrthreshold=* | --tls-server-name=* | --token=* | --user=* | --username=* | -v=* | --v=* | --vmodule )
K8S_GLOBAL_OPTS="${K8S_GLOBAL_OPTS} $1" ;;
-c | --container )
K8S_CONTAINER_OPTS="${K8S_CONTAINER_OPTS} $1 $2"
K8S_EXEC_OPTS="${K8S_EXEC_OPTS} $1 $2"
shift ;;
-c=* | --container=* )
K8S_CONTAINER_OPTS="${K8S_CONTAINER_OPTS} $1"
K8S_EXEC_OPTS="${K8S_EXEC_OPTS} $1" ;;
-i | -t | -it | -ti )
K8S_EXEC_OPTS="${K8S_EXEC_OPTS} $1" ;;
-f | --filename | --pod-running-timeout )
K8S_EXEC_OPTS="${K8S_EXEC_OPTS} $1 $2"
shift ;;
-f=* | --filename=* | --pod-running-timeout=* )
K8S_EXEC_OPTS="${K8S_EXEC_OPTS} $1" ;;
-- )
;;
-* )
K8S_UNKNOWN_OPTS="${K8S_UNKNOWN_OPTS} $1" ;;
* )
if [ -z "${K8S_POD:+_}" ]; then
K8S_POD="$1"
elif [ -z ${REMOTE_SHELL:+_} ]; then
REMOTE_SHELL="$1"
fi ;;
esac
shift
done
if [ -z "${K8S_POD:+_}" ] || [ -z "${REMOTE_SHELL:+_}" ]; then
exit 1
fi ;;
* )
kubectl "${K8S_SUBCMD}" $@
exit 0 ;;
esac
}
mk_tmp_dir() {
case "${K8S_SUBCMD}" in
exec )
TMP_DIR="$(kubectl exec ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} ${K8S_POD} -- mktemp -d /tmp/anyrc.XXXXXX)" ;;
esac
}
transfer() {
case "${K8S_SUBCMD}" in
exec )
# Transfer .anyrc
kubectl cp ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} "$(readlink -f ${ANYRC_DANYRC})" "${K8S_POD}:${TMP_DIR}/${DANYRC_NAME}"
if [ ! -z "${ANYRC_K8S_WO_TAR:+_}" ] || [ ! -z "${ANYRC_WO_TAR:+_}" ]; then
# Transfer .anyrc.d
kubectl cp ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} "$(readlink -f ${ANYRC_DANYRCD})" "${K8S_POD}:${TMP_DIR}/${DANYRCD_NAME}"
# Transfer *rc
local CMD_DIR="$(abs_dirname $0)"
for cmd in "${ANYRC_CMD}" "${ANYRC_CMDS[@]}"; do
if [ -f "${CMD_DIR}/${cmd}" ]; then
kubectl cp ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} "$(readlink -f ${CMD_DIR}/${cmd})" "${K8S_POD}:${TMP_DIR}/${cmd}"
fi
done
else
# Transfer .anyrc.d
tar cz -h -C "${DANYRCD_DIR}" "${DANYRCD_BASE}" \
| kubectl exec -i ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} ${K8S_POD} -- tar xz -C "${TMP_DIR}"
# Transfer *rc
local CMD_DIR=$(abs_dirname $0)
local RCS=()
for cmd in "${ANYRC_CMD}" "${ANYRC_CMDS[@]}"; do
if [ -f "${CMD_DIR}/${cmd}" ]; then
RCS=("${RCS[@]}" "${cmd}")
fi
done
tar czf - -h -C ${CMD_DIR} "${RCS[@]}" \
| kubectl exec -i ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} "${K8S_POD}" -- tar xz -C "${TMP_DIR}"
fi ;;
esac
}
connect() {
case "${K8S_SUBCMD}" in
exec )
kubectl exec ${K8S_GLOBAL_OPTS} ${K8S_CONTAINER_OPTS} ${K8S_EXEC_OPTS} "${K8S_POD}" \
-- "${REMOTE_SHELL}" -c "
export DANYRC_NAME=\"${DANYRC_NAME}\"
export DANYRCD_NAME=\"${DANYRCD_NAME}\"
export ANYRC_ROOT=\"${TMP_DIR}\"
export ANYRC_DANYRC=\"\${ANYRC_ROOT}/\${DANYRC_NAME}\"
export ANYRC_DANYRCD=\"\${ANYRC_ROOT}/\${DANYRCD_NAME}\"
export ANYRC_HOME=\"\${ANYRC_DANYRCD}\"
export SHELL=\$(which \"${REMOTE_SHELL}\")
export PATH=\"\${PATH}:\${ANYRC_ROOT}\"
trap \"rm -rf \${ANYRC_ROOT}; exit\" 0
source \"\${ANYRC_DANYRC}\"
if [ \"${REMOTE_SHELL##*/}\" = 'bash' ]; then
bash --rcfile \${ANYRC_DANYRC}
else
${REMOTE_SHELL}
fi" ;;
esac
}
source anyrc