You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ts...@apache.org on 2022/10/07 03:48:03 UTC

[camel-k] 01/13: fix(bundle-index-gen): Improves generation of bundle index

This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit d9ad23dce07257a810bad7b191d82b6f395e9513
Author: phantomjinx <p....@phantomjinx.co.uk>
AuthorDate: Tue Aug 9 21:50:49 2022 +0100

    fix(bundle-index-gen): Improves generation of bundle index
    
    * Uses yq to insert the test bundle into existing channels as well as
      appending bundle into any new channels
    
    * Better replicates what the bundle will actually do to the channels in
      the bundle index
---
 script/Makefile              |  4 +-
 script/build_bundle_index.sh | 93 +++++++++++++++++++++++++++++++-------------
 2 files changed, 69 insertions(+), 28 deletions(-)

diff --git a/script/Makefile b/script/Makefile
index 91fade9ef..afba2fa97 100644
--- a/script/Makefile
+++ b/script/Makefile
@@ -25,7 +25,7 @@ CONTROLLER_GEN_VERSION := v0.6.1
 CODEGEN_VERSION := v0.23.5
 OPERATOR_SDK_VERSION := v1.14.0
 KUSTOMIZE_VERSION := v4.1.2
-OPM_VERSION := v1.21.0
+OPM_VERSION := v1.24.0
 BASE_IMAGE := docker.io/adoptopenjdk/openjdk11:slim
 LOCAL_REPOSITORY := /tmp/artifacts/m2
 IMAGE_NAME ?= docker.io/apache/camel-k
@@ -644,5 +644,5 @@ bundle-push: bundle-build
 bundle-index: opm
 	BUNDLE_INDEX=$(BUNDLE_INDEX) INDEX_DIR=$(INDEX_DIR) PACKAGE=$(PACKAGE) \
 	OPM=$(OPM) BUNDLE_IMAGE=$(BUNDLE_IMAGE_NAME):$(CUSTOM_VERSION) CSV_NAME=$(CSV_PRODUCTION_NAME) \
-	CSV_SKIPS=$(CSV_SKIP_RANGE) CSV_REPLACES=$(CSV_REPLACES) CHANNEL="$(DEFAULT_CHANNEL)-dev" \
+	CSV_SKIPS=$(CSV_SKIP_RANGE) CSV_REPLACES=$(CSV_REPLACES) CHANNELS="$(CHANNELS)" \
 	./script/build_bundle_index.sh
diff --git a/script/build_bundle_index.sh b/script/build_bundle_index.sh
index cb6e71093..e03fc861f 100755
--- a/script/build_bundle_index.sh
+++ b/script/build_bundle_index.sh
@@ -14,9 +14,11 @@ check_env_var "OPM" ${OPM}
 check_env_var "BUNDLE_IMAGE" ${BUNDLE_IMAGE}
 check_env_var "CSV_NAME" ${CSV_NAME}
 check_env_var "CSV_REPLACES" ${CSV_REPLACES}
-check_env_var "CHANNEL" ${CHANNEL}
+check_env_var "CHANNELS" ${CHANNELS}
 
 PACKAGE_YAML=${INDEX_DIR}/${PACKAGE}.yaml
+INDEX_BASE_YAML=${INDEX_DIR}/bundles.yaml
+CHANNELS_YAML="${INDEX_DIR}/${PACKAGE}-channels.yaml"
 
 if ! command -v ${OPM} &> /dev/null
 then
@@ -58,43 +60,82 @@ fi
 
 mkdir -p "${INDEX_DIR}"
 
-if [ ! -f ${INDEX_DIR}/bundles.yaml ]; then
-  ${OPM} render ${BUNDLE_INDEX} -o yaml > ${INDEX_DIR}/bundles.yaml
+if [ ! -f ${INDEX_BASE_YAML} ]; then
+  ${OPM} render ${BUNDLE_INDEX} -o yaml > ${INDEX_BASE_YAML}
   if [ $? != 0 ]; then
     echo "Error: failed to render the base catalog"
     exit 1
   fi
 fi
 
-${OPM} render --skip-tls -o yaml \
-  ${BUNDLE_IMAGE} > ${PACKAGE_YAML}
-if [ $? != 0 ]; then
-  echo "Error: failed to render the ${PACKAGE} bundle catalog"
-  exit 1
+if [ ! -f ${PACKAGE_YAML} ]; then
+  ${OPM} render --skip-tls -o yaml \
+    ${BUNDLE_IMAGE} > ${PACKAGE_YAML}
+  if [ $? != 0 ]; then
+    echo "Error: failed to render the ${PACKAGE} bundle catalog"
+    exit 1
+  fi
 fi
 
+#
+# Extract the camel-k channels
+#
+yq eval ". | select(.package == \"${PACKAGE}\" and .schema == \"olm.channel\")" ${INDEX_BASE_YAML} > ${CHANNELS_YAML}
+if [ $? != 0 ] || [ ! -f "${CHANNELS_YAML}" ]; then
+  echo "ERROR: Failed to extract camel-k entries from bundle catalog"
+  exit 1
+fi
 
+#
+# Filter out the channels in the bundles file
+#
+yq -i eval ". | select(.package != \"${PACKAGE}\" or .schema != \"olm.channel\")" ${INDEX_BASE_YAML}
+if [ $? != 0 ]; then
+  echo "ERROR: Failed to remove camel-k channel entries from bundles catalog"
+  exit 1
+fi
 
-cat << EOF >> ${PACKAGE_YAML}
----
-schema: olm.channel
-package: ${PACKAGE}
-name: ${CHANNEL}
-entries:
-  - name: ${CSV_NAME}
-EOF
+#
+# Split the channels and append/insert the bundle into each one
+#
+IFS=','
+#Read the split words into an array based on comma delimiter
+read -a CHANNEL_ARR <<< "${CHANNELS}"
 
-if [ -n "${CSV_REPLACES}" ]; then
-cat << EOF >> ${PACKAGE_YAML}
-    replaces: ${CSV_REPLACES}
-EOF
-fi
+for channel in "${CHANNEL_ARR[@]}";
+do
+  channel_props=$(yq eval ". | select(.name == \"${channel}\")" ${CHANNELS_YAML})
 
-if [ -n "${CSV_SKIPS}" ]; then
-cat << EOF >> ${PACKAGE_YAML}
-    skipRange: "\'${CSV_SKIPS}\'"
-EOF
-fi
+  entry="{ \"name\": \"${CSV_NAME}\""
+  if [ -n "${CSV_REPLACES}" ]; then
+    entry="${entry}, \"replaces\": \"${CSV_REPLACES}\""
+  fi
+  if [ -n "${CSV_SKIPS}" ]; then
+    entry="${entry}, \"skipRange\": \"${CSV_SKIPS}\""
+  fi
+  entry="${entry} }"
+
+  if [ -z "${channel_props}" ]; then
+    #
+    # Append a new channel
+    #
+    echo "Appending channel ${channel} ..."
+    object="{ \"entries\": [${entry}], \"name\": \"${channel}\", \"package\": \"${PACKAGE}\", \"schema\": \"olm.channel\" }"
+
+    channel_file=$(mktemp ${channel}-channel-XXX.yaml)
+    trap "rm -f ${channel_file}" EXIT
+    yq -n eval "${object}" > ${channel_file}
+
+    echo "---" >> ${CHANNELS_YAML}
+    cat ${channel_file} >> ${CHANNELS_YAML}
+  else
+    #
+    # Channel already exists so insert entry
+    #
+    echo "Inserting channel ${channel} ..."
+    yq -i eval "(. | select(.name == \"${channel}\") | .entries) += ${entry}" ${CHANNELS_YAML}
+  fi
+done
 
 echo -n "Validating index ... "
 STATUS=$(${OPM} validate ${INDEX_DIR} 2>&1)