Skip to content

Commit

Permalink
Separate cached kubernetes binaries by OS on host machine (#6586)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
priyawadhwa authored Feb 12, 2020
1 parent b0edc72 commit af35331
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
19 changes: 11 additions & 8 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
)
Expand All @@ -55,19 +54,14 @@ minikube kubectl -- get pods --namespace kube-system`,
out.ErrLn("Error loading profile config: %v", err)
}

binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}

version := constants.DefaultKubernetesVersion
if cc != nil {
version = cc.KubernetesConfig.KubernetesVersion
}

path, err := machine.CacheBinary(binary, version, runtime.GOOS, runtime.GOARCH)
path, err := cacheKubectlBinary(version)
if err != nil {
exit.WithError("Failed to download kubectl", err)
out.ErrLn("Error caching kubectl: %v", err)
}

glog.Infof("Running %s %v", path, args)
Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) {
if err := doCacheBinaries(k8sVersion); err != nil {
exit.WithError("Failed to cache binaries", err)
}
if _, err := cacheKubectlBinary(k8sVersion); err != nil {
exit.WithError("Failed to cache kubectl", err)
}
waitCacheRequiredImages(cacheGroup)
if err := saveImagesToTarFromConfig(); err != nil {
exit.WithError("Failed to cache images to tar", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/machine/cache_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func KubernetesReleaseURLSHA1(binaryName, version, osName, archName string) stri

// CacheBinary will cache a binary on the host
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)

url := KubernetesReleaseURL(binary, version, osName, archName)
Expand Down
17 changes: 16 additions & 1 deletion test/integration/aaa_download_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -83,12 +84,26 @@ func TestDownloadOnly(t *testing.T) {

// checking binaries downloaded (kubelet,kubeadm)
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)
if err != nil {
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)
}
})
}

Expand Down

0 comments on commit af35331

Please sign in to comment.