-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.sh
More file actions
206 lines (183 loc) · 5.9 KB
/
validate.sh
File metadata and controls
206 lines (183 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
# Copyright 2023-2026 The Flux authors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
# 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.
# Prerequisites
# - yq >= 4.50
# - kustomize >= 5.8
# - kubeconform >= 0.7
set -o errexit
set -o pipefail
# mirror kustomize-controller build options
kustomize_flags=("--load-restrictor=LoadRestrictionsNone")
kustomize_config="kustomization.yaml"
# skip Kubernetes Secrets due to SOPS fields failing validation
kubeconform_flags=("-skip=Secret")
kubeconform_config=("-strict" "-ignore-missing-schemas" "-schema-location" "default" "-schema-location" "/tmp/flux-crd-schemas" "-verbose")
# root directory to validate
root_dir="."
# directories to exclude from validation
exclude_dirs=()
# directories auto-detected as non-Kubernetes (terraform, helm charts)
declare -a auto_skip_dirs=()
# directories that are kustomize overlays
declare -a kustomize_dirs=()
usage() {
echo "Usage: $0 [-d <dir>] [-e <dir>]... [-h]"
echo ""
echo "Validate Flux custom resources and kustomize overlays using kubeconform."
echo ""
echo "Options:"
echo " -d, --dir <dir> Root directory to validate (default: current directory)"
echo " -e, --exclude <dir> Directory to exclude from validation (can be repeated)"
echo " -h, --help Show this help message"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-d|--dir)
if [[ -z "${2:-}" ]]; then
echo "ERROR - --dir requires a directory argument" >&2
exit 1
fi
root_dir="${2%/}"
shift 2
;;
-e|--exclude)
if [[ -z "${2:-}" ]]; then
echo "ERROR - --exclude requires a directory argument" >&2
exit 1
fi
exclude_dirs+=("./${2#./}")
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "ERROR - Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
}
check_prerequisites() {
local missing=0
for cmd in yq kustomize kubeconform curl; do
if ! command -v "$cmd" &> /dev/null; then
echo "ERROR - $cmd is not installed" >&2
missing=1
fi
done
if [[ $missing -ne 0 ]]; then
exit 1
fi
}
download_schemas() {
echo "INFO - Downloading Flux OpenAPI schemas"
mkdir -p /tmp/flux-crd-schemas/master-standalone-strict
curl -sL https://github.com/controlplaneio-fluxcd/flux-operator/releases/latest/download/crd-schemas.tar.gz | tar zxf - -C /tmp/flux-crd-schemas/master-standalone-strict
curl -sL https://github.com/fluxcd/flux2/releases/latest/download/crd-schemas.tar.gz | tar zxf - -C /tmp/flux-crd-schemas/master-standalone-strict
}
# Normalize a path by stripping leading "./" for consistent comparisons
normalize_path() {
local p="${1#./}"
echo "${p%/}"
}
# Check if a path is under a user-excluded, auto-skipped, or kustomize directory
is_excluded_dir() {
local path
path="$(normalize_path "$1")"
for dir in "${exclude_dirs[@]}"; do
local d
d="$(normalize_path "$dir")"
if [[ "$path" == "$d"/* || "$path" == "$d" ]]; then
return 0
fi
done
for dir in "${auto_skip_dirs[@]}"; do
local d
d="$(normalize_path "$dir")"
if [[ "$path" == "$d"/* || "$path" == "$d" ]]; then
return 0
fi
done
for dir in "${kustomize_dirs[@]}"; do
local d
d="$(normalize_path "$dir")"
if [[ "$path" == "$d"/* || "$path" == "$d" ]]; then
return 0
fi
done
return 1
}
# Check if a path is under a user-excluded or auto-skipped directory (but not kustomize dirs)
is_non_kustomize_excluded_dir() {
local path
path="$(normalize_path "$1")"
for dir in "${exclude_dirs[@]}" "${auto_skip_dirs[@]}"; do
local d
d="$(normalize_path "$dir")"
if [[ "$path" == "$d"/* || "$path" == "$d" ]]; then
return 0
fi
done
return 1
}
# Detect directories containing Terraform files, Helm charts, or kustomize overlays
detect_excluded_dirs() {
while IFS= read -r -d $'\0' file; do
auto_skip_dirs+=("$(dirname "$file")")
done < <(find "$root_dir" -path '*/.*' -prune -o -type f \( -name '*.tf' -o -name 'Chart.yaml' \) -print0)
while IFS= read -r -d $'\0' file; do
kustomize_dirs+=("$(dirname "$file")")
done < <(find "$root_dir" -path '*/.*' -prune -o -type f -name "$kustomize_config" -print0)
}
validate_yaml_syntax() {
echo "INFO - Validating YAML syntax"
while IFS= read -r -d $'\0' file; do
dir="$(dirname "$file")"
if is_excluded_dir "$dir"; then
continue
fi
yq e 'true' "$file" > /dev/null
done < <(find "$root_dir" -path '*/.*' -prune -o -type f -name '*.yaml' -print0)
}
validate_kubernetes_manifests() {
echo "INFO - Validating Kubernetes manifests"
while IFS= read -r -d $'\0' file; do
dir="$(dirname "$file")"
if is_excluded_dir "$dir"; then
continue
fi
kubeconform "${kubeconform_flags[@]}" "${kubeconform_config[@]}" "${file}"
done < <(find "$root_dir" -path '*/.*' -prune -o -type f -name '*.yaml' -print0)
}
validate_kustomize_overlays() {
while IFS= read -r -d $'\0' file; do
dir="$(dirname "$file")"
if is_non_kustomize_excluded_dir "$dir"; then
continue
fi
echo "INFO - Validating kustomize overlay ${file/%$kustomize_config}"
kustomize build "${file/%$kustomize_config}" "${kustomize_flags[@]}" | \
kubeconform "${kubeconform_flags[@]}" "${kubeconform_config[@]}"
if [[ ${PIPESTATUS[0]} != 0 || ${PIPESTATUS[1]} != 0 ]]; then
exit 1
fi
done < <(find "$root_dir" -path '*/.*' -prune -o -type f -name "$kustomize_config" -print0)
}
# Main
parse_args "$@"
check_prerequisites
download_schemas
detect_excluded_dirs
validate_yaml_syntax
validate_kubernetes_manifests
validate_kustomize_overlays
echo "INFO - All validations passed"