|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script serves to validate our kustomize files/manifests with respect of the given kustomize version. |
| 4 | +# We use this to verify that there is no warning and not errors |
| 5 | +# |
| 6 | +# Local execution: [KUSTOMIZE_VERSION=5.3.1] ./ci/kustomize.sh |
| 7 | +# Note: please execute from the root directory so that whole dir tree is checked |
| 8 | +# |
| 9 | +# In case of the PR on GitHub, this check is tied to GitHub actions automatically, |
| 10 | +# see `.github/workflows` directory. |
| 11 | + |
| 12 | + |
| 13 | +# The default kustomize version that is determined based on what is currently used in the [rhods|opendatahub]-operator in runtime: |
| 14 | +# https://github.com/red-hat-data-services/rhods-operator/blob/7ccc405135f99c014982d7e297b8949e970dd750/go.mod#L28-L29 |
| 15 | +# and then to match appropriate kustomize release https://github.com/kubernetes-sigs/kustomize/releases/tag/kustomize%2Fv5.0.3 |
| 16 | +DEFAULT_KUSTOMIZE_VERSION=5.0.3 |
| 17 | +# The latest kustomize version we want to check with to be sure we're prepared for the future |
| 18 | +THE_LATEST_KUSTOMIZE=5.6.0 |
| 19 | + |
| 20 | +KUSTOMIZE_VERSION="${KUSTOMIZE_VERSION:-$DEFAULT_KUSTOMIZE_VERSION}" |
| 21 | + |
| 22 | +function download_kustomize() { |
| 23 | + local tmp_dir="${1}" |
| 24 | + local kustomize_version="${2}" |
| 25 | + |
| 26 | + local kustomize_tar="${tmp_dir}/kustomize-${kustomize_version}.tar.gz" |
| 27 | + local kustomize_bin="${tmp_dir}/kustomize-${kustomize_version}" |
| 28 | + |
| 29 | + echo "---------------------------------------------------------------------------------" |
| 30 | + echo "Download kustomize '${kustomize_version}'" |
| 31 | + echo "---------------------------------------------------------------------------------" |
| 32 | + |
| 33 | + # Detect OS |
| 34 | + local uname_out |
| 35 | + uname_out="$(uname -s)" |
| 36 | + case "${uname_out}" in |
| 37 | + Linux*) os=linux;; |
| 38 | + Darwin*) os=darwin;; |
| 39 | + *) echo "Unsupported OS: ${uname_out}" && return 1;; |
| 40 | + esac |
| 41 | + |
| 42 | + # Detect architecture |
| 43 | + local arch |
| 44 | + arch="$(uname -m)" |
| 45 | + case "${arch}" in |
| 46 | + x86_64) arch=amd64;; |
| 47 | + arm64) arch=arm64;; |
| 48 | + aarch64) arch=arm64;; |
| 49 | + *) echo "Unsupported architecture: ${arch}" && return 1;; |
| 50 | + esac |
| 51 | + |
| 52 | + local download_url="https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/v${kustomize_version}/kustomize_v${kustomize_version}_${os}_${arch}.tar.gz" |
| 53 | + echo "Downloading from: ${download_url}" |
| 54 | + |
| 55 | + wget --output-document="${kustomize_tar}" "${download_url}" |
| 56 | + tar -C "${tmp_dir}" -xvf "${kustomize_tar}" |
| 57 | + mv "${tmp_dir}/kustomize" "${kustomize_bin}" |
| 58 | + |
| 59 | + "${kustomize_bin}" version |
| 60 | +} |
| 61 | + |
| 62 | +function execute_kustomize() { |
| 63 | + local tmp_dir="${1}" |
| 64 | + local kustomize_version="${2}" |
| 65 | + |
| 66 | + local kustomize_stdout="${tmp_dir}/kustomize-${kustomize_version}-stdout.yaml" |
| 67 | + local kustomize_stderr="${tmp_dir}/kustomize-${kustomize_version}-stderr.txt" |
| 68 | + local kustomize_bin="${tmp_dir}/kustomize-${kustomize_version}" |
| 69 | + |
| 70 | + echo "---------------------------------------------------------------------------------------------------" |
| 71 | + echo "Starting to run kustomize '${kustomize_version}' for each kustomization.yaml file except components" |
| 72 | + echo "---------------------------------------------------------------------------------------------------" |
| 73 | + # We don't want to execute kustomization on the components part as it's not intended to be used that way. |
| 74 | + # This first run is for the actual execution to get the generated output and eventual errors/warnings. |
| 75 | + # find . -name "kustomization.yaml" | xargs dirname | grep -v "components" | xargs -I {} "${kustomize_bin}" build {} >"${kustomize_stdout}" 2>"${kustomize_stderr}" |
| 76 | + find . -name "kustomization.yaml" | xargs dirname | xargs -I {} "${kustomize_bin}" build {} >"${kustomize_stdout}" 2>"${kustomize_stderr}" |
| 77 | + # This second run is with verbose output to see eventual errors/warnings together with which command they are present for easier debugging. |
| 78 | + # find . -name "kustomization.yaml" | xargs dirname | grep -v "components" | xargs --verbose -I {} "${kustomize_bin}" build {} >/dev/null |
| 79 | + find . -name "kustomization.yaml" | xargs dirname | xargs --verbose -I {} "${kustomize_bin}" build {} >/dev/null |
| 80 | + |
| 81 | + echo "Let's print the STDERR:" |
| 82 | + cat "${kustomize_stderr}" |
| 83 | +} |
| 84 | + |
| 85 | +function check_the_results() { |
| 86 | + local tmp_dir="${1}" |
| 87 | + local kustomize_version_1="${2}" |
| 88 | + local kustomize_version_2="${3}" |
| 89 | + |
| 90 | + local kustomize_stdout_1="${tmp_dir}/kustomize-${kustomize_version_1}-stdout.yaml" |
| 91 | + local kustomize_stderr_1="${tmp_dir}/kustomize-${kustomize_version_1}-stderr.txt" |
| 92 | + local kustomize_stdout_2="${tmp_dir}/kustomize-${kustomize_version_2}-stdout.yaml" |
| 93 | + local kustomize_stderr_2="${tmp_dir}/kustomize-${kustomize_version_2}-stderr.txt" |
| 94 | + |
| 95 | + echo "---------------------------------------------------------------------------------" |
| 96 | + echo "Checking the generated outputs - should be identical:" |
| 97 | + echo " - ${kustomize_stdout_1}" |
| 98 | + echo " - ${kustomize_stdout_2}" |
| 99 | + echo "---------------------------------------------------------------------------------" |
| 100 | + diff -u "${kustomize_stdout_1}" "${kustomize_stdout_2}" || { |
| 101 | + echo "Generated files from kustomize differs between kustomize version ${kustomize_version_1} and ${kustomize_version_2}. Please check above!" |
| 102 | + return 1 |
| 103 | + } |
| 104 | + |
| 105 | + echo "---------------------------------------------------------------------------------" |
| 106 | + echo "No log in STDERR outputs should be printed:" |
| 107 | + echo " - ${kustomize_stderr_1}" |
| 108 | + echo " - ${kustomize_stderr_2}" |
| 109 | + echo "---------------------------------------------------------------------------------" |
| 110 | + if [ -s "${kustomize_stderr_1}" ] || [ -s "${kustomize_stderr_2}" ]; then |
| 111 | + echo "There were some logs generated to STDERR during the kustomize build. Please check the log above!" |
| 112 | + return 1 |
| 113 | + fi |
| 114 | +} |
| 115 | + |
| 116 | +function run_check() { |
| 117 | + local tmp_dir="${1}" |
| 118 | + local kustomize_version="${2}" |
| 119 | + |
| 120 | + download_kustomize "${tmp_dir}" "${kustomize_version}" || return 1 |
| 121 | + execute_kustomize "${tmp_dir}" "${kustomize_version}" || return 1 |
| 122 | +} |
| 123 | + |
| 124 | +function main() { |
| 125 | + local ret_code=0 |
| 126 | + |
| 127 | + local tmp_dir |
| 128 | + tmp_dir=$(mktemp --directory -t kustomize-XXXXXXXXXX) |
| 129 | + echo "Running in the following temporary directory: '${tmp_dir}'" |
| 130 | + |
| 131 | + run_check "${tmp_dir}" "${KUSTOMIZE_VERSION}" || return 1 |
| 132 | + run_check "${tmp_dir}" "${THE_LATEST_KUSTOMIZE}" || return 1 |
| 133 | + |
| 134 | + # -------------------------------------------------------------------------------------- |
| 135 | + |
| 136 | + check_the_results "${tmp_dir}" "${KUSTOMIZE_VERSION}" "${THE_LATEST_KUSTOMIZE}" || return 1 |
| 137 | + |
| 138 | + exit "${ret_code}" |
| 139 | +} |
| 140 | + |
| 141 | +# allows sourcing the script into interactive session without executing it |
| 142 | +if [[ "${0}" == "${BASH_SOURCE[0]}" ]]; then |
| 143 | + main "$@" |
| 144 | +fi |
0 commit comments