-
Notifications
You must be signed in to change notification settings - Fork 9
/
kapply
executable file
·79 lines (70 loc) · 2.15 KB
/
kapply
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
#!/bin/bash
set -e
TMPFILE=kapply-tmp${RANDOM}
CMD="apply"
OBJECT=""
VERBOSE=""
BLANKMISSING=""
while [[ "$1" == "-"* ]]; do
case "$1" in
-t) TESTRUN="true" ;;
-b) BLANKMISSING="true" ;;
-c) CMD="create" ;;
-p) CMD="path"
OBJECT="$2"
shift ;;
-v) VERBOSE="true" ;;
*) echo "Uknown option '$1'" ; exit 1 ;;
esac
shift
done
while [[ -n "${1}" ]]; do
file="${1}"
shift
rm -f "${TMPFILE}"
IFS=''
cat "${file}" | while read line ; do
pos=0
while [[ "${line:pos}" == *"\${"* && "$line" == *"}"* ]]; do
pre=$(echo "$line" | sed "s/\([^\$]*\)\${\([^}]*\)}\(.*\)/\1/")
var=$(echo "$line" | sed "s/\([^\$]*\)\${\([^}]*\)}\(.*\)/\2/")
rest=$(echo "$line" | sed "s/\([^\$]*\)\${\([^}]*\)}\(.*\)/\3/")
save_var="${var}"
# .file.property
if [[ "$var" == "."* ]]; then
if [[ "${var:1}" == *"."* ]]; then
file=$(echo "${var}" | sed "s/^\(.[^.]*\).*$/\1/")
var=$(echo "${var}" | sed "s/^\(.[^.]*\)\.\(.*\)$/\2/")
var=$(cat "${file}" | grep "^${var}=" | sed "s/^[^=]*=//")
else
var=$(cat "${var}")
fi
else
# var == env var name
if [[ "${!var}" == "" ]]; then
var=$(cat .env | grep "^${var}=" | sed "s/^[^=]*=//")
else
var=${!var} # Use env var named "${var}"'s value
fi
fi
if [[ "${var}" == "" && -z "${BLANKMISSING}" ]]; then
var='${'${save_var}'}'
fi
line="$pre$var" #$rest"
pos=${#line} # Don't reparse what we just added
line="$line$rest"
done
echo "$line" >> "${TMPFILE}"
done
# echo "> ${file}"
( [[ -n "$VERBOSE" ]] || [[ -n "$TESTRUN" ]] ) && cat "${TMPFILE}"
if [[ -n "$TESTRUN" ]]; then
# cat "${TMPFILE}"
true
elif [[ "$CMD" == "apply" || "$CMD" == "create" ]]; then
kubectl $CMD -f "${TMPFILE}"
else
kubectl patch $OBJECT --type=json -p "$(cat ${TMPFILE})"
fi
rm -f "${TMPFILE}"
done