Skip to content

Commit

Permalink
when kubernetes gt 1.20, use Containerd instead of docker, labring#540
Browse files Browse the repository at this point in the history
…suport 1.20 containerd

Signed-off-by: oldthreefeng <[email protected]>
  • Loading branch information
oldthreefeng committed Dec 7, 2020
1 parent 8fa46a0 commit 50e53b8
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 81 deletions.
156 changes: 156 additions & 0 deletions install/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,159 @@ const (
KUBECONTROLLERCONFIGFILE = "/etc/kubernetes/controller-manager.conf"
KUBESCHEDULERCONFIGFILE = "/etc/kubernetes/scheduler.conf"
)

const InitTemplateTextV1beta1 = string(`apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "{{.ApiServer}}:6443"
imageRepository: {{.Repo}}
networking:
# dnsDomain: cluster.local
podSubnet: {{.PodCIDR}}
serviceSubnet: {{.SvcCIDR}}
apiServer:
certSANs:
- 127.0.0.1
- {{.ApiServer}}
{{range .Masters -}}
- {{.}}
{{end -}}
{{range .CertSANS -}}
- {{.}}
{{end -}}
- {{.VIP}}
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- name: localtime
hostPath: /etc/localtime
mountPath: /etc/localtime
readOnly: true
pathType: File
controllerManager:
extraArgs:
feature-gates: TTLAfterFinished=true
experimental-cluster-signing-duration: 876000h
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
scheduler:
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"`)

const JoinCPTemplateTextV1beta2Docker = string(`apiVersion: kubeadm.k8s.io/v1beta2
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
apiServerEndpoint: {{.Master0}}:6443
token: {{.TokenDiscovery}}
caCertHashes:
- {{.TokenDiscoveryCAHash}}
timeout: 5m0s
kind: JoinConfiguration
controlPlane:
localAPIEndpoint:
advertiseAddress: {{.Master}}
bindPort: 6443`)

const JoinCPTemplateTextV1beate2Container = string(`apiVersion: kubeadm.k8s.io/v1beta2
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
apiServerEndpoint: {{.Master0}}:6443
token: {{.TokenDiscovery}}
caCertHashes:
- {{.TokenDiscoveryCAHash}}
timeout: 5m0s
kind: JoinConfiguration
controlPlane:
localAPIEndpoint:
advertiseAddress: {{.Master}}
bindPort: 6443
nodeRegistration:
criSocket: /run/containerd/containerd.sock`)

This comment has been minimized.

Copy link
@oldthreefeng

oldthreefeng Dec 9, 2020

Author Owner

其实kubernetes应该会自动去寻找对应的criSocket. 用户不需要关心这个..

const InitTemplateTextV1bate2 = string(`apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
nodeRegistration:
criSocket: /run/containerd/containerd.sock
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "{{.ApiServer}}:6443"
imageRepository: {{.Repo}}
networking:
# dnsDomain: cluster.local
podSubnet: {{.PodCIDR}}
serviceSubnet: {{.SvcCIDR}}
apiServer:
certSANs:
- 127.0.0.1
- {{.ApiServer}}
{{range .Masters -}}
- {{.}}
{{end -}}
{{range .CertSANS -}}
- {{.}}
{{end -}}
- {{.VIP}}
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- name: localtime
hostPath: /etc/localtime
mountPath: /etc/localtime
readOnly: true
pathType: File
controllerManager:
extraArgs:
feature-gates: TTLAfterFinished=true
experimental-cluster-signing-duration: 876000h
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
scheduler:
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"`)
95 changes: 15 additions & 80 deletions install/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,6 @@ import (
"text/template"
)

const TemplateText = string(`apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: {{.Master0}}
bindPort: 6443
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: {{.Version}}
controlPlaneEndpoint: "{{.ApiServer}}:6443"
imageRepository: {{.Repo}}
networking:
# dnsDomain: cluster.local
podSubnet: {{.PodCIDR}}
serviceSubnet: {{.SvcCIDR}}
apiServer:
certSANs:
- 127.0.0.1
- {{.ApiServer}}
{{range .Masters -}}
- {{.}}
{{end -}}
{{range .CertSANS -}}
- {{.}}
{{end -}}
- {{.VIP}}
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- name: localtime
hostPath: /etc/localtime
mountPath: /etc/localtime
readOnly: true
pathType: File
controllerManager:
extraArgs:
feature-gates: TTLAfterFinished=true
experimental-cluster-signing-duration: 876000h
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
scheduler:
extraArgs:
feature-gates: TTLAfterFinished=true
extraVolumes:
- hostPath: /etc/localtime
mountPath: /etc/localtime
name: localtime
readOnly: true
pathType: File
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
ipvs:
excludeCIDRs:
- "{{.VIP}}/32"`)

const JoinCPTemplateText = string(`apiVersion: kubeadm.k8s.io/v1beta2
caCertPath: /etc/kubernetes/pki/ca.crt
discovery:
bootstrapToken:
apiServerEndpoint: {{.Master0}}:6443
token: {{.TokenDiscovery}}
caCertHashes:
- {{.TokenDiscoveryCAHash}}
timeout: 5m0s
kind: JoinConfiguration
controlPlane:
localAPIEndpoint:
advertiseAddress: {{.Master}}
bindPort: 6443`)

var ConfigType string

func Config() {
Expand All @@ -98,19 +22,30 @@ func Config() {
}
}

func joinKubeadmConfig() string {
func joinKubeadmConfig() string {
var sb strings.Builder
sb.Write([]byte(JoinCPTemplateText))
// kubernetes gt 1.20, use Containerd instead of docker
if For120(Version) {
sb.Write([]byte(JoinCPTemplateTextV1beate2Container))
} else {
sb.Write([]byte(JoinCPTemplateTextV1beta2Docker))
}
return sb.String()
}

func printlnJoinKubeadmConfig() {
func printlnJoinKubeadmConfig() {
fmt.Println(joinKubeadmConfig())
}

func kubeadmConfig() string {
var sb strings.Builder
sb.Write([]byte(TemplateText))
// kubernetes gt 1.20, use Containerd instead of docker
if For120(Version) {
sb.Write([]byte(InitTemplateTextV1bate2))
} else {
sb.Write([]byte(InitTemplateTextV1beta1))
}

return sb.String()
}

Expand Down
9 changes: 8 additions & 1 deletion install/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (u *SealosUpgrade) SendPackage() {
all := append(u.Masters, u.Nodes...)
pkg := path.Base(u.NewPkgUrl)
// rm old sealos in package avoid old version problem. if sealos not exist in package then skip rm
kubeHook := fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && (docker load -i ../images/images.tar || true) && cp -f ../bin/* /usr/bin/ ", pkg)
var kubeHook string
if For120(Version) {
kubeHook = fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && (ctr cri load ../images/images.tar || true) && cp -f ../bin/* /usr/bin/ ", pkg)
} else {

This comment has been minimized.

Copy link
@oldthreefeng

oldthreefeng Dec 9, 2020

Author Owner

ctr -n=k8s.io image import ../images/images.tar || true

kubeHook = fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && (docker load -i ../images/images.tar || true) && cp -f ../bin/* /usr/bin/ ", pkg)

}

PkgUrl = SendPackage(pkg, all, "/root", nil, &kubeHook)
}
13 changes: 13 additions & 0 deletions install/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,16 @@ func CanUpgradeByNewVersion(new, old string) error {
}
return nil
}

func For120(version string) bool {
newMajor, _ := GetMajorMinorInt(version)
// // kubernetes gt 1.20, use Containerd instead of docker
if newMajor >= 120 {
logger.Info("install version is: %s, Use kubeadm v1beta2 InitConfig,OCI use containerd instead", version)
return true
} else {
//logger.Info("install version is: %s, Use kubeadm v1beta1 InitConfig, docker", version)
return false
}

}
22 changes: 22 additions & 0 deletions install/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,26 @@ func TestCanUpgradeByNewVersion(t *testing.T) {
}
})
}
}

func TestFor120(t *testing.T) {
type args struct {
version string
}
tests := []struct {
name string
args args
want bool
}{
{"test01",args{"v1.19.2"}, false},
{"test02",args{"v1.18.2"}, false},
{"test03",args{"v1.20.2"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := For120(tt.args.version); got != tt.want {
t.Errorf("For120() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 50e53b8

Please sign in to comment.