Skip to content

Commit

Permalink
Search required images from env for both csv openshift and kubernetes…
Browse files Browse the repository at this point in the history
…. Fix some shellchecks.

Signed-off-by: Oleksandr Andriienko <[email protected]>
  • Loading branch information
AndrienkoAleksandr committed Jul 7, 2020
1 parent 39bceb3 commit e930fc5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
31 changes: 16 additions & 15 deletions olm/addDigests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ echo "find ${CSV_FILES_DIR} -regextype posix-egrep -regex \"${CSV_FILES_DIR}/?${
CSV_FILES=( $(find ${CSV_FILES_DIR} -regextype posix-egrep -regex "${CSV_FILES_DIR}/?${CSV_FILE_PATH_REGEXP}") )
RELATED_IMAGE_PREFIX="RELATED_IMAGE_"

rm -Rf ${BASE_DIR}/generated/csv
mkdir -p ${BASE_DIR}/generated/csv
rm -Rf "${BASE_DIR}/generated/csv"
mkdir -p "${BASE_DIR}/generated/csv"
# Copy original csv files
for CSV_FILE in ${CSV_FILES[@]}
for CSV_FILE in "${CSV_FILES[@]}"
do
echo "CSV file: ${CSV_FILE}"
cp -pR ${CSV_FILE} ${BASE_DIR}/generated/csv
cp -pR "${CSV_FILE}" "${BASE_DIR}/generated/csv"
csvs_args="${csvs_args} -c ${CSV_FILE}"
done

# Collect list digest only once to save time. We expected that digest list is the same for "openshift" and "kubernetes" platforms.
source ${SCRIPTS_DIR}/buildDigestMap.sh -w ${BASE_DIR} -c ${CSV_FILES[0]} -t ${IMAGE_TAG} ${QUIET}
# shellcheck source=buildDigestMap.sh
eval "${SCRIPTS_DIR}/buildDigestMap.sh" -w "${BASE_DIR}" -t "${IMAGE_TAG}" "${csvs_args}" ${QUIET}

if [[ ! "${QUIET}" ]]; then cat ${BASE_DIR}/generated/digests-mapping.txt; fi
for CSV_FILE in ${CSV_FILES[@]}
if [[ ! "${QUIET}" ]]; then cat "${BASE_DIR}"/generated/digests-mapping.txt; fi
for CSV_FILE in "${CSV_FILES[@]}"
do
CSV_FILE_COPY=${BASE_DIR}/generated/csv/$(basename ${CSV_FILE})

Expand All @@ -97,7 +98,7 @@ do
# Image tag could contains invalid for Env variable name characters, so let's encode it using base32.
# But alphabet of base32 uses one invalid for env variable name character '=' at the end of the line, so let's replace it by '_'.
# To recovery original tag should be done opposite actions: replace '_' to '=', and decode string using 'base32 -d'.
encodedTag=$(echo ${tagOrDigest} | base32 -w 0 | tr = _)
encodedTag=$(echo "${tagOrDigest}" | base32 -w 0 | tr "=" "_")
relatedImageEnvName=$(echo "${RELATED_IMAGE_PREFIX}${name}_${imageLabel}_${encodedTag}" | sed -r 's/[-.]/_/g')
ENV="{ name: \"${relatedImageEnvName}\", value: \"${dest}\"}"
if [[ -z ${RELATED_IMAGES_ENV} ]]; then
Expand All @@ -114,21 +115,21 @@ do
RELATED_IMAGES="${RELATED_IMAGES}, ${RELATED_IMAGE}"
fi

sed -i -e "s;${source};${dest};" ${CSV_FILE_COPY}
sed -i -e "s;${source};${dest};" "${CSV_FILE_COPY}"
done

mv ${CSV_FILE_COPY} ${CSV_FILE_COPY}.old
mv "${CSV_FILE_COPY}" "${CSV_FILE_COPY}.old"
yq -ryY "
( .spec.relatedImages ) += [${RELATED_IMAGES}] |
( .spec.install.spec.deployments[0].spec.template.spec.containers[0].env ) += [${RELATED_IMAGES_ENV}]
" ${CSV_FILE_COPY}.old > ${CSV_FILE_COPY}
sed -i ${CSV_FILE_COPY} -r -e "s|tag: |# tag: |"
rm -f ${CSV_FILE_COPY}.old
" "${CSV_FILE_COPY}.old" > "${CSV_FILE_COPY}"
sed -i "${CSV_FILE_COPY}" -r -e "s|tag: |# tag: |"
rm -f "${CSV_FILE_COPY}.old"

# update original file with generated changes
mv "${CSV_FILE_COPY}" "${CSV_FILE}"
echo "[INFO] CSV updated: ${CSV_FILE}"
done

# cleanup
rm -fr ${BASE_DIR}/generated
rm -fr "${BASE_DIR}/generated"
37 changes: 19 additions & 18 deletions olm/buildDigestMap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Contributors:
# Red Hat, Inc. - initial API and implementation

SCRIPTS_DIR=$(cd "$(dirname "$0")"; pwd)
SCRIPTS_DIR=$(cd "$(dirname "$0")" || exit 1; pwd)
BASE_DIR="$1"
QUIET=""

Expand All @@ -35,7 +35,7 @@ if [[ $# -lt 1 ]]; then usage; exit; fi
while [[ "$#" -gt 0 ]]; do
case $1 in
'-w') BASE_DIR="$2"; shift 1;;
'-c') CSV="$2"; shift 1;;
'-c') CSV="$2"; CSVS+=("${CSV}");shift 1;;
'-t') IMAGE_TAG="$2"; shift 1;;
'-q') QUIET="-q"; shift 0;;
'--help'|'-h') usage; exit;;
Expand All @@ -45,42 +45,43 @@ done

if [[ ! $CSV ]] || [[ ! $IMAGE_TAG ]]; then usage; exit 1; fi

mkdir -p ${BASE_DIR}/generated
mkdir -p "${BASE_DIR}/generated"

echo "[INFO] Get images from CSV ${CSV}"

source ${SCRIPTS_DIR}/images.sh
# shellcheck source=images.sh
. "${SCRIPTS_DIR}"/images.sh

# todo create init method
setImagesFromDeploymentEnv

setOperatorImage
echo ${OPERATOR_IMAGE}
echo "${OPERATOR_IMAGE}"

setPluginRegistryList
echo ${PLUGIN_REGISTRY_LIST}
echo "${PLUGIN_REGISTRY_LIST}"

setDevfileRegistryList
echo ${DEVFILE_REGISTRY_LIST}
echo "${DEVFILE_REGISTRY_LIST}"

writeDigest() {
image=$1
imageType=$2
echo ${image}
case ${image} in
*@sha256:*)
withDigest="${image}";;
withDigest=${image};;
*@)
continue;;
return;;
*)
# for other build methods or for falling back to other registries when not found, can apply transforms here
orig_image="${image}"
orig_image=${image}
if [[ -x ${SCRIPTS_DIR}/buildDigestMapAlternateURLs.sh ]]; then
# shellcheck source=buildDigestMapAlternateURLs.sh
. ${SCRIPTS_DIR}/buildDigestMapAlternateURLs.sh
fi
if [[ ${digest} ]]; then
if [[ ! "${QUIET}" ]]; then echo -n "[INFO] Got digest"; fi
echo " $digest # ${image}"
echo " $digest \# ${image}"
else
image="${orig_image}"
digest="$(skopeo inspect --tls-verify=false docker://${image} 2>/dev/null | jq -r '.Digest')"
Expand All @@ -107,19 +108,19 @@ writeDigest() {
}

DIGEST_FILE=${BASE_DIR}/generated/digests-mapping.txt
rm -Rf ${DIGEST_FILE}
touch ${DIGEST_FILE}
rm -Rf "${DIGEST_FILE}"
touch "${DIGEST_FILE}"

writeDigest ${OPERATOR_IMAGE} "operator-image"
writeDigest "${OPERATOR_IMAGE}" "operator-image"

for image in ${REQUIRED_IMAGES}; do
writeDigest ${image} "required-image"
writeDigest "${image}" "required-image"
done

for image in ${PLUGIN_REGISTRY_LIST}; do
writeDigest ${image} "plugin-registry-image"
writeDigest "${image}" "plugin-registry-image"
done

for image in ${DEVFILE_REGISTRY_LIST}; do
writeDigest ${image} "devfile-registry-image"
writeDigest "${image}" "devfile-registry-image"
done
8 changes: 4 additions & 4 deletions olm/images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Red Hat, Inc. - initial API and implementation

setImagesFromDeploymentEnv() {
REQUIRED_IMAGES=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*"; "g")) | .value' "${CSV}")
REQUIRED_IMAGES=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.value) | select(.name | test("RELATED_IMAGE_.*"; "g")) | .value' "${CSVS[@]}" | sort | uniq)
}

setOperatorImage() {
Expand All @@ -20,15 +20,15 @@ setOperatorImage() {

setPluginRegistryList() {
registry=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*plugin_registry"; "g")) | .value' "${CSV}")
setRegistryImages ${registry}
setRegistryImages "${registry}"

PLUGIN_REGISTRY_LIST=${registryImages}
}

setDevfileRegistryList() {
registry=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*devfile_registry"; "g")) | .value' "${CSV}")

setRegistryImages ${registry}
setRegistryImages "${registry}"
DEVFILE_REGISTRY_LIST=${registryImages}
}

Expand All @@ -39,6 +39,6 @@ setRegistryImages() {
echo -n "[INFO] Pull container ${registry} ..."
${PODMAN} pull ${registry} ${QUIET}

registryImages="$(${PODMAN} run --rm --entrypoint /bin/sh ${registry} -c "cat /var/www/html/*/external_images.txt")"
registryImages="$(${PODMAN} run --rm --entrypoint /bin/sh "${registry}" -c "cat /var/www/html/*/external_images.txt")"
echo "[INFO] Found $(echo "${registryImages}" | wc -l) images in registry"
}

0 comments on commit e930fc5

Please sign in to comment.