-
Notifications
You must be signed in to change notification settings - Fork 1
/
argocd-helmfile
executable file
·114 lines (90 loc) · 3.23 KB
/
argocd-helmfile
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
#!/bin/bash
## standard build environment
## https://argoproj.github.io/argo-cd/user-guide/build-environment/
# ARGOCD_APP_NAME - name of application
# ARGOCD_APP_NAMESPACE - destination application namespace.
# ARGOCD_APP_REVISION - the resolved revision, e.g. f913b6cbf58aa5ae5ca1f8a2b149477aebcbd9d8
# ARGOCD_APP_SOURCE_PATH - the path of the app within the repo
# ARGOCD_APP_SOURCE_REPO_URL the repo's URL
# ARGOCD_APP_SOURCE_TARGET_REVISION - the target revision from the spec, e.g. master.
# KUBE_VERSION="<major>.<minor>"
# KUBE_API_VERSIONS="v1,apps/v1,..."
set -e
if [ "${ARGOCD_CMP_HELMFILE_DEBUG}" == "true" ] ; then
set -x
fi
cleanup() {
if [[ "${ARGOCD_ENV_ADDITIONAL_VALUES}" ]] ; then
rm -r $tmpfile
fi
}
trap "cleanup" ERR
echoerr() { printf "%s\n" "$*" >&2; }
# exit immediately if no phase is passed in
if [[ -z "${1}" ]]; then
echoerr "invalid invocation"
exit 1
fi
SCRIPT_NAME=$(basename "${0}")
phase=$1
# setup the env
helm="$(which helm)"
helmfile="$(which helmfile)"
helmfile="${helmfile} --helm-binary ${helm} --no-color --allow-no-matching-release"
if [[ "${ARGOCD_APP_NAMESPACE}" ]]; then
helmfile="${helmfile} --namespace ${ARGOCD_APP_NAMESPACE}"
fi
if [[ "${HELMFILE_GLOBAL_OPTIONS}" ]]; then
helmfile="${helmfile} ${HELMFILE_GLOBAL_OPTIONS}"
fi
# these should work for both v2 and v3
helm_full_version=$(${helm} version --short --client | cut -d " " -f2)
helm_major_version=$(echo "${helm_full_version%+*}" | cut -d "." -f1 | sed 's/[^0-9]//g')
helm_minor_version=$(echo "${helm_full_version%+*}" | cut -d "." -f2 | sed 's/[^0-9]//g')
# fix scenarios where KUBE_VERSION is improperly set with trailing +
# https://github.com/argoproj/argo-cd/issues/8249
KUBE_VERSION=$(echo "${KUBE_VERSION}" | sed 's/[^0-9\.]*//g')
echoerr "$(${helm} version --short --client)"
echoerr "$(${helmfile} --version)"
case $phase in
"init")
echoerr "starting init"
if [[ ! -d "./charts" ]]; then
echoerr "no charts directory found, doing helmfile fetch"
${helmfile} fetch
fi
;;
"generate")
echoerr "starting generate"
INTERNAL_HELM_TEMPLATE_OPTIONS=
HELM_VALUES_FILE=
# support added for --kube-version in 3.6
# https://github.com/helm/helm/pull/9040
if [[ ${helm_major_version} -eq 3 && ${helm_minor_version} -ge 6 && "${KUBE_VERSION}" ]]; then
INTERNAL_HELM_TEMPLATE_OPTIONS="${INTERNAL_HELM_TEMPLATE_OPTIONS} --kube-version=${KUBE_VERSION}"
fi
if [[ ${helm_major_version} -eq 3 && "${KUBE_API_VERSIONS}" ]]; then
INTERNAL_HELM_API_VERSIONS=""
for v in ${KUBE_API_VERSIONS//,/ }; do
INTERNAL_HELM_API_VERSIONS="${INTERNAL_HELM_API_VERSIONS} --api-versions=$v"
done
INTERNAL_HELM_TEMPLATE_OPTIONS="${INTERNAL_HELM_TEMPLATE_OPTIONS} ${INTERNAL_HELM_API_VERSIONS}"
fi
if [[ "${ARGOCD_ENV_ADDITIONAL_VALUES}" ]] ; then
echoerr "Detected additional values"
tmpfile=$(mktemp /tmp/helmfile-additional-values-XXXXXX)
echo "${ARGOCD_ENV_ADDITIONAL_VALUES}" > $tmpfile
HELM_VALUES_FILE="--values ${tmpfile}"
fi
${helmfile} \
template \
--skip-deps \
--args "${INTERNAL_HELM_TEMPLATE_OPTIONS}" \
${HELM_VALUES_FILE}
cleanup
;;
*)
echoerr "invalid invocation"
exit 1
;;
esac