Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migrate_cri #1519

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cmd/ctl/cri/cri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2022 The KubeSphere Authors.

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 cri

import (
"github.com/kubesphere/kubekey/cmd/ctl/options"
"github.com/spf13/cobra"
)

type MigrateOptions struct {
CommonOptions *options.CommonOptions
}

func NewMigrateOptions() *MigrateOptions {
return &MigrateOptions{
CommonOptions: options.NewCommonOptions(),
}
}

// NewCmdMigrate creates a new Migrate command
func NewCmdCri() *cobra.Command {
o := NewMigrateOptions()
cmd := &cobra.Command{
Use: "cri",
Short: "cri",
}

o.CommonOptions.AddCommonFlag(cmd)
cmd.AddCommand(NewCmdMigrateCri())
return cmd
}
103 changes: 103 additions & 0 deletions cmd/ctl/cri/migrate_cri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2022 The KubeSphere Authors.

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 cri

import (
"github.com/pkg/errors"

"github.com/kubesphere/kubekey/cmd/ctl/options"
"github.com/kubesphere/kubekey/cmd/ctl/util"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/pipelines"
"github.com/spf13/cobra"
)

type MigrateCriOptions struct {
CommonOptions *options.CommonOptions
ClusterCfgFile string
Kubernetes string
EnableKubeSphere bool
KubeSphere string
DownloadCmd string
Artifact string
Type string
Role string
}

func NewMigrateCriOptions() *MigrateCriOptions {
return &MigrateCriOptions{
CommonOptions: options.NewCommonOptions(),
}
}

// NewCmdDeleteCluster creates a new delete cluster command
func NewCmdMigrateCri() *cobra.Command {
o := NewMigrateCriOptions()
cmd := &cobra.Command{
Use: "migrate",
Short: "Migrate a container",
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(o.Validate())
util.CheckErr(o.Run())
},
}

o.CommonOptions.AddCommonFlag(cmd)
o.AddFlags(cmd)
return cmd
}

func (o *MigrateCriOptions) Run() error {
arg := common.Argument{
FilePath: o.ClusterCfgFile,
Debug: o.CommonOptions.Verbose,
KubernetesVersion: o.Kubernetes,
Type: o.Type,
Role: o.Role,
}
return pipelines.MigrateCri(arg, o.DownloadCmd)
}

func (o *MigrateCriOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Role, "role", "", "", "Role groups for migrating. Support: master, worker, all.")
cmd.Flags().StringVarP(&o.Type, "type", "", "", "Type of target CRI. Support: docker, containerd.")
cmd.Flags().StringVarP(&o.ClusterCfgFile, "filename", "f", "", "Path to a configuration file")
cmd.Flags().StringVarP(&o.Kubernetes, "with-kubernetes", "", "", "Specify a supported version of kubernetes")
cmd.Flags().StringVarP(&o.DownloadCmd, "download-cmd", "", "curl -L -o %s %s",
`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")
}

func (o *MigrateCriOptions) Validate() error {
if o.Role == "" {
return errors.New("node Role can not be empty")
}
if o.Role != common.Worker && o.Role != common.Master && o.Role != "all" {

return errors.Errorf("node Role is invalid: %s", o.Role)
}
if o.Type == "" {
return errors.New("cri Type can not be empty")
}
if o.Type != common.Docker && o.Type != common.Conatinerd {
return errors.Errorf("cri Type is invalid: %s", o.Type)
}
if o.ClusterCfgFile == "" {
return errors.New("configuration file can not be empty")
}
return nil
}
14 changes: 9 additions & 5 deletions cmd/ctl/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ package ctl

import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"syscall"

"github.com/kubesphere/kubekey/cmd/ctl/add"
"github.com/kubesphere/kubekey/cmd/ctl/artifact"
"github.com/kubesphere/kubekey/cmd/ctl/cert"
"github.com/kubesphere/kubekey/cmd/ctl/completion"
"github.com/kubesphere/kubekey/cmd/ctl/create"
"github.com/kubesphere/kubekey/cmd/ctl/cri"
"github.com/kubesphere/kubekey/cmd/ctl/delete"
initOs "github.com/kubesphere/kubekey/cmd/ctl/init"
"github.com/kubesphere/kubekey/cmd/ctl/options"
"github.com/kubesphere/kubekey/cmd/ctl/plugin"
"github.com/kubesphere/kubekey/cmd/ctl/upgrade"
"github.com/kubesphere/kubekey/cmd/ctl/version"
"github.com/spf13/cobra"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
)

type KubeKeyOptions struct {
Expand Down Expand Up @@ -115,6 +117,8 @@ func NewKubeKeyCommand(o KubeKeyOptions) *cobra.Command {

cmds.AddCommand(completion.NewCmdCompletion())
cmds.AddCommand(version.NewCmdVersion())

cmds.AddCommand(cri.NewCmdCri())
return cmds
}

Expand Down
48 changes: 48 additions & 0 deletions docs/commands/kk-cri-migrate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# NAME
**kk cri migrate**: migrate your cri smoothly to docker/containerd with this command.

# DESCRIPTION
migrate your cri smoothly to docker/containerd with this command.

# OPTIONS

## **--role**
Which node(worker/master/all) to migrate.

## **--type**
Which cri(docker/containerd) to migrate.

## **--debug**
Print detailed information. The default is `false`.

## **--filename, -f**
Path to a configuration file.This option is required.

## **--yes, -y**
Skip confirm check. The default is `false`.

# EXAMPLES
Migrate your master node's cri smoothly to docker.
```
$ ./kk cri migrate --role master --type docker -f config-sample.yaml
```
Migrate your master node's cri smoothly to containerd.
```
$ ./kk cri migrate --role master --type containerd -f config-sample.yaml
```
Migrate your worker node's cri smoothly to docker.
```
$ ./kk cri migrate --role worker --type docker -f config-sample.yaml
```
Migrate your worker node's cri smoothly to containerd.
```
$ ./kk cri migrate --role worker --type containerd -f config-sample.yaml
```
Migrate all your node's cri smoothly to containerd.
```
$ ./kk cri migrate --role all --type containerd -f config-sample.yaml
```
Migrate all your node's cri smoothly to docker.
```
$ ./kk cri migrate --role all --type docker -f config-sample.yaml
```
47 changes: 46 additions & 1 deletion pkg/binaries/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package binaries

import (
"fmt"
"os/exec"

kubekeyapiv1alpha2 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha2"
"github.com/kubesphere/kubekey/pkg/common"
"github.com/kubesphere/kubekey/pkg/core/cache"
"github.com/kubesphere/kubekey/pkg/core/logger"
"github.com/kubesphere/kubekey/pkg/core/util"
"github.com/kubesphere/kubekey/pkg/files"
"github.com/pkg/errors"
"os/exec"
)

// K8sFilesDownloadHTTP defines the kubernetes' binaries that need to be downloaded in advance and downloads them.
Expand Down Expand Up @@ -145,3 +146,47 @@ func KubernetesArtifactBinariesDownload(manifest *common.ArtifactManifest, path,

return nil
}

// CriDownloadHTTP defines the kubernetes' binaries that need to be downloaded in advance and downloads them.
func CriDownloadHTTP(kubeConf *common.KubeConf, path, arch string, pipelineCache *cache.Cache) error {

binaries := []*files.KubeBinary{}
switch kubeConf.Arg.Type {
case common.Docker:
docker := files.NewKubeBinary("docker", arch, kubekeyapiv1alpha2.DefaultDockerVersion, path, kubeConf.Arg.DownloadCommand)
binaries = append(binaries, docker)
case common.Conatinerd:
containerd := files.NewKubeBinary("containerd", arch, kubekeyapiv1alpha2.DefaultContainerdVersion, path, kubeConf.Arg.DownloadCommand)
runc := files.NewKubeBinary("runc", arch, kubekeyapiv1alpha2.DefaultRuncVersion, path, kubeConf.Arg.DownloadCommand)
crictl := files.NewKubeBinary("crictl", arch, kubekeyapiv1alpha2.DefaultCrictlVersion, path, kubeConf.Arg.DownloadCommand)
binaries = append(binaries, containerd, runc, crictl)
default:
}
binariesMap := make(map[string]*files.KubeBinary)
for _, binary := range binaries {
if err := binary.CreateBaseDir(); err != nil {
return errors.Wrapf(errors.WithStack(err), "create file %s base dir failed", binary.FileName)
}

logger.Log.Messagef(common.LocalHost, "downloading %s %s %s ...", arch, binary.ID, binary.Version)

binariesMap[binary.ID] = binary
if util.IsExist(binary.Path()) {
// download it again if it's incorrect
if err := binary.SHA256Check(); err != nil {
p := binary.Path()
_ = exec.Command("/bin/sh", "-c", fmt.Sprintf("rm -f %s", p)).Run()
} else {
logger.Log.Messagef(common.LocalHost, "%s is existed", binary.ID)
continue
}
}

if err := binary.Download(); err != nil {
return fmt.Errorf("Failed to download %s binary: %s error: %w ", binary.ID, binary.GetCmd(), err)
}
}

pipelineCache.Set(common.KubeBinaries+"-"+arch, binariesMap)
return nil
}
31 changes: 31 additions & 0 deletions pkg/binaries/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,34 @@ func (n *RegistryPackageModule) Init() {
download,
}
}

type CriBinariesModule struct {
common.KubeModule
}

func (i *CriBinariesModule) Init() {
i.Name = "CriBinariesModule"
i.Desc = "Download Cri package"
switch i.KubeConf.Arg.Type {
case common.Docker:
i.Tasks = CriBinaries(i)
case common.Conatinerd:
i.Tasks = CriBinaries(i)
default:
}

}

func CriBinaries(p *CriBinariesModule) []task.Interface {

download := &task.LocalTask{
Name: "DownloadCriPackage",
Desc: "Download Cri package",
Action: new(CriDownload),
}

p.Tasks = []task.Interface{
download,
}
return p.Tasks
}
26 changes: 26 additions & 0 deletions pkg/binaries/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,29 @@ func (k *RegistryPackageDownload) Execute(runtime connector.Runtime) error {

return nil
}

type CriDownload struct {
common.KubeAction
}

func (d *CriDownload) Execute(runtime connector.Runtime) error {
cfg := d.KubeConf.Cluster
archMap := make(map[string]bool)
for _, host := range cfg.Hosts {
switch host.Arch {
case "amd64":
archMap["amd64"] = true
case "arm64":
archMap["arm64"] = true
default:
return errors.New(fmt.Sprintf("Unsupported architecture: %s", host.Arch))
}
}

for arch := range archMap {
if err := CriDownloadHTTP(d.KubeConf, runtime.GetWorkDir(), arch, d.PipelineCache); err != nil {
return err
}
}
return nil
}
20 changes: 20 additions & 0 deletions pkg/bootstrap/confirm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,23 @@ func (c *CheckFileExistModule) Init() {
check,
}
}

type MigrateCriConfirmModule struct {
common.KubeModule
}

func (d *MigrateCriConfirmModule) Init() {
d.Name = "MigrateCriConfirmModule"
d.Desc = "Display Migrate Cri form"

display := &task.LocalTask{
Name: "ConfirmForm",
Desc: "Display confirmation form",
Action: &MigrateCri{},
}

d.Tasks = []task.Interface{
display,
}

}
Loading