From 10bfc7a6a7561a2c8ee37e49392b4c42d5df7cc7 Mon Sep 17 00:00:00 2001 From: Raiden Shogun Date: Tue, 12 Mar 2024 01:38:49 +0100 Subject: [PATCH] feat: download crictl at preload and runtime Co-authored-by: Steven Powell <44844360+spowelljr@users.noreply.github.com> --- hack/preload-images/generate.go | 5 +++++ pkg/minikube/bootstrapper/bsutil/binaries.go | 15 +++++++++++++++ pkg/minikube/bootstrapper/kubeadm/kubeadm.go | 17 +++++++++++++++-- pkg/minikube/download/binary.go | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/hack/preload-images/generate.go b/hack/preload-images/generate.go index 14c3e182a94f..c5fad63682df 100644 --- a/hack/preload-images/generate.go +++ b/hack/preload-images/generate.go @@ -127,6 +127,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") diff --git a/pkg/minikube/bootstrapper/bsutil/binaries.go b/pkg/minikube/bootstrapper/bsutil/binaries.go index 2800f489dfdd..7fff5dced462 100644 --- a/pkg/minikube/bootstrapper/bsutil/binaries.go +++ b/pkg/minikube/bootstrapper/bsutil/binaries.go @@ -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) diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go index c72ff85e3304..cc61fc55fb09 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go @@ -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" @@ -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"), @@ -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") } diff --git a/pkg/minikube/download/binary.go b/pkg/minikube/download/binary.go index 21b3edd5dabb..67d413a0e4d7 100644 --- a/pkg/minikube/download/binary.go +++ b/pkg/minikube/download/binary.go @@ -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" @@ -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 +}