Skip to content

Commit 589fcdc

Browse files
committed
Add kustomize validation check
Signed-off-by: Brandon Palm <[email protected]>
1 parent 22eb74d commit 589fcdc

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Kustomize Validation
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
kustomize-validation:
13+
name: Validate Kustomization Files
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Install Kustomize
20+
run: |
21+
# Download with retries to prevent flaky CI failures
22+
MAX_ATTEMPTS=3
23+
ATTEMPT=1
24+
25+
while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
26+
echo "Attempt $ATTEMPT of $MAX_ATTEMPTS: Downloading kustomize install script..."
27+
if curl -fsSL --retry 3 --retry-delay 2 "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash; then
28+
echo "Successfully downloaded and installed kustomize"
29+
break
30+
else
31+
echo "Failed to install kustomize"
32+
if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
33+
echo "All attempts failed"
34+
exit 1
35+
fi
36+
echo "Waiting 5 seconds before retry..."
37+
sleep 5
38+
ATTEMPT=$((ATTEMPT + 1))
39+
fi
40+
done
41+
42+
sudo mv kustomize /usr/local/bin/
43+
kustomize version
44+
45+
- name: Validate Kustomization Files
46+
run: make test-kustomize
47+

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ test-upgrade-e2e: build-e2e-all
151151
test-must-gather-e2e: build-must-gather-e2e
152152
hack/run-test-must-gather-e2e.sh
153153

154+
.PHONY: test-kustomize
155+
test-kustomize: ## Validate all kustomization.yaml files can build successfully
156+
hack/test-kustomize.sh
157+
154158
# intentional left out:
155159
# api/, because autogeneration
156160
# cmd/, because kubebuilder scaffolding

hack/test-kustomize.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Colors for output
6+
RED='\033[0;31m'
7+
GREEN='\033[0;32m'
8+
YELLOW='\033[1;33m'
9+
BLUE='\033[0;34m'
10+
NC='\033[0m' # No Color
11+
12+
# Directories that require external kustomize plugins
13+
# All kustomization.yaml files in this repository use standard kustomize functionality
14+
# and do not require any external plugins, so this array is empty.
15+
EXCLUDED_DIRS=(
16+
)
17+
18+
# Check if kustomize is installed
19+
if ! command -v kustomize &> /dev/null; then
20+
echo -e "${RED}ERROR: kustomize is not installed${NC}"
21+
echo ""
22+
echo "Please install kustomize to run this check:"
23+
echo " - macOS: brew install kustomize"
24+
echo " - Linux: curl -s \"https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh\" | bash"
25+
echo " - Manual: https://kubectl.docs.kubernetes.io/installation/kustomize/"
26+
echo ""
27+
exit 1
28+
fi
29+
30+
echo "Checking all kustomization.yaml files can build successfully..."
31+
echo ""
32+
33+
ERRORS=0
34+
CHECKED=0
35+
SKIPPED=0
36+
37+
# Helper function to check if directory should be excluded
38+
is_excluded() {
39+
local dir="$1"
40+
for excluded in "${EXCLUDED_DIRS[@]}"; do
41+
if [ "$dir" = "$excluded" ]; then
42+
return 0
43+
fi
44+
done
45+
return 1
46+
}
47+
48+
# Find all kustomization.yaml files
49+
kustomize_files=()
50+
while IFS= read -r file; do
51+
kustomize_files+=("$file")
52+
done < <(find . -name 'kustomization.yaml' -not -path '*/vendor/*' -not -path '*/.git/*' | sort)
53+
54+
if [ ${#kustomize_files[@]} -eq 0 ]; then
55+
echo -e "${YELLOW}WARNING: No kustomization.yaml files found${NC}"
56+
exit 0
57+
fi
58+
59+
for kustomize_file in "${kustomize_files[@]}"; do
60+
dir=$(dirname "$kustomize_file")
61+
echo -n " $dir: "
62+
63+
# Check if this directory requires external plugins
64+
if is_excluded "$dir"; then
65+
echo -e "${BLUE}SKIPPED${NC} (requires external plugins)"
66+
SKIPPED=$((SKIPPED + 1))
67+
continue
68+
fi
69+
70+
# Try to build the kustomization
71+
if kustomize build "$dir" > /dev/null 2>&1; then
72+
echo -e "${GREEN}OK${NC}"
73+
CHECKED=$((CHECKED + 1))
74+
else
75+
echo -e "${RED}FAILED${NC}"
76+
echo -e "${YELLOW} Error details:${NC}"
77+
kustomize build "$dir" 2>&1 | sed 's/^/ /'
78+
echo ""
79+
ERRORS=$((ERRORS + 1))
80+
CHECKED=$((CHECKED + 1))
81+
fi
82+
done
83+
84+
echo ""
85+
echo "Summary: Checked $CHECKED kustomization.yaml files, skipped $SKIPPED (require external plugins)"
86+
87+
if [[ $ERRORS -eq 0 ]]; then
88+
echo -e "${GREEN}All kustomization files validated successfully!${NC}"
89+
exit 0
90+
else
91+
echo -e "${RED}$ERRORS kustomization file(s) failed validation${NC}"
92+
exit 1
93+
fi
94+

0 commit comments

Comments
 (0)