Move linux/darwin binaries into cache/bin folder

pull/21664/head
Henry Chen 2025-09-29 18:00:57 +08:00
parent 574f958887
commit 0ce7767ba6
4 changed files with 35 additions and 4 deletions

View File

@ -19,7 +19,7 @@ package download
import ( import (
"fmt" "fmt"
"os" "os"
"path" "path/filepath"
"runtime" "runtime"
"github.com/blang/semver/v4" "github.com/blang/semver/v4"
@ -53,8 +53,12 @@ func binaryWithChecksumURL(binaryName, version, osName, archName, binaryURL stri
// Binary will download a binary onto the host // Binary will download a binary onto the host
func Binary(binary, version, osName, archName, binaryURL string) (string, error) { func Binary(binary, version, osName, archName, binaryURL string) (string, error) {
targetDir := localpath.MakeMiniPath("cache", osName, archName, version) targetFilepath := localpath.CachedBinaryPath(binary, version, osName, archName)
targetFilepath := path.Join(targetDir, binary) targetDir := filepath.Dir(targetFilepath)
if err := os.MkdirAll(targetDir, 0755); err != nil {
return "", errors.Wrapf(err, "failed to create target dir %s", targetDir)
}
targetLock := targetFilepath + ".lock" targetLock := targetFilepath + ".lock"
url, err := binaryWithChecksumURL(binary, version, osName, archName, binaryURL) url, err := binaryWithChecksumURL(binary, version, osName, archName, binaryURL)

View File

@ -20,11 +20,13 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath"
"sync" "sync"
"testing" "testing"
"time" "time"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath"
) )
// Force download tests to run in serial. // Force download tests to run in serial.
@ -242,3 +244,24 @@ func testPreloadWithCachedSizeZero(t *testing.T) {
t.Errorf("Expected only 1 download attempt but got %v!", downloadNum) t.Errorf("Expected only 1 download attempt but got %v!", downloadNum)
} }
} }
func TestBinaryCreatesBinariesDir(t *testing.T) {
// mock download function that creates a fake binary file
DownloadMock = func(src, dst string) error {
// create a fake binary file at dst
return os.WriteFile(dst, []byte("fake-binary"), 0755)
}
_, err := Binary("kubectl", "v1.31.0", "linux", "amd64", "")
if err != nil {
t.Fatalf("Binary() failed: %v", err)
}
// check that the bin directory was created
targetDir := filepath.Join(
localpath.MiniPath(), "cache", "bin", "linux", "amd64", "v1.31.0",
)
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
t.Errorf("Expected bin dir %s to exist, but it does not", targetDir)
}
}

View File

@ -247,3 +247,7 @@ func getWindowsVolumeNameCmd(d string) (string, error) {
} }
var getWindowsVolumeName = getWindowsVolumeNameCmd var getWindowsVolumeName = getWindowsVolumeNameCmd
func CachedBinaryPath(binary, version, goos, goarch string) string {
return filepath.Join(MiniPath(), "cache", "bin", goos, goarch, version, binary)
}

View File

@ -152,7 +152,7 @@ func TestDownloadOnly(t *testing.T) { // nolint:gocyclo
} }
// checking binaries downloaded (kubelet,kubeadm) // checking binaries downloaded (kubelet,kubeadm)
for _, bin := range constants.KubernetesReleaseBinaries { for _, bin := range constants.KubernetesReleaseBinaries {
fp := filepath.Join(localpath.MiniPath(), "cache", "linux", runtime.GOARCH, v, bin) fp := localpath.CachedBinaryPath(bin, v, "linux", runtime.GOARCH)
_, err := os.Stat(fp) _, err := os.Stat(fp)
if err != nil { if err != nil {
t.Errorf("expected the file for binary exist at %q but got error %v", fp, err) t.Errorf("expected the file for binary exist at %q but got error %v", fp, err)