Skip to content

Commit

Permalink
feat: download crictl at preload and runtime
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Powell <[email protected]>
  • Loading branch information
ComradeProgrammer and spowelljr committed Mar 13, 2024
1 parent cdf43db commit c654883
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions hack/preload-images/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/minikube/pkg/minikube/bootstrapper/images"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/detect"
"k8s.io/minikube/pkg/minikube/localpath"
Expand Down Expand Up @@ -127,6 +128,11 @@ func generateTarball(kubernetesVersion, containerRuntime, tarballFilename string
if err := bsutil.TransferBinaries(kcfg, runner, sm, ""); err != nil {
return errors.Wrap(err, "transferring k8s binaries")
}
// download crictl if needed

if err := bsutil.TransferCrictl(kcfg, runner); err != nil {
return errors.Wrap(err, "transferring crictl")
}
// Create image tarball
if err := createImageTarball(tarballFilename, containerRuntime); err != nil {
return errors.Wrap(err, "create tarball")
Expand Down
15 changes: 15 additions & 0 deletions pkg/minikube/bootstrapper/bsutil/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ import (
"k8s.io/minikube/pkg/minikube/vmpath"
)

// TransferCrictl transfers the crictl binary to the machine
func TransferCrictl(cfg config.KubernetesConfig, c command.Runner) error {

src, err := download.DownloadCrictlBinary(cfg.KubernetesVersion)
if err != nil {
return errors.Wrapf(err, "downloading crictl")
}
dir := binRoot(cfg.KubernetesVersion)
dst := path.Join(dir, "crictl")
if err := machine.CopyBinary(c, src, dst); err != nil {
return errors.Wrapf(err, "copybinary %s -> %s", src, dst)
}
return nil
}

// TransferBinaries transfers all required Kubernetes binaries
func TransferBinaries(cfg config.KubernetesConfig, c command.Runner, sm sysinit.Manager, binariesURL string) error {
ok, err := binariesExist(cfg, c)
Expand Down
17 changes: 15 additions & 2 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"github.com/spf13/viper"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -939,6 +940,19 @@ func (k *Bootstrapper) UpdateNode(cfg config.ClusterConfig, n config.Node, r cru

klog.Infof("kubelet %s config:\n%+v", kubeletCfg, cfg.KubernetesConfig)

sm := sysinit.New(k.c)

if err := bsutil.TransferBinaries(cfg.KubernetesConfig, k.c, sm, cfg.BinaryMirror); err != nil {
return errors.Wrap(err, "downloading binaries")
}
// download crictl if needed.
// since kubernetes v1.29 we need to have matching crictl and kubernetes version.
if !viper.GetBool("preload") && cfg.KubernetesConfig.KubernetesVersion != constants.DefaultKubernetesVersion {
if err := bsutil.TransferCrictl(cfg.KubernetesConfig, k.c); err != nil {
return errors.Wrap(err, "transferring crictl")
}
}

files := []assets.CopyableFile{
assets.NewMemoryAssetTarget(kubeletCfg, bsutil.KubeletSystemdConfFile, "0644"),
assets.NewMemoryAssetTarget(kubeletService, bsutil.KubeletServiceFile, "0644"),
Expand Down Expand Up @@ -973,8 +987,7 @@ func (k *Bootstrapper) UpdateNode(cfg config.ClusterConfig, n config.Node, r cru
}
}

sm := sysinit.New(k.c)

sm = sysinit.New(k.c)
if err := bsutil.TransferBinaries(cfg.KubernetesConfig, k.c, sm, cfg.BinaryMirror); err != nil {
return errors.Wrap(err, "downloading binaries")
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/minikube/download/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"runtime"

"k8s.io/minikube/pkg/minikube/detect"
"k8s.io/minikube/pkg/util"

"github.com/blang/semver/v4"
"github.com/pkg/errors"
Expand Down Expand Up @@ -88,3 +89,22 @@ func Binary(binary, version, osName, archName, binaryURL string) (string, error)
}
return targetFilepath, nil
}

// DownloadCrictlBinary download the crictl tar archive to the cache folder and untar it
func DownloadCrictlBinary(k8sversion string) (string, error) {
v, err := util.ParseKubernetesVersion(k8sversion)
if err != nil {
return "", err
}
crictlVersion := fmt.Sprintf("v%d.%d.0", v.Major, v.Minor)
url := fmt.Sprintf(
"https://github.com/kubernetes-sigs/cri-tools/releases/download/%s/crictl-%s-linux-%s.tar.gz",
crictlVersion, crictlVersion, runtime.GOARCH)
targetDir := localpath.MakeMiniPath("cache", "linux", runtime.GOARCH, k8sversion)
targetPath := path.Join(targetDir, "crictl")
if err := download(url, targetPath); err != nil {
return "", errors.Wrapf(err, "download failed: %s", url)
}

return targetPath, nil
}

0 comments on commit c654883

Please sign in to comment.