|
| 1 | +#!/bin/bash |
| 2 | +# Build a catalog image for use with the OpenShift internal registry. |
| 3 | +# |
| 4 | +# OPM cannot pull bundle images from the internal registry (unreachable from dev machine), |
| 5 | +# so this script: |
| 6 | +# 1. Pushes the bundle to the internal registry via the external route |
| 7 | +# 2. Uses OPM to build the catalog index from the external route (pullable) |
| 8 | +# 3. Fixes the bundlepath in the database to use the internal registry address |
| 9 | +# 4. Builds the final catalog image |
| 10 | +# |
| 11 | +# Usage: ocp-catalog-build.sh <container-tool> <bundle-img> <catalog-img> <opm> |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +CONTAINER_TOOL="${1:?Usage: $0 <container-tool> <bundle-img> <catalog-img> <opm>}" |
| 15 | +BUNDLE_IMG="${2:?Usage: $0 <container-tool> <bundle-img> <catalog-img> <opm>}" |
| 16 | +CATALOG_IMG="${3:?Usage: $0 <container-tool> <bundle-img> <catalog-img> <opm>}" |
| 17 | +OPM="${4:?Usage: $0 <container-tool> <bundle-img> <catalog-img> <opm>}" |
| 18 | + |
| 19 | +INTERNAL_PREFIX="image-registry.openshift-image-registry.svc:5000" |
| 20 | + |
| 21 | +echo "==> Ensuring the OpenShift image registry default route is exposed..." |
| 22 | +oc patch configs.imageregistry.operator.openshift.io/cluster \ |
| 23 | + --type merge -p '{"spec":{"defaultRoute":true}}' |
| 24 | + |
| 25 | +ROUTE_HOST="$(oc get route default-route -n openshift-image-registry \ |
| 26 | + -o jsonpath='{.spec.host}')" |
| 27 | +if [ -z "${ROUTE_HOST}" ]; then |
| 28 | + echo "Error: could not obtain the registry route host." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +ROUTE_BUNDLE="${BUNDLE_IMG/${INTERNAL_PREFIX}/${ROUTE_HOST}}" |
| 33 | + |
| 34 | +echo "==> Logging in to the registry..." |
| 35 | +${CONTAINER_TOOL} login "${ROUTE_HOST}" -u "$(oc whoami)" -p "$(oc whoami -t)" --tls-verify=false |
| 36 | + |
| 37 | +echo "==> Pushing bundle image to internal registry..." |
| 38 | +${CONTAINER_TOOL} tag "${BUNDLE_IMG}" "${ROUTE_BUNDLE}" |
| 39 | +${CONTAINER_TOOL} push "${ROUTE_BUNDLE}" --tls-verify=false |
| 40 | +${CONTAINER_TOOL} rmi "${ROUTE_BUNDLE}" 2>/dev/null || true |
| 41 | + |
| 42 | +echo "==> Building catalog index from external route..." |
| 43 | +WORKDIR=$(mktemp -d) |
| 44 | +# shellcheck disable=SC2064 |
| 45 | +trap "rm -rf ${WORKDIR}" EXIT |
| 46 | + |
| 47 | +${OPM} index add \ |
| 48 | + --build-tool "${CONTAINER_TOOL}" --pull-tool none --skip-tls-verify \ |
| 49 | + --generate --mode semver \ |
| 50 | + --out-dockerfile "${WORKDIR}/index.Dockerfile" \ |
| 51 | + --bundles "${ROUTE_BUNDLE}" \ |
| 52 | + --tag "${CATALOG_IMG}" |
| 53 | +mv database "${WORKDIR}/" |
| 54 | + |
| 55 | +echo "==> Fixing bundle path in catalog database..." |
| 56 | +sqlite3 "${WORKDIR}/database/index.db" \ |
| 57 | + "UPDATE operatorbundle SET bundlepath = REPLACE(bundlepath, '${ROUTE_HOST}', '${INTERNAL_PREFIX}');" |
| 58 | + |
| 59 | +echo "==> Building catalog image..." |
| 60 | +${CONTAINER_TOOL} build -f "${WORKDIR}/index.Dockerfile" -t "${CATALOG_IMG}" "${WORKDIR}" |
| 61 | + |
| 62 | +echo "==> Catalog image built successfully: ${CATALOG_IMG}" |
0 commit comments