Skip to content

Commit

Permalink
fix merge add array k8s version
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Feb 25, 2023
1 parent 2e652c9 commit ff1b3f0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
54 changes: 32 additions & 22 deletions pkg/apply/applydriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

type Applier struct {
Runtimes []v1.RuntimeConfig
Runtimes []v1.RuntimeStatus
DefaultFile string
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkg/merge/testdata/config-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

config:
cri: docker
cri:
- docker
runtime: k8s
runtimeVersion: 1.23
runtimeVersion:
- 1.24.0
7 changes: 5 additions & 2 deletions pkg/merge/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# limitations under the License.

config:
cri: docker
cri:
- docker
- containerd
runtime: k8s
runtimeVersion: 1.23.0
runtimeVersion:
- 1.23.0
17 changes: 13 additions & 4 deletions types/v1/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 9 additions & 10 deletions types/v1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,25 @@ 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
}

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
Expand Down

0 comments on commit ff1b3f0

Please sign in to comment.