Skip to content

Commit cd26643

Browse files
committed
Add kustomize validation check
1 parent f27e751 commit cd26643

3 files changed

Lines changed: 190 additions & 2 deletions

File tree

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ check-coverage: install-go-test-coverage
211211
${GOBIN}/go-test-coverage --config=./.testcoverage.yml
212212

213213
.PHONY: ci-job
214-
ci-job: common-deps-update generate fmt vet golangci-lint unittest shellcheck bashate yamllint bundle-check
214+
ci-job: common-deps-update generate fmt vet golangci-lint unittest shellcheck bashate yamllint test-kustomize bundle-check
215215

216216
# Download go tools
217217
.PHONY: controller-gen
@@ -345,11 +345,17 @@ GINKGO_FLAGS = -ginkgo.focus="$(FOCUS)" -ginkgo.v -ginkgo.skip="$(SKIP)"
345345
##@ Tools and Linting
346346

347347
.PHONY: lint
348-
lint: bashate golangci-lint shellcheck yamllint markdownlint
348+
lint: bashate golangci-lint shellcheck yamllint markdownlint test-kustomize
349349

350350
.PHONY: tools
351351
tools: opm operator-sdk yq
352352

353+
.PHONY: test-kustomize
354+
test-kustomize: kustomize ## Validate all kustomization.yaml files can build successfully
355+
@echo "Running kustomize validation on all kustomization.yaml files..."
356+
@$(PROJECT_DIR)/hack/test-kustomize.sh
357+
@echo "Kustomize validation completed successfully."
358+
353359
.PHONY: bashate-download
354360
bashate-download: sync-git-submodules $(LOCALBIN) ## Download bashate locally if necessary and run against bash files. If wrong version is installed, it will be removed before downloading.
355361
@echo "Downloading bashate..."

hack/test-kustomize.sh

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
# Temporary file tracking for cleanup
13+
TEMP_FILES=()
14+
15+
# Cleanup function
16+
cleanup() {
17+
if [ ${#TEMP_FILES[@]} -gt 0 ]; then
18+
echo ""
19+
echo "Cleaning up temporary files..."
20+
for temp_file in "${TEMP_FILES[@]}"; do
21+
if [ -f "$temp_file" ]; then
22+
rm -f "$temp_file"
23+
echo " Removed: $temp_file"
24+
fi
25+
done
26+
fi
27+
}
28+
29+
# Set trap to ensure cleanup on exit
30+
trap cleanup EXIT
31+
32+
# Directories that require external kustomize plugins
33+
# Currently, this operator repository uses only standard kustomization resources
34+
# that do not require external plugins. If future directories require plugins like
35+
# PolicyGenerator, ClusterInstance, SiteConfig, or PolicyGenTemplate, add them here.
36+
#
37+
# These plugins would require:
38+
# - KUSTOMIZE_PLUGIN_HOME environment variable
39+
# - kustomize --enable-alpha-plugins flag
40+
# - The plugin binaries extracted from container images
41+
#
42+
# Example excluded directories:
43+
# EXCLUDED_DIRS=(
44+
# "./config/some-plugin-dir"
45+
# )
46+
EXCLUDED_DIRS=()
47+
48+
# Check if kustomize is installed
49+
if ! command -v kustomize &> /dev/null; then
50+
echo -e "${RED}ERROR: kustomize is not installed${NC}"
51+
echo ""
52+
echo "Please install kustomize to run this check:"
53+
echo " - macOS: brew install kustomize"
54+
echo " - Linux: curl -s \"https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh\" | bash"
55+
echo " - Manual: https://kubectl.docs.kubernetes.io/installation/kustomize/"
56+
echo ""
57+
exit 1
58+
fi
59+
60+
# Generate temporary patch files that are normally created during build
61+
# config/manager/related-images/patch.yaml is generated from in.yaml with envsubst
62+
PATCH_FILE="./config/manager/related-images/patch.yaml"
63+
if [ ! -f "$PATCH_FILE" ] && [ -f "./config/manager/related-images/in.yaml" ]; then
64+
echo "Generating temporary patch file for validation: $PATCH_FILE"
65+
# Use a placeholder value for PRECACHE_WORKLOAD_IMG
66+
PRECACHE_WORKLOAD_IMG="quay.io/openshift-kni/lifecycle-agent-operator:latest" envsubst < "./config/manager/related-images/in.yaml" > "$PATCH_FILE"
67+
TEMP_FILES+=("$PATCH_FILE")
68+
echo ""
69+
fi
70+
71+
echo "Checking all kustomization.yaml files can build successfully..."
72+
echo ""
73+
74+
ERRORS=0
75+
CHECKED=0
76+
SKIPPED=0
77+
78+
# Helper function to check if directory should be excluded
79+
is_excluded() {
80+
local dir="$1"
81+
for excluded in "${EXCLUDED_DIRS[@]}"; do
82+
if [ "$dir" = "$excluded" ]; then
83+
return 0
84+
fi
85+
done
86+
return 1
87+
}
88+
89+
# Find all kustomization.yaml files
90+
kustomize_files=()
91+
while IFS= read -r file; do
92+
kustomize_files+=("$file")
93+
done < <(find . -name 'kustomization.yaml' -not -path '*/vendor/*' -not -path '*/.git/*' -not -path '*/bin/*' -not -path '*/telco5g-konflux/*' | sort)
94+
95+
if [ ${#kustomize_files[@]} -eq 0 ]; then
96+
echo -e "${YELLOW}WARNING: No kustomization.yaml files found${NC}"
97+
exit 0
98+
fi
99+
100+
for kustomize_file in "${kustomize_files[@]}"; do
101+
dir=$(dirname "$kustomize_file")
102+
echo -n " $dir: "
103+
104+
# Check if this directory requires external plugins
105+
if is_excluded "$dir"; then
106+
echo -e "${BLUE}SKIPPED${NC} (requires external plugins)"
107+
SKIPPED=$((SKIPPED + 1))
108+
continue
109+
fi
110+
111+
# Try to build the kustomization
112+
if kustomize build "$dir" > /dev/null 2>&1; then
113+
echo -e "${GREEN}OK${NC}"
114+
CHECKED=$((CHECKED + 1))
115+
else
116+
echo -e "${RED}FAILED${NC}"
117+
echo -e "${YELLOW} Error details:${NC}"
118+
kustomize build "$dir" 2>&1 | sed 's/^/ /'
119+
echo ""
120+
ERRORS=$((ERRORS + 1))
121+
CHECKED=$((CHECKED + 1))
122+
fi
123+
done
124+
125+
echo ""
126+
echo "Summary: Checked $CHECKED kustomization.yaml files, skipped $SKIPPED (require external plugins)"
127+
128+
if [[ $ERRORS -eq 0 ]]; then
129+
echo -e "${GREEN}All kustomization files validated successfully!${NC}"
130+
exit 0
131+
else
132+
echo -e "${RED}$ERRORS kustomization file(s) failed validation${NC}"
133+
exit 1
134+
fi
135+

0 commit comments

Comments
 (0)