Skip to content

Commit

Permalink
bugfix: dynamically switch apiVersion of JoinConfiguration to adapt t…
Browse files Browse the repository at this point in the history
…o different versions of k8s

Signed-off-by: HIHIA <[email protected]>
  • Loading branch information
YTGhost committed Jan 2, 2023
1 parent fe5075d commit a46a066
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion pkg/yurtadm/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ preferences: {}
`

KubeadmJoinConf = `
apiVersion: kubeadm.k8s.io/v1beta3
apiVersion: {{.apiVersion}}
kind: JoinConfiguration
discovery:
file:
Expand Down
17 changes: 17 additions & 0 deletions pkg/yurtadm/util/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a46a066

Please sign in to comment.