Skip to content

Commit 19c5ff3

Browse files
committed
Add validate-flux scrip
1 parent 5fecf36 commit 19c5ff3

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

scripts/validate-flux.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 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+
# - kubeconform v0.4.12
30+
31+
type yq >/dev/null 2>&1 || {
32+
echo >&2 "yq is not installed. Please install it to be able to run the script. Aborting."
33+
exit 1
34+
}
35+
type kustomize >/dev/null 2>&1 || {
36+
echo >&2 "kustomize is not installed. Please install it to be able to run the script. Aborting."
37+
exit 1
38+
}
39+
type kubeconform >/dev/null 2>&1 || {
40+
echo >&2 " kubeconform is not installed. Please install it to be able to run the script. Aborting."
41+
exit 1
42+
}
43+
44+
set -o errexit
45+
46+
echo "INFO - Downloading Flux OpenAPI schemas"
47+
mkdir -p /tmp/flux-crd-schemas/master-standalone-strict
48+
curl -sL https://github.com/fluxcd/flux2/releases/latest/download/crd-schemas.tar.gz | tar zxf - -C /tmp/flux-crd-schemas/master-standalone-strict
49+
50+
find . -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do
51+
echo "INFO - Validating $file"
52+
yq e 'true' "$file" >/dev/null
53+
done
54+
55+
kubeconform_config=("-strict" "-ignore-missing-schemas" "-schema-location" "default" "-schema-location" "/tmp/flux-crd-schemas" "-verbose")
56+
57+
echo "INFO - Validating clusters"
58+
find ./clusters -maxdepth 2 -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file; do
59+
kubeconform "${kubeconform_config[@]}" "${file}"
60+
if [[ ${PIPESTATUS[0]} != 0 ]]; then
61+
exit 1
62+
fi
63+
done
64+
65+
# mirror kustomize-controller build options
66+
kustomize_flags=("--load-restrictor=LoadRestrictionsNone")
67+
kustomize_config="kustomization.yaml"
68+
69+
echo "INFO - Validating kustomize overlays"
70+
find . -type f -name $kustomize_config -print0 | while IFS= read -r -d $'\0' file; do
71+
echo "INFO - Validating kustomization ${file/%$kustomize_config/}"
72+
kustomize build "${file/%$kustomize_config/}" "${kustomize_flags[@]}" |
73+
kubeconform "${kubeconform_config[@]}"
74+
if [[ ${PIPESTATUS[0]} != 0 ]]; then
75+
exit 1
76+
fi
77+
done

0 commit comments

Comments
 (0)