|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Update Argo Rollouts CRDs from upstream |
| 4 | +# |
| 5 | +# Usage: ./scripts/update-argo-rollouts-crds.sh <version> |
| 6 | +# Example: ./scripts/update-argo-rollouts-crds.sh v1.8.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 v1.8.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-rollouts/templates/crds" |
| 32 | + |
| 33 | +UPSTREAM_BASE_URL="https://raw.githubusercontent.com/argoproj/argo-rollouts/${VERSION}/manifests/crds" |
| 34 | + |
| 35 | +# Function to get CRD file list from GitHub API |
| 36 | +get_crd_files() { |
| 37 | + local api_url="https://api.github.com/repos/argoproj/argo-rollouts/contents/manifests/crds?ref=${VERSION}" |
| 38 | + |
| 39 | + curl -sSfL "$api_url" | jq -r '.[] | select(.name | test(".*-crd\\.yaml$")) | .name' |
| 40 | +} |
| 41 | + |
| 42 | +# Process a downloaded CRD into a Helm template: |
| 43 | +# - Wrap in {{- if .Values.installCRDs }} conditional |
| 44 | +# - Preserve upstream annotations (e.g. controller-gen.kubebuilder.io/version) |
| 45 | +# - Add helm.sh/resource-policy and custom annotation support |
| 46 | +# - Add app.kubernetes.io labels |
| 47 | +process_crd() { |
| 48 | + local src_file="$1" |
| 49 | + local dest_file="$2" |
| 50 | + local crd_name |
| 51 | + crd_name=$(awk '/^ name:/ { print $2; exit }' "$src_file") |
| 52 | + |
| 53 | + { |
| 54 | + cat <<'HEADER' |
| 55 | +{{- if .Values.installCRDs }} |
| 56 | +HEADER |
| 57 | + |
| 58 | + # Remove leading "---" if present, then process the YAML |
| 59 | + sed '/^---$/d' "$src_file" | awk -v crd_name="$crd_name" ' |
| 60 | + BEGIN { state = "init" } |
| 61 | +
|
| 62 | + state == "init" && /^metadata:$/ { |
| 63 | + state = "meta" |
| 64 | + print "metadata:" |
| 65 | + print " annotations:" |
| 66 | + next |
| 67 | + } |
| 68 | +
|
| 69 | + # Inside upstream annotations block - print them, then append Helm directives |
| 70 | + state == "meta" && /^ annotations:$/ { state = "anno"; next } |
| 71 | + state == "anno" && /^ / { print; next } |
| 72 | + state == "anno" && !/^ / { |
| 73 | + # End of upstream annotations, add Helm annotation directives |
| 74 | + print " {{- if .Values.keepCRDs }}" |
| 75 | + print " \"helm.sh/resource-policy\": keep" |
| 76 | + print " {{- end }}" |
| 77 | + print " {{- if .Values.crdAnnotations }}" |
| 78 | + print " {{- toYaml .Values.crdAnnotations | nindent 4 }}" |
| 79 | + print " {{- end }}" |
| 80 | + print " labels:" |
| 81 | + print " app.kubernetes.io/name: argo-rollouts" |
| 82 | + print " app.kubernetes.io/part-of: argo-rollouts" |
| 83 | + state = "done" |
| 84 | + } |
| 85 | +
|
| 86 | + # Handle labels section if upstream ever adds one - keep upstream labels |
| 87 | + state == "meta" && /^ labels:$/ { state = "labels"; print; next } |
| 88 | + state == "labels" && /^ / { print; next } |
| 89 | + state == "labels" && !/^ / { state = "done" } |
| 90 | +
|
| 91 | + # Handle name line when there are no annotations or labels |
| 92 | + state == "meta" && /^ name:/ { |
| 93 | + print " annotations:" |
| 94 | + print " {{- if .Values.keepCRDs }}" |
| 95 | + print " \"helm.sh/resource-policy\": keep" |
| 96 | + print " {{- end }}" |
| 97 | + print " {{- if .Values.crdAnnotations }}" |
| 98 | + print " {{- toYaml .Values.crdAnnotations | nindent 4 }}" |
| 99 | + print " {{- end }}" |
| 100 | + print " labels:" |
| 101 | + print " app.kubernetes.io/name: argo-rollouts" |
| 102 | + print " app.kubernetes.io/part-of: argo-rollouts" |
| 103 | + state = "done" |
| 104 | + } |
| 105 | +
|
| 106 | + # Default: print everything else |
| 107 | + { print } |
| 108 | + ' |
| 109 | + |
| 110 | + echo "{{- end }}" |
| 111 | + } > "$dest_file" |
| 112 | +} |
| 113 | + |
| 114 | +echo "Updating Argo Rollouts CRDs to $VERSION" |
| 115 | +echo "=========================================" |
| 116 | + |
| 117 | +mkdir -p "$CRD_DIR" |
| 118 | + |
| 119 | +# Clean existing CRD files before downloading in case upstream have deleted a CRD |
| 120 | +rm -f "$CRD_DIR"/*-crd.yaml |
| 121 | + |
| 122 | +# Get file list dynamically from GitHub API |
| 123 | +crd_files=$(get_crd_files) |
| 124 | + |
| 125 | +if [[ -z "$crd_files" ]]; then |
| 126 | + echo " Error: Failed to fetch CRD file list" |
| 127 | + exit 1 |
| 128 | +fi |
| 129 | + |
| 130 | +TMP_DIR=$(mktemp -d) |
| 131 | +trap 'rm -rf "$TMP_DIR"' EXIT |
| 132 | + |
| 133 | +while IFS= read -r crd_file; do |
| 134 | + url="$UPSTREAM_BASE_URL/$crd_file" |
| 135 | + tmp_file="$TMP_DIR/$crd_file" |
| 136 | + dest="$CRD_DIR/$crd_file" |
| 137 | + |
| 138 | + echo " Downloading $crd_file..." |
| 139 | + if ! curl -sSfL "$url" -o "$tmp_file"; then |
| 140 | + echo " Warning: Failed to download $crd_file" |
| 141 | + continue |
| 142 | + fi |
| 143 | + |
| 144 | + process_crd "$tmp_file" "$dest" |
| 145 | + echo " Downloaded and processed $crd_file" |
| 146 | +done <<< "$crd_files" |
| 147 | + |
| 148 | +echo "" |
| 149 | +echo "Done! CRDs updated to $VERSION" |
| 150 | +echo "" |
| 151 | +echo "Files updated in:" |
| 152 | +echo " - $CRD_DIR/" |
0 commit comments