Skip to content

Commit 7381c60

Browse files
Merge remote-tracking branch 'origin/master' into release-1.34
2 parents c5d688b + 17d6c9c commit 7381c60

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

test/e2e_dra/upgradedowngrade_test.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import (
6262
admissionapi "k8s.io/pod-security-admission/api"
6363
)
6464

65+
var errHTTP404 = errors.New("resource not found (404)")
66+
6567
func init() {
6668
// -v=5 may be useful to debug driver operations, but usually isn't needed.
6769
ktesting.SetDefaultVerbosity(2)
@@ -121,6 +123,7 @@ var _ = ginkgo.Describe("DRA upgrade/downgrade", func() {
121123
version, err := version.ParseGeneric(gitVersion)
122124
tCtx.ExpectNoError(err, "parse version %s of repo root %q", gitVersion, repoRoot)
123125
major, previousMinor := version.Major(), version.Minor()-1
126+
tCtx.Logf("got version: major: %d, minor: %d, previous minor: %d", major, version.Minor(), previousMinor)
124127
tCtx = ktesting.End(tCtx)
125128

126129
// KUBERNETES_SERVER_CACHE_DIR can be set to keep downloaded files across test restarts.
@@ -130,8 +133,18 @@ var _ = ginkgo.Describe("DRA upgrade/downgrade", func() {
130133
}
131134
haveBinaries := false
132135

133-
// Get the previous release, if necessary.
134-
previousURL, previousVersion := serverDownloadURL(tCtx, major, previousMinor)
136+
// Get the previous release.
137+
tCtx = ktesting.Begin(tCtx, "get previous release info")
138+
tCtx.Logf("stable release %d.%d", major, previousMinor)
139+
previousURL, previousVersion, err := serverDownloadURL(tCtx, "stable", major, previousMinor)
140+
if errors.Is(err, errHTTP404) {
141+
tCtx.Logf("stable doesn't exist, get latest release %d.%d", major, previousMinor)
142+
previousURL, previousVersion, err = serverDownloadURL(tCtx, "latest", major, previousMinor)
143+
}
144+
tCtx.ExpectNoError(err)
145+
tCtx.Logf("got previous release version: %s, URL: %s", previousVersion, previousURL)
146+
tCtx = ktesting.End(tCtx)
147+
135148
if cacheBinaries {
136149
binDir = path.Join(binDir, previousVersion)
137150
_, err := os.Stat(path.Join(binDir, string(localupcluster.KubeClusterComponents[0])))
@@ -299,6 +312,7 @@ func sourceVersion(tCtx ktesting.TContext, kubeRoot string) (gitVersion string,
299312
if err != nil {
300313
return "", "", err
301314
}
315+
tCtx.Logf("workspace status:\n%s", output)
302316

303317
// Parse it.
304318
for _, line := range strings.Split(string(output), "\n") {
@@ -355,29 +369,54 @@ func sourceVersion(tCtx ktesting.TContext, kubeRoot string) (gitVersion string,
355369
// return output.String()
356370
// }
357371

358-
// serverDownloadURL returns the full URL for a kubernetes-server archive matching
359-
// the current GOOS/GOARCH for the given major/minor version of Kubernetes.
372+
// serverDownloadURL constructs a download URL for a Kubernetes server tarball based on the given
373+
// prefix, major, and minor version numbers. It performs an HTTP GET request to retrieve the version
374+
// string from a remote text file, then builds the final tarball URL using the retrieved version,
375+
// the current OS, and architecture. If the version file is not found (HTTP 404), it returns
376+
// errHTTP404 to allow the caller to try another prefix. Returns the tarball URL, the version string,
377+
// or an error if any step fails.
378+
// The function uses the provided testing context for logging and error handling.
379+
//
380+
// Parameters:
381+
// - tCtx: a ktesting.TContext used for test context and error handling.
382+
// - prefix: the release prefix (e.g., "stable", "latest").
383+
// - major: the major version number.
384+
// - minor: the minor version number.
360385
//
361-
// This considers only proper releases.
362-
func serverDownloadURL(tCtx ktesting.TContext, major, minor uint) (string, string) {
386+
// Returns:
387+
// - The constructed tarball download URL as a string.
388+
// - The version string as retrieved from the remote file.
389+
// - An error if the request fails, the response is invalid, or the version file is not found.
390+
func serverDownloadURL(tCtx ktesting.TContext, prefix string, major, minor uint) (string, string, error) {
363391
tCtx.Helper()
364-
url := fmt.Sprintf("https://dl.k8s.io/release/stable-%d.%d.txt", major, minor)
392+
url := fmt.Sprintf("https://dl.k8s.io/release/%s-%d.%d.txt", prefix, major, minor)
365393
get, err := http.NewRequestWithContext(tCtx, http.MethodGet, url, nil)
366-
tCtx.ExpectNoError(err, "construct GET for %s", url)
394+
if err != nil {
395+
return "", "", fmt.Errorf("constructing GET for %s failed: %w", url, err)
396+
}
367397
resp, err := http.DefaultClient.Do(get)
368-
tCtx.ExpectNoError(err, "get %s", url)
398+
if err != nil {
399+
return "", "", fmt.Errorf("downloading %s failed: %w", url, err)
400+
}
401+
if resp.StatusCode == http.StatusNotFound {
402+
// Caller should be able to distinguish HTTP 404
403+
// to try another prefix (usually 'latest' if 'stable' returns 404)
404+
return "", "", errHTTP404
405+
}
369406
if resp.StatusCode != http.StatusOK {
370-
tCtx.Fatalf("get %s: %d - %s", url, resp.StatusCode, resp.Status)
407+
return "", "", fmt.Errorf("getting %s failed: status code: %d, status: %s", url, resp.StatusCode, resp.Status)
371408
}
372409
if resp.Body == nil {
373-
tCtx.Fatalf("empty response for %s", url)
410+
return "", "", fmt.Errorf("empty response for %s", url)
374411
}
375412
defer func() {
376413
tCtx.ExpectNoError(resp.Body.Close(), "close response body")
377414
}()
378415
version, err := io.ReadAll(resp.Body)
379-
tCtx.ExpectNoError(err, "read response body for %s", url)
380-
return fmt.Sprintf("https://dl.k8s.io/release/%s/kubernetes-server-%s-%s.tar.gz", string(version), runtime.GOOS, runtime.GOARCH), string(version)
416+
if err != nil {
417+
return "", "", fmt.Errorf("reading response body for %s failed: %w", url, err)
418+
}
419+
return fmt.Sprintf("https://dl.k8s.io/release/%s/kubernetes-server-%s-%s.tar.gz", string(version), runtime.GOOS, runtime.GOARCH), string(version), nil
381420
}
382421

383422
// testResourceClaimDeviceStatus corresponds to testResourceClaimDeviceStatus in test/integration/dra

0 commit comments

Comments
 (0)