|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# The file containing CONFORMANCE_VERSION and CONFORMANCE_CHANNEL environment variables |
| 6 | +SETUP_KIND_FILE="./ci/kind/setup-kind.sh" |
| 7 | + |
| 8 | +# Function to source specific variables from the setup-kind.sh script |
| 9 | +source_specific_vars() { |
| 10 | + local vars_to_source=("CONFORMANCE_VERSION" "CONFORMANCE_CHANNEL") |
| 11 | + |
| 12 | + for var in "${vars_to_source[@]}"; do |
| 13 | + eval $(grep "^$var=" "$SETUP_KIND_FILE") |
| 14 | + done |
| 15 | +} |
| 16 | + |
| 17 | +# Function to update the CONFORMANCE_VERSION variable in setup-kind.sh |
| 18 | +update_conformance_version_in_setup_kind() { |
| 19 | + local key="CONFORMANCE_VERSION" |
| 20 | + local value="$1" |
| 21 | + |
| 22 | + if grep -q "^${key}=" "$SETUP_KIND_FILE"; then |
| 23 | + current_value=$(grep "^${key}=" "$SETUP_KIND_FILE" | sed -E 's/^.*:-([^}]+)}/\1/') |
| 24 | + if [ "$current_value" != "$value" ]; then |
| 25 | + echo "Updating $key in $SETUP_KIND_FILE from \"${current_value}\" to \"${value}\"..." |
| 26 | + sed -i.bak "s|^\(${key}=\"\${${key}:-\)[^\}]*\(}\"\)|\1${value}\2|" "$SETUP_KIND_FILE" |
| 27 | + rm "$SETUP_KIND_FILE.bak" |
| 28 | + echo "Updated $key to \"${value}\"." |
| 29 | + else |
| 30 | + echo "$key is already set to \"${value}\"." |
| 31 | + fi |
| 32 | + else |
| 33 | + echo "$key not found in $SETUP_KIND_FILE. Adding it..." |
| 34 | + echo "${key}=\"\${${key}:-${value}}\"" >> "$SETUP_KIND_FILE" |
| 35 | + echo "Added $key with value \"${value}\"." |
| 36 | + fi |
| 37 | +} |
| 38 | + |
| 39 | +# Source the required variables |
| 40 | +source_specific_vars |
| 41 | + |
| 42 | +# Update CONFORMANCE_VERSION in ./ci/kind/setup-kind.sh if needed |
| 43 | +update_conformance_version_in_setup_kind "$CONFORMANCE_VERSION" |
| 44 | + |
| 45 | +# Capitalize the first letter of CONFORMANCE_CHANNEL |
| 46 | +CAPITALIZED_CHANNEL="$(echo "${CONFORMANCE_CHANNEL:0:1}" | tr '[:lower:]' '[:upper:]')${CONFORMANCE_CHANNEL:1}" |
| 47 | + |
| 48 | +# Define output directory and filenames |
| 49 | +OUT_DIR="${OUT_DIR:-projects/gateway2/crds}" |
| 50 | +OUT_FILENAME="${OUT_FILENAME:-gateway-crds.yaml}" |
| 51 | +TCPROUTE_FILENAME="${TCPROUTE_FILENAME:-"tcproute-crd.yaml"}" |
| 52 | + |
| 53 | +# Create the output directory if it doesn't exist |
| 54 | +mkdir -p "${OUT_DIR}" |
| 55 | + |
| 56 | +# URLs for CRDs |
| 57 | +GATEWAY_CRD_URL="https://github.com/kubernetes-sigs/gateway-api/releases/download/${CONFORMANCE_VERSION}/${CONFORMANCE_CHANNEL}-install.yaml" |
| 58 | +TCPROUTE_CRD_URL="https://raw.githubusercontent.com/kubernetes-sigs/gateway-api/refs/tags/${CONFORMANCE_VERSION}/config/crd/experimental/gateway.networking.k8s.io_tcproutes.yaml" |
| 59 | + |
| 60 | +# Header to prepend to the TCPRoute CRD file |
| 61 | +HEADER=$(cat <<EOF |
| 62 | +# Copyright 2024 The Kubernetes Authors. |
| 63 | +# |
| 64 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 65 | +# you may not use this file except in compliance with the License. |
| 66 | +# You may obtain a copy of the License at |
| 67 | +# |
| 68 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 69 | +# |
| 70 | +# Unless required by applicable law or agreed to in writing, software |
| 71 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 72 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 73 | +# See the License for the specific language governing permissions and |
| 74 | +# limitations under the License. |
| 75 | +
|
| 76 | +# |
| 77 | +# Gateway API ${CAPITALIZED_CHANNEL} channel install |
| 78 | +# |
| 79 | +--- |
| 80 | +EOF |
| 81 | +) |
| 82 | + |
| 83 | +# Function to compare files ignoring headers (for TCPRoute only) |
| 84 | +compare_files_no_header() { |
| 85 | + local file1="$1" |
| 86 | + local file2="$2" |
| 87 | + |
| 88 | + # Strip the header from both files and compare the rest of the content |
| 89 | + tail -n +$(($(echo "$HEADER" | wc -l) + 1)) "$file1" > "$file1.stripped" |
| 90 | + tail -n +$(($(echo "$HEADER" | wc -l) + 1)) "$file2" > "$file2.stripped" |
| 91 | + cmp -s "$file1.stripped" "$file2.stripped" |
| 92 | + local result=$? |
| 93 | + rm -f "$file1.stripped" "$file2.stripped" |
| 94 | + return $result |
| 95 | +} |
| 96 | + |
| 97 | +download_gateway_crd() { |
| 98 | + local url="$1" |
| 99 | + local dest="$2" |
| 100 | + |
| 101 | + echo "Downloading Gateway CRD from $url to $dest..." |
| 102 | + curl -sLo "$dest.tmp" "$url" |
| 103 | + |
| 104 | + if [ -f "$dest" ] && cmp -s "$dest" "$dest.tmp"; then |
| 105 | + echo "No changes detected in $dest." |
| 106 | + rm "$dest.tmp" |
| 107 | + else |
| 108 | + mv "$dest.tmp" "$dest" |
| 109 | + echo "Updated $dest." |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +download_tcproute_crd() { |
| 114 | + local url="$1" |
| 115 | + local dest="$2" |
| 116 | + |
| 117 | + echo "Downloading TCPRoute CRD from $url to $dest..." |
| 118 | + curl -sLo "$dest.tmp" "$url" |
| 119 | + |
| 120 | + # Always create the temporary file with the header |
| 121 | + echo "$HEADER" > "$dest.tmp.full" |
| 122 | + cat "$dest.tmp" >> "$dest.tmp.full" |
| 123 | + |
| 124 | + if [ -f "$dest" ]; then |
| 125 | + # Compare files ignoring the header |
| 126 | + if compare_files_no_header "$dest" "$dest.tmp.full"; then |
| 127 | + echo "No changes detected in $dest." |
| 128 | + rm "$dest.tmp" "$dest.tmp.full" |
| 129 | + return |
| 130 | + fi |
| 131 | + fi |
| 132 | + |
| 133 | + # Update the file with the new content and header |
| 134 | + mv "$dest.tmp.full" "$dest" |
| 135 | + rm "$dest.tmp" |
| 136 | + echo "Updated $dest." |
| 137 | +} |
| 138 | + |
| 139 | +# Update sigs.k8s.io/gateway-api in go.mod |
| 140 | +update_go_mod() { |
| 141 | + local module="sigs.k8s.io/gateway-api" |
| 142 | + if grep -q "$module" go.mod; then |
| 143 | + current_version=$(grep "$module" go.mod | awk '{print $2}') |
| 144 | + if [ "$current_version" != "$CONFORMANCE_VERSION" ]; then |
| 145 | + echo "Updating $module from $current_version to $CONFORMANCE_VERSION..." |
| 146 | + go get "$module@$CONFORMANCE_VERSION" |
| 147 | + go mod tidy |
| 148 | + echo "Updated $module to $CONFORMANCE_VERSION." |
| 149 | + else |
| 150 | + echo "$module is already at version $CONFORMANCE_VERSION." |
| 151 | + fi |
| 152 | + else |
| 153 | + echo "$module not found in go.mod." |
| 154 | + fi |
| 155 | +} |
| 156 | + |
| 157 | +# Update k8sgateway_api_version in nightly-tests/max_versions.env |
| 158 | +update_max_versions_env() { |
| 159 | + local env_file=".github/workflows/.env/nightly-tests/max_versions.env" |
| 160 | + local key="k8sgateway_api_version=" |
| 161 | + if [ -f "$env_file" ]; then |
| 162 | + if grep -q "$key" "$env_file"; then |
| 163 | + current_version=$(grep "$key" "$env_file" | cut -d '=' -f 2 | tr -d "'") |
| 164 | + if [ "$current_version" != "$CONFORMANCE_VERSION" ]; then |
| 165 | + echo "Updating $key in $env_file from '$current_version' to '$CONFORMANCE_VERSION'..." |
| 166 | + sed -i.bak "s|^$key.*|$key'$CONFORMANCE_VERSION'|" "$env_file" |
| 167 | + rm "$env_file.bak" |
| 168 | + echo "Updated $key to '$CONFORMANCE_VERSION'." |
| 169 | + else |
| 170 | + echo "$key is already set to '$CONFORMANCE_VERSION'." |
| 171 | + fi |
| 172 | + else |
| 173 | + echo "$key not found in $env_file. Adding it..." |
| 174 | + echo "$key'$CONFORMANCE_VERSION'" >> "$env_file" |
| 175 | + echo "Added $key with value '$CONFORMANCE_VERSION'." |
| 176 | + fi |
| 177 | + else |
| 178 | + echo "$env_file not found." |
| 179 | + fi |
| 180 | +} |
| 181 | + |
| 182 | +# Download Gateway API CRDs (leave as is) |
| 183 | +download_gateway_crd "$GATEWAY_CRD_URL" "${OUT_DIR}/${OUT_FILENAME}" |
| 184 | + |
| 185 | +# Download TCPRoute CRD (manage header) |
| 186 | +download_tcproute_crd "$TCPROUTE_CRD_URL" "${OUT_DIR}/${TCPROUTE_FILENAME}" |
| 187 | + |
| 188 | +# Update dependencies and environment |
| 189 | +update_go_mod |
| 190 | +update_max_versions_env |
| 191 | + |
| 192 | +echo "Gateway API sync complete." |
0 commit comments