Skip to content

Commit

Permalink
add conversion
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu committed Mar 1, 2023
1 parent b4d65d1 commit 839bf72
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
12 changes: 12 additions & 0 deletions cmd/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
},
}
Expand Down
32 changes: 31 additions & 1 deletion pkg/apply/applydriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Applier struct {
RuntimeConfigs []v1.RuntimeConfig
DefaultFile string
Yaml bool
Sync *cri.Sync
}

func NewApplier() *Applier {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions pkg/cri/comparable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/cri/sync.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/merge/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ config:
- containerd
runtime: k8s
runtimeVersion:
- 1.23.0
- 1.26.0
12 changes: 6 additions & 6 deletions pkg/merge/testdata/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions pkg/version/default_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
7 changes: 7 additions & 0 deletions types/v1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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], ".")

}
24 changes: 24 additions & 0 deletions types/v1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 839bf72

Please sign in to comment.