-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.sh
executable file
·175 lines (147 loc) · 7.79 KB
/
entrypoint.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
SUPPORTED_MODES=(ENV_VAR IMAGE_TAG HELM_VALUES)
MODE=$1
CONTAINER_NAME=$2
FILES=$3
NEW_IMAGE_TAG=$4
ENV_NAME=$5
NEW_ENV_VALUE=$6
HELM_IMAGE_KEY=".image.tag"
HELM_CRONJOB_IMAGE_KEY=".cronJobs.*.image.tag"
if [[ ! " ${SUPPORTED_MODES[@]} " =~ " ${MODE} " ]]; then
echo " +++++++++ ERROR MODE \"${MODE}\" is not part of the supported values [ ${SUPPORTED_MODES[@]} ] " >&2
exit 1
fi
IFS=","
for FILEPATH in $FILES; do
if test -f "${FILEPATH}"; then
echo " +++ + Updating file ${FILEPATH}"
else
echo " +++++++++ ERROR file \"${FILEPATH}\" does not exist" >&2
exit 1
fi
if [[ ${MODE} == "IMAGE_TAG" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet CronJob Kustomization)
if [ -z "${NEW_IMAGE_TAG}" ]; then
echo " +++++++++ ERROR NEW_IMAGE_TAG \"${NEW_IMAGE_TAG}\" is not correct " >&2
exit 1
fi
objectKind=$(yq r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "
if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi
if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )) ; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2
exit 1
fi
echo " +++ + Container Index $containerIndex"
currentImageValue=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2
exit 1
fi
echo " +++ + + Processing image from $currentImageValue"
imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi
if [[ ${objectKind} == "CronJob" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file CronJob ${FILEPATH}" >&2
exit 1
fi
echo " +++ + Container Index in CronJob $containerIndex"
currentImageValue=$(yq r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2
exit 1
fi
echo " +++ + + Processing image from $currentImageValue"
imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi
if [[ ${objectKind} == "Kustomization" ]] ; then
kustomizeBuildPath="${FILEPATH%/*}"
echo " +++ + Building kustomize in directory ${kustomizeBuildPath}"
fullKustomizeBuild=$(kustomize build ${kustomizeBuildPath})
delimiter="---"
s=$fullKustomizeBuild$delimiter
kustomizeImageNameToUpdate=""
while [[ $s ]]; do
object="${s%%"$delimiter"*}"
containerPosition=$(echo "$object" | yq r - spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
if [[ $containerPosition ]]; then
containerIndex=$((${containerPosition/M/}-1))
currentImageValue=$(echo "$object" | yq r - spec.template.spec.containers[${containerIndex}].image)
if [[ ! $currentImageValue ]]; then
currentImageValue=$(echo "$object" | yq r - spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)
fi
imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
kustomizeImageNameToUpdate=${imageFullName}
fi
s=${s#*"$delimiter"};
done;
if [[ ! $kustomizeImageNameToUpdate ]]; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in any file build by kustomize from folder ${kustomizeBuildPath}" >&2
exit 1
fi
kustomizeImageNamePosition=$(yq r ${FILEPATH} images.*.name | grep -n ${kustomizeImageNameToUpdate} | cut -d: -f1)
kustomizeContainerIndex=$((${kustomizeImageNamePosition/M/}-1))
kustomizeCurrentNewTagValue=$(yq r ${FILEPATH} images[${kustomizeContainerIndex}].newTag)
echo " +++ + + Processing newTag for image name: $kustomizeImageNameToUpdate"
echo " +++ + + + from newTag: ${kustomizeCurrentNewTagValue}"
echo " +++ + + + to newTag: ${NEW_IMAGE_TAG}"
sed -i "s+${kustomizeCurrentNewTagValue}+${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi
fi
if [[ ${MODE} == "ENV_VAR" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet)
objectKind=$(yq r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "
if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi
if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2
exit 1
fi
echo " +++ + Container Index $containerIndex"
envPosition=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[*].name | grep -n ${ENV_NAME}$ | cut -d: -f1)
envIndex=$((${envPosition/M/}-1))
if (( ${envIndex} < 0 )); then
echo " +++++++++ ERROR Environment variable with name ${ENV_NAME} not found in ${CONTAINER_NAME}" >&2
exit 1
fi
currentEnvValue=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[${envIndex}].value)
echo " +++ + + Updating ${ENV_NAME} in container ${CONTAINER_NAME} from ${currentEnvValue}"
echo " +++ + + To env ${ENV_NAME} in container ${CONTAINER_NAME} to ${NEW_ENV_VALUE}"
sanitizedOldString=$(echo $currentEnvValue | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sanitizedNewString=$(echo $NEW_ENV_VALUE | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sed -i "s+${sanitizedOldString}+${sanitizedNewString}+g" ${FILEPATH}
fi
fi;
if [[ ${MODE} == "HELM_VALUES" ]]; then
if [[ $(yq4 'has("cronJobs")' "${FILEPATH}" 2>/dev/null) == "true" ]]; then
yq4 "${HELM_CRONJOB_IMAGE_KEY} = \"${NEW_IMAGE_TAG}\"" -i ${FILEPATH}
fi
if [[ $(yq4 'has("image")' "${FILEPATH}" 2>/dev/null) == "true" ]]; then
yq4 "${HELM_IMAGE_KEY} = \"${NEW_IMAGE_TAG}\"" -i ${FILEPATH}
fi
echo "+++ + + Updated ${HELM_IMAGE_KEY} key in ${FILEPATH} to ${NEW_IMAGE_TAG}"
fi
done