-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s-shell
executable file
·44 lines (36 loc) · 1.19 KB
/
k8s-shell
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
#!/usr/bin/env bash
# Author: github.com/danielhoherd
# License: MIT
# Purpose: Spawn a new container and drop into a shell
usage(){ cat <<EOF
Launches a k8s pod with an optional docker container given.
-h Print help
-d Dry run and print output spec
-i Docker image
-l Labels to add as k1=v1,k2=v2
-N Specify a node
-n Namespace
-x Enable xtrace
${0##*/} [-h] | [-x] [-d] [-n namespace] [-N k8s-nodename] [-i docker-image] [command]
EOF
}
extra_args=()
dry_or_interactive=( -t -i --rm '--restart=Never' )
while getopts ':i:hl:xdn:N:' option ; do
case "${option}"
in
x) set -x ;;
d) dry_or_interactive=( '--dry-run=client' -o yaml '--restart=OnFailure' ) ;;
i) IMAGE="$OPTARG" ;;
l) extra_args+=( --labels "$OPTARG" ) ;;
n) extra_args+=( --namespace "$OPTARG" ) ;;
N) node="$OPTARG" ;;
h|*) usage ; exit 1 ;
esac
done
shift $((OPTIND - 1))
epochtime="$(date +%s)"
if [[ -n "$node" ]] ; then
extra_args+=( '--overrides={ "spec": { "nodeSelector": { "kubernetes.io/hostname": "'"$node"'" } } }' )
fi
kubectl run --rm "${extra_args[@]}" "${dry_or_interactive[@]}" "${USER//./-}-test-${epochtime}" --image="${IMAGE:-quay.io/danielhoherd/dw}" "${@:-bash}"