|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script downloads the Flux OpenAPI schemas, then it validates the |
| 4 | +# Flux custom resources and the kustomize overlays using kubeval. |
| 5 | +# This script is meant to be run locally and in CI before the changes |
| 6 | +# are merged on the main branch that's synced by Flux. |
| 7 | + |
| 8 | +# Copyright 2020 The Flux authors. All rights reserved. |
| 9 | +# |
| 10 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +# you may not use this file except in compliance with the License. |
| 12 | +# You may obtain a copy of the License at |
| 13 | +# |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# |
| 16 | +# Unless required by applicable law or agreed to in writing, software |
| 17 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# See the License for the specific language governing permissions and |
| 20 | +# limitations under the License. |
| 21 | + |
| 22 | +# This script is meant to be run locally and in CI to validate the Kubernetes |
| 23 | +# manifests (including Flux custom resources) before changes are merged into |
| 24 | +# the branch synced by Flux in-cluster. |
| 25 | + |
| 26 | +# Prerequisites |
| 27 | +# - yq v4.6 |
| 28 | +# - kustomize v4.1 |
| 29 | +# - kubeval v0.15.x |
| 30 | + |
| 31 | +set -o errexit |
| 32 | + |
| 33 | +echo "INFO - Downloading Flux OpenAPI schemas" |
| 34 | +mkdir -p /tmp/flux-crd-schemas/master-standalone-strict |
| 35 | +#curl -sL https://github.com/fluxcd/flux2/releases/latest/download/crd-schemas.tar.gz | tar zxf - -C /tmp/flux-crd-schemas/master-standalone-strict |
| 36 | + |
| 37 | +# mirror kustomize-controller build options |
| 38 | +kustomize_flags="--load-restrictor=LoadRestrictionsNone --reorder=legacy" |
| 39 | +kustomize_config="kustomization.yaml" |
| 40 | + |
| 41 | + |
| 42 | +# find . -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; |
| 43 | +# do |
| 44 | +# echo "INFO - Validating $file" |
| 45 | +# yq -e 'true' "$file" > /dev/null |
| 46 | +# done |
| 47 | + |
| 48 | +echo "INFO - Validating clusters" |
| 49 | +find ./k8s/clusters -type f -name '*.yaml' -maxdepth 1 -print0 | while IFS= read -r -d $'\0' file; |
| 50 | + do |
| 51 | + kubeval "${file}" --strict --ignore-missing-schemas --additional-schema-locations=file:///tmp/flux-crd-schemas |
| 52 | + if [[ ${PIPESTATUS[0]} != 0 ]]; then |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +done |
| 56 | + |
| 57 | +echo "INFO - Validating kustomize overlays" |
| 58 | +find . -type f -name $kustomize_config -print0 | while IFS= read -r -d $'\0' file; |
| 59 | + do |
| 60 | + echo "INFO - Validating kustomization ${file/%$kustomize_config}" |
| 61 | + # Secrets are ignored with --skip-kinds due to using SOPS with FluxCD |
| 62 | + # shellcheck disable=SC2086 |
| 63 | + kustomize build "${file/%$kustomize_config}" $kustomize_flags | kubeval --ignore-missing-schemas --strict --additional-schema-locations=file:///tmp/flux-crd-schemas --skip-kinds Secret |
| 64 | + if [[ ${PIPESTATUS[0]} != 0 ]]; then |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +done |
0 commit comments