2022-12-22 11:14:19 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# This script downloads the Flux OpenAPI schemas, then it validates the
|
|
|
|
# Flux custom resources and the kustomize overlays using kubeconform.
|
|
|
|
# This script is meant to be run locally and in CI before the changes
|
|
|
|
# are merged on the main branch that's synced by Flux.
|
|
|
|
|
|
|
|
# Copyright 2020 The Flux authors. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
# This script is meant to be run locally and in CI to validate the Kubernetes
|
|
|
|
# manifests (including Flux custom resources) before changes are merged into
|
|
|
|
# the branch synced by Flux in-cluster.
|
|
|
|
|
2022-12-22 13:44:03 +00:00
|
|
|
set -eo pipefail
|
2022-12-22 11:14:19 +00:00
|
|
|
|
|
|
|
KUSTOMIZE_FLAGS=("--load-restrictor=LoadRestrictionsNone")
|
|
|
|
KUSTOMIZE_CONFIG="kustomization.yaml"
|
|
|
|
|
|
|
|
FLUX_PATH="${1:-.}"
|
|
|
|
|
|
|
|
find "${FLUX_PATH%/}" -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do
|
|
|
|
printf "INFO - Validating %s\n" "$file"
|
|
|
|
yq e 'true' "$file" >/dev/null
|
|
|
|
done
|
|
|
|
|
|
|
|
kubeconform_config=(
|
|
|
|
"-strict"
|
|
|
|
"-schema-location" "default"
|
2023-01-10 15:35:55 +00:00
|
|
|
"-schema-location" "/etc/kube-tools/schemas/flux"
|
|
|
|
"-schema-location" "/etc/kube-tools/schemas/calico/master-standalone-strict/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json"
|
2023-01-24 08:21:27 +00:00
|
|
|
"-schema-location" "/etc/kube-tools/schemas/cert-manager/master-standalone-strict/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json"
|
2022-12-22 11:14:19 +00:00
|
|
|
"-exit-on-error"
|
|
|
|
"-summary"
|
2023-01-10 15:35:55 +00:00
|
|
|
"-skip" "Secret,CustomResourceDefinition"
|
2022-12-22 11:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
printf "\nINFO - Validating clusters\n"
|
|
|
|
find "${FLUX_PATH%/}/clusters" -maxdepth 2 -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do
|
|
|
|
printf "INFO - Validating cluster file %s\n" "${file}"
|
|
|
|
kubeconform "${kubeconform_config[@]}" "${file}"
|
|
|
|
if [[ ${PIPESTATUS[0]} != 0 ]]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
printf "\nINFO - Validating kustomize overlays\n"
|
|
|
|
find "${FLUX_PATH%/}" -type f -name $KUSTOMIZE_CONFIG -print0 | while IFS= read -r -d $'\0' file; do
|
|
|
|
printf "INFO - Validating kustomization %s\n" "${file/%$KUSTOMIZE_CONFIG/}"
|
|
|
|
kustomize build "${file/%$KUSTOMIZE_CONFIG/}" "${KUSTOMIZE_FLAGS[@]}" |
|
|
|
|
kubeconform "${kubeconform_config[@]}"
|
|
|
|
echo
|
|
|
|
if [[ ${PIPESTATUS[0]} != 0 ]]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|