From 41354b3f8fe5eb0922b5af44331bd84876dba218 Mon Sep 17 00:00:00 2001 From: Jakub Cechacek Date: Mon, 4 Sep 2023 10:51:54 +0200 Subject: [PATCH] DBZ-6738 OLM support scripts --- .gitignore | 5 ++- scripts/create-olm-bundle-image.sh | 28 +++++++++++++ scripts/create-olm-bundle.sh | 48 ++++++++++++++++++++++ scripts/create-olm-test-catalog.sh | 61 ++++++++++++++++++++++++++++ scripts/create-olm-test-resources.sh | 56 +++++++++++++++++++++++++ scripts/functions.sh | 17 ++++++++ 6 files changed, 214 insertions(+), 1 deletion(-) create mode 100755 scripts/create-olm-bundle-image.sh create mode 100755 scripts/create-olm-bundle.sh create mode 100755 scripts/create-olm-test-catalog.sh create mode 100755 scripts/create-olm-test-resources.sh create mode 100644 scripts/functions.sh diff --git a/.gitignore b/.gitignore index bf781c7..9a4db10 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,7 @@ nb-configuration.xml /.quarkus/cli/plugins/ # kubernetes -kubernetes.json \ No newline at end of file +kubernetes.json + +# buil dprocess +olm/ \ No newline at end of file diff --git a/scripts/create-olm-bundle-image.sh b/scripts/create-olm-bundle-image.sh new file mode 100755 index 0000000..90a62d9 --- /dev/null +++ b/scripts/create-olm-bundle-image.sh @@ -0,0 +1,28 @@ +#! /bin/bash +set -euxo pipefail + +VERSION=${1:-"all"} +BUNDLE_IMAGE=${2:-"quay.io/debezium/operator-bundle"} +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +BUNDLE_BASE_DIR="$SCRIPT_DIR/../olm/bundles" + +source "$SCRIPT_DIR/functions.sh" + +if [[ $VERSION = "all" ]] +then + BUNDLES=( $BUNDLE_BASE_DIR/*/ ) +else + BUNDLES=( "$BUNDLE_BASE_DIR/$VERSION" ) +fi + + +for bundle in "${BUNDLES[@]}"; do + name="$(csvName $bundle)" + version="$(csvVersion $bundle)" + path="$(echo "${bundle%/}")" + image="$BUNDLE_IMAGE:$version" + + docker build -t "$image" -f "$path/bundle.Dockerfile" "$path" + docker push "$image" +done; + diff --git a/scripts/create-olm-bundle.sh b/scripts/create-olm-bundle.sh new file mode 100755 index 0000000..6c854ca --- /dev/null +++ b/scripts/create-olm-bundle.sh @@ -0,0 +1,48 @@ +#!/bin/bash +set -euxo pipefail + +# Ex: 2.4.1 +VERSION=$1 +# Ex: 2.4.0 +# Ex: none [if no replaces] +REPLACES_VERSION=$2 +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +BUNDLE_BASE_DIR=$SCRIPT_DIR/../olm/bundles/$VERSION + +source "$SCRIPT_DIR/functions.sh" + +{ set +x; } 2>/dev/null +echo "" +echo "Creating OLM bundle for version $VERSION replacing version $REPLACES_VERSION" +echo "" +set -x + +rm -rf "$BUNDLE_BASE_DIR" +mkdir -p "$BUNDLE_BASE_DIR" + +# Copy the files generated by Quarkus during the maven build +pushd $SCRIPT_DIR +cp -r ../target/bundle/debezium-operator/* "$BUNDLE_BASE_DIR" +popd + +# Find the CSV YAML +CSV_PATH="$(csvPath "$BUNDLE_BASE_DIR")" + +# Edit the CSV version, replaces, etc. +yq ea -i ".metadata.annotations.createdAt = \"$(date "+%D %T")\"" "$CSV_PATH" +yq ea -i ".spec.version = \"$VERSION\"" "$CSV_PATH" +yq ea -i ".metadata.name = \"debezium-operator.v$VERSION\"" "$CSV_PATH" + +if [[ $REPLACES_VERSION = "none" ]] +then + yq ea -i "del(.spec.replaces)" "$CSV_PATH" +else + yq ea -i ".spec.replaces = \"debezium-operator.v$REPLACES_VERSION\"" "$CSV_PATH" +fi + +# Wire OLM target namespaces and QOSDK configuration together +yq ea -i '.spec.install.spec.deployments[0].spec.template.spec.containers[0].env += [{"name": "QUARKUS_OPERATOR_SDK_NAMESPACES", "valueFrom": {"fieldRef": {"fieldPath": "metadata.annotations['"'"'olm.targetNamespaces'"'"']"}}}]' "$CSV_PATH" + +{ set +x; } 2>/dev/null +echo "" +echo "OLM bundle created!" diff --git a/scripts/create-olm-test-catalog.sh b/scripts/create-olm-test-catalog.sh new file mode 100755 index 0000000..75f1387 --- /dev/null +++ b/scripts/create-olm-test-catalog.sh @@ -0,0 +1,61 @@ +#! /bin/bash +set -euxo pipefail + +VERSION=${1:-"all"} +BUNDLE_IMAGE=${2:-"quay.io/debezium/operator-bundle"} +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +CATALOG_NAME="debezium-catalog" +CATALOG_BASE_DIR="$SCRIPT_DIR/../olm/catalog" +CATALOG_DIR="$CATALOG_BASE_DIR/$CATALOG_NAME" +BUNDLE_CATALOG_BASE_DIR="$SCRIPT_DIR/../olm/bundles" + +source "$SCRIPT_DIR/functions.sh" + +if [[ $VERSION = "all" ]] +then + BUNDLES=( $BUNDLE_CATALOG_BASE_DIR/*/ ) +else + BUNDLES=( "$BUNDLE_CATALOG_BASE_DIR/$VERSION" ) +fi + +rm -rf "$CATALOG_DIR" +rm -rf "$CATALOG_BASE_DIR/$CATALOG_NAME.Dockerfile" +mkdir -p "$CATALOG_DIR" + + +# Generate dockerfile +opm generate dockerfile "$CATALOG_DIR" + +# Initialize catalog manifest +opm init debezium-operator \ + --default-channel=alpha \ + --output yaml > "$CATALOG_DIR/operator.yal" + +# Render each bundle +for bundle in "${BUNDLES[@]}"; do + version="$(csvVersion $bundle)" + image="$BUNDLE_IMAGE:$version" + echo "Rendering bundle for $image" + opm render "$image" --output=yaml >> "$CATALOG_DIR/operator.yal" +done; + +# Write out channel declaration +cat << EOF >> "$CATALOG_DIR/operator.yal" +--- +schema: olm.channel +package: debezium-operator +name: alpha +entries: +EOF + +# Write out channel entries +for bundle in "${BUNDLES[@]}"; do + name="$(csvName $bundle)" + replaces="$(csvReplaces $bundle)" + + echo " - name: $name" >> "$CATALOG_DIR/operator.yal" + [ $replaces != "null" ] && echo " replaces: $replaces" >> "$CATALOG_DIR/operator.yal" +done; + +# Validate generated catalog +opm validate "$CATALOG_DIR" \ No newline at end of file diff --git a/scripts/create-olm-test-resources.sh b/scripts/create-olm-test-resources.sh new file mode 100755 index 0000000..7e2c22b --- /dev/null +++ b/scripts/create-olm-test-resources.sh @@ -0,0 +1,56 @@ +#! /bin/bash +set -euxo pipefail + +VERSION=$1 +CATALOG_IMAGE=${2:-"quay.io/debezium/operator-catalog:latest"} + + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +rm -rf "$SCRIPT_DIR/../olm/testing-resources" +mkdir -p "$SCRIPT_DIR/../olm/testing-resources" + +cat << EOF >> "$SCRIPT_DIR/../olm/testing-resources/catalog.yaml" +apiVersion: operators.coreos.com/v1alpha1 +kind: CatalogSource +metadata: + name: debezium-catalog + namespace: olm +spec: + grpcPodConfig: + securityContextConfig: restricted + sourceType: grpc + image: $CATALOG_IMAGE + displayName: Debezium Test Catalog + publisher: Me + updateStrategy: + registryPoll: + interval: 10m +EOF + + +cat << EOF >> "$SCRIPT_DIR/../olm/testing-resources/operatorgroup.yaml" +kind: OperatorGroup +apiVersion: operators.coreos.com/v1 +metadata: + name: og-own +spec: + targetNamespaces: + - debezium +EOF + +cat << EOF >> "$SCRIPT_DIR/../olm/testing-resources/subscription.yaml" +apiVersion: operators.coreos.com/v1alpha1 +kind: Subscription +metadata: + name: debezium-operator +spec: + installPlanApproval: Automatic + name: debezium-operator + source: debezium-catalog + sourceNamespace: olm + startingCSV: debezium-operator.v$VERSION +EOF + + + diff --git a/scripts/functions.sh b/scripts/functions.sh new file mode 100644 index 0000000..5b847d9 --- /dev/null +++ b/scripts/functions.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +function csvPath() { + find "$1" -type f -name '*.clusterserviceversion.yaml' +} + +function csvName() { + yq ".metadata.name" $(csvPath $1) +} + +function csvVersion() { + yq ".spec.version" $(csvPath $1) +} + +function csvReplaces() { + yq ".spec.replaces" $(csvPath $1) +} \ No newline at end of file