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

Docker to containerd automated migration #1362

Merged
merged 11 commits into from
Jun 9, 2021
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/aws/aws-sdk-go v1.36.2
github.com/docker/distribution v2.7.1+incompatible
github.com/dominodatalab/os-release v0.0.0-20190522011736-bcdb4a3e3c2f
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.4
github.com/imdario/mergo v0.3.11
github.com/koron-go/prefixw v0.0.0-20181013140428-271b207a7572
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.2.2-0.20190730201129-28a6bbf47e48/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
Expand Down Expand Up @@ -512,6 +513,7 @@ github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-2019
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -1113,6 +1115,7 @@ golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
Expand All @@ -1122,6 +1125,7 @@ golang.org/x/tools v0.0.0-20201105220310-78b158585360/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201202200335-bef1c476418a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/clientutil/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package clientutil
import (
"context"

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/config-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type listImagesOpts struct {
Filter string `longflag:"filter"`
}

func imagesCmd(rootFlags *pflag.FlagSet) *cobra.Command {
func configImagesCmd(rootFlags *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "images",
Short: "images manipulations",
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ func configCmd(rootFlags *pflag.FlagSet) *cobra.Command {
Short: "Commands for working with the KubeOneCluster configuration manifests",
}

cmd.AddCommand(printCmd())
cmd.AddCommand(migrateCmd(rootFlags))
cmd.AddCommand(machinedeploymentsCmd(rootFlags))
cmd.AddCommand(imagesCmd(rootFlags))
cmd.AddCommand(configPrintCmd())
cmd.AddCommand(configMigrateCmd(rootFlags))
cmd.AddCommand(configMachinedeploymentsCmd(rootFlags))
cmd.AddCommand(configImagesCmd(rootFlags))

return cmd
}

// printCmd setups the print command
func printCmd() *cobra.Command {
// configPrintCmd setups the print command
func configPrintCmd() *cobra.Command {
opts := &printOpts{}
cmd := &cobra.Command{
Use: "print",
Expand Down Expand Up @@ -177,8 +177,8 @@ func printCmd() *cobra.Command {
return cmd
}

// migrateCmd setups the migrate command
func migrateCmd(rootFlags *pflag.FlagSet) *cobra.Command {
// configMigrateCmd setups the migrate command
func configMigrateCmd(rootFlags *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Migrate the v1alpha1 KubeOneCluster manifest to the v1beta1 version",
Expand All @@ -203,8 +203,8 @@ The new manifest is printed on the standard output.
return cmd
}

// machinedeploymentsCmd setups the machinedeployments command
func machinedeploymentsCmd(rootFlags *pflag.FlagSet) *cobra.Command {
// configMachinedeploymentsCmd setups the machinedeployments command
func configMachinedeploymentsCmd(rootFlags *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "machinedeployments",
Short: "Print the manifest for creating MachineDeployments",
Expand Down
65 changes: 65 additions & 0 deletions pkg/cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2019 The KubeOne 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 cmd

import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"k8c.io/kubeone/pkg/tasks"
)

func migrateCmd(fs *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Commands for running different migrations",
}

cmd.AddCommand(migrateToContainerdCmd(fs))
return cmd
}

func migrateToContainerdCmd(fs *pflag.FlagSet) *cobra.Command {
return &cobra.Command{
Use: "to-containerd",
Short: "Migrate live cluster from docker to containerd",
Long: heredoc.Doc(`

Following the dockershim deprecation https://kubernetes.io/blog/2020/12/02/dockershim-faq/
this command helps to migrate Container Runtime to ContainerD.
`),
RunE: func(_ *cobra.Command, _ []string) error {
gopts, err := persistentGlobalOptions(fs)
if err != nil {
return errors.Wrap(err, "unable to get global flags")
}

return runMigrateToContainerd(gopts)
},
}
}

func runMigrateToContainerd(opts *globalOptions) error {
s, err := opts.BuildState()
if err != nil {
return errors.Wrap(err, "failed to initialize State")
}

return errors.Wrap(tasks.WithContainerDMigration(nil).Run(s), "failed to get cluster status")
}
1 change: 1 addition & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func newRoot() *cobra.Command {
versionCmd(),
statusCmd(fs),
proxyCmd(fs),
migrateCmd(fs),
completionCmd(rootCmd),
documentCmd(rootCmd),
)
Expand Down
30 changes: 30 additions & 0 deletions pkg/scripts/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,37 @@ limitations under the License.

package scripts

import (
"github.com/MakeNowJust/heredoc/v2"
)

const (
defaultKubernetesCNIVersion = "0.8.7"
defaultCriToolsVersion = "1.21.0"
)

var migrateToContainerdScriptTemplate = heredoc.Doc(`
sudo systemctl stop kubelet
sudo docker ps -q | xargs sudo docker stop || true
sudo docker ps -qa | xargs sudo docker rm || true

{{ if .GENERATE_CONTAINERD_CONFIG -}}
{{ template "containerd-config" . }}
{{- end }}

{{- /*
/var/lib/kubelet/kubeadm-flags.env should be modified by the caller of
this script, following flags should be added:
* --container-runtime=remote
* --container-runtime-endpoint=unix:///run/containerd/containerd.sock
*/ -}}

sudo systemctl restart kubelet
`)

func MigrateToContainerd(insecureRegistry string, generateContainerdConfig bool) (string, error) {
return Render(migrateToContainerdScriptTemplate, Data{
"INSECURE_REGISTRY": insecureRegistry,
"GENERATE_CONTAINERD_CONFIG": generateContainerdConfig,
})
}
38 changes: 38 additions & 0 deletions pkg/scripts/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,44 @@ func TestKubeadmDebian(t *testing.T) {
}
}

func TestMigrateToContainerd(t *testing.T) {
t.Parallel()

tests := []struct {
name string
insecureRegistry string
generateContainerdConfig bool
err error
}{
{
name: "simple",
generateContainerdConfig: true,
},
{
name: "flatcat",
generateContainerdConfig: false,
},
{
name: "insecureRegistry",
insecureRegistry: "some.registry",
generateContainerdConfig: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := MigrateToContainerd(tt.insecureRegistry, tt.generateContainerdConfig)
if err != tt.err {
t.Errorf("MigrateToContainerd() error = %v, wantErr %v", err, tt.err)
return
}

testhelper.DiffOutput(t, testhelper.FSGoldenName(t), got, *updateFlag)
})
}
}

func TestKubeadmCentOS(t *testing.T) {
t.Parallel()

Expand Down
Loading