This repository was archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelm
More file actions
executable file
·150 lines (129 loc) · 4.5 KB
/
helm
File metadata and controls
executable file
·150 lines (129 loc) · 4.5 KB
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
#!/bin/bash
#####
# helm-wrapper tool simplifies helm/tiller version and namespace management.
#
# See https://github.com/cradlepoint/helm-wrapper for installation instructions.
#
#####
set -e # exit on any error
# helper to scan caller-supplied parameters
isParamSpecified() {
PARAM=$1
for P in "${@:2}"; do
if [ $P == "$PARAM" ]; then
return 0 # true
fi
done
return 1 # false
}
# save the original params off- the processing we do is destructive if original
# strings
SAVED_PARAMS="$@"
# parse caller-supplied values
PARAMS=""
while (( "$#" )); do
case "$1" in
--tiller-namespace)
PARAM_TILLER_NAMESPACE=$2
shift 2
;;
--kube-context)
PARAM_KUBECONTEXT=$2
shift 2
;;
--kubeconfig)
PARAM_KUBECONFIG=$2
shift 2
;;
--) # end argument parsing
shift
break
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
eval set -- "$PARAMS"
##############
# Step 1: figure out tiller namespace; this is required because we ask tiller for its version later.
if [ -n "$PARAM_TILLER_NAMESPACE" ]; then
NS_DEBUG="helm-wrapper: using --tiller-namespace $PARAM_TILLER_NAMESPACE"
NS="$PARAM_TILLER_NAMESPACE"
else
# caller did not specify --tiller-namespace so calculate it
if [ -n "$PARAM_KUBECONFIG" ]; then
(>&2 echo "helm-wrapper: error, --kubeconfig is unsupported."); exit 1
elif [ -n "$PARAM_KUBECONTEXT" ]; then
(>&2 echo "helm-wrapper: error, --kube-context is unsupported."); exit 1
# check TILLER_NAMESPACE in environment
elif [ -n "$TILLER_NAMESPACE" ]; then
NS_DEBUG="helm-wrapper: using env TILLER_NAMESPACE $TILLER_NAMESPACE"
NS=$TILLER_NAMESPACE
# check default kubeconfig
elif [ ! -z "`kubectl config current-context 2>/dev/null`" ] ; then
NS=$(kubectl config view --minify --output 'jsonpath={..namespace}')
if [ -n "$NS" ]; then
NS_DEBUG="helm-wrapper: using kubeconfig namespace $NS"
fi
fi
# otherwise use the default tiller namespace, kube-system
if [ -z "$NS" ]; then
NS_DEBUG="helm-wrapper: using (default) kube-system namespace"
NS="kube-system"
fi
fi
NS="--tiller-namespace $NS"
##############
# Step 2: make the helm call, matching tiller version for server calls
# search for commands that require helm and tiller to match
TILLER_COMMANDS=(ls list history get install upgrade delete status)
TILLER_COMMAND_FOUND=false
for COMMAND in "${TILLER_COMMANDS[@]}"; do
if isParamSpecified $COMMAND $@; then
TILLER_COMMAND_FOUND=true
break
fi
done
HELM_LATEST=$(ls /usr/local/bin/helm-* | sort -V | tail -1)
if [ "$TILLER_COMMAND_FOUND" == "true" ]; then
# execute command using helm that matches tiller version
# get server tiller version
SERVER_TILLER_VERSION=$(exec $HELM_LATEST $NS version --short | grep Server | sed 's/.*v\(.*\)+.*/\1/')
if [ -n "$SERVER_TILLER_VERSION" ]; then
(>&2 echo "$NS_DEBUG with helm v$SERVER_TILLER_VERSION")
HELM_CURRENT="/usr/local/bin/helm-v$SERVER_TILLER_VERSION"
if [ -f "$HELM_CURRENT" ]; then
exec $HELM_CURRENT $NS $SAVED_PARAMS
else
# determine whether HELM_LATEST is newer than SERVER_TILLER_VERSION
newer_found=""
HELM_LATEST_VERSION=`echo $HELM_LATEST | sed 's/.*v//'`
local_order=`printf "$HELM_LATEST_VERSION\n$SERVER_TILLER_VERSION" | sort -V | grep -n $HELM_LATEST_VERSION | sed 's/:.*//'`
tiller_order=`printf "$HELM_LATEST_VERSION\n$SERVER_TILLER_VERSION" | sort -V | grep -n $SERVER_TILLER_VERSION | sed 's/:.*//'`
local_major=`echo $HELM_LATEST_VERSION | sed 's/\...*//'`
tiller_major=`echo $SERVER_TILLER_VERSION | sed 's/\...*//'`
if [[ $local_order && $tiller_order && $local_major && $tiller_major ]] ; then
if [[ $local_major == $tiller_major && $local_order > $tiller_order && -f "$HELM_LATEST" ]] ; then
(>&2 echo "use helm $HELM_LATEST since it is a newer version than tiller $SERVER_TILLER_VERSION")
newer_found=true
exec $HELM_LATEST $NS $SAVED_PARAMS
fi
fi
if [ "$newer_found" != "true" ] ; then
(>&2 echo "helm-wrapper: error, helm client version $SERVER_TILLER_VERSION (or newer) is not installed.")
exit 1
fi
fi
else
(>&2 echo "helm-wrapper: error, unable to get tiller version in $NS")
fi
else
# execute command using latest helm
# output namespace info for "helm init"
if isParamSpecified "init" $@; then
(>&2 echo "$NS_DEBUG with helm $HELM_LATEST")
fi
exec $HELM_LATEST $NS $SAVED_PARAMS
fi