From a46a0666f8ff4e0b9dc3c0e2a80bf9fda31132ba Mon Sep 17 00:00:00 2001 From: HIHIA <283304489@qq.com> Date: Mon, 2 Jan 2023 12:17:55 +0800 Subject: [PATCH] bugfix: dynamically switch apiVersion of JoinConfiguration to adapt to different versions of k8s Signed-off-by: HIHIA <283304489@qq.com> --- go.mod | 1 + go.sum | 2 ++ pkg/yurtadm/constants/constants.go | 2 +- pkg/yurtadm/util/kubernetes/kubernetes.go | 17 +++++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bd005d9ce29..aea48683ac4 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/fsnotify/fsnotify v1.4.10-0.20200417215612-7f4cf4dd2b52 // indirect github.com/google/uuid v1.1.2 github.com/gorilla/mux v1.7.4 + github.com/hashicorp/go-version v1.6.0 github.com/mattn/go-isatty v0.0.14 // indirect github.com/onsi/ginkgo/v2 v2.1.4 github.com/onsi/gomega v1.19.0 diff --git a/go.sum b/go.sum index 6bc985375d9..8580b0a9661 100644 --- a/go.sum +++ b/go.sum @@ -385,6 +385,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/pkg/yurtadm/constants/constants.go b/pkg/yurtadm/constants/constants.go index 2711c5ae1df..5f8a66b91ea 100644 --- a/pkg/yurtadm/constants/constants.go +++ b/pkg/yurtadm/constants/constants.go @@ -166,7 +166,7 @@ preferences: {} ` KubeadmJoinConf = ` -apiVersion: kubeadm.k8s.io/v1beta3 +apiVersion: {{.apiVersion}} kind: JoinConfiguration discovery: file: diff --git a/pkg/yurtadm/util/kubernetes/kubernetes.go b/pkg/yurtadm/util/kubernetes/kubernetes.go index 04ea6fa7cd8..8f1dfbcd1ec 100644 --- a/pkg/yurtadm/util/kubernetes/kubernetes.go +++ b/pkg/yurtadm/util/kubernetes/kubernetes.go @@ -29,6 +29,7 @@ import ( "strings" "time" + "github.com/hashicorp/go-version" pkgerrors "github.com/pkg/errors" batchv1 "k8s.io/api/batch/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -355,6 +356,22 @@ func SetKubeadmJoinConfig(data joindata.YurtJoinData) error { ctx["containerRuntimeEndpoint"] = nodeReg.CRISocket } + v1, err := version.NewVersion(data.KubernetesVersion()) + if err != nil { + return err + } + v2, err := version.NewVersion("v1.22.0") + if err != nil { + return err + } + // This is to adapt the apiVersion of JoinConfiguration + // https://kubernetes.io/docs/reference/config-api/kubeadm-config.v1beta3/ + if v1.LessThan(v2) { + ctx["apiVersion"] = "kubeadm.k8s.io/v1beta2" + } else { + ctx["apiVersion"] = "kubeadm.k8s.io/v1beta3" + } + kubeadmJoinTemplate, err := templates.SubsituteTemplate(constants.KubeadmJoinConf, ctx) if err != nil { return err