From 50e53b8dd580a9a67f3a09f8a7f8128856c1aabf Mon Sep 17 00:00:00 2001 From: oldthreefeng Date: Tue, 8 Dec 2020 01:59:26 +0800 Subject: [PATCH] when kubernetes gt 1.20, use Containerd instead of docker, #540 suport 1.20 containerd Signed-off-by: oldthreefeng --- install/constants.go | 156 ++++++++++++++++++++++++++++++++++++++++++ install/generator.go | 95 ++++--------------------- install/send.go | 9 ++- install/utils.go | 13 ++++ install/utils_test.go | 22 ++++++ 5 files changed, 214 insertions(+), 81 deletions(-) diff --git a/install/constants.go b/install/constants.go index 6f3c089c96a..23e74fd16c2 100644 --- a/install/constants.go +++ b/install/constants.go @@ -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`) + +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"`) \ No newline at end of file diff --git a/install/generator.go b/install/generator.go index d8de0f751f9..b3f8a1ad1ed 100644 --- a/install/generator.go +++ b/install/generator.go @@ -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() { @@ -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() } diff --git a/install/send.go b/install/send.go index 2b9e9038794..6d9879f7035 100644 --- a/install/send.go +++ b/install/send.go @@ -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 { + 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) } diff --git a/install/utils.go b/install/utils.go index f2b36ef3487..17d6869bd5d 100644 --- a/install/utils.go +++ b/install/utils.go @@ -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 + } + +} diff --git a/install/utils_test.go b/install/utils_test.go index 48ca164582e..8099321e534 100644 --- a/install/utils_test.go +++ b/install/utils_test.go @@ -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) + } + }) + } } \ No newline at end of file