1
+ #! /usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+
5
+ REPO_ROOT=$( git rev-parse --show-toplevel)
6
+ SCRIPT_BUNDLE_CONTENTS=" $REPO_ROOT /hack/generate-operator-bundle-contents.py"
7
+ BASE_FOLDER=" "
8
+ DIR_BUNDLE=" "
9
+ DIR_EXEC=" "
10
+ DIR_MANIFESTS=" "
11
+
12
+ GOOS=$( go env GOOS)
13
+ OPM_VERSION=" v1.23.2"
14
+ COMMAND_OPM=" "
15
+ GRPCURL_VERSION=" 1.7.0"
16
+ COMMAND_GRPCURL=" "
17
+
18
+ export REGISTRY_AUTH_FILE=${CONTAINER_ENGINE_CONFIG_DIR} /config.json
19
+
20
+ OLM_BUNDLE_VERSIONS_REPO=" gitlab.cee.redhat.com/service/saas-operator-versions.git"
21
+ OLM_BUNDLE_VERSIONS_REPO_FOLDER=" versions_repo"
22
+ VERSIONS_FILE=" deployment-validation-operator/deployment-validation-operator-versions.txt"
23
+ PREV_VERSION=" "
24
+
25
+ OLM_BUNDLE_IMAGE_VERSION=" ${OLM_BUNDLE_IMAGE} :g${CURRENT_COMMIT} "
26
+ OLM_BUNDLE_IMAGE_LATEST=" ${OLM_BUNDLE_IMAGE} :latest"
27
+
28
+ OLM_CATALOG_IMAGE_VERSION=" ${OLM_CATALOG_IMAGE} :${CURRENT_COMMIT} "
29
+ OLM_CATALOG_IMAGE_LATEST=" ${OLM_CATALOG_IMAGE} :latest"
30
+
31
+ function log() {
32
+ echo " $( date " +%Y-%m-%d %H:%M:%S" ) -- ${1} "
33
+ }
34
+
35
+ function precheck_required_files() {
36
+ if [[ ! -x " $SCRIPT_BUNDLE_CONTENTS " ]]; then
37
+ log " The script $SCRIPT_BUNDLE_CONTENTS cannot be run. Exiting."
38
+ return 1
39
+ fi
40
+ return 0
41
+ }
42
+
43
+ function prepare_temporary_folders() {
44
+ BASE_FOLDER=$( mktemp -d --suffix " -$( basename " $0 " ) " )
45
+ DIR_BUNDLE=$( mktemp -d -p " $BASE_FOLDER " bundle.XXXX)
46
+ DIR_MANIFESTS=$( mktemp -d -p " $DIR_BUNDLE " manifests.XXXX)
47
+ DIR_EXEC=$( mktemp -d -p " $BASE_FOLDER " bin.XXXX)
48
+ }
49
+
50
+ function download_dependencies() {
51
+ cd " $DIR_EXEC "
52
+
53
+ local opm_url=" https://github.com/operator-framework/operator-registry/releases/download/$OPM_VERSION /$GOOS -amd64-opm"
54
+ curl -sfL " ${opm_url} " -o opm
55
+ chmod +x opm
56
+ COMMAND_OPM=" $DIR_EXEC /opm"
57
+
58
+ local grpcurl_url=" https://github.com/fullstorydev/grpcurl/releases/download/v$GRPCURL_VERSION /grpcurl_${GRPCURL_VERSION} _${GOOS} _x86_64.tar.gz"
59
+ curl -sfL " $grpcurl_url " | tar -xz -O grpcurl > " grpcurl"
60
+ chmod +x grpcurl
61
+ COMMAND_GRPCURL=" $DIR_EXEC /grpcurl"
62
+
63
+ cd ~ -
64
+ }
65
+
66
+
67
+ function clone_versions_repo() {
68
+ local folder=" $BASE_FOLDER /$OLM_BUNDLE_VERSIONS_REPO_FOLDER "
69
+ log " path: $folder "
70
+
71
+ if [[ -n " ${APP_SRE_BOT_PUSH_TOKEN:- } " ]]; then
72
+ log " Using APP_SRE_BOT_PUSH_TOKEN credentials to authenticate"
73
+ git clone " https://app:${APP_SRE_BOT_PUSH_TOKEN} @$OLM_BUNDLE_VERSIONS_REPO " " $folder " --quiet
74
+ else
75
+ git clone " https://$OLM_BUNDLE_VERSIONS_REPO " " $folder " --quiet
76
+ fi
77
+ }
78
+
79
+ function set_previous_operator_version() {
80
+ local filename=" $BASE_FOLDER /$OLM_BUNDLE_VERSIONS_REPO_FOLDER /$VERSIONS_FILE "
81
+
82
+ if [[ ! -a " $filename " ]]; then
83
+ log " No file $VERSIONS_FILE exist. Exiting."
84
+ exit 1
85
+ fi
86
+ PREV_VERSION=$( tail -n 1 " $filename " | awk ' {print $1}' )
87
+ }
88
+
89
+ function setup_environment() {
90
+ log " Generating temporary folders to contain artifacts"
91
+ prepare_temporary_folders
92
+ log " base path: $BASE_FOLDER "
93
+
94
+ log " Downloading needed commands: opm and grpcurl"
95
+ download_dependencies
96
+ log " path: $DIR_EXEC "
97
+
98
+ log " Cloning $OLM_BUNDLE_VERSIONS_REPO "
99
+ clone_versions_repo
100
+
101
+ log " Determining previous operator version checking $VERSIONS_FILE file"
102
+ set_previous_operator_version
103
+ log " previous version: $PREV_VERSION "
104
+ }
105
+
106
+ function build_opm_bundle() {
107
+ # set venv with needed dependencies
108
+ python3 -m venv .venv; source .venv/bin/activate; pip install pyyaml
109
+
110
+ log " Generating patched bundle contents"
111
+ $SCRIPT_BUNDLE_CONTENTS --name " $OPERATOR_NAME " \
112
+ --current-version " $OPERATOR_VERSION " \
113
+ --image " $OPERATOR_IMAGE " \
114
+ --image-tag " $OPERATOR_IMAGE_TAG " \
115
+ --output-dir " $DIR_MANIFESTS " \
116
+ --replaces " $PREV_VERSION "
117
+
118
+ log " Creating bundle image $OLM_BUNDLE_IMAGE_VERSION "
119
+ cd " $DIR_BUNDLE "
120
+ ${COMMAND_OPM} alpha bundle build --directory " $DIR_MANIFESTS " \
121
+ --channels " $OLM_CHANNEL " \
122
+ --default " $OLM_CHANNEL " \
123
+ --package " $OPERATOR_NAME " \
124
+ --tag " $OLM_BUNDLE_IMAGE_VERSION " \
125
+ --image-builder " $( basename " $CONTAINER_ENGINE " | awk ' {print $1}' ) " \
126
+ --overwrite \
127
+ 1>&2
128
+ cd ~ -
129
+ }
130
+
131
+ function validate_opm_bundle() {
132
+ log " Pushing bundle image $OLM_BUNDLE_IMAGE_VERSION "
133
+ $CONTAINER_ENGINE push " $OLM_BUNDLE_IMAGE_VERSION "
134
+
135
+ log " Validating bundle $OLM_BUNDLE_IMAGE_VERSION "
136
+ ${COMMAND_OPM} alpha bundle validate --tag " $OLM_BUNDLE_IMAGE_VERSION " \
137
+ --image-builder " $( basename " $CONTAINER_ENGINE " | awk ' {print $1}' ) "
138
+ }
139
+
140
+ function build_opm_catalog() {
141
+ local FROM_INDEX=" "
142
+ local PREV_COMMIT=${PREV_VERSION#* g} # remove versioning and the g commit hash prefix
143
+ # check if the previous catalog image is available
144
+ if [ " $( ${CONTAINER_ENGINE} pull " ${OLM_CATALOG_IMAGE} " :" ${PREV_COMMIT} " & > /dev/null; echo $? ) " -eq 0 ]; then
145
+ FROM_INDEX=" --from-index ${OLM_CATALOG_IMAGE} :${PREV_COMMIT} "
146
+ log " Index argument is $FROM_INDEX "
147
+ fi
148
+
149
+ log " Creating catalog image $OLM_CATALOG_IMAGE_VERSION using opm"
150
+
151
+ ${COMMAND_OPM} index add --bundles " $OLM_BUNDLE_IMAGE_VERSION " \
152
+ --tag " $OLM_CATALOG_IMAGE_VERSION " \
153
+ --build-tool " $( basename " $CONTAINER_ENGINE " | awk ' {print $1}' ) " \
154
+ $FROM_INDEX
155
+ }
156
+
157
+ function validate_opm_catalog() {
158
+ log " Checking that catalog we have built returns the correct version $OPERATOR_VERSION "
159
+
160
+ local free_port=" "
161
+ local container_id=" "
162
+ local catalog_current_version=" "
163
+
164
+ free_port=$( python3 -c ' import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()' )
165
+
166
+ log " Running $OLM_CATALOG_IMAGE_VERSION and exposing $free_port "
167
+ container_id=$( ${CONTAINER_ENGINE} run -d -p " $free_port :50051" " $OLM_CATALOG_IMAGE_VERSION " )
168
+
169
+ log " Getting current version from running catalog"
170
+ catalog_current_version=$(
171
+ ${COMMAND_GRPCURL} -plaintext -d ' {"name": "' " $OPERATOR_NAME " ' "}' \
172
+ " localhost:$free_port " api.Registry/GetPackage | \
173
+ jq -r ' .channels[] | select(.name=="' " $OLM_CHANNEL " ' ") | .csvName' | \
174
+ sed " s/$OPERATOR_NAME \.//"
175
+ )
176
+ log " catalog version: $catalog_current_version "
177
+
178
+ log " Removing docker container $container_id "
179
+ ${CONTAINER_ENGINE} rm -f " $container_id "
180
+
181
+ if [[ " $catalog_current_version " != " v$OPERATOR_VERSION " ]]; then
182
+ log " Version from catalog $catalog_current_version != v$OPERATOR_VERSION "
183
+ return 1
184
+ fi
185
+ }
186
+
187
+ function update_versions_repo() {
188
+ log " Adding the current version $OPERATOR_VERSION to the bundle versions file in $OLM_BUNDLE_VERSIONS_REPO "
189
+ local folder=" $BASE_FOLDER /$OLM_BUNDLE_VERSIONS_REPO_FOLDER "
190
+
191
+ cd " $folder "
192
+
193
+ echo " $OPERATOR_VERSION " >> " $VERSIONS_FILE "
194
+ git add .
195
+ message=" add version $OPERATOR_VERSION
196
+
197
+ replaces $PREV_VERSION "
198
+ git commit -m " $message "
199
+
200
+ log " Pushing the repository changes to $OLM_BUNDLE_VERSIONS_REPO into master branch"
201
+ git push origin master
202
+ cd ~ -
203
+ }
204
+
205
+ function tag_and_push_images() {
206
+ log " Tagging bundle image $OLM_BUNDLE_IMAGE_VERSION as $OLM_BUNDLE_IMAGE_LATEST "
207
+ ${CONTAINER_ENGINE} tag " $OLM_BUNDLE_IMAGE_VERSION " " $OLM_BUNDLE_IMAGE_LATEST "
208
+
209
+ log " Tagging catalog image $OLM_CATALOG_IMAGE_VERSION as $OLM_CATALOG_IMAGE_LATEST "
210
+ ${CONTAINER_ENGINE} tag " $OLM_CATALOG_IMAGE_VERSION " " $OLM_CATALOG_IMAGE_LATEST "
211
+
212
+ log " Pushing catalog image $OLM_CATALOG_IMAGE_VERSION "
213
+ ${CONTAINER_ENGINE} push " $OLM_CATALOG_IMAGE_VERSION "
214
+
215
+ log " Pushing bundle image $OLM_CATALOG_IMAGE_LATEST "
216
+ ${CONTAINER_ENGINE} push " $OLM_CATALOG_IMAGE_LATEST "
217
+
218
+ log " Pushing bundle image $OLM_BUNDLE_IMAGE_LATEST "
219
+ ${CONTAINER_ENGINE} push " $OLM_BUNDLE_IMAGE_LATEST "
220
+ }
221
+
222
+ function main() {
223
+ log " Building $OPERATOR_NAME version $OPERATOR_VERSION "
224
+
225
+ precheck_required_files || return 1
226
+
227
+ setup_environment
228
+
229
+ build_opm_bundle
230
+ validate_opm_bundle
231
+
232
+ build_opm_catalog
233
+ validate_opm_catalog
234
+
235
+ if [[ -n " ${APP_SRE_BOT_PUSH_TOKEN:- } " ]]; then
236
+ update_versions_repo
237
+ else
238
+ log " APP_SRE_BOT_PUSH_TOKEN credentials were not found"
239
+ log " it will be necessary to manually update $OLM_BUNDLE_VERSIONS_REPO repo"
240
+ fi
241
+ tag_and_push_images
242
+ }
243
+
244
+ if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
245
+ main
246
+ fi
0 commit comments