diff --git a/.github/workflows/kustomize-validation.yml b/.github/workflows/kustomize-validation.yml new file mode 100644 index 0000000000..350503af83 --- /dev/null +++ b/.github/workflows/kustomize-validation.yml @@ -0,0 +1,23 @@ +name: Kustomize Validation + +on: + pull_request: + branches: + - main + +permissions: + contents: read + +jobs: + kustomize-validation: + name: Validate Kustomization Files + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Validate Kustomization Files + run: make test-kustomize + diff --git a/Makefile b/Makefile index d073fb24a9..52b2b88a21 100644 --- a/Makefile +++ b/Makefile @@ -213,7 +213,7 @@ check-coverage: install-go-test-coverage ${GOBIN}/go-test-coverage --config=./.testcoverage.yml .PHONY: ci-job -ci-job: common-deps-update generate fmt vet golangci-lint unittest shellcheck bashate yamllint bundle-check +ci-job: common-deps-update generate fmt vet golangci-lint unittest shellcheck bashate yamllint test-kustomize bundle-check # Download go tools .PHONY: controller-gen @@ -363,11 +363,17 @@ GINKGO_FLAGS = -ginkgo.focus="$(FOCUS)" -ginkgo.v -ginkgo.skip="$(SKIP)" ##@ Tools and Linting .PHONY: lint -lint: bashate golangci-lint shellcheck yamllint markdownlint +lint: bashate golangci-lint shellcheck yamllint markdownlint test-kustomize .PHONY: tools tools: opm operator-sdk yq +.PHONY: test-kustomize +test-kustomize: kustomize ## Validate all kustomization.yaml files can build successfully + @echo "Running kustomize validation on all kustomization.yaml files..." + @$(PROJECT_DIR)/hack/test-kustomize.sh + @echo "Kustomize validation completed successfully." + .PHONY: bashate-download 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. @echo "Downloading bashate..." diff --git a/hack/test-kustomize.sh b/hack/test-kustomize.sh new file mode 100755 index 0000000000..0cde7e968e --- /dev/null +++ b/hack/test-kustomize.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +set -euo pipefail + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Temporary file tracking for cleanup +TEMP_FILES=() + +# Cleanup function (invoked via trap, not called directly) +# shellcheck disable=SC2329 +cleanup() { + for temp_file in "${TEMP_FILES[@]}"; do + if [ -f "$temp_file" ]; then + rm -f "$temp_file" + echo "Cleaned up: $temp_file" + fi + done +} + +# Set trap to ensure cleanup on exit +trap cleanup EXIT + +# Check if kustomize is installed +if ! command -v kustomize &> /dev/null; then + echo -e "${RED}ERROR: kustomize is not installed${NC}" + echo "Install via: make kustomize" + exit 1 +fi + +# Generate temporary patch files that are normally created during build. +# config/manager/related-images/patch.yaml is generated from in.yaml with envsubst. +PATCH_FILE="./config/manager/related-images/patch.yaml" +if [ ! -f "$PATCH_FILE" ] && [ -f "./config/manager/related-images/in.yaml" ]; then + if ! command -v envsubst &> /dev/null; then + echo -e "${RED}ERROR: envsubst is not installed (provided by gettext)${NC}" + exit 1 + fi + echo "Generating temporary patch file for validation: $PATCH_FILE" + PRECACHE_WORKLOAD_IMG="quay.io/openshift-kni/lifecycle-agent-operator:latest" \ + envsubst < "./config/manager/related-images/in.yaml" > "$PATCH_FILE" + TEMP_FILES+=("$PATCH_FILE") +fi + +echo "Checking all kustomization.yaml files can build successfully..." +echo "" + +ERRORS=0 +CHECKED=0 + +# Find all kustomization.yaml files +kustomize_files=() +while IFS= read -r file; do + kustomize_files+=("$file") +done < <(find . -name 'kustomization.yaml' -not -path '*/vendor/*' -not -path '*/.git/*' -not -path '*/bin/*' -not -path '*/telco5g-konflux/*' | sort) + +if [ ${#kustomize_files[@]} -eq 0 ]; then + echo -e "${YELLOW}WARNING: No kustomization.yaml files found${NC}" + exit 0 +fi + +for kustomize_file in "${kustomize_files[@]}"; do + dir=$(dirname "$kustomize_file") + echo -n " $dir: " + + if BUILD_OUTPUT=$(kustomize build "$dir" 2>&1); then + echo -e "${GREEN}OK${NC}" + else + echo -e "${RED}FAILED${NC}" + echo -e "${YELLOW} Error details:${NC}" + echo " ${BUILD_OUTPUT//$'\n'/$'\n' }" + echo "" + ERRORS=$((ERRORS + 1)) + fi + CHECKED=$((CHECKED + 1)) +done + +echo "" +echo "Summary: Checked $CHECKED kustomization.yaml file(s)" + +if [[ $ERRORS -eq 0 ]]; then + echo -e "${GREEN}All kustomization files validated successfully!${NC}" + exit 0 +else + echo -e "${RED}$ERRORS kustomization file(s) failed validation${NC}" + exit 1 +fi