From 839bf7212c0b48cf6bb0cc6f24900a9e2cb4e17f Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Wed, 1 Mar 2023 16:46:12 +0800 Subject: [PATCH] add conversion Signed-off-by: cuisongliu --- .github/workflows/go.yml | 2 +- cmd/conversion.go | 12 ++++++++++++ pkg/apply/applydriver.go | 32 +++++++++++++++++++++++++++++++- pkg/cri/comparable.go | 19 ------------------- pkg/cri/sync.go | 25 +++++++++++++++++++++++++ pkg/merge/testdata/config.yaml | 2 +- pkg/merge/testdata/default.yaml | 12 ++++++------ pkg/version/default_version.go | 6 +++--- types/v1/validation.go | 7 +++++++ types/v1/validation_test.go | 24 ++++++++++++++++++++++++ 10 files changed, 110 insertions(+), 31 deletions(-) create mode 100644 pkg/cri/sync.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0f452be..e75d9a6 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,4 +29,4 @@ jobs: - name: Test conversion run: | - dist/runtime-ctl_linux_amd64_v1/runtime-ctl conversion -f pkg/merge/testdata/config.yaml -f pkg/merge/testdata/config-2.yaml -d pkg/merge/testdata/default.yaml \ No newline at end of file + dist/runtime-ctl_linux_amd64_v1/runtime-ctl conversion -f pkg/merge/testdata/config.yaml -f pkg/merge/testdata/config-2.yaml -d pkg/merge/testdata/default.yaml --yaml \ No newline at end of file diff --git a/cmd/conversion.go b/cmd/conversion.go index dabadfd..d61abf4 100644 --- a/cmd/conversion.go +++ b/cmd/conversion.go @@ -16,6 +16,7 @@ package cmd import ( "github.com/labring-actions/runtime-ctl/pkg/apply" + "github.com/labring-actions/runtime-ctl/pkg/cri" "github.com/pkg/errors" "k8s.io/klog/v2" @@ -53,6 +54,17 @@ var conversionCmd = &cobra.Command{ if err := applier.WithConfigFiles(conversionFiles...); err != nil { return errors.WithMessage(err, "validate config error") } + klog.Info("begin sync cri all versions") + s := &cri.Sync{} + if err := s.Do(); err != nil { + return errors.WithMessage(err, "sync cri error") + } + klog.Info("sync cri all versions finished") + + if err := applier.WithCRISync(s); err != nil { + return errors.WithMessage(err, "set sync cri error") + } + return applier.WithYaml(yamlEnable) }, } diff --git a/pkg/apply/applydriver.go b/pkg/apply/applydriver.go index 2e057b3..9a62b4f 100644 --- a/pkg/apply/applydriver.go +++ b/pkg/apply/applydriver.go @@ -32,6 +32,7 @@ type Applier struct { RuntimeConfigs []v1.RuntimeConfig DefaultFile string Yaml bool + Sync *cri.Sync } func NewApplier() *Applier { @@ -49,10 +50,23 @@ func (a *Applier) WithDefaultFile(file string) error { if err = v1.ValidationDefaultComponent(cfg.Version); err != nil { return err } + const printInfo = `All Default Version: + docker: %s + containerd: %s + sealos: %s + crun: %s + runc: %s +` + klog.Infof(printInfo, cfg.Version.Docker, cfg.Version.Containerd, cfg.Version.Sealos, cfg.Version.Crun, cfg.Version.Runc) a.DefaultFile = file return nil } +func (a *Applier) WithCRISync(sync *cri.Sync) error { + a.Sync = sync + return nil +} + func (a *Applier) WithYaml(yamlEnable bool) error { a.Yaml = yamlEnable return nil @@ -112,17 +126,33 @@ func (a *Applier) Apply() error { localRuntime := a.StatusComponents[i] switch rt.Runtime { case v1.RuntimeK8s: + kubeBigVersion := v1.ToBigVersion(rt.RuntimeVersion) switch rt.CRIType { case v1.CRIDocker: dockerVersion, criDockerVersion := cri.FetchDockerVersion(rt.RuntimeVersion) if dockerVersion != "" { localRuntime.CRIVersion = dockerVersion } else { - localRuntime.CRIVersion = a.RuntimeConfigs[i].Version.Docker + cfgDocker := a.RuntimeConfigs[i].Version.Docker + localRuntime.CRIVersion = v1.ToBigVersion(cfgDocker) } + versions := a.Sync.Docker[localRuntime.CRIVersion] + sortList := cri.List(versions) + newVersion := sortList[len(sortList)-1] + klog.Infof("docker version is %s, docker using latest version: %s", localRuntime.CRIVersion, newVersion) + localRuntime.CRIVersion = newVersion localRuntime.CRIDockerd = criDockerVersion case v1.CRIContainerd: localRuntime.CRIVersion = a.RuntimeConfigs[i].Version.Containerd + containerdBigVersion := v1.ToBigVersion(a.RuntimeConfigs[i].Version.Containerd) + if v1.Compare(kubeBigVersion, "1.26") && !v1.Compare(containerdBigVersion, "1.6") { + return fmt.Errorf("if kubernetes version gt 1.26 your containerd must be gt 1.6") + } + case v1.CRICRIO: + versions := a.Sync.CRIO[kubeBigVersion] + sortList := cri.List(versions) + newVersion := sortList[len(sortList)-1] + klog.Infof("kube version is %s, crio using latest version: %s", rt.RuntimeVersion, newVersion) } newVersion, err := k8s.FetchFinalVersion(rt.RuntimeVersion) diff --git a/pkg/cri/comparable.go b/pkg/cri/comparable.go index b4af3ee..d6562b5 100644 --- a/pkg/cri/comparable.go +++ b/pkg/cri/comparable.go @@ -23,25 +23,6 @@ import ( "strings" ) -//type Version string -// -//type VersionList []Version -// -//func (a VersionList) Len() int { return len(a) } -// -//func (a VersionList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -// -//func (a VersionList) Less(i, j int) bool { -// left := a[i] -// right := a[j] -// -// lstr := strings.Split(string(left), ".")[2] -// rstr := strings.Split(string(right), ".")[2] -// lint, _ := strconv.Atoi(lstr) -// rint, _ := strconv.Atoi(rstr) -// return lint < rint -//} - // List returns the contents as a sorted T slice. // // This is a separate function and not a method because not all types supported diff --git a/pkg/cri/sync.go b/pkg/cri/sync.go new file mode 100644 index 0000000..923c5fd --- /dev/null +++ b/pkg/cri/sync.go @@ -0,0 +1,25 @@ +package cri + +import "k8s.io/apimachinery/pkg/util/sets" + +type Sync struct { + Docker map[string]sets.Set[string] + CRIO map[string]sets.Set[string] +} + +func (s *Sync) Do() error { + var err error + if s.CRIO == nil { + s.CRIO, err = FetchCRIOAllVersion() + if err != nil { + return err + } + } + if s.Docker == nil { + s.Docker, err = FetchDockerAllVersion() + if err != nil { + return err + } + } + return nil +} diff --git a/pkg/merge/testdata/config.yaml b/pkg/merge/testdata/config.yaml index c8e087c..f53720d 100644 --- a/pkg/merge/testdata/config.yaml +++ b/pkg/merge/testdata/config.yaml @@ -18,4 +18,4 @@ config: - containerd runtime: k8s runtimeVersion: - - 1.23.0 + - 1.26.0 diff --git a/pkg/merge/testdata/default.yaml b/pkg/merge/testdata/default.yaml index 96967e2..10f99e3 100644 --- a/pkg/merge/testdata/default.yaml +++ b/pkg/merge/testdata/default.yaml @@ -13,9 +13,9 @@ # limitations under the License. version: - containerd: 1.6.16 - docker: 20.10 - sealos: 4.1.5-rc1 - crun: 1.8 - runc: 1.1.4 - ctl: 0.0.2 + containerd: "1.6.16" + docker: "20.10" + sealos: "4.1.5-rc1" + crun: "1.8" + runc: "1.1.4" + ctl: "0.0.2" diff --git a/pkg/version/default_version.go b/pkg/version/default_version.go index b45c7c8..5033b2a 100644 --- a/pkg/version/default_version.go +++ b/pkg/version/default_version.go @@ -19,7 +19,7 @@ package version const ( CRIDockerd3x = "0.3.1" CRIDockerd2x = "0.2.6" - Docker19 = "19.03.15" - Docker18 = "18.09.9" - Docker20 = "20.10.23" + Docker19 = "19.03" + Docker18 = "18.09" + Docker20 = "20.10" ) diff --git a/types/v1/validation.go b/types/v1/validation.go index 2424eda..07a630f 100644 --- a/types/v1/validation.go +++ b/types/v1/validation.go @@ -89,3 +89,10 @@ func Compare(v1, v2 string) bool { return true } + +func ToBigVersion(v string) string { + v = strings.Replace(v, "v", "", -1) + v1List := strings.Split(v, ".") + return strings.Join(v1List[:2], ".") + +} diff --git a/types/v1/validation_test.go b/types/v1/validation_test.go index 79ceafa..4a85abd 100644 --- a/types/v1/validation_test.go +++ b/types/v1/validation_test.go @@ -83,3 +83,27 @@ func TestCompare(t *testing.T) { }) } } + +func TestToBigVersion(t *testing.T) { + type args struct { + v string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "cc", + args: args{v: "20.10"}, + want: "20.10", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ToBigVersion(tt.args.v); got != tt.want { + t.Errorf("ToBigVersion() = %v, want %v", got, tt.want) + } + }) + } +}