|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +redhat_registry='registry.connect.redhat.com' |
| 4 | +redhat_catalog_api="${REDHAT_CATALOG_API:-https://catalog.redhat.com/api/containers/v1}" |
| 5 | +redhat_catalog_curl_timeout="${REDHAT_CATALOG_CURL_TIMEOUT:-20}" |
| 6 | +redhat_operator_repository='percona/percona-postgresql-operator' |
| 7 | +redhat_containers_repository='percona/percona-postgresql-operator-containers' |
| 8 | +redhat_release="${REDHAT_RELEASE:-${VERSION}}" |
| 9 | +redhat_operator_tag="${REDHAT_OPERATOR_TAG:-${redhat_release}-postgres-operator}" |
| 10 | +redhat_upgrade_tag="${REDHAT_UPGRADE_TAG:-${redhat_release}-upgrade}" |
| 11 | +redhat_operator_image='' |
| 12 | +redhat_postgres_image='' |
| 13 | +redhat_pgbouncer_image='' |
| 14 | +redhat_pgbackrest_image='' |
| 15 | +redhat_pmm_image='' |
| 16 | +redhat_upgrade_image='' |
| 17 | + |
| 18 | +declare -A redhat_image_refs=() |
| 19 | + |
| 20 | +redhat_digest_override_key() { |
| 21 | + printf '%s' "$1" | sed -E 's/[^[:alnum:]]+/_/g' | tr '[:lower:]' '[:upper:]' |
| 22 | +} |
| 23 | + |
| 24 | +redhat_catalog_digest() { |
| 25 | + local repository="$1" tag="$2" digest='' |
| 26 | + |
| 27 | + log "Resolving Red Hat digest for ${redhat_registry}/${repository}:${tag}" |
| 28 | + digest=$(curl -fsSL --connect-timeout 5 --max-time "${redhat_catalog_curl_timeout}" \ |
| 29 | + "${redhat_catalog_api}/repositories/registry/${redhat_registry}/repository/${repository}/tag/${tag}" \ |
| 30 | + | jq -er '.docker_image_digest // .data.docker_image_digest // .data[0].docker_image_digest' 2>/dev/null) \ |
| 31 | + || digest=$(curl -fsSL --connect-timeout 5 --max-time "${redhat_catalog_curl_timeout}" \ |
| 32 | + "${redhat_catalog_api}/repositories/registry/${redhat_registry}/repository/${repository}/images?page_size=500" \ |
| 33 | + | jq -er --arg tag "${tag}" \ |
| 34 | + 'first(.data[] | select(any(.repositories[]?.tags[]?; .name == $tag)) | .docker_image_digest)' 2>/dev/null) \ |
| 35 | + || abort "unable to resolve Red Hat image digest for ${redhat_registry}/${repository}:${tag}" |
| 36 | + |
| 37 | + [[ ${digest} == sha256:* ]] || digest="sha256:${digest#sha256:}" |
| 38 | + printf '%s\n' "${digest}" |
| 39 | +} |
| 40 | + |
| 41 | +redhat_image_ref() { |
| 42 | + local name="$1" repository="$2" tag="$3" digest_var digest |
| 43 | + digest_var="REDHAT_IMAGE_DIGEST_$(redhat_digest_override_key "${name}")" |
| 44 | + digest="${!digest_var:-}" |
| 45 | + if [[ -z ${digest} ]]; then |
| 46 | + digest=$(redhat_catalog_digest "${repository}" "${tag}") || exit |
| 47 | + fi |
| 48 | + [[ -n ${digest} ]] || abort "empty Red Hat image digest for ${redhat_registry}/${repository}:${tag}" |
| 49 | + [[ ${digest} == sha256:* ]] || digest="sha256:${digest#sha256:}" |
| 50 | + printf '%s/%s@%s\n' "${redhat_registry}" "${repository}" "${digest}" |
| 51 | +} |
| 52 | + |
| 53 | +add_redhat_related_image() { |
| 54 | + local name="$1" repository="$2" tag="$3" image |
| 55 | + image=$(redhat_image_ref "${name}" "${repository}" "${tag}") |
| 56 | + log "Related image ${name}: ${image}" |
| 57 | + redhat_image_refs["${name}"]="${image}" |
| 58 | + related_images=$(jq -c \ |
| 59 | + --arg name "${name}" \ |
| 60 | + --arg image "${image}" \ |
| 61 | + '. + [{ name: $name, image: $image }]' <<<"${related_images}") |
| 62 | +} |
| 63 | + |
| 64 | +build_redhat_related_images() { |
| 65 | + local pg_major current_pg_major |
| 66 | + local pg_versions=() |
| 67 | + |
| 68 | + log "Building Red Hat related images from release versions" |
| 69 | + # shellcheck source=/dev/null |
| 70 | + source "${repo_root}/e2e-tests/release_versions" |
| 71 | + |
| 72 | + mapfile -t pg_versions < <(compgen -A variable IMAGE_POSTGRESQL | sed 's/^IMAGE_POSTGRESQL//' | sort -rn) |
| 73 | + |
| 74 | + for pg_major in "${pg_versions[@]}"; do |
| 75 | + add_redhat_related_image "ppg${pg_major}-postgres" \ |
| 76 | + "${redhat_containers_repository}" "${redhat_release}-ppg-${pg_major}-postgres" |
| 77 | + done |
| 78 | + |
| 79 | + for pg_major in "${pg_versions[@]}"; do |
| 80 | + add_redhat_related_image "ppg${pg_major}-postgis" \ |
| 81 | + "${redhat_containers_repository}" "${redhat_release}-ppg-${pg_major}-postgis" |
| 82 | + done |
| 83 | + |
| 84 | + add_redhat_related_image "pgbackrest" "${redhat_containers_repository}" "${redhat_release}-ppg-18-pgbackrest" |
| 85 | + add_redhat_related_image "pgbouncer" "${redhat_containers_repository}" "${redhat_release}-ppg-18-pgbouncer" |
| 86 | + add_redhat_related_image "pmm" "${redhat_containers_repository}" "${redhat_release}-pmm" |
| 87 | + add_redhat_related_image "operator" "${redhat_operator_repository}" "${redhat_operator_tag}" |
| 88 | + add_redhat_related_image "pgupgrade" "${redhat_containers_repository}" "${redhat_upgrade_tag}" |
| 89 | + |
| 90 | + current_pg_major=$(yq --raw-output '.spec.postgresVersion' ../../deploy/cr.yaml) |
| 91 | + redhat_operator_image="${redhat_image_refs[operator]}" |
| 92 | + redhat_postgres_image="${redhat_image_refs[ppg${current_pg_major}-postgres]}" |
| 93 | + redhat_pgbouncer_image="${redhat_image_refs[pgbouncer]}" |
| 94 | + redhat_pgbackrest_image="${redhat_image_refs[pgbackrest]}" |
| 95 | + redhat_pmm_image="${redhat_image_refs[pmm]}" |
| 96 | + redhat_upgrade_image="${redhat_image_refs[pgupgrade]}" |
| 97 | +} |
| 98 | + |
| 99 | +distribution_prepare() { |
| 100 | + log "Preparing Red Hat certified bundle metadata" |
| 101 | + package_name='percona-postgresql-operator-certified' |
| 102 | + file_name='percona-postgresql-operator-certified' |
| 103 | + build_redhat_related_images |
| 104 | +} |
| 105 | + |
| 106 | +distribution_render_annotations() { |
| 107 | + yq --yaml-roundtrip \ |
| 108 | + --arg package "${package_name}" \ |
| 109 | + --arg package_channel "${PACKAGE_CHANNEL}" \ |
| 110 | + --arg openshift_supported_versions "${OPENSHIFT_VERSIONS}" \ |
| 111 | + ' |
| 112 | + .annotations["operators.operatorframework.io.bundle.package.v1"] = $package | |
| 113 | + .annotations["operators.operatorframework.io.bundle.channels.v1"] = $package_channel | |
| 114 | + .annotations["operators.operatorframework.io.bundle.channel.default.v1"] = $package_channel | |
| 115 | + .annotations["com.redhat.openshift.versions"] = $openshift_supported_versions | |
| 116 | + .' <bundle.annotations.yaml |
| 117 | +} |
| 118 | + |
| 119 | +distribution_rewrite_examples() { |
| 120 | + local examples="$1" |
| 121 | + |
| 122 | + log "Rewriting Red Hat alm-examples image references" |
| 123 | + jq \ |
| 124 | + --arg operator_image "${redhat_operator_image}" \ |
| 125 | + --arg postgres_image "${redhat_postgres_image}" \ |
| 126 | + --arg pgbouncer_image "${redhat_pgbouncer_image}" \ |
| 127 | + --arg pgbackrest_image "${redhat_pgbackrest_image}" \ |
| 128 | + --arg pmm_image "${redhat_pmm_image}" \ |
| 129 | + --arg upgrade_image "${redhat_upgrade_image}" \ |
| 130 | + ' |
| 131 | + map( |
| 132 | + if .kind == "PerconaPGCluster" then |
| 133 | + .spec = ( |
| 134 | + { crVersion: .spec.crVersion, initContainer: { image: $operator_image } } + |
| 135 | + (.spec | del(.crVersion, .initContainer)) |
| 136 | + ) | |
| 137 | + .spec.image = $postgres_image | |
| 138 | + .spec.proxy.pgBouncer.image = $pgbouncer_image | |
| 139 | + .spec.backups.pgbackrest.image = $pgbackrest_image | |
| 140 | + .spec.pmm.image = $pmm_image |
| 141 | + elif .kind == "PerconaPGUpgrade" then |
| 142 | + .spec.image = $upgrade_image | |
| 143 | + .spec.toPostgresImage = $postgres_image | |
| 144 | + .spec.toPgBouncerImage = $pgbouncer_image | |
| 145 | + .spec.toPgBackRestImage = $pgbackrest_image |
| 146 | + else |
| 147 | + . |
| 148 | + end |
| 149 | + ) |
| 150 | + ' <<<"${examples}" |
| 151 | +} |
| 152 | + |
| 153 | +distribution_apply_csv_overrides() { |
| 154 | + local csv_file="$1" |
| 155 | + |
| 156 | + log "Applying Red Hat certified CSV overrides" |
| 157 | + # https://redhat-connect.gitbook.io/certified-operator-guide/appendix/what-if-ive-already-published-a-community-operator |
| 158 | + yq --in-place --yaml-roundtrip \ |
| 159 | + --arg operator_image "${redhat_operator_image}" \ |
| 160 | + ' |
| 161 | + .metadata.annotations.certified = "true" | |
| 162 | + .metadata.annotations["containerImage"] = $operator_image | |
| 163 | + .spec.install.spec.deployments[].spec.template.spec.containers[].image = $operator_image | |
| 164 | + .' \ |
| 165 | + "${csv_file}" |
| 166 | +} |
0 commit comments