validate.sh (2544B)
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 kubeconform. 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 2023 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 # Prerequisites 23 # - yq v4.34 24 # - kustomize v5.0 25 # - kubeconform v0.6 26 27 set -o errexit 28 set -o pipefail 29 30 # mirror kustomize-controller build options 31 kustomize_flags=("--load-restrictor=LoadRestrictionsNone") 32 kustomize_config="kustomization.yaml" 33 34 # skip Kubernetes Secrets due to SOPS fields failing validation 35 kubeconform_flags=("-skip=Secret -ignore-filename-pattern=./apps/production/secrets/*.yaml") 36 kubeconform_config=("-ignore-missing-schemas" "-schema-location" "default" "-schema-location" "/tmp/flux-crd-schemas" "-verbose") 37 38 echo "INFO - Downloading Flux OpenAPI schemas" 39 mkdir -p /tmp/flux-crd-schemas/master-standalone-strict 40 curl -sL https://github.com/fluxcd/flux2/releases/latest/download/crd-schemas.tar.gz | tar zxf - -C /tmp/flux-crd-schemas/master-standalone-strict 41 42 find . -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do 43 echo "INFO - Validating $file" 44 yq e 'true' "$file" >/dev/null 45 done 46 47 echo "INFO - Validating clusters" 48 find ./clusters -maxdepth 2 -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do 49 kubeconform "${kubeconform_flags[@]}" "${kubeconform_config[@]}" "${file}" || echo "WARN - Validation failed for ${file}, continuing..." 50 done 51 52 echo "INFO - Validating kustomize overlays" 53 find . -type f -name $kustomize_config -print0 | while IFS= read -r -d $'\0' file; do 54 echo "INFO - Validating kustomization ${file/%$kustomize_config/}" 55 kustomize build "${file/%$kustomize_config/}" "${kustomize_flags[@]}" | 56 kubeconform "${kubeconform_flags[@]}" "${kubeconform_config[@]}" || echo "WARN - Validation failed for ${file/%$kustomize_config/}, continuing..." 57 done