Skip to content

✨ envtest: search the assets index for latest of a release series #3260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions pkg/envtest/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,54 @@ func downloadBinaryAssets(ctx context.Context, binaryAssetsDirectory, binaryAsse
}
}

var requestedRange semver.Range
if binaryAssetsVersion != "" {
binaryAssetsVersion = strings.TrimPrefix(binaryAssetsVersion, "v")
parsedVersion, errV := semver.ParseTolerant(binaryAssetsVersion)
parsedRange, errR := semver.ParseRange(binaryAssetsVersion)

switch {
// When an exact version is requested, don't look for it in the release index.
case errV == nil && parsedVersion.String() == binaryAssetsVersion:
requestedRange = nil

// When the version looks like a range, apply it to the index.
case errR == nil:
requestedRange = parsedRange

// When the version isn't exact, turn it into a range.
//
// The `setup-envtest` tool interprets a partial version to mean "latest stable with that prefix."
// For example, "1" and "1.2" are akin to "1.x.x" and "1.2.x" in [semver.ParseRange].
// [semver.ParseTolerant] fills in missing minor or patch with "0", so this replaces those with "x".
//
// That *should* produce a valid range. If it doesn't, use the original value and skip the index.
case errV == nil:
suffix := strings.TrimPrefix(parsedVersion.FinalizeVersion(), binaryAssetsVersion)
suffix = strings.ReplaceAll(suffix, "0", "x")
parsedRange, errR = semver.ParseRange(binaryAssetsVersion + suffix)

if errR == nil {
requestedRange = parsedRange
} else {
requestedRange = nil
}
}
} else {
// When no version is requested, look for one in the release index.
requestedRange = semver.MustParseRange(">0.0.0")
}

// When a range a versions is requested, select one from the release index.
var binaryAssetsIndex *index
if binaryAssetsVersion == "" {
if requestedRange != nil {
var err error
binaryAssetsIndex, err = getIndex(ctx, binaryAssetsIndexURL)
if err != nil {
return "", "", "", err
}

binaryAssetsVersion, err = latestStableVersionFromIndex(binaryAssetsIndex)
binaryAssetsVersion, err = latestStableVersionFromIndex(binaryAssetsIndex, requestedRange)
if err != nil {
return "", "", "", err
}
Expand Down Expand Up @@ -219,6 +258,8 @@ func fileExists(path string) bool {
}

func downloadBinaryAssetsArchive(ctx context.Context, index *index, version string, out io.Writer) error {
version = "v" + strings.TrimPrefix(version, "v")

archives, ok := index.Releases[version]
if !ok {
return fmt.Errorf("failed to find envtest binaries for version %s", version)
Expand Down Expand Up @@ -252,7 +293,7 @@ func downloadBinaryAssetsArchive(ctx context.Context, index *index, version stri
return readBody(resp, out, archiveName, archive.Hash)
}

func latestStableVersionFromIndex(index *index) (string, error) {
func latestStableVersionFromIndex(index *index, satisfying semver.Range) (string, error) {
if len(index.Releases) == 0 {
return "", fmt.Errorf("failed to find latest stable version from index: index is empty")
}
Expand All @@ -264,8 +305,8 @@ func latestStableVersionFromIndex(index *index) (string, error) {
return "", fmt.Errorf("failed to parse version %q: %w", releaseVersion, err)
}

// Filter out pre-releases.
if len(v.Pre) > 0 {
// Filter out pre-releases and undesirable versions.
if len(v.Pre) > 0 || !satisfying(v) {
continue
}

Expand Down
32 changes: 30 additions & 2 deletions pkg/envtest/binaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ var _ = Describe("Test download binaries", func() {
Expect(actualFiles).To(ConsistOf("some-file"))
})

It("should download v1.32.0 binaries", func() {
It("should download binaries of an exact version", func() {
apiServerPath, etcdPath, kubectlPath, err := downloadBinaryAssets(context.Background(), downloadDirectory, "v1.31.0", fmt.Sprintf("http://%s/%s", server.Addr(), "envtest-releases.yaml"))
Expect(err).ToNot(HaveOccurred())

// Verify latest stable version (v1.32.0) was downloaded
// Verify exact version (v1.31.0) was downloaded
versionDownloadDirectory := path.Join(downloadDirectory, fmt.Sprintf("1.31.0-%s-%s", runtime.GOOS, runtime.GOARCH))
Expect(apiServerPath).To(Equal(path.Join(versionDownloadDirectory, "kube-apiserver")))
Expect(etcdPath).To(Equal(path.Join(versionDownloadDirectory, "etcd")))
Expand All @@ -87,6 +87,25 @@ var _ = Describe("Test download binaries", func() {
}
Expect(actualFiles).To(ConsistOf("some-file"))
})

It("should download binaries of latest stable version of a release series", func() {
apiServerPath, etcdPath, kubectlPath, err := downloadBinaryAssets(context.Background(), downloadDirectory, "1.31", fmt.Sprintf("http://%s/%s", server.Addr(), "envtest-releases.yaml"))
Expect(err).ToNot(HaveOccurred())

// Verify stable version (v1.31.4) was downloaded
versionDownloadDirectory := path.Join(downloadDirectory, fmt.Sprintf("1.31.4-%s-%s", runtime.GOOS, runtime.GOARCH))
Expect(apiServerPath).To(Equal(path.Join(versionDownloadDirectory, "kube-apiserver")))
Expect(etcdPath).To(Equal(path.Join(versionDownloadDirectory, "etcd")))
Expect(kubectlPath).To(Equal(path.Join(versionDownloadDirectory, "kubectl")))

dirEntries, err := os.ReadDir(versionDownloadDirectory)
Expect(err).ToNot(HaveOccurred())
var actualFiles []string
for _, e := range dirEntries {
actualFiles = append(actualFiles, e.Name())
}
Expect(actualFiles).To(ConsistOf("some-file"))
})
})

var (
Expand All @@ -101,6 +120,15 @@ var (
"envtest-v1.32.0-linux-s390x.tar.gz": {},
"envtest-v1.32.0-windows-amd64.tar.gz": {},
},
"v1.31.4": map[string]archive{
"envtest-v1.31.4-darwin-amd64.tar.gz": {},
"envtest-v1.31.4-darwin-arm64.tar.gz": {},
"envtest-v1.31.4-linux-amd64.tar.gz": {},
"envtest-v1.31.4-linux-arm64.tar.gz": {},
"envtest-v1.31.4-linux-ppc64le.tar.gz": {},
"envtest-v1.31.4-linux-s390x.tar.gz": {},
"envtest-v1.31.4-windows-amd64.tar.gz": {},
},
"v1.31.0": map[string]archive{
"envtest-v1.31.0-darwin-amd64.tar.gz": {},
"envtest-v1.31.0-darwin-arm64.tar.gz": {},
Expand Down