From ff1b3f087c1dc9d598ef5deb4359651cc907c0ee Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sun, 26 Feb 2023 01:21:42 +0800 Subject: [PATCH] fix merge add array k8s version --- pkg/apply/applydriver.go | 54 +++++++++++++++++++------------- pkg/merge/testdata/config-2.yaml | 6 ++-- pkg/merge/testdata/config.yaml | 7 +++-- types/v1/runtime.go | 17 +++++++--- types/v1/validation.go | 19 ++++++----- 5 files changed, 63 insertions(+), 40 deletions(-) diff --git a/pkg/apply/applydriver.go b/pkg/apply/applydriver.go index 54df8ff..9adf580 100644 --- a/pkg/apply/applydriver.go +++ b/pkg/apply/applydriver.go @@ -27,7 +27,7 @@ import ( ) type Applier struct { - Runtimes []v1.RuntimeConfig + Runtimes []v1.RuntimeStatus DefaultFile string } @@ -74,11 +74,26 @@ func (a *Applier) WithConfigFiles(files ...string) error { if err = validationFunc(i, cfg); err != nil { return fmt.Errorf("file is %s is validation error: %+v", f, err) } - setKey := fmt.Sprintf("%s-%s", cfg.Config.Runtime, cfg.Config.RuntimeVersion) - if !versions.Has(setKey) { - versions.Insert(setKey) - a.Runtimes = append(a.Runtimes, *cfg) + if cfg.Config.CRI == nil || len(cfg.Config.CRI) == 0 { + cfg.Config.CRI = []string{v1.CRIContainerd, v1.CRIDocker, v1.CRICRIO} } + for _, version := range cfg.Config.RuntimeVersion { + for _, cri := range cfg.Config.CRI { + setKey := fmt.Sprintf("%s-%s-%s", cri, cfg.Config.Runtime, version) + if !versions.Has(setKey) { + versions.Insert(setKey) + a.Runtimes = append(a.Runtimes, v1.RuntimeStatus{ + RuntimeConfigDefaultComponent: cfg.Default, + RuntimeStatusConfigData: &v1.RuntimeStatusConfigData{ + CRI: cri, + Runtime: cfg.Config.Runtime, + RuntimeVersion: version, + }, + }) + } + } + } + } return nil } @@ -87,34 +102,29 @@ func (a *Applier) Apply() error { statusList := &v1.RuntimeStatusList{ Include: []v1.RuntimeStatus{}, } - for _, rt := range a.Runtimes { - switch rt.Config.Runtime { + for i, rt := range a.Runtimes { + switch rt.Runtime { case v1.RuntimeK8s: - dockerVersion, criDockerVersion := docker.FetchVersion(rt.Config.RuntimeVersion) - status := &v1.RuntimeStatus{ - RuntimeConfigDefaultComponent: rt.Default, - RuntimeConfigData: rt.Config, - } - status.Docker = dockerVersion + dockerVersion, criDockerVersion := docker.FetchVersion(rt.RuntimeVersion) + a.Runtimes[i].Docker = dockerVersion switch criDockerVersion { case docker.CRIDockerV2: - status.CRIDocker = status.CRIDockerV2 + a.Runtimes[i].CRIDocker = a.Runtimes[i].CRIDockerV2 case docker.CRIDockerV3: - status.CRIDocker = status.CRIDockerV3 + a.Runtimes[i].CRIDocker = a.Runtimes[i].CRIDockerV3 } - status.CRIDockerV2 = "" - status.CRIDockerV3 = "" - newVersion, err := k8s.FetchFinalVersion(status.RuntimeVersion) + a.Runtimes[i].CRIDockerV2 = "" + a.Runtimes[i].CRIDockerV3 = "" + newVersion, err := k8s.FetchFinalVersion(rt.RuntimeVersion) if err != nil { - return fmt.Errorf("runtime is %s,runtime version is %s,get new version is error: %+v", status.Runtime, status.RuntimeVersion, err) + return fmt.Errorf("runtime is %s,runtime version is %s,get new version is error: %+v", rt.Runtime, rt.RuntimeVersion, err) } - status.RuntimeVersion = newVersion - statusList.Include = append(statusList.Include, *status) - + a.Runtimes[i].RuntimeVersion = newVersion default: return fmt.Errorf("not found runtime,current version not support") } } + statusList.Include = a.Runtimes actionJSON, err := json.Marshal(statusList) if err != nil { return err diff --git a/pkg/merge/testdata/config-2.yaml b/pkg/merge/testdata/config-2.yaml index 6c158c9..5079c27 100644 --- a/pkg/merge/testdata/config-2.yaml +++ b/pkg/merge/testdata/config-2.yaml @@ -13,6 +13,8 @@ # limitations under the License. config: - cri: docker + cri: + - docker runtime: k8s - runtimeVersion: 1.23 \ No newline at end of file + runtimeVersion: + - 1.24.0 diff --git a/pkg/merge/testdata/config.yaml b/pkg/merge/testdata/config.yaml index b50b6ab..c8e087c 100644 --- a/pkg/merge/testdata/config.yaml +++ b/pkg/merge/testdata/config.yaml @@ -13,6 +13,9 @@ # limitations under the License. config: - cri: docker + cri: + - docker + - containerd runtime: k8s - runtimeVersion: 1.23.0 \ No newline at end of file + runtimeVersion: + - 1.23.0 diff --git a/types/v1/runtime.go b/types/v1/runtime.go index c372e77..3f937fc 100644 --- a/types/v1/runtime.go +++ b/types/v1/runtime.go @@ -27,18 +27,27 @@ type RuntimeConfigDefaultComponent struct { } const ( - RuntimeK8s string = "k8s" + RuntimeK8s string = "k8s" + CRIDocker string = "docker" + CRIContainerd string = "containerd" + CRICRIO string = "crio" ) type RuntimeConfigData struct { - CRI string `json:"cri"` + CRI []string `json:"cri,omitempty"` + Runtime string `json:"runtime"` + RuntimeVersion []string `json:"runtimeVersion,omitempty"` +} + +type RuntimeStatusConfigData struct { + CRI string `json:"cri,omitempty"` Runtime string `json:"runtime"` - RuntimeVersion string `json:"runtimeVersion"` + RuntimeVersion string `json:"runtimeVersion,omitempty"` } type RuntimeStatus struct { *RuntimeConfigDefaultComponent `json:",inline"` - *RuntimeConfigData `json:",inline"` + *RuntimeStatusConfigData `json:",inline"` } type RuntimeConfig struct { diff --git a/types/v1/validation.go b/types/v1/validation.go index ecd649f..0eb0ca1 100644 --- a/types/v1/validation.go +++ b/types/v1/validation.go @@ -46,14 +46,11 @@ func ValidationDefaultComponent(c *RuntimeConfigDefaultComponent) error { } func ValidationConfigData(c *RuntimeConfigData) error { - if c.CRI == "" { - return fmt.Errorf("cri not set,please retry config it") - } if c.Runtime == "" { return fmt.Errorf("runtime not set,please retry config it") } - if c.RuntimeVersion == "" { - return fmt.Errorf("runtime version not set,please retry config it") + if len(c.RuntimeVersion) == 0 { + return fmt.Errorf("runtime versions not set,please retry config it") } return nil } @@ -61,11 +58,13 @@ func ValidationConfigData(c *RuntimeConfigData) error { func ValidationRuntimeConfig(c *RuntimeConfig) error { if c.Config.Runtime == "k8s" { //kubernetes gt 1.26 - if Compare(c.Config.RuntimeVersion, "v1.26") && !Compare(c.Default.Sealos, "v4.1.3") { - // echo "INFO::skip $KUBE(kube>=1.26) when $SEALOS(sealos<=4.1.3)" - // echo https://kubernetes.io/blog/2022/11/18/upcoming-changes-in-kubernetes-1-26/#cri-api-removal - klog.Info("Please see https://kubernetes.io/blog/2022/11/18/upcoming-changes-in-kubernetes-1-26/#cri-api-removal") - return fmt.Errorf("skip $KUBE(kube>=1.26) when $SEALOS(sealos<=4.1.3)") + for _, v := range c.Config.RuntimeVersion { + if Compare(v, "v1.26") && !Compare(c.Default.Sealos, "v4.1.3") { + // echo "INFO::skip $KUBE(kube>=1.26) when $SEALOS(sealos<=4.1.3)" + // echo https://kubernetes.io/blog/2022/11/18/upcoming-changes-in-kubernetes-1-26/#cri-api-removal + klog.Info("Please see https://kubernetes.io/blog/2022/11/18/upcoming-changes-in-kubernetes-1-26/#cri-api-removal") + return fmt.Errorf("skip $KUBE(kube>=1.26) when $SEALOS(sealos<=4.1.3)") + } } } return nil