Skip to content

Commit a8e813a

Browse files
committed
CNFCERT-1258: Add kustomize validation check
Add a PR check that validates all kustomization.yaml files can build successfully. Relies on the Makefile-managed kustomize installation with a pinned version. Skips directories requiring external plugins. - Add hack/test-kustomize.sh validation script - Add make test-kustomize target - Add GitHub Actions workflow for PR checks against main - Add temporary patch file generation for related-images validation
1 parent a96cead commit a8e813a

3 files changed

Lines changed: 150 additions & 2 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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: Validate Kustomization Files
20+
run: make test-kustomize
21+

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ check-coverage: install-go-test-coverage
213213
${GOBIN}/go-test-coverage --config=./.testcoverage.yml
214214

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

218218
# Download go tools
219219
.PHONY: controller-gen
@@ -363,11 +363,17 @@ GINKGO_FLAGS = -ginkgo.focus="$(FOCUS)" -ginkgo.v -ginkgo.skip="$(SKIP)"
363363
##@ Tools and Linting
364364

365365
.PHONY: lint
366-
lint: bashate golangci-lint shellcheck yamllint markdownlint
366+
lint: bashate golangci-lint shellcheck yamllint markdownlint test-kustomize
367367

368368
.PHONY: tools
369369
tools: opm operator-sdk yq
370370

371+
.PHONY: test-kustomize
372+
test-kustomize: kustomize ## Validate all kustomization.yaml files can build successfully
373+
@echo "Running kustomize validation on all kustomization.yaml files..."
374+
@$(PROJECT_DIR)/hack/test-kustomize.sh
375+
@echo "Kustomize validation completed successfully."
376+
371377
.PHONY: bashate-download
372378
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.
373379
@echo "Downloading bashate..."

hack/test-kustomize.sh

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

0 commit comments

Comments
 (0)