Skip to content

Commit 5db322a

Browse files
Merge pull request #357 from bharath-b-rh/main
CM-556: Verify bundle image before updating fbc
2 parents 3fb2129 + 53567b2 commit 5db322a

File tree

1 file changed

+79
-34
lines changed

1 file changed

+79
-34
lines changed

hack/update_catalog.sh

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,70 @@ declare REPLICATE_BUNDLE_FILE_IN_CATALOGS
1818
declare USE_MIGRATE_LEVEL_FLAG
1919

2020
CERT_MANAGER_CATALOG_NAME="openshift-cert-manager-operator"
21+
GREEN_COLOR_TEXT='\033[0;32m'
22+
RED_COLOR_TEXT='\033[0;31m'
23+
REVERT_COLOR_TEXT='\033[0m'
24+
25+
log_info()
26+
{
27+
echo -e "[$(date)] ${GREEN_COLOR_TEXT}-- INFO --${REVERT_COLOR_TEXT} ${1}"
28+
}
29+
30+
log_error()
31+
{
32+
echo -e "[$(date)] ${RED_COLOR_TEXT}-- ERROR --${REVERT_COLOR_TEXT} ${1}"
33+
}
34+
35+
verify_bundle_image()
36+
{
37+
auth_file=""
38+
if [[ -n ${REGISTRY_AUTH_FILE} ]]; then
39+
auth_file=${REGISTRY_AUTH_FILE}
40+
elif [[ -f ${XDG_RUNTIME_DIR}/containers/auth.json ]]; then
41+
auth_file=${XDG_RUNTIME_DIR}/containers/auth.json
42+
elif [[ -f ${HOME}/.docker/config.json ]]; then
43+
auth_file=${HOME}/.docker/config.json
44+
else
45+
log_error "registry auth config lookup failed, expected REGISTRY_AUTH_FILE env var to be set, \
46+
or config to be present in podman/docker recognised path"
47+
exit 1
48+
fi
49+
50+
log_info "inspecting ${OPERATOR_BUNDLE_IMAGE} bundle image"
51+
media_type="$(podman run -e REGISTRY_AUTH_FILE="/tmp/auth.json" --rm -v "${auth_file}:/tmp/auth.json" \
52+
quay.io/skopeo/stable:latest inspect --raw docker://"${OPERATOR_BUNDLE_IMAGE}" | jq -r .mediaType)"
53+
54+
case $media_type in
55+
application/vnd.oci.image.manifest.v1+json|application/vnd.docker.distribution.manifest.v2+json)
56+
;;
57+
*)
58+
log_error "bundle image not having expected media type, possibly index image was created"
59+
exit 1
60+
esac
61+
62+
return
63+
}
2164

2265
render_catalog_bundle()
2366
{
24-
render_cmd_args=""
25-
# --migrate-level=bundle-object-to-csv-metadata is used for creating bundle metadata in `olm.csv.metadata` format.
26-
# Refer https://github.com/konflux-ci/build-definitions/blob/main/task/fbc-validation/0.1/TROUBLESHOOTING.md for details.
27-
if [[ ${USE_MIGRATE_LEVEL_FLAG} == "yes" ]]; then
28-
render_cmd_args="--migrate-level=bundle-object-to-csv-metadata"
29-
fi
30-
31-
bundle_file="${CATALOG_DIR}/${CERT_MANAGER_CATALOG_NAME}/${BUNDLE_FILE_NAME}"
32-
echo "[$(date)] -- INFO -- generating catalog bundle \"${bundle_file}\""
33-
if ! "${OPM_TOOL_PATH}" render "${OPERATOR_BUNDLE_IMAGE}" $render_cmd_args -o yaml > "${bundle_file}"; then
34-
echo "[$(date)] -- ERROR -- failed to render catalog bundle"
35-
exit 1
36-
fi
37-
38-
if ! "${OPM_TOOL_PATH}" validate "${CATALOG_DIR}"; then
39-
echo "[$(date)] -- ERROR -- failed to validate catalog"
40-
exit 1
41-
fi
67+
render_cmd_args=""
68+
# --migrate-level=bundle-object-to-csv-metadata is used for creating bundle metadata in `olm.csv.metadata` format.
69+
# Refer https://github.com/konflux-ci/build-definitions/blob/main/task/fbc-validation/0.1/TROUBLESHOOTING.md for details.
70+
if [[ ${USE_MIGRATE_LEVEL_FLAG} == "yes" ]]; then
71+
render_cmd_args="--migrate-level=bundle-object-to-csv-metadata"
72+
fi
73+
74+
bundle_file="${CATALOG_DIR}/${CERT_MANAGER_CATALOG_NAME}/${BUNDLE_FILE_NAME}"
75+
log_info "generating catalog bundle \"${bundle_file}\""
76+
if ! "${OPM_TOOL_PATH}" render "${OPERATOR_BUNDLE_IMAGE}" $render_cmd_args -o yaml > "${bundle_file}"; then
77+
log_error "failed to render catalog bundle"
78+
exit 1
79+
fi
80+
81+
if ! "${OPM_TOOL_PATH}" validate "${CATALOG_DIR}"; then
82+
log_error "failed to validate catalog"
83+
exit 1
84+
fi
4285
}
4386

4487
usage()
@@ -55,21 +98,21 @@ usage()
5598

5699
replicate_catalog_bundle()
57100
{
58-
if [[ "${REPLICATE_BUNDLE_FILE_IN_CATALOGS}" == "no" ]]; then
59-
return
60-
fi
101+
if [[ "${REPLICATE_BUNDLE_FILE_IN_CATALOGS}" == "no" ]]; then
102+
return
103+
fi
61104

62-
bundle_file="${CATALOG_DIR}/${CERT_MANAGER_CATALOG_NAME}/${BUNDLE_FILE_NAME}"
105+
bundle_file="${CATALOG_DIR}/${CERT_MANAGER_CATALOG_NAME}/${BUNDLE_FILE_NAME}"
63106

64-
find catalogs/*/catalog/openshift-cert-manager-operator -type d ! -path "${CATALOG_DIR}/*" -exec /bin/cp "${bundle_file}" {} \; -print
107+
find catalogs/*/catalog/openshift-cert-manager-operator -type d ! -path "${CATALOG_DIR}/*" -exec /bin/cp "${bundle_file}" {} \; -print
65108
}
66109

67110
##############################################
68111
############### MAIN #######################
69112
##############################################
70113

71114
if [[ $# -ne 6 ]]; then
72-
usage
115+
usage
73116
fi
74117

75118
OPM_TOOL_PATH=$1
@@ -79,33 +122,35 @@ BUNDLE_FILE_NAME=$4
79122
REPLICATE_BUNDLE_FILE_IN_CATALOGS=$5
80123
USE_MIGRATE_LEVEL_FLAG=$6
81124

82-
echo "[$(date)] -- INFO -- $*"
125+
log_info "$*"
83126

84127
if [[ ! -d "${CATALOG_DIR}" ]]; then
85-
echo "[$(date)] -- ERROR -- catalog directory \"${CATALOG_DIR}\" does not exist"
128+
log_error "catalog directory \"${CATALOG_DIR}\" does not exist"
86129
exit 1
87130
fi
88131

89132
if [[ ! -x "${OPM_TOOL_PATH}" ]]; then
90-
echo "[$(date)] -- ERROR -- \"${OPM_TOOL_PATH}\" does not exist or does not execute permissions"
91-
exit 1
133+
log_error "\"${OPM_TOOL_PATH}\" does not exist or does not execute permissions"
134+
exit 1
92135
fi
93136

94137
if [[ -z "${BUNDLE_FILE_NAME}" ]]; then
95-
echo "[$(date)] -- ERROR -- \"\" bundle file name cannot be empty"
96-
exit 1
138+
log_error "bundle file name cannot be empty"
139+
exit 1
97140
fi
98141

99142
if [[ -z "${REPLICATE_BUNDLE_FILE_IN_CATALOGS}" ]] || [[ "${REPLICATE_BUNDLE_FILE_IN_CATALOGS}" != @(yes|no) ]]; then
100-
echo "[$(date)] -- ERROR -- invalid value provided for \"REPLICATE_BUNDLE_FILE_IN_CATALOGS\", must be \"yes\" or \"no\""
101-
exit 1
143+
log_error "invalid value provided for \"REPLICATE_BUNDLE_FILE_IN_CATALOGS\", must be \"yes\" or \"no\""
144+
exit 1
102145
fi
103146

104147
if [[ -z "${USE_MIGRATE_LEVEL_FLAG}" ]] || [[ "${USE_MIGRATE_LEVEL_FLAG}" != @(yes|no) ]]; then
105-
echo "[$(date)] -- ERROR -- invalid value provided for \"USE_MIGRATE_LEVEL_FLAG\", must be \"yes\" or \"no\""
106-
exit 1
148+
log_error "invalid value provided for \"USE_MIGRATE_LEVEL_FLAG\", must be \"yes\" or \"no\""
149+
exit 1
107150
fi
108151

152+
verify_bundle_image
153+
109154
render_catalog_bundle
110155

111156
replicate_catalog_bundle

0 commit comments

Comments
 (0)