Skip to content

Commit

Permalink
Merge pull request #2228 from hellocn9/feature/buildx
Browse files Browse the repository at this point in the history
add a flag --with-buildx to install buildx when container runtime is docker
  • Loading branch information
ks-ci-bot committed May 10, 2024
2 parents ec96e49 + 0f6ff74 commit 86b4551
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/kk/apis/kubekey/v1alpha2/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
DefaultEtcdPort = "2379"
DefaultDockerVersion = "24.0.9"
DefaultCriDockerdVersion = "0.3.10"
DefaultBuildxVersion = "v0.14.0"
DefaultContainerdVersion = "1.7.13"
DefaultRuncVersion = "v1.1.12"
DefaultCrictlVersion = "v1.29.0"
Expand Down
3 changes: 3 additions & 0 deletions cmd/kk/cmd/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CreateClusterOptions struct {
DownloadCmd string
Artifact string
InstallPackages bool
WithBuildx bool

localStorageChanged bool
}
Expand Down Expand Up @@ -122,6 +123,7 @@ func (o *CreateClusterOptions) Run() error {
Artifact: o.Artifact,
InstallPackages: o.InstallPackages,
Namespace: o.CommonOptions.Namespace,
WithBuildx: o.WithBuildx,
}

if o.localStorageChanged {
Expand All @@ -145,6 +147,7 @@ func (o *CreateClusterOptions) AddFlags(cmd *cobra.Command) {
`The user defined command to download the necessary binary files. The first param '%s' is output path, the second param '%s', is the URL`)
cmd.Flags().StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact")
cmd.Flags().BoolVarP(&o.InstallPackages, "with-packages", "", false, "install operation system packages by artifact")
cmd.Flags().BoolVarP(&o.WithBuildx, "with-buildx", "", false, "install buildx when Container runtime is docker")
}

func completionSetting(cmd *cobra.Command) (err error) {
Expand Down
5 changes: 5 additions & 0 deletions cmd/kk/pkg/binaries/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ func K8sFilesDownloadHTTP(kubeConf *common.KubeConf, path, version, arch string,
runc := files.NewKubeBinary("runc", arch, kubekeyapiv1alpha2.DefaultRuncVersion, path, kubeConf.Arg.DownloadCommand)
calicoctl := files.NewKubeBinary("calicoctl", arch, kubekeyapiv1alpha2.DefaultCalicoVersion, path, kubeConf.Arg.DownloadCommand)

buildx := files.NewKubeBinary(common.Buildx, arch, kubekeyapiv1alpha2.DefaultBuildxVersion, path, kubeConf.Arg.DownloadCommand)

binaries := []*files.KubeBinary{kubeadm, kubelet, kubectl, helm, kubecni, crictl, etcd}

if kubeConf.Cluster.Kubernetes.ContainerManager == kubekeyapiv1alpha2.Docker {
binaries = append(binaries, docker)
if kubeConf.Cluster.Kubernetes.IsAtLeastV124() && kubeConf.Cluster.Kubernetes.ContainerManager == common.Docker {
binaries = append(binaries, criDockerd)
}
if kubeConf.Arg.WithBuildx {
binaries = append(binaries, buildx)
}
} else if kubeConf.Cluster.Kubernetes.ContainerManager == kubekeyapiv1alpha2.Containerd {
binaries = append(binaries, containerd, runc)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/kk/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const (
Isula = "isula"
Runc = "runc"

Buildx = "buildx"

// global cache key
// PreCheckModule
NodePreCheck = "nodePreCheck"
Expand Down
1 change: 1 addition & 0 deletions cmd/kk/pkg/common/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Argument struct {
Role string
Type string
EtcdUpgrade bool
WithBuildx bool
}

func NewKubeRuntime(flag string, arg Argument) (*KubeRuntime, error) {
Expand Down
33 changes: 33 additions & 0 deletions cmd/kk/pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ import (
"github.com/pkg/errors"
)

type SyncDockerBuildxPluginBinaries struct {
common.KubeAction
}

func (s *SyncDockerBuildxPluginBinaries) Execute(runtime connector.Runtime) error {
if err := utils.ResetTmpDir(runtime); err != nil {
return err
}

binariesMapObj, ok := s.PipelineCache.Get(common.KubeBinaries + "-" + runtime.RemoteHost().GetArch())
if !ok {
return errors.New("get KubeBinary by pipeline cache failed")
}
binariesMap := binariesMapObj.(map[string]*files.KubeBinary)

buildx, ok := binariesMap[common.Buildx]
if !ok {
return errors.New("get KubeBinary key buildx by pipeline cache failed")
}

dst := filepath.Join(common.TmpDir, buildx.FileName)
if err := runtime.GetRunner().Scp(buildx.Path(), dst); err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("sync docker binaries failed"))
}

if _, err := runtime.GetRunner().SudoCmd(
fmt.Sprintf("mkdir -p /usr/local/lib/docker/cli-plugins && mv %s /usr/local/lib/docker/cli-plugins/docker-buildx && chmod +x /usr/local/lib/docker/cli-plugins/docker-buildx && rm -rf %s", dst, dst),
false); err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("install docker-buildx binaries failed"))
}
return nil
}

type SyncDockerBinaries struct {
common.KubeAction
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/kk/pkg/container/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ func (i *InstallContainerModule) Init() {
}

func InstallDocker(m *InstallContainerModule) []task.Interface {

syncBuildxPluginBinaries := &task.RemoteTask{
Name: "SyncDockerBuildxBinaries",
Desc: "Sync docker buildx binaries",
Hosts: m.Runtime.GetHostsByRole(common.K8s),
Prepare: &prepare.PrepareCollection{
&WithBuildxPlugin{},
},
Action: new(SyncDockerBuildxPluginBinaries),
Parallel: true,
Retry: 2,
}

syncBinaries := &task.RemoteTask{
Name: "SyncDockerBinaries",
Desc: "Sync docker binaries",
Expand Down Expand Up @@ -169,6 +182,7 @@ func InstallDocker(m *InstallContainerModule) []task.Interface {
enableContainerdForDocker,
enableDocker,
dockerLoginRegistry,
syncBuildxPluginBinaries,
}
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/kk/pkg/container/prepares.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ import (
"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
)

type WithBuildxPlugin struct {
common.KubePrepare
}

func (w *WithBuildxPlugin) PreCheck(runtime connector.Runtime) (bool, error) {
if w.KubeConf.Arg.WithBuildx {
return true, nil
} else {
return false, nil
}
}

type DockerExist struct {
common.KubePrepare
Not bool
Expand Down
6 changes: 6 additions & 0 deletions cmd/kk/pkg/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
containerd = "containerd"
runc = "runc"
calicoctl = "calicoctl"
buildx = "buildx"
)

// KubeBinary Type field const
Expand All @@ -66,6 +67,7 @@ const (
REGISTRY = "registry"
CONTAINERD = "containerd"
RUNC = "runc"
BUILD = "buildx"
)

var (
Expand Down Expand Up @@ -225,6 +227,10 @@ func NewKubeBinary(name, arch, version, prePath string, getCmd func(path, url st
if component.Zone == "cn" {
component.Url = fmt.Sprintf("https://kubernetes-release.pek3b.qingstor.com/projectcalico/calico/releases/download/%s/calicoctl-linux-%s", version, arch)
}
case buildx:
component.Type = BUILD
component.FileName = fmt.Sprintf("buildx-%s.linux-%s", version, arch)
component.Url = fmt.Sprintf("https://github.com/docker/buildx/releases/download/%s/buildx-%s.linux-%s", version, version, arch)
default:
logger.Log.Fatalf("unsupported kube binaries %s", name)
}
Expand Down
8 changes: 8 additions & 0 deletions version/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -1082,5 +1082,13 @@
"v3.27.2": "0fd1f65a511338cf9940835987d420c94ab95b5386288ba9673b736a4d347463",
"v3.27.3": "1fc5f58a18d8b1c487b4663fc5cbe23b45bd9d31617debd309f6dfac7c11a8ef"
}
},
"buildx": {
"amd64": {
"v0.14.0": "32f8f17eca35bf2efe6c0e47f40e4692a876f34531b421efc984799a5b41226e"
},
"arm64": {
"v0.14.0": "38bf0ea9c48743edb8243f14272be65a2bad7092228068337aea584309ea664c"
}
}
}

0 comments on commit 86b4551

Please sign in to comment.