Separate cached kubernetes binaries by OS on host machine (#6586)

* Separate cached kubernetes binaries by OS on host machine

This way, all kubernetes binaries stored in ~/.minikube/cache/linux will be copied over to the VM and used for `minikube kubectl` on linux machines. kubectl will be stored in `~/.minikube/cache/{windows or darwin}` on windows/darwin so that `minikube kubectl` still works.

* Review comment

* Update integration test

* Make sure --download-only includes OS specific kubectl

Update integration test to make sure we download
OS specific kubectl on darwin/windows when using --download-only flag
pull/6588/head
priyawadhwa 2020-02-11 22:23:37 -08:00 committed by GitHub
parent b0edc720b4
commit af353314e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 10 deletions

View File

@ -28,7 +28,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine" "k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/out"
) )
@ -55,19 +54,14 @@ minikube kubectl -- get pods --namespace kube-system`,
out.ErrLn("Error loading profile config: %v", err) out.ErrLn("Error loading profile config: %v", err)
} }
binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}
version := constants.DefaultKubernetesVersion version := constants.DefaultKubernetesVersion
if cc != nil { if cc != nil {
version = cc.KubernetesConfig.KubernetesVersion version = cc.KubernetesConfig.KubernetesVersion
} }
path, err := machine.CacheBinary(binary, version, runtime.GOOS, runtime.GOARCH) path, err := cacheKubectlBinary(version)
if err != nil { if err != nil {
exit.WithError("Failed to download kubectl", err) out.ErrLn("Error caching kubectl: %v", err)
} }
glog.Infof("Running %s %v", path, args) glog.Infof("Running %s %v", path, args)
@ -88,3 +82,12 @@ minikube kubectl -- get pods --namespace kube-system`,
} }
}, },
} }
func cacheKubectlBinary(k8sVerison string) (string, error) {
binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}
return machine.CacheBinary(binary, k8sVerison, runtime.GOOS, runtime.GOARCH)
}

View File

@ -484,6 +484,9 @@ func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) {
if err := doCacheBinaries(k8sVersion); err != nil { if err := doCacheBinaries(k8sVersion); err != nil {
exit.WithError("Failed to cache binaries", err) exit.WithError("Failed to cache binaries", err)
} }
if _, err := cacheKubectlBinary(k8sVersion); err != nil {
exit.WithError("Failed to cache kubectl", err)
}
waitCacheRequiredImages(cacheGroup) waitCacheRequiredImages(cacheGroup)
if err := saveImagesToTarFromConfig(); err != nil { if err := saveImagesToTarFromConfig(); err != nil {
exit.WithError("Failed to cache images to tar", err) exit.WithError("Failed to cache images to tar", err)

View File

@ -63,7 +63,7 @@ func KubernetesReleaseURLSHA1(binaryName, version, osName, archName string) stri
// CacheBinary will cache a binary on the host // CacheBinary will cache a binary on the host
func CacheBinary(binary, version, osName, archName string) (string, error) { func CacheBinary(binary, version, osName, archName string) (string, error) {
targetDir := localpath.MakeMiniPath("cache", version) targetDir := localpath.MakeMiniPath("cache", osName, version)
targetFilepath := path.Join(targetDir, binary) targetFilepath := path.Join(targetDir, binary)
url := KubernetesReleaseURL(binary, version, osName, archName) url := KubernetesReleaseURL(binary, version, osName, archName)

View File

@ -25,6 +25,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -83,12 +84,26 @@ func TestDownloadOnly(t *testing.T) {
// 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", v, bin) fp := filepath.Join(localpath.MiniPath(), "cache", "linux", v, bin)
_, 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)
} }
} }
// If we are on darwin/windows, check to make sure OS specific kubectl has been downloaded
// as well for the `minikube kubectl` command
if runtime.GOOS == "linux" {
return
}
binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}
fp := filepath.Join(localpath.MiniPath(), "cache", runtime.GOOS, v, binary)
if _, err := os.Stat(fp); err != nil {
t.Errorf("expected the file for binary exist at %q but got error %v", fp, err)
}
}) })
} }