-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathversion-discovery.sh
More file actions
executable file
·314 lines (246 loc) · 11.6 KB
/
version-discovery.sh
File metadata and controls
executable file
·314 lines (246 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# Version discovery functions for multiarch-tuning-operator upgrades
#
# This library provides functions to dynamically discover compatible versions
# for Go, Kubernetes, controller-runtime, and all required tools.
#
# NO HARDCODED VERSIONS - all discovered from authoritative sources.
set -euo pipefail
# Discover K8s version used by OCP from release.txt
discover_k8s_from_ocp_release() {
local ocp_version="$1"
echo "Finding K8s version from OCP $ocp_version release..." >&2
# Try to fetch from OCP release mirror (candidate or stable)
local k8s_version
k8s_version=$(curl -sf "https://mirror.openshift.com/pub/openshift-v4/amd64/clients/ocp-dev-preview/candidate-$ocp_version/release.txt" 2>/dev/null | \
grep -E 'kubernetes ' | head -1 | awk '{print $2}')
if [[ -n "$k8s_version" ]]; then
echo "$k8s_version"
else
# Fallback: discover from openshift/api go.mod
local k8s_module_version
k8s_module_version=$(curl -sf "https://raw.githubusercontent.com/openshift/api/release-$ocp_version/go.mod" | \
grep 'k8s.io/api ' | awk '{print $2}' | sed -E -n '/^v0\.[0-9]+\.[0-9]+$/p')
if [[ -n "$k8s_module_version" ]]; then
# Transform v0.X.Y to 1.X.Y (preserving patch version)
echo "${k8s_module_version/v0./1.}"
else
echo ""
fi
fi
}
# Discover latest stable Go patch version for a given minor version
discover_latest_go_patch() {
local go_minor="$1"
echo "Finding latest stable Go 1.$go_minor patch version..." >&2
# Query go.dev for the latest stable patch release
local latest_patch
latest_patch=$(curl -s "https://go.dev/dl/?mode=json&include=all" | \
grep '"version"' | \
grep -v 'rc\|beta' | \
sed -E 's/.*"version":\s*"go([0-9.]+)".*/\1/' | \
grep "^1\\.${go_minor}\\." | \
sort -uV | \
tail -1)
if [[ -n "$latest_patch" ]]; then
echo "$latest_patch"
else
echo "1.${go_minor}.0"
fi
}
# Discover required OCP version from target K8s version
# This queries openshift/api branches to find which OCP version uses the target K8s version
discover_required_ocp_version() {
local k8s_version="$1"
local k8s_minor
k8s_minor=$(echo "$k8s_version" | cut -d. -f2)
echo "Discovering required OCP version for K8s 1.$k8s_minor..." >&2
# Get all openshift/api release branches
local branches
branches=$(curl -s "https://api.github.com/repos/openshift/api/branches" | grep '"name"' | grep 'release-4' | sed -E 's/.*"release-([^"]+)".*/\1/')
# Check each branch to find which uses our target K8s version
for ocp_version in $branches; do
local openshift_api_k8s
openshift_api_k8s=$(curl -sf "https://raw.githubusercontent.com/openshift/api/release-$ocp_version/go.mod" | grep 'k8s.io/api ' | awk '{print $2}' | sed -E 's/^v0\.([0-9]+).*/\1/')
if [[ "$openshift_api_k8s" == "$k8s_minor" ]]; then
echo "✅ Found OCP $ocp_version uses K8s 1.$k8s_minor" >&2
echo "$ocp_version"
return 0
fi
done
echo "ERROR: Could not find OCP version for K8s 1.$k8s_minor" >&2
echo " Checked openshift/api release branches" >&2
return 1
}
# Discover target OCP version from current BUILD_IMAGE
discover_current_ocp_version() {
local current_ocp_version
current_ocp_version=$(grep 'BUILD_IMAGE' Makefile | grep -oP 'openshift-\K[0-9]+\.[0-9]+')
if [[ -z "$current_ocp_version" ]]; then
echo "ERROR: Could not extract OCP version from BUILD_IMAGE in Makefile" >&2
return 1
fi
echo "$current_ocp_version"
}
# Validate K8s version is compatible with target OCP version
validate_k8s_ocp_compatibility() {
local k8s_version="$1"
local ocp_version="$2"
local k8s_minor
k8s_minor=$(echo "$k8s_version" | cut -d. -f2)
echo "Validating K8s $k8s_version is compatible with OCP $ocp_version..." >&2
# Check openshift/api release branch for K8s version
local openshift_api_k8s
openshift_api_k8s=$(curl -sf "https://raw.githubusercontent.com/openshift/api/release-$ocp_version/go.mod" | grep 'k8s.io/api ' | awk '{print $2}' | sed -E 's/^v0\.([0-9]+).*/\1/')
if [[ "$openshift_api_k8s" != "$k8s_minor" ]]; then
echo "ERROR: K8s version mismatch" >&2
echo " Target: K8s 1.$k8s_minor.x" >&2
echo " OCP $ocp_version uses: K8s 1.$openshift_api_k8s.x" >&2
echo " Update target K8s version to match OCP $ocp_version" >&2
return 1
fi
echo "✅ K8s $k8s_version is compatible with OCP $ocp_version" >&2
return 0
}
# Discover compatible controller-runtime version
discover_controller_runtime_version() {
local k8s_version="$1"
local go_version="$2"
local k8s_minor go_minor
k8s_minor=$(echo "$k8s_version" | cut -d. -f2)
go_minor=$(echo "$go_version" | cut -d. -f2)
echo "Discovering compatible controller-runtime version..." >&2
local releases
releases=$(curl -s https://api.github.com/repos/kubernetes-sigs/controller-runtime/releases | grep '"tag_name"' | grep -E '"v0\.' | grep -Ev -- '-(alpha|beta|rc)[0-9.]*"' | sed -E 's/.*"v([^"]+)".*/\1/')
for version in $releases; do
local gomod
gomod=$(curl -sf "https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v${version}/go.mod")
if [[ -z "$gomod" ]]; then
continue
fi
local cr_k8s_minor cr_go_minor
cr_k8s_minor=$(echo "$gomod" | grep 'k8s.io/apimachinery' | awk '{print $2}' | sed -E 's/^v0\.([0-9]+).*/\1/' | head -1)
cr_go_minor=$(echo "$gomod" | grep '^go ' | awk '{print $2}' | cut -d. -f2)
if [[ "$cr_k8s_minor" == "$k8s_minor" ]] && [[ "$cr_go_minor" -le "$go_minor" ]]; then
echo "✅ Found controller-runtime v$version (k8s.io v0.$cr_k8s_minor, Go 1.$cr_go_minor)" >&2
echo "$version"
return 0
fi
done
echo "ERROR: Could not find compatible controller-runtime version" >&2
return 1
}
# Discover compatible kustomize version
discover_kustomize_version() {
local go_version="$1"
local go_minor
go_minor=$(echo "$go_version" | cut -d. -f2)
echo "Discovering compatible kustomize version..." >&2
local releases
releases=$(curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases | grep '"tag_name"' | grep 'kustomize/v5' | sed -E 's/.*"kustomize\/v([^"]+)".*/\1/')
for version in $releases; do
local go_req
go_req=$(curl -sf "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${version}/kustomize/go.mod" | grep '^go ' | awk '{print $2}' | cut -d. -f2)
if [[ -n "$go_req" ]] && [[ "$go_req" -le "$go_minor" ]]; then
echo "✅ Found kustomize v$version (requires Go 1.$go_req)" >&2
echo "$version"
return 0
fi
done
# Fallback to current version
local current_version
current_version=$(grep 'KUSTOMIZE_VERSION' Makefile | grep -oP 'v\K[0-9]+\.[0-9]+\.[0-9]+')
echo "⚠️ Using current version from Makefile: v$current_version" >&2
echo "$current_version"
}
# Discover compatible controller-tools version
discover_controller_tools_version() {
local k8s_version="$1"
local go_version="$2"
local k8s_minor go_minor
# Extract minor version from k8s_version parameter (e.g., "1.34.1" -> "34")
k8s_minor=$(echo "$k8s_version" | sed -E 's/^1\.([0-9]+).*/\1/')
go_minor=$(echo "$go_version" | cut -d. -f2)
echo "Discovering compatible controller-tools version..." >&2
local releases
releases=$(curl -s https://api.github.com/repos/kubernetes-sigs/controller-tools/releases | grep '"tag_name"' | grep -E '"v0\.' | grep -Ev -- '-(alpha|beta|rc)[0-9.]*"' | sed -E 's/.*"v([^"]+)".*/\1/')
for version in $releases; do
local gomod
gomod=$(curl -sf "https://raw.githubusercontent.com/kubernetes-sigs/controller-tools/v${version}/go.mod")
if [[ -z "$gomod" ]]; then
continue
fi
local ct_k8s_minor ct_go_minor
ct_k8s_minor=$(echo "$gomod" | grep 'k8s.io/apimachinery' | awk '{print $2}' | sed -E 's/^v0\.([0-9]+).*/\1/' | head -1)
ct_go_minor=$(echo "$gomod" | grep '^go ' | awk '{print $2}' | cut -d. -f2)
if [[ "$ct_k8s_minor" == "$k8s_minor" ]] && [[ "$ct_go_minor" -le "$go_minor" ]]; then
echo "✅ Found controller-tools v$version (k8s.io v0.$ct_k8s_minor, Go 1.$ct_go_minor)" >&2
echo "$version"
return 0
fi
done
# Fallback to current version
local current_version
current_version=$(grep 'CONTROLLER_TOOLS_VERSION' Makefile | grep -oP 'v\K[0-9]+\.[0-9]+\.[0-9]+')
echo "⚠️ Using current version from Makefile: v$current_version" >&2
echo "$current_version"
}
# Derive setup-envtest version from controller-runtime version
derive_setup_envtest_version() {
local controller_runtime_version="$1"
local minor
minor=$(echo "$controller_runtime_version" | cut -d. -f2)
local version="release-0.${minor}"
echo "Setup-envtest version: $version (from controller-runtime v0.${minor}.x)" >&2
# Verify tag exists
local exists
exists=$(curl -sf "https://api.github.com/repos/kubernetes-sigs/controller-runtime/git/refs/tags/${version}" | grep '"ref"')
if [[ -z "$exists" ]]; then
echo "⚠️ WARNING: $version tag not found in controller-runtime" >&2
echo " This may cause setup-envtest installation to fail" >&2
fi
echo "$version"
}
# Extract envtest K8s version from controller-runtime's dependencies
extract_envtest_k8s_version() {
local controller_runtime_version="$1"
echo "Extracting envtest K8s version from controller-runtime v$controller_runtime_version..." >&2
local gomod
gomod=$(curl -sf "https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v${controller_runtime_version}/go.mod")
if [[ -z "$gomod" ]]; then
echo "ERROR: Could not fetch controller-runtime go.mod" >&2
return 1
fi
# Get k8s.io/api version in Go module format (e.g., v0.34.1)
local k8s_module_version
k8s_module_version=$(echo "$gomod" | grep 'k8s.io/api ' | awk '{print $2}')
# Convert Go module version (v0.34.1) to Kubernetes version format (1.34.1)
# Go modules use v0.X.Y while Kubernetes uses 1.X.Y
local version
version=$(echo "$k8s_module_version" | sed -E 's/v0\./1./')
echo "✅ ENVTEST_K8S_VERSION=$version (from controller-runtime's k8s.io/api $k8s_module_version)" >&2
echo "$version"
}
# Discover compatible golangci-lint version
discover_golangci_lint_version() {
local go_version="$1"
local go_minor
go_minor=$(echo "$go_version" | cut -d. -f2)
echo "Discovering compatible golangci-lint version..." >&2
local releases
releases=$(curl -s https://api.github.com/repos/golangci/golangci-lint/releases | grep '"tag_name"' | grep -Ev -- '-(alpha|beta|rc)[0-9.]*"' | sed -E 's/.*"v([^"]+)".*/\1/')
for version in $releases; do
local go_req
go_req=$(curl -sf "https://raw.githubusercontent.com/golangci/golangci-lint/v${version}/go.mod" | grep '^go ' | awk '{print $2}' | cut -d. -f2)
if [[ -n "$go_req" ]] && [[ "$go_req" -le "$go_minor" ]]; then
echo "✅ Found golangci-lint v$version (requires Go 1.$go_req)" >&2
echo "$version"
return 0
fi
done
# Fallback to current version
local current_version
current_version=$(grep 'GOLINT_VERSION' Makefile | grep -oP 'v\K[0-9]+\.[0-9]+\.[0-9]+')
echo "⚠️ Using current version from Makefile: v$current_version" >&2
echo "$current_version"
}