|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +IFS=$'\n\t' |
| 5 | + |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | +readonly SCRIPT_DIR |
| 8 | + |
| 9 | +# shellcheck source=hack/common.sh |
| 10 | +source "${SCRIPT_DIR}/../common.sh" |
| 11 | + |
| 12 | +if [ -z "${CALICO_VERSION-}" ]; then |
| 13 | + echo "Missing environment variable: CALICO_VERSION" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | +readonly CALICO_CNI_ASSETS_DIR="${GIT_REPO_ROOT}/.local/cni/calico/${CALICO_VERSION}" |
| 17 | +mkdir -p "${CALICO_CNI_ASSETS_DIR}" |
| 18 | + |
| 19 | +curl -fsSL "https://docs.projectcalico.org/archive/${CALICO_VERSION}/manifests/tigera-operator.yaml" \ |
| 20 | + -o "${CALICO_CNI_ASSETS_DIR}/tigera-operator.yaml" |
| 21 | + |
| 22 | +# The operator manifest in YAML format is 1226666 bytes. It turns out that much of that is whitespace. Converting the |
| 23 | +# manifest to JSON without indentation allows us to remove most of the whitespace, reducing the size by more than half, |
| 24 | +# to 527614 bytes. |
| 25 | +# |
| 26 | +# Some important notes: |
| 27 | +# 1. The YAML manifest includes many documents, and the documents must become elements in a JSON array in order for the ClusterResourceController to [parse them](https://github.com/mesosphere/cluster-api//blob/65586de0080a960d085031de87ec627b2d606a6b/exp/addons/internal/controllers/clusterresourceset_helpers.go#L59). We create a JSON array with the --slurp flag. |
| 28 | +# 2. The YAML manifest has some whitespace between YAML document markers (`---`), and these become `null` entries in the JSON array. This causes the ["SortForCreate" subroutine](https://github.com/mesosphere/cluster-api//blob/65586de0080a960d085031de87ec627b2d606a6b/exp/addons/internal/controllers/clusterresourceset_helpers.go#L84) of the ClusterResourceSet controller to misbehave. We remove these null entries using a filter expression. |
| 29 | +# 3. If we indent the JSON document, it is nearly as large as the YAML document, at 1099093 bytes. We remove indentation with the --indent=0 flag. |
| 30 | +gojq --yaml-input --slurp --indent=0 \ |
| 31 | + '[ .[] | select( . != null ) ]' \ |
| 32 | + <"${CALICO_CNI_ASSETS_DIR}/tigera-operator.yaml" \ |
| 33 | + >"${CALICO_CNI_ASSETS_DIR}/tigera-operator.json" |
| 34 | + |
| 35 | +kubectl create configmap tigera-operator --dry-run=client --output yaml \ |
| 36 | + --from-file "${CALICO_CNI_ASSETS_DIR}/tigera-operator.json" \ |
| 37 | + >> "${GIT_REPO_ROOT}/pkg/addons/templates/cni/tigera-operator-configmap.yaml" |
0 commit comments