From e00f07219ec9e66040ba9da3f7591fd0b51d6945 Mon Sep 17 00:00:00 2001 From: Medya Ghazizadeh Date: Wed, 24 Sep 2025 15:26:10 -0700 Subject: [PATCH] remove manual verifying checksum since it is already handeled by go-getter lib --- pkg/minikube/download/download_test.go | 8 +-- pkg/minikube/download/preload.go | 75 ++++++++------------------ 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/pkg/minikube/download/download_test.go b/pkg/minikube/download/download_test.go index fd70a33257..12e690d62d 100644 --- a/pkg/minikube/download/download_test.go +++ b/pkg/minikube/download/download_test.go @@ -97,7 +97,7 @@ func testPreloadDownloadPreventsMultipleDownload(t *testing.T) { return os.Stat(f.Name()) } checkPreloadExists = func(_, _, _ string, _ ...bool) bool { return true } - getChecksum = func(_, _ string) ([]byte, error) { return []byte("check"), nil } + getChecksumGCS = func(_, _ string) ([]byte, error) { return []byte("check"), nil } ensureChecksumValid = func(_, _, _ string, _ []byte) error { return nil } var group sync.WaitGroup @@ -125,7 +125,7 @@ func testPreloadNotExists(t *testing.T) { checkCache = func(_ string) (fs.FileInfo, error) { return nil, fmt.Errorf("cache not found") } checkPreloadExists = func(_, _, _ string, _ ...bool) bool { return false } - getChecksum = func(_, _ string) ([]byte, error) { return []byte("check"), nil } + getChecksumGCS = func(_, _ string) ([]byte, error) { return []byte("check"), nil } ensureChecksumValid = func(_, _, _ string, _ []byte) error { return nil } err := Preload(constants.DefaultKubernetesVersion, constants.Docker, "docker") @@ -144,7 +144,7 @@ func testPreloadChecksumMismatch(t *testing.T) { checkCache = func(_ string) (fs.FileInfo, error) { return nil, fmt.Errorf("cache not found") } checkPreloadExists = func(_, _, _ string, _ ...bool) bool { return true } - getChecksum = func(_, _ string) ([]byte, error) { return []byte("check"), nil } + getChecksumGCS = func(_, _ string) ([]byte, error) { return []byte("check"), nil } ensureChecksumValid = func(_, _, _ string, _ []byte) error { return fmt.Errorf("checksum mismatch") } @@ -256,7 +256,7 @@ func testPreloadWithCachedSizeZero(t *testing.T) { checkCache = func(_ string) (fs.FileInfo, error) { return os.Stat(f.Name()) } checkPreloadExists = func(_, _, _ string, _ ...bool) bool { return true } - getChecksum = func(_, _ string) ([]byte, error) { return []byte("check"), nil } + getChecksumGCS = func(_, _ string) ([]byte, error) { return []byte("check"), nil } ensureChecksumValid = func(_, _, _ string, _ []byte) error { return nil } if err := Preload(constants.DefaultKubernetesVersion, constants.Docker, "docker"); err != nil { diff --git a/pkg/minikube/download/preload.go b/pkg/minikube/download/preload.go index 35aa7e18dd..e0b8eb4346 100644 --- a/pkg/minikube/download/preload.go +++ b/pkg/minikube/download/preload.go @@ -18,7 +18,6 @@ package download import ( "context" - "crypto/md5" "encoding/hex" "fmt" "net/http" @@ -34,6 +33,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/viper" "k8s.io/klog/v2" + "k8s.io/minikube/pkg/minikube/download/gh" "k8s.io/minikube/pkg/minikube/driver" "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/out" @@ -46,9 +46,9 @@ const ( // NOTE: You may need to bump this version up when upgrading auxiliary docker images PreloadVersion = "v18" // PreloadBucket is the name of the GCS bucket where preloaded volume tarballs exist - PreloadBucket = "minikube-preloaded-volume-tarballs" - // PreloadGitHubRepo is the GitHub repo that hosts the preload artifacts - PreloadGitHubRepo = "kubernetes/minikube-preloads" + PreloadBucket = "minikube-preloaded-volume-tarballs" + PreloadGitHubOrg = "kubernetes-sigs" + PreloadGitHubRepo = "minikube-preloads" ) type preloadSource string @@ -110,7 +110,7 @@ func remoteTarballURLGCS(k8sVersion, containerRuntime string) string { // remoteTarballURLGitHub returns the URL for the remote tarball hosted on GitHub releases func remoteTarballURLGitHub(k8sVersion, containerRuntime string) string { - return fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", PreloadGitHubRepo, PreloadVersion, TarballName(k8sVersion, containerRuntime)) + return fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s", PreloadGitHubOrg, PreloadGitHubRepo, PreloadVersion, TarballName(k8sVersion, containerRuntime)) } func remoteTarballURL(k8sVersion, containerRuntime string, source preloadSource) string { @@ -257,9 +257,16 @@ func Preload(k8sVersion, containerRuntime, driverName string) error { source = state.source } url := remoteTarballURL(k8sVersion, containerRuntime, source) - checksum, err := getChecksum(k8sVersion, containerRuntime) + var checksum []byte + var chksErr error + if source == preloadSourceGCS { + checksum, chksErr = getChecksumGCS(k8sVersion, containerRuntime) + } + if source == preloadSourceGitHub { + checksum, chksErr = getChecksumGithub(k8sVersion, containerRuntime) + } var realPath string - if err != nil { + if chksErr != nil { klog.Warningf("No checksum for preloaded tarball for k8s version %s: %v", k8sVersion, err) realPath = targetPath tmp, err := os.CreateTemp(targetDir(), TarballName(k8sVersion, containerRuntime)+".*") @@ -276,10 +283,6 @@ func Preload(k8sVersion, containerRuntime, driverName string) error { return errors.Wrapf(err, "download failed: %s", url) } - if err := ensureChecksumValid(k8sVersion, containerRuntime, targetPath, checksum); err != nil { - return err - } - if realPath != "" { klog.Infof("renaming tempfile to %s ...", TarballName(k8sVersion, containerRuntime)) err := os.Rename(targetPath, realPath) @@ -306,9 +309,9 @@ func getStorageAttrs(name string) (*storage.ObjectAttrs, error) { return attrs, nil } -// getChecksum returns the MD5 checksum of the preload tarball -var getChecksum = func(k8sVersion, containerRuntime string) ([]byte, error) { - klog.Infof("getting checksum for %s ...", TarballName(k8sVersion, containerRuntime)) +// getChecksumGCS returns the MD5 checksum of the preload tarball +var getChecksumGCS = func(k8sVersion, containerRuntime string) ([]byte, error) { + klog.Infof("getting checksum for %s from gcs...", TarballName(k8sVersion, containerRuntime)) filename := fmt.Sprintf("%s/%s/%s", PreloadVersion, k8sVersion, TarballName(k8sVersion, containerRuntime)) attrs, err := getStorageAttrs(filename) if err != nil { @@ -317,46 +320,10 @@ var getChecksum = func(k8sVersion, containerRuntime string) ([]byte, error) { return attrs.MD5, nil } -// saveChecksumFile saves the checksum to a local file for later verification -func saveChecksumFile(k8sVersion, containerRuntime string, checksum []byte) error { - klog.Infof("saving checksum for %s ...", TarballName(k8sVersion, containerRuntime)) - return os.WriteFile(PreloadChecksumPath(k8sVersion, containerRuntime), checksum, 0o644) -} - -// verifyChecksum returns true if the checksum of the local binary matches -// the checksum of the remote binary -func verifyChecksum(k8sVersion, containerRuntime, binaryPath string) error { - klog.Infof("verifying checksum of %s ...", binaryPath) - // get md5 checksum of tarball path - contents, err := os.ReadFile(binaryPath) - if err != nil { - return errors.Wrap(err, "reading tarball") - } - checksum := md5.Sum(contents) - - remoteChecksum, err := os.ReadFile(PreloadChecksumPath(k8sVersion, containerRuntime)) - if err != nil { - return errors.Wrap(err, "reading checksum file") - } - - // create a slice of checksum, which is [16]byte - if string(remoteChecksum) != string(checksum[:]) { - return fmt.Errorf("checksum of %s does not match remote checksum (%s != %s)", binaryPath, string(remoteChecksum), string(checksum[:])) - } - return nil -} - -// ensureChecksumValid saves and verifies local binary checksum matches remote binary checksum -var ensureChecksumValid = func(k8sVersion, containerRuntime, targetPath string, checksum []byte) error { - if err := saveChecksumFile(k8sVersion, containerRuntime, checksum); err != nil { - return errors.Wrap(err, "saving checksum file") - } - - if err := verifyChecksum(k8sVersion, containerRuntime, targetPath); err != nil { - return errors.Wrap(err, "verify") - } - - return nil +var getChecksumGithub = func(k8sVersion, containerRuntime string) ([]byte, error) { + klog.Infof("getting checksum for %s from github...", TarballName(k8sVersion, containerRuntime)) + gh.ReleaseAssets(PreloadGitHubRepo, PreloadGitHubRepo, PreloadVersion) + return nil, nil } // CleanUpOlderPreloads deletes preload files belonging to older minikube versions