From 815faad215bc9d0b9f9d19ffc6e17edbc1d680bb Mon Sep 17 00:00:00 2001 From: Daneyon Hansen Date: Thu, 2 Jan 2025 14:23:13 -0800 Subject: [PATCH] Adds `sync-gateway-api` Make Target for Managing the Gateway API Version (#10446) Signed-off-by: Daneyon Hansen --- Makefile | 5 + changelog/v1.19.0-beta3/issue_10445.yaml | 6 + ci/sync-gateway-api.sh | 192 +++++++++++++++++++++++ projects/gateway2/crds/tcproute-crd.yaml | 3 - 4 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 changelog/v1.19.0-beta3/issue_10445.yaml create mode 100755 ci/sync-gateway-api.sh diff --git a/Makefile b/Makefile index abaada394ab..e840319c9fa 100644 --- a/Makefile +++ b/Makefile @@ -405,6 +405,7 @@ generated-code: go-generate-all generate-cli-docs getter-check mod-tidy generated-code: verify-enterprise-protos generate-helm-files update-licenses generated-code: generate-crd-reference-docs generated-code: fmt +generated-code: sync-gateway-api # Sync Gateway API version with the provided $CONFORMANCE_VERSION .PHONY: go-generate-all go-generate-all: clean-vendor-any ## Run all go generate directives in the repo, including codegen for protos, mockgen, and more @@ -1257,6 +1258,10 @@ conformance-%: $(TEST_ASSET_DIR)/conformance/conformance_test.go go test -mod=mod -ldflags=$(LDFLAGS) -tags conformance -test.v $(TEST_ASSET_DIR)/conformance/... -args $(CONFORMANCE_ARGS) \ -run-test=$* +.PHONY: sync-gateway-api +sync-gateway-api: ## Syncronize the Gateway API version used by the repo with the provided $CONFORMANCE_VERSION. + @./ci/sync-gateway-api.sh + #---------------------------------------------------------------------------------- # Security Scan #---------------------------------------------------------------------------------- diff --git a/changelog/v1.19.0-beta3/issue_10445.yaml b/changelog/v1.19.0-beta3/issue_10445.yaml new file mode 100644 index 00000000000..b0ad428c04d --- /dev/null +++ b/changelog/v1.19.0-beta3/issue_10445.yaml @@ -0,0 +1,6 @@ +changelog: + - type: NON_USER_FACING + issueLink: https://github.com/solo-io/gloo/issues/10445 + resolvesIssue: true + description: >- + Adds the `sync-gateway-api` Make target for managing Gateway API version dependencies. diff --git a/ci/sync-gateway-api.sh b/ci/sync-gateway-api.sh new file mode 100755 index 00000000000..008e3706e12 --- /dev/null +++ b/ci/sync-gateway-api.sh @@ -0,0 +1,192 @@ +#!/bin/bash + +set -euo pipefail + +# The file containing CONFORMANCE_VERSION and CONFORMANCE_CHANNEL environment variables +SETUP_KIND_FILE="./ci/kind/setup-kind.sh" + +# Function to source specific variables from the setup-kind.sh script +source_specific_vars() { + local vars_to_source=("CONFORMANCE_VERSION" "CONFORMANCE_CHANNEL") + + for var in "${vars_to_source[@]}"; do + eval $(grep "^$var=" "$SETUP_KIND_FILE") + done +} + +# Function to update the CONFORMANCE_VERSION variable in setup-kind.sh +update_conformance_version_in_setup_kind() { + local key="CONFORMANCE_VERSION" + local value="$1" + + if grep -q "^${key}=" "$SETUP_KIND_FILE"; then + current_value=$(grep "^${key}=" "$SETUP_KIND_FILE" | sed -E 's/^.*:-([^}]+)}/\1/') + if [ "$current_value" != "$value" ]; then + echo "Updating $key in $SETUP_KIND_FILE from \"${current_value}\" to \"${value}\"..." + sed -i.bak "s|^\(${key}=\"\${${key}:-\)[^\}]*\(}\"\)|\1${value}\2|" "$SETUP_KIND_FILE" + rm "$SETUP_KIND_FILE.bak" + echo "Updated $key to \"${value}\"." + else + echo "$key is already set to \"${value}\"." + fi + else + echo "$key not found in $SETUP_KIND_FILE. Adding it..." + echo "${key}=\"\${${key}:-${value}}\"" >> "$SETUP_KIND_FILE" + echo "Added $key with value \"${value}\"." + fi +} + +# Source the required variables +source_specific_vars + +# Update CONFORMANCE_VERSION in ./ci/kind/setup-kind.sh if needed +update_conformance_version_in_setup_kind "$CONFORMANCE_VERSION" + +# Capitalize the first letter of CONFORMANCE_CHANNEL +CAPITALIZED_CHANNEL="$(echo "${CONFORMANCE_CHANNEL:0:1}" | tr '[:lower:]' '[:upper:]')${CONFORMANCE_CHANNEL:1}" + +# Define output directory and filenames +OUT_DIR="${OUT_DIR:-projects/gateway2/crds}" +OUT_FILENAME="${OUT_FILENAME:-gateway-crds.yaml}" +TCPROUTE_FILENAME="${TCPROUTE_FILENAME:-"tcproute-crd.yaml"}" + +# Create the output directory if it doesn't exist +mkdir -p "${OUT_DIR}" + +# URLs for CRDs +GATEWAY_CRD_URL="https://github.com/kubernetes-sigs/gateway-api/releases/download/${CONFORMANCE_VERSION}/${CONFORMANCE_CHANNEL}-install.yaml" +TCPROUTE_CRD_URL="https://raw.githubusercontent.com/kubernetes-sigs/gateway-api/refs/tags/${CONFORMANCE_VERSION}/config/crd/experimental/gateway.networking.k8s.io_tcproutes.yaml" + +# Header to prepend to the TCPRoute CRD file +HEADER=$(cat < "$file1.stripped" + tail -n +$(($(echo "$HEADER" | wc -l) + 1)) "$file2" > "$file2.stripped" + cmp -s "$file1.stripped" "$file2.stripped" + local result=$? + rm -f "$file1.stripped" "$file2.stripped" + return $result +} + +download_gateway_crd() { + local url="$1" + local dest="$2" + + echo "Downloading Gateway CRD from $url to $dest..." + curl -sLo "$dest.tmp" "$url" + + if [ -f "$dest" ] && cmp -s "$dest" "$dest.tmp"; then + echo "No changes detected in $dest." + rm "$dest.tmp" + else + mv "$dest.tmp" "$dest" + echo "Updated $dest." + fi +} + +download_tcproute_crd() { + local url="$1" + local dest="$2" + + echo "Downloading TCPRoute CRD from $url to $dest..." + curl -sLo "$dest.tmp" "$url" + + # Always create the temporary file with the header + echo "$HEADER" > "$dest.tmp.full" + cat "$dest.tmp" >> "$dest.tmp.full" + + if [ -f "$dest" ]; then + # Compare files ignoring the header + if compare_files_no_header "$dest" "$dest.tmp.full"; then + echo "No changes detected in $dest." + rm "$dest.tmp" "$dest.tmp.full" + return + fi + fi + + # Update the file with the new content and header + mv "$dest.tmp.full" "$dest" + rm "$dest.tmp" + echo "Updated $dest." +} + +# Update sigs.k8s.io/gateway-api in go.mod +update_go_mod() { + local module="sigs.k8s.io/gateway-api" + if grep -q "$module" go.mod; then + current_version=$(grep "$module" go.mod | awk '{print $2}') + if [ "$current_version" != "$CONFORMANCE_VERSION" ]; then + echo "Updating $module from $current_version to $CONFORMANCE_VERSION..." + go get "$module@$CONFORMANCE_VERSION" + go mod tidy + echo "Updated $module to $CONFORMANCE_VERSION." + else + echo "$module is already at version $CONFORMANCE_VERSION." + fi + else + echo "$module not found in go.mod." + fi +} + +# Update k8sgateway_api_version in nightly-tests/max_versions.env +update_max_versions_env() { + local env_file=".github/workflows/.env/nightly-tests/max_versions.env" + local key="k8sgateway_api_version=" + if [ -f "$env_file" ]; then + if grep -q "$key" "$env_file"; then + current_version=$(grep "$key" "$env_file" | cut -d '=' -f 2 | tr -d "'") + if [ "$current_version" != "$CONFORMANCE_VERSION" ]; then + echo "Updating $key in $env_file from '$current_version' to '$CONFORMANCE_VERSION'..." + sed -i.bak "s|^$key.*|$key'$CONFORMANCE_VERSION'|" "$env_file" + rm "$env_file.bak" + echo "Updated $key to '$CONFORMANCE_VERSION'." + else + echo "$key is already set to '$CONFORMANCE_VERSION'." + fi + else + echo "$key not found in $env_file. Adding it..." + echo "$key'$CONFORMANCE_VERSION'" >> "$env_file" + echo "Added $key with value '$CONFORMANCE_VERSION'." + fi + else + echo "$env_file not found." + fi +} + +# Download Gateway API CRDs (leave as is) +download_gateway_crd "$GATEWAY_CRD_URL" "${OUT_DIR}/${OUT_FILENAME}" + +# Download TCPRoute CRD (manage header) +download_tcproute_crd "$TCPROUTE_CRD_URL" "${OUT_DIR}/${TCPROUTE_FILENAME}" + +# Update dependencies and environment +update_go_mod +update_max_versions_env + +echo "Gateway API sync complete." diff --git a/projects/gateway2/crds/tcproute-crd.yaml b/projects/gateway2/crds/tcproute-crd.yaml index 825e04115dd..6e22f805620 100644 --- a/projects/gateway2/crds/tcproute-crd.yaml +++ b/projects/gateway2/crds/tcproute-crd.yaml @@ -16,9 +16,6 @@ # Gateway API Experimental channel install # --- -# -# config/crd/experimental/gateway.networking.k8s.io_tcproutes.yaml -# apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: