|
| 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