Skip to content

Commit

Permalink
Move the cache functions from kubeadm to machine
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Mar 23, 2019
1 parent ae3bd25 commit 3692035
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 53 deletions.
55 changes: 2 additions & 53 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@ package kubeadm

import (
"bytes"
"crypto"
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"path"
"strings"
"time"

"github.com/blang/semver"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/state"
"github.com/golang/glog"
"github.com/jimmidyson/go-download"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -470,11 +466,11 @@ func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error {
for _, bin := range constants.GetKubeadmCachedBinaries() {
bin := bin
g.Go(func() error {
path, err := CacheBinary(bin, cfg.KubernetesVersion)
path, err := machine.CacheBinary(bin, cfg.KubernetesVersion)
if err != nil {
return errors.Wrapf(err, "downloading %s", bin)
}
err = CopyBinary(k.c, bin, path)
err = machine.CopyBinary(k.c, bin, path)
if err != nil {
return errors.Wrapf(err, "copying %s", bin)
}
Expand Down Expand Up @@ -581,50 +577,3 @@ func generateConfig(k8s config.KubernetesConfig, r cruntime.Manager) (string, er

return b.String(), nil
}

// CacheBinary will cache a binary on the host
func CacheBinary(binary, version string) (string, error) {
targetDir := constants.MakeMiniPath("cache", version)
targetFilepath := path.Join(targetDir, binary)

url := constants.GetKubernetesReleaseURL(binary, version)

_, err := os.Stat(targetFilepath)
// If it exists, do no verification and continue
if err == nil {
glog.Infof("Not caching binary, using %s", url)
return targetFilepath, nil
}
if !os.IsNotExist(err) {
return "", errors.Wrapf(err, "stat %s version %s at %s", binary, version, targetDir)
}

if err = os.MkdirAll(targetDir, 0777); err != nil {
return "", errors.Wrapf(err, "mkdir %s", targetDir)
}

options := download.FileOptions{
Mkdirs: download.MkdirAll,
}

options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version)
options.ChecksumHash = crypto.SHA1

console.OutStyle("file-download", "Downloading %s %s", binary, version)
if err := download.ToFile(url, targetFilepath, options); err != nil {
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
}
return targetFilepath, nil
}

// CopyBinary copies previously cached binaries into the path
func CopyBinary(cr bootstrapper.CommandRunner, binary, path string) error {
f, err := assets.NewFileAsset(path, "/usr/bin", binary, "0641")
if err != nil {
return errors.Wrap(err, "new file asset")
}
if err := cr.Copy(f); err != nil {
return errors.Wrapf(err, "copy")
}
return nil
}
79 changes: 79 additions & 0 deletions pkg/minikube/machine/cache_binaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package machine

import (
"crypto"
"os"
"path"

"github.com/golang/glog"
"github.com/jimmidyson/go-download"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/bootstrapper"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
)

// CacheBinary will cache a binary on the host
func CacheBinary(binary, version string) (string, error) {
targetDir := constants.MakeMiniPath("cache", version)
targetFilepath := path.Join(targetDir, binary)

url := constants.GetKubernetesReleaseURL(binary, version)

_, err := os.Stat(targetFilepath)
// If it exists, do no verification and continue
if err == nil {
glog.Infof("Not caching binary, using %s", url)
return targetFilepath, nil
}
if !os.IsNotExist(err) {
return "", errors.Wrapf(err, "stat %s version %s at %s", binary, version, targetDir)
}

if err = os.MkdirAll(targetDir, 0777); err != nil {
return "", errors.Wrapf(err, "mkdir %s", targetDir)
}

options := download.FileOptions{
Mkdirs: download.MkdirAll,
}

options.Checksum = constants.GetKubernetesReleaseURLSHA1(binary, version)
options.ChecksumHash = crypto.SHA1

console.OutStyle("file-download", "Downloading %s %s", binary, version)
if err := download.ToFile(url, targetFilepath, options); err != nil {
return "", errors.Wrapf(err, "Error downloading %s %s", binary, version)
}
return targetFilepath, nil
}

// CopyBinary copies previously cached binaries into the path
func CopyBinary(cr bootstrapper.CommandRunner, binary, path string) error {
f, err := assets.NewFileAsset(path, "/usr/bin", binary, "0641")
if err != nil {
return errors.Wrap(err, "new file asset")
}
if err := cr.Copy(f); err != nil {
return errors.Wrapf(err, "copy")
}
return nil
}

0 comments on commit 3692035

Please sign in to comment.