|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +# Test runner using KUTTL against a target cluster. |
| 20 | +# https://kuttl.dev |
| 21 | +# https://kind.sigs.k8s.io |
| 22 | +# |
| 23 | +# Materialize a kustomize directory for a kuttl test. |
| 24 | +# |
| 25 | +# Kustomize is clunky for automated testing. It's pretty opinionated in that it will only evaluate |
| 26 | +# a directory off of disk -- you cannot generate a kustomization and pass it in via stdin. |
| 27 | +# In order to use kuttl generated namespaces within the kustomization, we have to modify the |
| 28 | +# kustomization.yaml before applying it. If we modify that file in the source tree, we end up with |
| 29 | +# the test namespace appended to the file under source control. So, this script creates a temp |
| 30 | +# directory, copies all the resources into that directory, and modifies the kustomization.yaml as |
| 31 | +# necessary. It then runs `kubectl apply -k` against that temporary directory. |
| 32 | +# |
| 33 | + |
| 34 | +declare DEBUG="${DEBUG:false}" |
| 35 | +if [ "${DEBUG}" = 'true' ] ; then |
| 36 | + set -x |
| 37 | +fi |
| 38 | + |
| 39 | +set -eou pipefail |
| 40 | + |
| 41 | +declare NAMESPACE |
| 42 | +declare NEW_RESOURCES='[]' |
| 43 | +declare NEW_COMPONENTS='[]' |
| 44 | +declare kustomize_dir |
| 45 | +declare -a rewritten_resources=() |
| 46 | +declare -a rewritten_components=() |
| 47 | + |
| 48 | +kustomize_dir="$(mktemp -d -p /tmp "${NAMESPACE}.XXXXXXXXXX")" |
| 49 | +trap '[ -d "${kustomize_dir}" ] && rm -rf "${kustomize_dir}"' EXIT |
| 50 | + |
| 51 | +cp -r ./* "${kustomize_dir}/" |
| 52 | + |
| 53 | +for r in $(yq '.resources[]' kustomization.yaml) ; do |
| 54 | + if [[ "${r}" =~ ^\.\./.* ]] ; then |
| 55 | + # resolve the new relative location for any resource path that is not in the local directory |
| 56 | + canonized="$(cd "${r}" ; pwd)" |
| 57 | + r="../..${canonized}" |
| 58 | + fi |
| 59 | + rewritten_resources+=("'${r}'") |
| 60 | +done |
| 61 | +if [ "${#rewritten_resources[@]}" -gt 0 ] ; then |
| 62 | + NEW_RESOURCES="[ $(printf '%s,' "${rewritten_resources[@]}") ]" |
| 63 | +fi |
| 64 | + |
| 65 | +for r in $(yq '.components[]' kustomization.yaml) ; do |
| 66 | + if [[ "${r}" =~ ^\.\./.* ]] ; then |
| 67 | + # resolve the new relative location for any resource path that is not in the local directory |
| 68 | + canonized="$(cd "${r}" ; pwd)" |
| 69 | + r="../..${canonized}" |
| 70 | + fi |
| 71 | + rewritten_components+=("'${r}'") |
| 72 | +done |
| 73 | +if [ "${#rewritten_components[@]}" -gt 0 ] ; then |
| 74 | + NEW_COMPONENTS="[ $(printf '%s,' "${rewritten_components[@]}") ]" |
| 75 | +fi |
| 76 | + |
| 77 | +env NAMESPACE="${NAMESPACE}" \ |
| 78 | + NEW_RESOURCES="${NEW_RESOURCES}" \ |
| 79 | + NEW_COMPONENTS="${NEW_COMPONENTS}" \ |
| 80 | + yq -i ' |
| 81 | + .namespace = strenv(NAMESPACE) | |
| 82 | + .resources = env(NEW_RESOURCES) | |
| 83 | + .components = env(NEW_COMPONENTS) |
| 84 | +' "${kustomize_dir}/kustomization.yaml" |
| 85 | + |
| 86 | +if [ "${DEBUG}" = 'true' ] ; then |
| 87 | + cat "${kustomize_dir}/kustomization.yaml" |
| 88 | +fi |
| 89 | + |
| 90 | +kubectl apply -k "${kustomize_dir}" |
0 commit comments