This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit fa8253061577f0393d6337266d93d6cbbf4d155f Author: phantomjinx <[email protected]> AuthorDate: Fri Jan 7 19:29:44 2022 +0000 Create pre-flight action and adds to cleanup action * Pre-flight * Adds action to execute a pre-flight test to ensure the kamel operator is the correct version as that built by the workflow * Cleanup * Adds function to clean up any image streams left around by pushing images to exposed cluster registries --- .github/actions/e2e-kubernetes/action.yml | 12 ++ .github/actions/kamel-cleanup/action.yaml | 6 +- .github/actions/kamel-cleanup/cleanup.sh | 19 ++- .github/actions/kamel-preflight-test/action.yml | 55 +++++++ .../actions/kamel-preflight-test/preflight-test.sh | 180 +++++++++++++++++++++ 5 files changed, 270 insertions(+), 2 deletions(-) diff --git a/.github/actions/e2e-kubernetes/action.yml b/.github/actions/e2e-kubernetes/action.yml index 2d004dc..ad5f6f5 100644 --- a/.github/actions/e2e-kubernetes/action.yml +++ b/.github/actions/e2e-kubernetes/action.yml @@ -60,6 +60,17 @@ runs: with: kube-admin-user-ctx: ${{ steps.config-cluster.outputs.cluster-kube-admin-user-ctx }} + - id: preflight-test + name: Preflight Check Test + uses: ./.github/actions/kamel-preflight-test + with: + build-catalog-source: ${{ steps.build-kamel.outputs.build-bundle-catalog-source-name }} + image-namespace: ${{ steps.config-cluster.outputs.cluster-image-namespace }} + image-registry-host: ${{ steps.config-cluster.outputs.cluster-image-registry-pull-host }} + image-name: ${{ steps.build-kamel.outputs.build-binary-local-image-name }} + image-registry-insecure: ${{steps.config-cluster.outputs.cluster-image-registry-insecure }} + image-version: ${{ steps.build-kamel.outputs.build-binary-local-image-version }} + - id: run-it name: Run IT shell: bash @@ -78,3 +89,4 @@ runs: if: ${{ always() }} with: build-bundle-catalog-source: ${{ steps.build-kamel.outputs.build-bundle-catalog-source-name }} + image-namespace: ${{ steps.config-cluster.outputs.cluster-image-namespace }} diff --git a/.github/actions/kamel-cleanup/action.yaml b/.github/actions/kamel-cleanup/action.yaml index 2f970a5..cace15a 100644 --- a/.github/actions/kamel-cleanup/action.yaml +++ b/.github/actions/kamel-cleanup/action.yaml @@ -22,6 +22,9 @@ inputs: build-bundle-catalog-source: description: "Name of the catalog source for the build bundle image" required: true + image-namespace: + description: "Installed location of the images if resident on the cluster (only applies to clusters with partnered registries)" + required: false runs: using: "composite" @@ -32,4 +35,5 @@ runs: if: ${{ always() }} run: | ./.github/actions/kamel-cleanup/cleanup.sh \ - -c "${{ inputs.build-bundle-catalog-source-name }}" + -c "${{ inputs.build-bundle-catalog-source }}" \ + -i "${{ inputs.image-namespace }}" diff --git a/.github/actions/kamel-cleanup/cleanup.sh b/.github/actions/kamel-cleanup/cleanup.sh index 03875dc..3a5b4aa 100755 --- a/.github/actions/kamel-cleanup/cleanup.sh +++ b/.github/actions/kamel-cleanup/cleanup.sh @@ -25,11 +25,14 @@ set -e -while getopts ":c:" opt; do +while getopts ":c:i:" opt; do case "${opt}" in c) BUILD_CATALOG_SOURCE=${OPTARG} ;; + i) + IMAGE_NAMESPACE=${OPTARG} + ;; :) echo "ERROR: Option -$OPTARG requires an argument" exit 1 @@ -55,6 +58,19 @@ fi kubectl get crds | grep camel | awk '{print $1}' | xargs kubectl delete crd &> /dev/null set -e +if [ -n "${IMAGE_NAMESPACE}" ]; then + imgstreams="camel-k camel-k-bundle camel-k-iib" + set +e + for cis in ${imgstreams} + do + if kubectl get is ${cis} -n ${IMAGE_NAMESPACE} &> /dev/null + then + kubectl delete is ${cis} -n ${IMAGE_NAMESPACE} + fi + done + set -e +fi + # # Remove Catalog Source # @@ -63,6 +79,7 @@ if [ -z "${BUILD_CATALOG_SOURCE}" ]; then exit 0 fi + set +e CATALOG_NS=$(kubectl get catalogsource --all-namespaces | grep ${BUILD_CATALOG_SOURCE} | awk {'print $1'}) for ns in ${CATALOG_NS} diff --git a/.github/actions/kamel-preflight-test/action.yml b/.github/actions/kamel-preflight-test/action.yml new file mode 100644 index 0000000..5cf1041 --- /dev/null +++ b/.github/actions/kamel-preflight-test/action.yml @@ -0,0 +1,55 @@ +# --------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# --------------------------------------------------------------------------- + +name: kamel-preflight-test +description: 'Does a preflight-test to check the operator can be installed correctly' + +inputs: + build-catalog-source: + description: "The name of the bundle catalog (only installed in the cluster if OLM is used)" + required: false + image-namespace: + description: 'Namespace in which the image is stored' + required: true + image-registry-host: + description: 'Location of image registry host' + required: true + image-name: + description: 'Reference of the camel-k image' + required: true + image-registry-insecure: + description: "Whether the registry is insecure" + required: true + image-version: + description: "Reference of the camel-k image version" + required: true + +runs: + using: "composite" + + steps: + - id: preflight-test + name: Execute Preflight Test + shell: bash + run: | + ./.github/actions/kamel-preflight-test/preflight-test.sh \ + -c "${{ inputs.build-catalog-source }}" \ + -i "${{ inputs.image-namespace }}" \ + -l "${{ inputs.image-registry-host }}" \ + -n "${{ inputs.image-name }}" \ + -s "${{ inputs.image-registry-insecure }}" \ + -v "${{ inputs.image-version }}" diff --git a/.github/actions/kamel-preflight-test/preflight-test.sh b/.github/actions/kamel-preflight-test/preflight-test.sh new file mode 100755 index 0000000..4061d30 --- /dev/null +++ b/.github/actions/kamel-preflight-test/preflight-test.sh @@ -0,0 +1,180 @@ +#!/bin/bash + +# --------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# --------------------------------------------------------------------------- + +#### +# +# Execute the kubernetes tests +# +#### + +set -e + +while getopts ":c:i:l:n:s:v:" opt; do + case "${opt}" in + c) + BUILD_CATALOG_SOURCE=${OPTARG} + ;; + i) + IMAGE_NAMESPACE=${OPTARG} + ;; + l) + REGISTRY_PULL_HOST=${OPTARG} + ;; + n) + IMAGE_NAME=${OPTARG} + ;; + s) + REGISTRY_INSECURE=${OPTARG} + ;; + v) + IMAGE_VERSION=${OPTARG} + ;; + :) + echo "ERROR: Option -$OPTARG requires an argument" + exit 1 + ;; + \?) + echo "ERROR: Invalid option -$OPTARG" + exit 1 + ;; + esac +done +shift $((OPTIND-1)) + +if [ -z "${IMAGE_NAME}" ]; then + echo "Error: local-image-name not defined" + exit 1 +fi + +if [ -z "${IMAGE_VERSION}" ]; then + echo "Error: local-image-version not defined" + exit 1 +fi + +if [ -z "${IMAGE_NAMESPACE}" ]; then + echo "Error: image-namespace not defined" + exit 1 +fi + +if [ -z "${REGISTRY_PULL_HOST}" ]; then + echo "Error: image-registry-pull-host not defined" + exit 1 +fi + +if [ -z "${REGISTRY_INSECURE}" ]; then + echo "Error: image-registry-insecure not defined" + exit 1 +fi + +# +# Create the preflight test namespace +# +NAMESPACE="preflight" +set +e +if kubectl get ns ${NAMESPACE} &> /dev/null +then + kubectl delete ns ${NAMESPACE} +fi +set -e + +kubectl create namespace ${NAMESPACE} +if [ $? != 0 ]; then + echo "Error: failed to create the ${NAMESPACE} namespace" + exit 1 +fi + +#trap "kubectl delete ns ${NAMESPACE} &> /dev/null" EXIT + +# Cluster environment +export CUSTOM_IMAGE=${IMAGE_NAME} +export CUSTOM_VERSION=${IMAGE_VERSION} + +# +# If bundle has been built and installed then use it +# +has_olm="false" +if [ -n "${BUILD_CATALOG_SOURCE}" ]; then + export KAMEL_INSTALL_OLM_SOURCE_NAMESPACE=${IMAGE_NAMESPACE} + export KAMEL_INSTALL_OLM_SOURCE=${BUILD_CATALOG_SOURCE} + has_olm="true" +fi + +export KAMEL_INSTALL_MAVEN_REPOSITORIES=$(make get-staging-repo) +export KAMEL_INSTALL_REGISTRY=${REGISTRY_PULL_HOST} +export KAMEL_INSTALL_REGISTRY_INSECURE=${REGISTRY_INSECURE} +export KAMEL_INSTALL_OPERATOR_IMAGE=${CUSTOM_IMAGE}:${CUSTOM_VERSION} + +# Will only have an effect if olm=false +# since, for OLM, the csv determine the policy +export KAMEL_INSTALL_OPERATOR_IMAGE_PULL_POLICY="Always" + +# +# Install the operator +# +kamel install -n ${NAMESPACE} --olm=${has_olm} +if [ $? != 0 ]; then + echo "Error: kamel install returned an error." + exit 1 +fi + +sleep 3 + +# +# Wait for the operator to be running +# +timeout=180 +i=1 +command="kubectl get pods -n ${NAMESPACE} 2> /dev/null | grep camel-k | grep Running &> /dev/null" + +until eval "${command}" +do + ((i++)) + if [ "${i}" -gt "${timeout}" ]; then + echo "kamel operator not successfully installed, aborting due to ${timeout}s timeout" + exit 1 + fi + + sleep 1 +done + +echo "Camel-K operator up and running" + +camel_operator=$(kubectl get pods -n ${NAMESPACE} | grep camel-k | awk '{print $1}') +camel_op_version=$(kubectl logs ${camel_operator} -n ${NAMESPACE} | sed -n 's/.*"Camel K Operator Version: \(.*\)"}/\1/p') +camel_op_commit=$(kubectl logs ${camel_operator} -n ${NAMESPACE} | sed -n 's/.*"Camel K Git Commit: \(.*\)"}/\1/p') + +src_commit=$(git rev-parse HEAD) + +# +# Test whether the versions are the same +# +if [ "${camel_op_version}" != "${IMAGE_VERSION}" ]; then + echo "Preflight Test: Failure - Installed operator version (${camel_op_version} does not match expected version (${IMAGE_VERSION})" + exit 1 +fi + +# +# Test whether the commit ids are the same +# +if [ "${camel_op_commit}" != "${src_commit}" ]; then + echo "Preflight Test: Failure - Installed operator commit id (${camel_op_commit}) does not match expected commit id (${src_commit})" + exit 1 +fi + +echo "Preflight Test: Success"
