|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit |
| 4 | +set -o nounset |
| 5 | +set -o pipefail |
| 6 | + |
| 7 | +################################################## |
| 8 | +# Modify these as needed |
| 9 | +################################################## |
| 10 | + |
| 11 | +# This is the namespace where all namespace-scoped resources live |
| 12 | +NAMESPACE=openshift-catalogd |
| 13 | + |
| 14 | +# This is a mapping of deployment container names to image placeholder values. For example, given a deployment with |
| 15 | +# 2 containers named kube-rbac-proxy and manager, their images will be set to ${KUBE_RBAC_PROXY_IMAGE} and |
| 16 | +# ${CATALOGD_IMAGE}, respectively. The cluster-olm-operator will replace these placeholders will real image values. |
| 17 | +declare -A IMAGE_MAPPINGS |
| 18 | +# shellcheck disable=SC2016 |
| 19 | +IMAGE_MAPPINGS[kube-rbac-proxy]='${KUBE_RBAC_PROXY_IMAGE}' |
| 20 | +# shellcheck disable=SC2016 |
| 21 | +IMAGE_MAPPINGS[manager]='${CATALOGD_IMAGE}' |
| 22 | + |
| 23 | +# This is a mapping of catalogd flag names to values. For example, given a deployment with a container |
| 24 | +# named "manager" and arguments: |
| 25 | +# args: |
| 26 | +# - --flagname=one |
| 27 | +# and an entry to the FLAG_MAPPINGS of FLAG_MAPPINGS[flagname]='two', the argument will be updated to: |
| 28 | +# args: |
| 29 | +# - --flagname=two |
| 30 | +# |
| 31 | +# If the flag doesn't already exist - it will be appended to the list. |
| 32 | +declare -A FLAG_MAPPINGS |
| 33 | +# shellcheck disable=SC2016 |
| 34 | +FLAG_MAPPINGS[external-address]="catalogd-service.${NAMESPACE}.svc" |
| 35 | +FLAG_MAPPINGS[global-pull-secret]="openshift-config/pull-secret" |
| 36 | + |
| 37 | +################################################## |
| 38 | +# You shouldn't need to change anything below here |
| 39 | +################################################## |
| 40 | + |
| 41 | +# Know where the repo root is so we can reference things relative to it |
| 42 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 43 | + |
| 44 | +# Source bingo so we can use kustomize and yq |
| 45 | +. "${REPO_ROOT}/openshift/.bingo/variables.env" |
| 46 | + |
| 47 | +# We're going to do file manipulation, so let's work in a temp dir |
| 48 | +TMP_ROOT="$(mktemp -p . -d 2>/dev/null || mktemp -d ./tmpdir.XXXXXXX)" |
| 49 | +# Make sure to delete the temp dir when we exit |
| 50 | +trap 'rm -rf $TMP_ROOT' EXIT |
| 51 | + |
| 52 | +# Copy all kustomize files into a temp dir |
| 53 | +cp -a "${REPO_ROOT}/catalogd/config/" "${TMP_ROOT}/config/" |
| 54 | + |
| 55 | +mkdir -p "${TMP_ROOT}/openshift/catalogd/" |
| 56 | +cp -a "${REPO_ROOT}/openshift/catalogd/kustomize" "${TMP_ROOT}/openshift/catalogd/kustomize" |
| 57 | + |
| 58 | +# Override OPENSHIFT-NAMESPACE to ${NAMESPACE} |
| 59 | +find "${TMP_ROOT}" -name "*.yaml" -exec sed -i'.bak' "s/OPENSHIFT-NAMESPACE/${NAMESPACE}/g" {} \; |
| 60 | +find "${TMP_ROOT}" -name "*.bak" -exec rm {} \; |
| 61 | + |
| 62 | +# Create a temp dir for manifests |
| 63 | +TMP_MANIFEST_DIR="${TMP_ROOT}/manifests" |
| 64 | +mkdir -p "$TMP_MANIFEST_DIR" |
| 65 | + |
| 66 | +# Run kustomize, which emits a single yaml file |
| 67 | +TMP_KUSTOMIZE_OUTPUT="${TMP_MANIFEST_DIR}/temp.yaml" |
| 68 | +$KUSTOMIZE build "${TMP_ROOT}/openshift/catalogd/kustomize/overlays/openshift" -o "$TMP_KUSTOMIZE_OUTPUT" |
| 69 | + |
| 70 | +for container_name in "${!IMAGE_MAPPINGS[@]}"; do |
| 71 | + placeholder="${IMAGE_MAPPINGS[$container_name]}" |
| 72 | + $YQ -i "(select(.kind == \"Deployment\")|.spec.template.spec.containers[]|select(.name==\"$container_name\")|.image) = \"$placeholder\"" "$TMP_KUSTOMIZE_OUTPUT" |
| 73 | + $YQ -i 'select(.kind == "Deployment").spec.template.metadata.annotations += {"target.workload.openshift.io/management": "{\"effect\": \"PreferredDuringScheduling\"}"}' "$TMP_KUSTOMIZE_OUTPUT" |
| 74 | + $YQ -i 'select(.kind == "Deployment").spec.template.metadata.annotations += {"openshift.io/required-scc": "privileged"}' "$TMP_KUSTOMIZE_OUTPUT" |
| 75 | + $YQ -i 'select(.kind == "Deployment").spec.template.spec += {"priorityClassName": "system-cluster-critical"}' "$TMP_KUSTOMIZE_OUTPUT" |
| 76 | + $YQ -i 'select(.kind == "Namespace").metadata.annotations += {"workload.openshift.io/allowed": "management"}' "$TMP_KUSTOMIZE_OUTPUT" |
| 77 | +done |
| 78 | + |
| 79 | +# Loop through any flag updates that need to be made to the manager container |
| 80 | +for flag_name in "${!FLAG_MAPPINGS[@]}"; do |
| 81 | + flagval="${FLAG_MAPPINGS[$flag_name]}" |
| 82 | + |
| 83 | + # First, update the flag if it exists |
| 84 | + $YQ -i "(select(.kind == \"Deployment\") | .spec.template.spec.containers[] | select(.name == \"manager\") | .args[] | select(. | contains(\"--$flag_name=\")) | .) = \"--$flag_name=$flagval\"" "$TMP_KUSTOMIZE_OUTPUT" |
| 85 | + |
| 86 | + # Then, append the flag if it doesn't exist |
| 87 | + $YQ -i "(select(.kind == \"Deployment\") | .spec.template.spec.containers[] | select(.name == \"manager\") | .args) |= (select(.[] | contains(\"--$flag_name=\")) | .) // . + [\"--$flag_name=$flagval\"]" "$TMP_KUSTOMIZE_OUTPUT" |
| 88 | +done |
| 89 | + |
| 90 | +# Use yq to split the single yaml file into 1 per document. |
| 91 | +# Naming convention: $index-$kind-$namespace-$name. If $namespace is empty, just use the empty string. |
| 92 | +( |
| 93 | + cd "$TMP_MANIFEST_DIR" |
| 94 | + |
| 95 | + # shellcheck disable=SC2016 |
| 96 | + ${YQ} -s '$index +"-"+ (.kind|downcase) +"-"+ (.metadata.namespace // "") +"-"+ .metadata.name' temp.yaml |
| 97 | +) |
| 98 | + |
| 99 | +# Delete the single yaml file |
| 100 | +rm "$TMP_KUSTOMIZE_OUTPUT" |
| 101 | + |
| 102 | +# Delete and recreate the actual manifests directory |
| 103 | +MANIFEST_DIR="${REPO_ROOT}/openshift/catalogd/manifests" |
| 104 | +rm -rf "${MANIFEST_DIR}" |
| 105 | +mkdir -p "${MANIFEST_DIR}" |
| 106 | + |
| 107 | +# Copy everything we just generated and split into the actual manifests directory |
| 108 | +cp "$TMP_MANIFEST_DIR"/* "$MANIFEST_DIR"/ |
| 109 | + |
| 110 | +# Update file names to be in the format nn-$kind-$namespace-$name |
| 111 | +( |
| 112 | + cd "$MANIFEST_DIR" |
| 113 | + |
| 114 | + for f in *; do |
| 115 | + # Get the numeric prefix from the filename |
| 116 | + index=$(echo "$f" | cut -d '-' -f 1) |
| 117 | + # Keep track of the full file name without the leading number and dash |
| 118 | + name_without_index=${f#$index-} |
| 119 | + # Fix the double dash in cluster-scoped names |
| 120 | + name_without_index=${name_without_index//--/-} |
| 121 | + # Reformat the name so the leading number is always padded to 2 digits |
| 122 | + new_name=$(printf "%02d" "$index")-$name_without_index |
| 123 | + # Some file names (namely CRDs) don't end in .yml - make them |
| 124 | + if ! [[ "$new_name" =~ yml$ ]]; then |
| 125 | + new_name="${new_name}".yml |
| 126 | + fi |
| 127 | + if [[ "$f" != "$new_name" ]]; then |
| 128 | + # Rename |
| 129 | + mv "$f" "${new_name}" |
| 130 | + fi |
| 131 | + done |
| 132 | +) |
0 commit comments