Skip to content

Commit

Permalink
ability to set a mirror for downloading kubectl, kubelet, & kubeadm
Browse files Browse the repository at this point in the history
  • Loading branch information
presztak committed Nov 22, 2021
1 parent 9e4ca85 commit 26e98e0
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var dashboardCmd = &cobra.Command{
}

out.ErrT(style.Launch, "Launching proxy ...")
p, hostPort, err := kubectlProxy(kubectlVersion, cname, dashboardExposedPort)
p, hostPort, err := kubectlProxy(kubectlVersion, co.Config.KubeBinariesMirror, cname, dashboardExposedPort)
if err != nil {
exit.Error(reason.HostKubectlProxy, "kubectl proxy", err)
}
Expand Down Expand Up @@ -132,15 +132,15 @@ var dashboardCmd = &cobra.Command{
}

// kubectlProxy runs "kubectl proxy", returning host:port
func kubectlProxy(kubectlVersion string, contextName string, port int) (*exec.Cmd, string, error) {
func kubectlProxy(kubectlVersion string, binaryURL string, contextName string, port int) (*exec.Cmd, string, error) {
// port=0 picks a random system port

kubectlArgs := []string{"--context", contextName, "proxy", "--port", strconv.Itoa(port)}

var cmd *exec.Cmd
if kubectl, err := exec.LookPath("kubectl"); err == nil {
cmd = exec.Command(kubectl, kubectlArgs...)
} else if cmd, err = KubectlCommand(kubectlVersion, kubectlArgs...); err != nil {
} else if cmd, err = KubectlCommand(kubectlVersion, binaryURL, kubectlArgs...); err != nil {
return nil, "", err
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/minikube/cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ host. Please be aware that when using --ssh all paths will apply to the remote m
args = append(cluster, args...)
}

c, err := KubectlCommand(version, args...)
c, err := KubectlCommand(version, cc.KubeBinariesMirror, args...)
if err != nil {
out.ErrLn("Error caching kubectl: %v", err)
os.Exit(1)
Expand Down Expand Up @@ -134,12 +134,12 @@ func kubeconfigPath(cfg config.ClusterConfig) string {
}

// KubectlCommand will return kubectl command with a version matching the cluster
func KubectlCommand(version string, args ...string) (*exec.Cmd, error) {
func KubectlCommand(version, binaryURL string, args ...string) (*exec.Cmd, error) {
if version == "" {
version = constants.DefaultKubernetesVersion
}

path, err := node.CacheKubectlBinary(version)
path, err := node.CacheKubectlBinary(version, binaryURL)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion hack/preload-images/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func generateTarball(kubernetesVersion, containerRuntime, tarballFilename string

sm := sysinit.New(runner)

if err := bsutil.TransferBinaries(kcfg, runner, sm); err != nil {
if err := bsutil.TransferBinaries(kcfg, runner, sm, ""); err != nil {
return errors.Wrap(err, "transferring k8s binaries")
}
// Create image tarball
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/bootstrapper/bsutil/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

// TransferBinaries transfers all required Kubernetes binaries
func TransferBinaries(cfg config.KubernetesConfig, c command.Runner, sm sysinit.Manager) error {
func TransferBinaries(cfg config.KubernetesConfig, c command.Runner, sm sysinit.Manager, binariesURL string) error {
ok, err := binariesExist(cfg, c)
if err == nil && ok {
klog.Info("Found k8s binaries, skipping transfer")
Expand All @@ -56,7 +56,7 @@ func TransferBinaries(cfg config.KubernetesConfig, c command.Runner, sm sysinit.
for _, name := range constants.KubernetesReleaseBinaries {
name := name
g.Go(func() error {
src, err := download.Binary(name, cfg.KubernetesVersion, "linux", runtime.GOARCH)
src, err := download.Binary(name, cfg.KubernetesVersion, "linux", runtime.GOARCH, binariesURL)
if err != nil {
return errors.Wrapf(err, "downloading %s", name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ func (k *Bootstrapper) UpdateNode(cfg config.ClusterConfig, n config.Node, r cru

sm := sysinit.New(k.c)

if err := bsutil.TransferBinaries(cfg.KubernetesConfig, k.c, sm); err != nil {
if err := bsutil.TransferBinaries(cfg.KubernetesConfig, k.c, sm, cfg.KubeBinariesMirror); err != nil {
return errors.Wrap(err, "downloading binaries")
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/minikube/download/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ import (
"k8s.io/minikube/pkg/minikube/localpath"
)

// DefaultKubeBinariesURL returns a URL to kube binaries
func DefaultKubeBinariesURL() string {
return fmt.Sprintf("https://%s/kubernetes-release/release", downloadHost)
}

// binaryWithChecksumURL gets the location of a Kubernetes binary
func binaryWithChecksumURL(binaryName, version, osName, archName string) (string, error) {
base := fmt.Sprintf("https://%s/kubernetes-release/release/%s/bin/%s/%s/%s", downloadHost, version, osName, archName, binaryName)
func binaryWithChecksumURL(binaryName, version, osName, archName, binaryURL string) (string, error) {
if binaryURL == "" {
binaryURL = DefaultKubeBinariesURL()
}

base := fmt.Sprintf("%s/%s/bin/%s/%s/%s", binaryURL, version, osName, archName, binaryName)
v, err := semver.Make(version[1:])
if err != nil {
return "", err
Expand All @@ -45,12 +54,12 @@ func binaryWithChecksumURL(binaryName, version, osName, archName string) (string
}

// Binary will download a binary onto the host
func Binary(binary, version, osName, archName string) (string, error) {
func Binary(binary, version, osName, archName, binaryURL string) (string, error) {
targetDir := localpath.MakeMiniPath("cache", osName, version)
targetFilepath := path.Join(targetDir, binary)
targetLock := targetFilepath + ".lock"

url, err := binaryWithChecksumURL(binary, version, osName, archName)
url, err := binaryWithChecksumURL(binary, version, osName, archName, binaryURL)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testBinaryDownloadPreventsMultipleDownload(t *testing.T) {
var group sync.WaitGroup
group.Add(2)
dlCall := func() {
if _, err := Binary("kubectl", "v1.20.2", "linux", "amd64"); err != nil {
if _, err := Binary("kubectl", "v1.20.2", "linux", "amd64", ""); err != nil {
t.Errorf("Failed to download binary: %+v", err)
}
group.Done()
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/machine/cache_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func isExcluded(binary string, excludedBinaries []string) bool {
}

// CacheBinariesForBootstrapper will cache binaries for a bootstrapper
func CacheBinariesForBootstrapper(version string, clusterBootstrapper string, excludeBinaries []string) error {
func CacheBinariesForBootstrapper(version string, clusterBootstrapper string, excludeBinaries []string, binariesURL string) error {
binaries := bootstrapper.GetCachedBinaryList(clusterBootstrapper)

var g errgroup.Group
Expand All @@ -53,7 +53,7 @@ func CacheBinariesForBootstrapper(version string, clusterBootstrapper string, ex
}
bin := bin // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
if _, err := download.Binary(bin, version, "linux", detect.EffectiveArch()); err != nil {
if _, err := download.Binary(bin, version, "linux", detect.EffectiveArch(), binariesURL); err != nil {
return errors.Wrapf(err, "caching binary %s", bin)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/machine/cache_binaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestCacheBinariesForBootstrapper(t *testing.T) {
for _, test := range tc {
t.Run(test.version, func(t *testing.T) {
os.Setenv("MINIKUBE_HOME", test.minikubeHome)
err := CacheBinariesForBootstrapper(test.version, test.clusterBootstrapper, nil)
err := CacheBinariesForBootstrapper(test.version, test.clusterBootstrapper, nil, "")
if err != nil && !test.err {
t.Fatalf("Got unexpected error %v", err)
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestExcludedBinariesNotDownloaded(t *testing.T) {
}
}()

if err := CacheBinariesForBootstrapper("v1.16.0", clusterBootstrapper, []string{binaryToExclude}); err != nil {
if err := CacheBinariesForBootstrapper("v1.16.0", clusterBootstrapper, []string{binaryToExclude}, ""); err != nil {
t.Errorf("Failed to cache binaries: %v", err)
}
}
14 changes: 8 additions & 6 deletions pkg/minikube/node/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ func handleDownloadOnly(cacheGroup, kicGroup *errgroup.Group, k8sVersion, contai
if !viper.GetBool("download-only") {
return
}
if err := doCacheBinaries(k8sVersion, containerRuntime, driverName); err != nil {

binariesURL := viper.GetString("kube-binaries-mirror")
if err := doCacheBinaries(k8sVersion, containerRuntime, driverName, binariesURL); err != nil {
exit.Error(reason.InetCacheBinaries, "Failed to cache binaries", err)
}
if _, err := CacheKubectlBinary(k8sVersion); err != nil {
if _, err := CacheKubectlBinary(k8sVersion, binariesURL); err != nil {
exit.Error(reason.InetCacheKubectl, "Failed to cache kubectl", err)
}
waitCacheRequiredImages(cacheGroup)
Expand All @@ -94,22 +96,22 @@ func handleDownloadOnly(cacheGroup, kicGroup *errgroup.Group, k8sVersion, contai
}

// CacheKubectlBinary caches the kubectl binary
func CacheKubectlBinary(k8sVersion string) (string, error) {
func CacheKubectlBinary(k8sVersion, binaryURL string) (string, error) {
binary := "kubectl"
if runtime.GOOS == "windows" {
binary = "kubectl.exe"
}

return download.Binary(binary, k8sVersion, runtime.GOOS, detect.EffectiveArch())
return download.Binary(binary, k8sVersion, runtime.GOOS, detect.EffectiveArch(), binaryURL)
}

// doCacheBinaries caches Kubernetes binaries in the foreground
func doCacheBinaries(k8sVersion, containerRuntime, driverName string) error {
func doCacheBinaries(k8sVersion, containerRuntime, driverName, binariesURL string) error {
existingBinaries := constants.KubernetesReleaseBinaries
if !download.PreloadExists(k8sVersion, containerRuntime, driverName) {
existingBinaries = nil
}
return machine.CacheBinariesForBootstrapper(k8sVersion, viper.GetString(cmdcfg.Bootstrapper), existingBinaries)
return machine.CacheBinariesForBootstrapper(k8sVersion, viper.GetString(cmdcfg.Bootstrapper), existingBinaries, binariesURL)
}

// beginDownloadKicBaseImage downloads the kic image
Expand Down

0 comments on commit 26e98e0

Please sign in to comment.