Skip to content

Commit 815faad

Browse files
authored
Adds sync-gateway-api Make Target for Managing the Gateway API Version (#10446)
Signed-off-by: Daneyon Hansen <[email protected]>
1 parent b3f8ed0 commit 815faad

File tree

4 files changed

+203
-3
lines changed

4 files changed

+203
-3
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ generated-code: go-generate-all generate-cli-docs getter-check mod-tidy
405405
generated-code: verify-enterprise-protos generate-helm-files update-licenses
406406
generated-code: generate-crd-reference-docs
407407
generated-code: fmt
408+
generated-code: sync-gateway-api # Sync Gateway API version with the provided $CONFORMANCE_VERSION
408409

409410
.PHONY: go-generate-all
410411
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
12571258
go test -mod=mod -ldflags=$(LDFLAGS) -tags conformance -test.v $(TEST_ASSET_DIR)/conformance/... -args $(CONFORMANCE_ARGS) \
12581259
-run-test=$*
12591260

1261+
.PHONY: sync-gateway-api
1262+
sync-gateway-api: ## Syncronize the Gateway API version used by the repo with the provided $CONFORMANCE_VERSION.
1263+
@./ci/sync-gateway-api.sh
1264+
12601265
#----------------------------------------------------------------------------------
12611266
# Security Scan
12621267
#----------------------------------------------------------------------------------
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changelog:
2+
- type: NON_USER_FACING
3+
issueLink: https://github.com/solo-io/gloo/issues/10445
4+
resolvesIssue: true
5+
description: >-
6+
Adds the `sync-gateway-api` Make target for managing Gateway API version dependencies.

ci/sync-gateway-api.sh

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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."

projects/gateway2/crds/tcproute-crd.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
# Gateway API Experimental channel install
1717
#
1818
---
19-
#
20-
# config/crd/experimental/gateway.networking.k8s.io_tcproutes.yaml
21-
#
2219
apiVersion: apiextensions.k8s.io/v1
2320
kind: CustomResourceDefinition
2421
metadata:

0 commit comments

Comments
 (0)