|
| 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 | + BUILD_OUTPUT=$(kustomize build "$dir" 2>&1) |
| 100 | + if [ $? -eq 0 ]; then |
| 101 | + echo -e "${GREEN}OK${NC}" |
| 102 | + else |
| 103 | + echo -e "${RED}FAILED${NC}" |
| 104 | + echo -e "${YELLOW} Error details:${NC}" |
| 105 | + echo " ${BUILD_OUTPUT//$'\n'/$'\n' }" |
| 106 | + echo "" |
| 107 | + ERRORS=$((ERRORS + 1)) |
| 108 | + fi |
| 109 | + CHECKED=$((CHECKED + 1)) |
| 110 | +done |
| 111 | + |
| 112 | +echo "" |
| 113 | +echo "Summary: Checked $CHECKED kustomization.yaml files, skipped $SKIPPED (require external plugins)" |
| 114 | + |
| 115 | +if [[ $ERRORS -eq 0 ]]; then |
| 116 | + echo -e "${GREEN}All kustomization files validated successfully!${NC}" |
| 117 | + exit 0 |
| 118 | +else |
| 119 | + echo -e "${RED}$ERRORS kustomization file(s) failed validation${NC}" |
| 120 | + exit 1 |
| 121 | +fi |
| 122 | + |
0 commit comments