From 1e10232325440763494c173671464bccabe45d05 Mon Sep 17 00:00:00 2001 From: Scott Hebert Date: Mon, 16 Sep 2024 23:11:57 -0400 Subject: [PATCH 1/2] feat(KONFLUX-4136): new reduce-snapshot script - add new script to ec-cli image Signed-off-by: Scott Hebert --- Dockerfile | 3 ++ hack/reduce-snapshot.sh | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 hack/reduce-snapshot.sh diff --git a/Dockerfile b/Dockerfile index 396b679af..8ce1f15c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -72,6 +72,9 @@ COPY --from=build "/build/dist/ec_${TARGETOS}_${TARGETARCH}" /usr/local/bin/ec # Copy the one kubectl binary that can run in this container COPY --from=build "/build/dist/kubectl_${TARGETOS}_${TARGETARCH}" /usr/local/bin/kubectl +# Copt reduce-snapshot script needed for single component mode +COPY hack/reduce-snapshot.sh /usr/local/bin + # OpenShift preflight check requires a license COPY --from=build /build/LICENSE /licenses/LICENSE diff --git a/hack/reduce-snapshot.sh b/hack/reduce-snapshot.sh new file mode 100755 index 000000000..c7b46c5d1 --- /dev/null +++ b/hack/reduce-snapshot.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# Copyright The Enterprise Contract Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# +# This script attempts to reduce a snapshot to a single component +# It determines the component via a custom resource's labels. +# It requires that the following environment variables be defined: +# +# - SINGLE_COMPONENT: true if single component mode is enabled. +# - SNAPSHOT: String representation of Snapshot +# - CUSTOM_RESOURCE: Custom Resource to query for built component in Snapshot +# - CUSTOM_RESOURCE_NAMESPACE: Namespace where Custom Resource is found +# - SNAPSHOT_PATH: The location to place the reduced Snapshot json file + +set -o errexit +set -o nounset +set -o pipefail + +echo "Single Component mode? ${SINGLE_COMPONENT}" +if [ "${SINGLE_COMPONENT}" == "true" ]; then + + CR_NAMESPACE_ARG= + if [ "${CUSTOM_RESOURCE_NAMESPACE}" != "" ]; then + CR_NAMESPACE_ARG="-n ${CUSTOM_RESOURCE_NAMESPACE}" + fi + SNAPSHOT_CREATION_TYPE=$(kubectl get "$CUSTOM_RESOURCE" ${CR_NAMESPACE_ARG:+$CR_NAMESPACE_ARG} -ojson \ + | jq -r '.metadata.labels."test.appstudio.openshift.io/type" // ""') + SNAPSHOT_CREATION_COMPONENT=$(kubectl get "$CUSTOM_RESOURCE" ${CR_NAMESPACE_ARG:+$CR_NAMESPACE_ARG} -ojson \ + | jq -r '.metadata.labels."appstudio.openshift.io/component" // ""') + + echo "SNAPSHOT_CREATION_TYPE: ${SNAPSHOT_CREATION_TYPE}" + echo "SNAPSHOT_CREATION_COMPONENT: ${SNAPSHOT_CREATION_COMPONENT}" + if [ "${SNAPSHOT_CREATION_TYPE}" == "component" ] && [ "${SNAPSHOT_CREATION_COMPONENT}" != "" ]; then + echo "Single Component mode is ${SINGLE_COMPONENT} and Snapshot type is component" + + # verify if in json form + SNAPSHOT="$(cat "${SNAPSHOT}" 2> /dev/null || echo "${SNAPSHOT}")" + + [[ ! "${SNAPSHOT}" =~ ^\s*\{ ]] && echo 'Error: Cannot load snapshot from JSON string or file' && exit 1 + + REDUCED_SNAPSHOT=$(echo "${SNAPSHOT}" | jq --arg component "${SNAPSHOT_CREATION_COMPONENT}" \ + 'del(.components[] | select(.name != $component))') + + ## make sure we still have 1 component + COMPONENT_COUNT=$(echo "$REDUCED_SNAPSHOT" | jq -r '[ .components[] ] | length') + echo "COMPONENT_COUNT: ${COMPONENT_COUNT}" + if [ "${COMPONENT_COUNT}" != "1" ] ; then + echo "Error: Reduced Snapshot has ${COMPONENT_COUNT} components. It should contain 1" + echo " Verify that the Snapshot contains the built component: ${SNAPSHOT_CREATION_COMPONENT}" + exit 1 + fi + + echo "Reducing Snapshot to:" + echo "$REDUCED_SNAPSHOT" | jq . + SNAPSHOT=$(echo "$REDUCED_SNAPSHOT" | tr -d ' ' | tr -d '\n') + echo "$SNAPSHOT" | jq -c . > "${SNAPSHOT_PATH}" + fi +fi From fba5ae73433e7a907b8b58139301f5ce1bd8db4c Mon Sep 17 00:00:00 2001 From: Scott Hebert Date: Tue, 17 Sep 2024 09:39:57 -0400 Subject: [PATCH 2/2] fix saving of snapshot json --- hack/reduce-snapshot.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hack/reduce-snapshot.sh b/hack/reduce-snapshot.sh index c7b46c5d1..0e7242880 100755 --- a/hack/reduce-snapshot.sh +++ b/hack/reduce-snapshot.sh @@ -64,8 +64,6 @@ if [ "${SINGLE_COMPONENT}" == "true" ]; then fi echo "Reducing Snapshot to:" - echo "$REDUCED_SNAPSHOT" | jq . - SNAPSHOT=$(echo "$REDUCED_SNAPSHOT" | tr -d ' ' | tr -d '\n') - echo "$SNAPSHOT" | jq -c . > "${SNAPSHOT_PATH}" + echo "$REDUCED_SNAPSHOT" | jq '.' | tee "${SNAPSHOT_PATH}" fi fi