-
Notifications
You must be signed in to change notification settings - Fork 1
/
pvc
executable file
·163 lines (137 loc) · 3.33 KB
/
pvc
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
160
161
162
163
#!/bin/bash
# FIXME Workaround for https://github.com/kubernetes/kubernetes/issues/55708
[[ "$KUBECTL_PLUGINS_CALLER" ]] && {
export ORIGINAL_PWD=$1
shift 1
}
usage() {
CMD=$0
[[ "$KUBECTL_PLUGINS_CALLER" ]] && CMD="kubectl plugin pvc"
cat <<EOM
Usage: $CMD create PVCNAME SIZE [SRC DST]"
$CMD cp PVCNAME SRC DST
$CMD cat PVCNAME DST
create -- This command will create a new PVC of the given name and size.
Optionally it will copy SRC to DST (within the new PVC).
cp -- Copy a local file SRC to the remote destination DST
cat -- Cat the contents of DST
EOM
exit 1
}
fatal() { echo "FATAL: $@" >&2 ; exit 1 ; }
pvc_tpl() {
local NAME=$1
local SIZE=${2:10Gi}
cat <<EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: $NAME
spec:
volumeMode: Filesystem
resources:
requests:
storage: $SIZE
EOF
}
tpls() {
local TPLS=$1
local NAME=$2
local SIZE=$3
[[ "$TPLS" == *pvc* ]] && cat <<EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: $NAME
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: $SIZE
---
EOF
[[ "$TPLS" == *pod* ]] && cat <<EOF
kind: Pod
apiVersion: v1
metadata:
name: $PVCNAME
spec:
terminationGracePeriodSeconds: 0
containers:
- name: shell
image: busybox
command: ["/bin/sleep", "3600"]
volumeMounts:
- mountPath: "/dst"
name: dst
volumes:
- name: dst
persistentVolumeClaim:
claimName: $PVCNAME
---
EOF
}
wait_running() { until [[ "$(kubectl get pod $1 -o jsonpath='{.status.phase}')" == Running ]]; do sleep 1; done ; }
_create() {
local PVCNAME=$1
local SIZE=$2
local SRC=$3
local DST=$4
set -e
[[ "$PVCNAME" ]] || fatal "No PVC name given"
[[ "$SIZE" ]] || fatal "No size given"
kubectl get pvc $PVCNAME >/dev/null 2>&1 && fatal "PVC '$PVCNAME' already exists"
kubectl get pod $PVCNAME >/dev/null 2>&1 && fatal "Pod '$PVCNAME' already exists"
echo "Creating PVC"
tpls pvc $PVCNAME $SIZE | kubectl create -f -
[[ "$SRC" ]] && {
_cp $PVCNAME $SRC $DST
}
}
_cp() {
local PVCNAME=$1
local SRC=$2
local DST=$3
[[ "$SRC" ]] || fatal "No source file given"
[[ "$DST" ]] || fatal "No destination file given"
[[ "$SRC" == /* ]] || SRC="$ORIGINAL_PWD/$SRC"
echo "Populating PVC"
tpls pod $PVCNAME | kubectl create -f -
wait_running $PVCNAME
# kubectl cp $SRC $PVCNAME:"/dst/$DST"
cat $SRC | bzip2 -1 -z | kubectl exec -it $PVCNAME -- sh -c "bzip2 -d > /dst/$DST"
kubectl exec $PVCNAME -- sh -c "cd dst && ls -shAl"
kubectl exec $PVCNAME -- sh -c "killall sleep"
echo "Cleanup"
tpls pod $PVCNAME | kubectl delete -f -
}
_cat() {
local PVCNAME=$1
local FN=$2
[[ "$PVCNAME" ]] || fatal "No PVC name given"
kubectl get pvc $PVCNAME >/dev/null 2>&1 || fatal "PVC '$PVCNAME' does not exists"
[[ "$FN" ]] || fatal "No filename given"
tpls pod $PVCNAME | kubectl create -f - >&2
wait_running $PVCNAME
kubectl exec $PVCNAME -- sh -c "cd dst && cat $FN | bzip2 -1 -z" | bzip2 -d
kubectl exec $PVCNAME -- sh -c "killall sleep"
tpls pod $PVCNAME | kubectl delete -f - >&2
}
main() {
if [[ "$1" == create || "$1" == cat || "$1" == cp ]];
then
local CMD=_$1
shift 1
$CMD $@
else
fatal "Unknown command: $1"
fi
}
if [[ ${#} == 0 || $@ == *-h* ]];
then
usage
else
main $@
fi