|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Update Argo Workflows CRDs from upstream |
| 4 | +# |
| 5 | +# Usage: ./scripts/update-argo-workflows-crds.sh <version> |
| 6 | +# Example: ./scripts/update-argo-workflows-crds.sh v3.7.4 |
| 7 | +# |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +if ! command -v jq &> /dev/null; then |
| 12 | + echo "Error: jq is required but not installed" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +VERSION="${1:-}" |
| 17 | + |
| 18 | +if [[ -z "$VERSION" ]]; then |
| 19 | + echo "Usage: $0 <version>" |
| 20 | + echo "Example: $0 v3.7.4" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# Ensure version starts with 'v' |
| 25 | +if [[ ! "$VERSION" =~ ^v ]]; then |
| 26 | + VERSION="v${VERSION}" |
| 27 | +fi |
| 28 | + |
| 29 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 30 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 31 | +CRD_DIR="$REPO_ROOT/charts/argo-workflows/files/crds" |
| 32 | + |
| 33 | +UPSTREAM_BASE_URL="https://raw.githubusercontent.com/argoproj/argo-workflows/${VERSION}/manifests/base/crds" |
| 34 | + |
| 35 | +# Function to get CRD file list from GitHub API |
| 36 | +get_crd_files() { |
| 37 | + local type="$1" |
| 38 | + local api_url="https://api.github.com/repos/argoproj/argo-workflows/contents/manifests/base/crds/${type}?ref=${VERSION}" |
| 39 | + |
| 40 | + curl -sSfL "$api_url" | jq -r '.[] | select(.name | test("^argoproj\\.io_.*\\.yaml$")) | .name' |
| 41 | +} |
| 42 | + |
| 43 | +# Function to process a CRD file: |
| 44 | +# - Remove the "auto-generated" comment line |
| 45 | +# - Add helm.sh/resource-policy annotation |
| 46 | +# - Ensure 'name:' comes before 'annotations:' in metadata |
| 47 | +process_crd() { |
| 48 | + local file="$1" |
| 49 | + local tmp_file="${file}.tmp" |
| 50 | + |
| 51 | + # Remove the auto-generated comment line if present |
| 52 | + sed -i '/^# This is an auto-generated file/d' "$file" |
| 53 | + |
| 54 | + awk ' |
| 55 | + BEGIN { in_metadata = 0; name_line = ""; has_annotations = 0 } |
| 56 | + /^metadata:$/ { |
| 57 | + in_metadata = 1 |
| 58 | + print |
| 59 | + next |
| 60 | + } |
| 61 | + in_metadata && /^ name:/ { |
| 62 | + name_line = $0 |
| 63 | + next |
| 64 | + } |
| 65 | + in_metadata && /^[^ ]/ { |
| 66 | + # End of metadata block |
| 67 | + in_metadata = 0 |
| 68 | + # If we still have a name_line, annotations block was not present |
| 69 | + if (name_line != "") { |
| 70 | + print name_line |
| 71 | + print " annotations:" |
| 72 | + print " helm.sh/resource-policy: keep" |
| 73 | + name_line = "" |
| 74 | + } |
| 75 | + } |
| 76 | + { print } |
| 77 | + ' "$file" > "$tmp_file" && mv "$tmp_file" "$file" |
| 78 | +} |
| 79 | + |
| 80 | +# Function to download and process CRDs for a specific type (full or minimal) |
| 81 | +download_crds() { |
| 82 | + local type="$1" |
| 83 | + local dest_dir="$CRD_DIR/$type" |
| 84 | + |
| 85 | + echo "Downloading $type CRDs for Argo Workflows $VERSION..." |
| 86 | + |
| 87 | + mkdir -p "$dest_dir" |
| 88 | + |
| 89 | + # Clean existing CRD files before downloading in case upstream have deleted a CRD |
| 90 | + rm -f "$dest_dir"/*.yaml |
| 91 | + |
| 92 | + # Get file list dynamically from GitHub API |
| 93 | + local crd_files |
| 94 | + crd_files=$(get_crd_files "$type") |
| 95 | + |
| 96 | + if [[ -z "$crd_files" ]]; then |
| 97 | + echo " Error: Failed to fetch CRD file list for $type" |
| 98 | + return 1 |
| 99 | + fi |
| 100 | + |
| 101 | + while IFS= read -r crd_file; do |
| 102 | + local url="$UPSTREAM_BASE_URL/$type/$crd_file" |
| 103 | + local dest="$dest_dir/$crd_file" |
| 104 | + |
| 105 | + echo " Downloading $crd_file..." |
| 106 | + if ! curl -sSfL "$url" -o "$dest"; then |
| 107 | + echo " Warning: Failed to download $crd_file" |
| 108 | + rm -f "$dest" |
| 109 | + continue |
| 110 | + fi |
| 111 | + |
| 112 | + process_crd "$dest" |
| 113 | + echo " Downloaded and processed $crd_file" |
| 114 | + done <<< "$crd_files" |
| 115 | +} |
| 116 | + |
| 117 | +echo "Updating Argo Workflows CRDs to $VERSION" |
| 118 | +echo "=========================================" |
| 119 | + |
| 120 | +# Download both full and minimal CRDs |
| 121 | +download_crds "full" |
| 122 | +download_crds "minimal" |
| 123 | + |
| 124 | +echo "" |
| 125 | +echo "Done! CRDs updated to $VERSION" |
| 126 | +echo "" |
| 127 | +echo "Files updated in:" |
| 128 | +echo " - $CRD_DIR/full/" |
| 129 | +echo " - $CRD_DIR/minimal/" |
0 commit comments