|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script serves to check and validate the definitions for runtime images. |
| 4 | +# It does just a brief check of the metadata defined in the json file: |
| 5 | +# 1. checks that given `.metadata.image_name` is valid and can be accessed by skopeo tool |
| 6 | +# 2. checks that tag in `.metadata.tags[0]` can be found in the output from skopeo tool |
| 7 | +# |
| 8 | +# THIS FILE DOESN'T CHECK THAT THE USED LINK TO IMAGE IS THE LATEST ONE AVAILABLE! |
| 9 | +# |
| 10 | +# This script uses `skopeo` and `jq` tools installed locally for retrieving |
| 11 | +# information about the particular remote images. |
| 12 | +# |
| 13 | +# Local execution: ./ci/check-runtime-image.sh |
| 14 | +# Note: please execute from the root directory so that relative path matches |
| 15 | +# |
| 16 | +# In case of the PR on GitHub, this check is tied to GitHub actions automatically, |
| 17 | +# see `.github/workflows` directory. |
| 18 | + |
| 19 | +# ---------------------------- DEFINED FUNCTIONS ----------------------------- # |
| 20 | + |
| 21 | +function check_image() { |
| 22 | + local runtime_image_file="${1}" |
| 23 | + |
| 24 | + echo "---------------------------------------------" |
| 25 | + echo "Checking file: '${runtime_image_file}'" |
| 26 | + |
| 27 | + local img_tag |
| 28 | + local img_url |
| 29 | + local img_metadata |
| 30 | + |
| 31 | + img_tag=$(jq -r '.metadata.tags[0]' "${runtime_image_file}") || { |
| 32 | + echo "ERROR: Couldn't parse image tags metadata for '${runtime_image_file}' runtime image file!" |
| 33 | + return 1 |
| 34 | + } |
| 35 | + img_url=$(jq -r '.metadata.image_name' "${runtime_image_file}") || { |
| 36 | + echo "ERROR: Couldn't parse image URL metadata for '${runtime_image_file}' runtime image file!" |
| 37 | + return 1 |
| 38 | + } |
| 39 | + |
| 40 | + img_metadata="$(skopeo inspect --config "docker://${img_url}")" || { |
| 41 | + echo "ERROR: Couldn't download '${img_url}' image metadata with skopeo tool!" |
| 42 | + return 1 |
| 43 | + } |
| 44 | + |
| 45 | + local expected_string="runtime-${img_tag}-ubi" |
| 46 | + echo "Checking that '${expected_string}' is present in the image metadata" |
| 47 | + echo "${img_metadata}" | grep --quiet "${expected_string}" || { |
| 48 | + echo "ERROR: The string '${expected_string}' isn't present in the image metadata at all. Please check that the referenced image '${img_url}' is the correct one!" |
| 49 | + return 1 |
| 50 | + } |
| 51 | + |
| 52 | + # TODO: we shall extend this check to check also Label "io.openshift.build.commit.ref" value (e.g. '2024a') or something similar |
| 53 | +} |
| 54 | + |
| 55 | +function main() { |
| 56 | + ret_code=0 |
| 57 | + |
| 58 | + # If name of the directory isn't good enough, maybe we can improve this to search for the: `"schema_name": "runtime-image"` string. |
| 59 | + runtime_image_files=$(find . -name "*.json" | grep "runtime-images" | sort --unique) |
| 60 | + |
| 61 | + IFS=$'\n' |
| 62 | + for file in ${runtime_image_files}; do |
| 63 | + check_image "${file}" || { |
| 64 | + echo "ERROR: Check for '${file}' failed!" |
| 65 | + ret_code=1 |
| 66 | + } |
| 67 | + done |
| 68 | + |
| 69 | + echo "---------------------------------------------" |
| 70 | + echo "" |
| 71 | + if test "${ret_code}" -eq 0; then |
| 72 | + echo "Validation of runtime images definitions was successful! Congrats :)" |
| 73 | + else |
| 74 | + echo "ERROR: Some of the runtime image definitions aren't valid, please check above!" |
| 75 | + fi |
| 76 | + |
| 77 | + return "${ret_code}" |
| 78 | +} |
| 79 | + |
| 80 | +# ------------------------------ MAIN SCRIPT --------------------------------- # |
| 81 | + |
| 82 | +main |
| 83 | + |
| 84 | +exit "${?}" |
0 commit comments