| garciadeblas | 8d8cd99 | 2024-05-21 16:04:14 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | set -e -o pipefail |
| 19 | |
| 20 | export HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")") |
| 21 | source "${HERE}/library/functions.sh" |
| 22 | source "${HERE}/library/trap.sh" |
| 23 | |
| 24 | |
| 25 | # Input values |
| 26 | export PROJECT_DIR="$1" |
| 27 | export PROFILE_NAME="$2" |
| 28 | export PUBLIC_KEY="$3" |
| 29 | |
| 30 | |
| 31 | # Helper functions to clone secret from one namespace to other |
| 32 | function clone_secret_to_new_ns_stdout() { |
| 33 | local SECRET_NAME="$1" |
| 34 | local SOURCE_NS="$2" |
| 35 | local DESTINATION_NS="$3" |
| 36 | |
| 37 | kubectl get secret "${SECRET_NAME}" -n "${SOURCE_NS}" -o yaml | \ |
| 38 | yq 'del(.metadata.uid) | del(.metadata.resourceVersion) | del(.metadata.creationTimestamp)' | \ |
| 39 | yq ".metadata.namespace = \"${DESTINATION_NS}\"" |
| 40 | } |
| 41 | |
| 42 | # Helper function to encrypt secrets from stdin |
| 43 | function encrypt_secret_from_stdin() { |
| 44 | local PUBLIC_KEY="$1" |
| 45 | |
| 46 | # Save secret manifest to temporary file |
| 47 | local TMPFILE=$(mktemp /tmp/secret.XXXXXXXXXX.yaml) || exit 1 |
| 48 | cat > "${TMPFILE}" |
| 49 | |
| 50 | # Encrypt |
| 51 | sops \ |
| 52 | --age=${PUBLIC_KEY} \ |
| 53 | --encrypt \ |
| 54 | --encrypted-regex '^(data|stringData)$' \ |
| 55 | --in-place "${TMPFILE}" |
| 56 | |
| 57 | # Outputs the result and removes the temporary file |
| 58 | cat "${TMPFILE}" && rm -f "${TMPFILE}" |
| 59 | } |
| 60 | |
| 61 | |
| 62 | # Reference folder for addon configs |
| 63 | export ADDON_CONFIG_DIR="${PROJECT_DIR}/infra-config-profiles/${PROFILE_NAME}" |
| 64 | |
| 65 | # KSU folder for workflows |
| 66 | export RESOURCES_DIR="${ADDON_CONFIG_DIR}/osm-workflows" |
| 67 | mkdir -p "${RESOURCES_DIR}" |
| 68 | |
| 69 | # Create namespace for OSM workflows |
| 70 | WORKFLOWS_NS=osm-workflows |
| 71 | kubectl create ns ${WORKFLOWS_NS} \ |
| 72 | -o yaml \ |
| 73 | --dry-run=client \ |
| 74 | > "${RESOURCES_DIR}/namespace.yaml" |
| 75 | |
| 76 | # Copy secrets for Git repos from `flux-system` to `osm-workflows` namespace |
| 77 | clone_secret_to_new_ns_stdout \ |
| 78 | fleet-repo \ |
| 79 | flux-system \ |
| 80 | "${WORKFLOWS_NS}" | \ |
| 81 | encrypt_secret_from_stdin \ |
| 82 | "${PUBLIC_KEY}" \ |
| 83 | > "${RESOURCES_DIR}/secret-fleet-repo.yaml" |
| 84 | |
| 85 | clone_secret_to_new_ns_stdout \ |
| 86 | sw-catalogs \ |
| 87 | flux-system \ |
| 88 | "${WORKFLOWS_NS}" | \ |
| 89 | encrypt_secret_from_stdin \ |
| 90 | "${PUBLIC_KEY}" \ |
| 91 | > "${RESOURCES_DIR}/secret-sw-catalogs.yaml" |
| 92 | |
| 93 | # Add appropriate configurations and workflow templates for Argo WorkFlows into the namespace |
| 94 | PACKAGE="${SW_CATALOGS_REPO_DIR}/infra-configs/osm-workflows" |
| 95 | cp -r "${PACKAGE}/templates"/* "${RESOURCES_DIR}/" |