From 55381a33a84bac09cb937480c62013fd3e824261 Mon Sep 17 00:00:00 2001 From: oldthreefeng Date: Tue, 29 Jun 2021 00:07:22 +0800 Subject: [PATCH] fixed to add cni config to show default yaml, add metadate to clare cni version Signed-off-by: oldthreefeng --- cmd/cni.go | 4 ++-- install/init.go | 19 ++++++++++++++++++- install/vars.go | 6 ++++++ net/calico.go | 10 ++++++++-- net/net.go | 2 +- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/cmd/cni.go b/cmd/cni.go index 180285093..3a1befc20 100644 --- a/cmd/cni.go +++ b/cmd/cni.go @@ -49,7 +49,7 @@ to quickly create a Cobra application.`, Interface: "interface=eth.*|en.*|em.*", IPIP: true, MTU: "1440", - K8sVersion: version, + Version: version, }).Manifests("") fmt.Println(yaml) }, @@ -59,7 +59,7 @@ func init() { rootCmd.AddCommand(cniCmd) cniCmd.Flags().StringVarP(&cniType,"cni-type", "t", "calico", "print cni yaml") - cniCmd.Flags().StringVarP(&version,"version", "v", "", "k8s version to adpat the cni version like v1.21.0") + cniCmd.Flags().StringVarP(&version,"version", "v", "", "calico version") // Here you will define your flags and configuration settings. diff --git a/install/init.go b/install/init.go index 53de8f00a..9fc06cb7b 100644 --- a/install/init.go +++ b/install/init.go @@ -2,6 +2,7 @@ package install import ( "bufio" + "encoding/json" "fmt" "io" "io/ioutil" @@ -191,6 +192,22 @@ func (s *SealosInstaller) InstallMaster0() { Interface = "interface=" + Interface } + + var cniVersion string + if SSHConfig.IsFileExist(s.Masters[0], "/root/kube/Metadata") { + var metajson []byte + var tmpdata metadata + SSHConfig.CopyConfigFile(s.Masters[0], "/root/kube/Metadata", metajson) + err := json.Unmarshal(metajson, &tmpdata) + if err != nil { + logger.Warn("get metadata version err: ", err) + } else { + cniVersion = tmpdata.CniVersion + Network = tmpdata.CniName + } + } + + netyaml := net.NewNetwork(Network, net.MetaData{ Interface: Interface, CIDR: PodCIDR, @@ -198,7 +215,7 @@ func (s *SealosInstaller) InstallMaster0() { MTU: MTU, CniRepo: Repo, K8sServiceHost: s.ApiServer, - K8sVersion: Version, + Version: cniVersion, }).Manifests("") cmd = fmt.Sprintf(`echo '%s' | kubectl apply -f -`, netyaml) diff --git a/install/vars.go b/install/vars.go index 2ac34d101..e2a19b26c 100644 --- a/install/vars.go +++ b/install/vars.go @@ -83,3 +83,9 @@ func vlogToStr() string { str := strconv.Itoa(Vlog) return " -v " + str } + +type metadata struct { + K8sVersion string `json:"k8sVersion"` + CniVersion string `json:"cniVersion"` + CniName string `json:"cniName"` +} diff --git a/net/calico.go b/net/calico.go index b543b834d..e397f8774 100644 --- a/net/calico.go +++ b/net/calico.go @@ -19,14 +19,20 @@ func (c Calico) Manifests(template string) string { c.metadata.CniRepo = "calico" } + if c.metadata.Version == "" { + c.metadata.Version = "v3.8.2" + } + return render(c.metadata, template) } func (c Calico) Template() string { - if For121(c.metadata.K8sVersion) { + if c.metadata.Version == "v3.19.1" { return CalicoV3191Manifests + } else if c.metadata.Version == "v3.8.2" { + return CalicoManifests } - return CalicoManifests + return "" } const CalicoManifests = ` diff --git a/net/net.go b/net/net.go index daf64692d..0b9305012 100644 --- a/net/net.go +++ b/net/net.go @@ -29,7 +29,7 @@ type MetaData struct { CniRepo string K8sServiceHost string K8sServicePort string - K8sVersion string + Version string } // Net is CNI interface