Skip to content

Commit 765a802

Browse files
Add revision alignment check for images in CSV (#3541)
Co-authored-by: Christoph Stäbler <[email protected]>
1 parent bb6f4fc commit 765a802

File tree

3 files changed

+109
-18
lines changed

3 files changed

+109
-18
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ generated-files-release-next: release-files
383383
(cd olm-catalog/serverless-operator && USE_RELEASE_NEXT=true ./hack/update-manifests.sh)
384384
./hack/update-deps.sh
385385

386+
verify-csv-revisions: install-tools
387+
./hack/verify-csv-revisions.sh
388+
386389
# Runs the lints Github Actions do too.
387390
lint:
388391
woke

hack/lib/images.bash

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -296,24 +296,8 @@ function latest_registry_redhat_io_image_sha() {
296296
exit 1
297297
fi
298298

299-
digest="${image##*@}" # Get only sha
300-
301-
image_name=${image_without_tag##*/} # Get image name after last slash
302-
303-
# Add rhel suffix
304-
if [ "${image_name}" == "serverless-openshift-kn-operator" ]; then
305-
# serverless-openshift-kn-operator is special, as it has rhel in the middle of the name
306-
# see https://redhat-internal.slack.com/archives/CKR568L8G/p1729684088850349
307-
image_name="serverless-openshift-kn-rhel$(get_serverless_operator_rhel_version)-operator"
308-
elif [ "${image_name}" == "serverless-bundle" ]; then
309-
# serverless-bundle is special, as it has no rhelXYZ in the name
310-
image_name="serverless-bundle"
311-
else
312-
# for other images simply add it as a suffix
313-
image_name="${image_name}-rhel$(get_serverless_operator_rhel_version)"
314-
fi
315-
316-
echo "${registry_redhat_io}/${image_name}@${digest}"
299+
rh_registry_image="$(get_rh_registry_image_ref "$image")"
300+
echo "$rh_registry_image"
317301
}
318302

319303
function latest_konflux_image_sha() {
@@ -355,3 +339,62 @@ function get_app_version_from_tag() {
355339
app_version=${app_version/./} # -> 134
356340
echo "${app_version}"
357341
}
342+
343+
# returns the quay image for a given rh registry image ref
344+
function get_quay_image_ref() {
345+
local rh_registry_image_ref
346+
rh_registry_image_ref="${1}"
347+
348+
if [[ $rh_registry_image_ref =~ $registry_redhat_io ]]; then
349+
image=${rh_registry_image_ref##*/} # Get image name after last slash
350+
image_sha=${image##*@} # Get SHA of image
351+
image_name=${image%@*} # Remove sha
352+
353+
if [[ "${image_name}" =~ ^serverless-openshift-kn-rhel[0-9]+-operator$ ]]; then
354+
# serverless-openshift-kn-operator is special, as it has rhel in the middle of the name
355+
# see https://redhat-internal.slack.com/archives/CKR568L8G/p1729684088850349
356+
component="serverless-openshift-kn-operator"
357+
elif [[ "${image_name}" == "serverless-operator-bundle" ]]; then
358+
# serverless-operator-bundle is special, as it is named only serverless-bundle in quay
359+
component="serverless-bundle"
360+
else
361+
# for other images simply remove the -rhelXYZ suffix
362+
component=${image_name%-rhel*}
363+
fi
364+
365+
echo "${registry_quay}/${component}@${image_sha}"
366+
else
367+
echo "Image must be from ${registry_redhat_io}, got ${rh_registry_image_ref}"
368+
return 1
369+
fi
370+
}
371+
372+
# returns the RH registry image for a given quay image ref
373+
function get_rh_registry_image_ref() {
374+
local quay_registry_image_ref
375+
quay_registry_image_ref="${1}"
376+
377+
if [[ $quay_registry_image_ref =~ $registry_quay ]]; then
378+
image=${quay_registry_image_ref##*/} # Get image name after last slash
379+
image_sha=${image##*@} # Get SHA of image
380+
image_name=${image%@*} # Remove sha
381+
382+
# Add rhel suffix
383+
if [ "${image_name}" == "serverless-openshift-kn-operator" ]; then
384+
# serverless-openshift-kn-operator is special, as it has rhel in the middle of the name
385+
# see https://redhat-internal.slack.com/archives/CKR568L8G/p1729684088850349
386+
image_name="serverless-openshift-kn-rhel$(get_serverless_operator_rhel_version)-operator"
387+
elif [ "${image_name}" == "serverless-bundle" ]; then
388+
# serverless-bundle is special, as it has no rhelXYZ in the name
389+
image_name="serverless-bundle"
390+
else
391+
# for other images simply add it as a suffix
392+
image_name="${image_name}-rhel$(get_serverless_operator_rhel_version)"
393+
fi
394+
395+
echo "${registry_redhat_io}/${image_name}@${image_sha}"
396+
else
397+
echo "Image must be from ${registry_quay}, got ${quay_registry_image_ref}"
398+
return 1
399+
fi
400+
}

hack/verify-csv-revisions.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
source "$(dirname "${BASH_SOURCE[0]}")/lib/__sources__.bash"
4+
5+
# verify that the revisions (git commit) for components from the same repo match
6+
function verify_image_revisions {
7+
local root_dir csv_file repo_revision rc
8+
root_dir="$(dirname "$(dirname "$(realpath "${BASH_SOURCE[0]}")")")"
9+
csv_file="${root_dir}/olm-catalog/serverless-operator/manifests/serverless-operator.clusterserviceversion.yaml"
10+
declare -A repo_revision=()
11+
rc=0
12+
13+
while IFS= read -r image_ref; do
14+
15+
if [[ $image_ref =~ $registry_redhat_io ]]; then
16+
quay_image_ref="$(get_quay_image_ref "$image_ref")"
17+
parameters="$(cosign download attestation "${quay_image_ref}" | jq -r '.payload' | base64 -d | jq -c '.predicate.invocation.parameters')"
18+
repo="$(echo "${parameters}" | jq -r '."git-url"')"
19+
revision="$(echo "${parameters}" | jq -r ".revision")"
20+
repo=${repo%".git"} # remove optional .git suffix from repo name
21+
22+
if [[ ! -v repo_revision[$repo] ]]; then
23+
# no revision for repo so far --> add it to map
24+
repo_revision[$repo]=$revision
25+
else
26+
if [[ "${repo_revision[$repo]}" != "$revision" ]]; then
27+
# revisions don't match
28+
image=${image_ref##*/} # Get image name after last slash
29+
30+
echo "Revision for ${image} didn't match. Expected revision ${repo_revision[$repo]} for repo ${repo}, but got ${revision}"
31+
rc=1
32+
fi
33+
fi
34+
fi
35+
36+
done <<< "$(yq read "${csv_file}" 'spec.relatedImages[*].image' | sort | uniq)"
37+
38+
if [[ "$rc" == "0" ]]; then
39+
echo "All revisions matched correctly"
40+
fi
41+
42+
return $rc
43+
}
44+
45+
verify_image_revisions

0 commit comments

Comments
 (0)