From 7acaebd90a36a16c5e08d32d2e0103f4994ff843 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 29 Jul 2019 14:31:18 +0800 Subject: [PATCH 1/4] add generator file --- cmd/generator.go | 44 +++++++++++++++++++++++++++++++++++ install/generator.go | 48 +++++++++++++++++++++++++++++++++++++++ install/generator_test.go | 7 ++++++ install/utils.go | 25 ++------------------ 4 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 cmd/generator.go create mode 100644 install/generator.go create mode 100644 install/generator_test.go diff --git a/cmd/generator.go b/cmd/generator.go new file mode 100644 index 000000000..37a77a7cd --- /dev/null +++ b/cmd/generator.go @@ -0,0 +1,44 @@ +// Copyright © 2019 NAME HERE +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/fanux/sealos/install" + + "github.com/spf13/cobra" +) + +// generatorCmd represents the generator command +var generatorCmd = &cobra.Command{ + Use: "generator", + Short: "generator template file for sealos", + Run: func(cmd *cobra.Command, args []string) { + install.Generator() + }, +} + +func init() { + rootCmd.AddCommand(generatorCmd) + initCmd.Flags().StringVarP(&install.GeneratorType, "type", "t", "kubeadm-config", "template type (Optional: kubeadm-config)") + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // generatorCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // generatorCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/install/generator.go b/install/generator.go new file mode 100644 index 000000000..dbc8a9512 --- /dev/null +++ b/install/generator.go @@ -0,0 +1,48 @@ +package install + +import ( + "github.com/wonderivan/logger" + "io/ioutil" +) + +const TemplateText = string(`apiVersion: kubeadm.k8s.io/v1beta1 +kind: ClusterConfiguration +kubernetesVersion: {{.Version}} +controlPlaneEndpoint: "apiserver.cluster.local:6443" +networking: + podSubnet: 100.64.0.0/10 +apiServer: + certSANs: + - 127.0.0.1 + - apiserver.cluster.local + {{range .Masters -}} + - {{.}} + {{end -}} + - {{.VIP}} +--- +apiVersion: kubeproxy.config.k8s.io/v1alpha1 +kind: KubeProxyConfiguration +mode: "ipvs" +ipvs: + excludeCIDRs: + - "{{.VIP}}/32"`) + +var GeneratorType string + +func Generator() { + switch GeneratorType { + case "kubeadm-config": + generatorKubeadmConfig() + default: + generatorKubeadmConfig() + } +} + +func generatorKubeadmConfig() { + const fileName = "kubeadm-config.yaml" + data := []byte(TemplateText) + if ioutil.WriteFile(fileName, data, 0755) == nil { + logger.Info("generator kubeadm-config.yaml success.") + logger.Info("\n" + TemplateText) + } +} diff --git a/install/generator_test.go b/install/generator_test.go new file mode 100644 index 000000000..2204772aa --- /dev/null +++ b/install/generator_test.go @@ -0,0 +1,7 @@ +package install + +import "testing" + +func Test_generatorKubeadmConfig(t *testing.T) { + generatorKubeadmConfig() +} diff --git a/install/utils.go b/install/utils.go index 4a1e3e455..eda5f5d77 100644 --- a/install/utils.go +++ b/install/utils.go @@ -28,7 +28,7 @@ var ( const oneMBByte = 1024 * 1024 -func AddrReformat(host string) (string) { +func AddrReformat(host string) string { if strings.Index(host, ":") == -1 { host = fmt.Sprintf("%s:22", host) } @@ -231,28 +231,7 @@ func SftpConnect(user, password, host string) (*sftp.Client, error) { //Template is func Template(masters []string, vip string, version string) []byte { - var templateText = string(`apiVersion: kubeadm.k8s.io/v1beta1 -kind: ClusterConfiguration -kubernetesVersion: {{.Version}} -controlPlaneEndpoint: "apiserver.cluster.local:6443" -networking: - podSubnet: 100.64.0.0/10 -apiServer: - certSANs: - - 127.0.0.1 - - apiserver.cluster.local - {{range .Masters -}} - - {{.}} - {{end -}} - - {{.VIP}} ---- -apiVersion: kubeproxy.config.k8s.io/v1alpha1 -kind: KubeProxyConfiguration -mode: "ipvs" -ipvs: - excludeCIDRs: - - "{{.VIP}}/32"`) - tmpl, err := template.New("text").Parse(templateText) + tmpl, err := template.New("text").Parse(TemplateText) if err != nil { logger.Error("template parse failed:", err) panic(1) From 05ac3a43a61f5dcb2315451f02085c071d0d9d47 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 29 Jul 2019 14:42:42 +0800 Subject: [PATCH 2/4] fix #75 --- cmd/generator.go | 4 ++-- cmd/init.go | 2 +- install/generator.go | 22 ++++++++++++++++++++++ install/generator_test.go | 9 +++++++++ install/sealos.go | 5 +---- install/utils.go | 17 ----------------- install/utils_test.go | 9 --------- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/cmd/generator.go b/cmd/generator.go index 37a77a7cd..14626b9d8 100644 --- a/cmd/generator.go +++ b/cmd/generator.go @@ -23,7 +23,7 @@ import ( // generatorCmd represents the generator command var generatorCmd = &cobra.Command{ Use: "generator", - Short: "generator template file for sealos", + Short: "generator template file for sealos,the file is generator current location.", Run: func(cmd *cobra.Command, args []string) { install.Generator() }, @@ -31,7 +31,7 @@ var generatorCmd = &cobra.Command{ func init() { rootCmd.AddCommand(generatorCmd) - initCmd.Flags().StringVarP(&install.GeneratorType, "type", "t", "kubeadm-config", "template type (Optional: kubeadm-config)") + generatorCmd.Flags().StringVarP(&install.GeneratorType, "type", "t", "kubeadm-config", "template type (Optional: kubeadm-config)") // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command diff --git a/cmd/init.go b/cmd/init.go index 45d1a8a01..cfdb0678f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -51,7 +51,7 @@ func init() { // Here you will define your flags and configuration settings. initCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh") initCmd.Flags().StringVar(&install.Passwd, "passwd", "passwd", "password for ssh") - initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml local") + initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml template file") initCmd.Flags().StringVar(&vip, "vip", "10.103.97.2", "virtual ip") initCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters") diff --git a/install/generator.go b/install/generator.go index dbc8a9512..3649f7e23 100644 --- a/install/generator.go +++ b/install/generator.go @@ -1,8 +1,10 @@ package install import ( + "bytes" "github.com/wonderivan/logger" "io/ioutil" + "text/template" ) const TemplateText = string(`apiVersion: kubeadm.k8s.io/v1beta1 @@ -46,3 +48,23 @@ func generatorKubeadmConfig() { logger.Info("\n" + TemplateText) } } + +//Template is +func Template(masters []string, vip string, version string) []byte { + return TemplateFromTemplateContent(masters, vip, version, TemplateText) +} + +func TemplateFromTemplateContent(masters []string, vip, version, templateContent string) []byte { + tmpl, err := template.New("text").Parse(templateContent) + if err != nil { + logger.Error("template parse failed:", err) + panic(1) + } + var envMap = make(map[string]interface{}) + envMap["VIP"] = vip + envMap["Masters"] = masters + envMap["Version"] = version + var buffer bytes.Buffer + _ = tmpl.Execute(&buffer, envMap) + return buffer.Bytes() +} diff --git a/install/generator_test.go b/install/generator_test.go index 2204772aa..e863de75a 100644 --- a/install/generator_test.go +++ b/install/generator_test.go @@ -5,3 +5,12 @@ import "testing" func Test_generatorKubeadmConfig(t *testing.T) { generatorKubeadmConfig() } + +func TestTemplate(t *testing.T) { + var masters = []string{"172.20.241.205", "172.20.241.206", "172.20.241.207"} + var vip = "10.103.97.1" + User = "cuisongliu" + Passwd = "admin" + Cmd("127.0.0.1", "echo \""+string(Template(masters, vip, "v1.14.1"))+"\" > ~/aa") + t.Log(string(Template(masters, vip, "v1.14.0"))) +} diff --git a/install/sealos.go b/install/sealos.go index 4f27aabb1..1d1a9c6bc 100644 --- a/install/sealos.go +++ b/install/sealos.go @@ -49,7 +49,7 @@ func (s *SealosInstaller) KubeadmConfigInstall() { logger.Error("template file read failed:", err) panic(1) } - templateData = string(fileData) + templateData = string(TemplateFromTemplateContent(s.Masters, s.VIP, Version, string(fileData))) } cmd := "echo \"" + templateData + "\" > /root/kubeadm-config.yaml" Cmd(s.Masters[0], cmd) @@ -60,9 +60,6 @@ func (s *SealosInstaller) InstallMaster0() { cmd := fmt.Sprintf("echo %s apiserver.cluster.local >> /etc/hosts", s.Masters[0]) Cmd(s.Masters[0], cmd) - cmd = "echo \"" + string(Template(s.Masters, s.VIP, Version)) + "\" > /root/kubeadm-config.yaml" - Cmd(s.Masters[0], cmd) - cmd = `kubeadm init --config=/root/kubeadm-config.yaml --experimental-upload-certs` output := Cmd(s.Masters[0], cmd) s.decodeOutput(output) diff --git a/install/utils.go b/install/utils.go index eda5f5d77..30c472e27 100644 --- a/install/utils.go +++ b/install/utils.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/tls" "fmt" - "html/template" "net" "net/http" "os" @@ -228,19 +227,3 @@ func SftpConnect(user, password, host string) (*sftp.Client, error) { return sftpClient, nil } - -//Template is -func Template(masters []string, vip string, version string) []byte { - tmpl, err := template.New("text").Parse(TemplateText) - if err != nil { - logger.Error("template parse failed:", err) - panic(1) - } - var envMap = make(map[string]interface{}) - envMap["VIP"] = vip - envMap["Masters"] = masters - envMap["Version"] = version - var buffer bytes.Buffer - _ = tmpl.Execute(&buffer, envMap) - return buffer.Bytes() -} diff --git a/install/utils_test.go b/install/utils_test.go index c0e22d30f..e56e47447 100644 --- a/install/utils_test.go +++ b/install/utils_test.go @@ -23,15 +23,6 @@ func TestCopy(t *testing.T) { } -func TestTemplate(t *testing.T) { - var masters = []string{"172.20.241.205", "172.20.241.206", "172.20.241.207"} - var vip = "10.103.97.1" - User = "cuisongliu" - Passwd = "admin" - Cmd("127.0.0.1", "echo \""+string(Template(masters, vip, "v1.14.1"))+"\" > ~/aa") - t.Log(string(Template(masters, vip, "v1.14.0"))) -} - func TestRemoteFilExist(t *testing.T) { User = "cuisongliu" Passwd = "admin" From 8e6eca496958c5e24840da234b56405f57ae14a8 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 29 Jul 2019 14:59:19 +0800 Subject: [PATCH 3/4] fix #75 --- cmd/{generator.go => config.go} | 19 ++++++++++--------- install/generator.go | 23 +++++++++-------------- install/generator_test.go | 2 +- 3 files changed, 20 insertions(+), 24 deletions(-) rename cmd/{generator.go => config.go} (66%) diff --git a/cmd/generator.go b/cmd/config.go similarity index 66% rename from cmd/generator.go rename to cmd/config.go index 14626b9d8..fd1f5678c 100644 --- a/cmd/generator.go +++ b/cmd/config.go @@ -20,25 +20,26 @@ import ( "github.com/spf13/cobra" ) -// generatorCmd represents the generator command -var generatorCmd = &cobra.Command{ - Use: "generator", - Short: "generator template file for sealos,the file is generator current location.", +// configCmd represents the config command +var configCmd = &cobra.Command{ + Use: "config", + Short: "print config template to console", Run: func(cmd *cobra.Command, args []string) { - install.Generator() + install.Config() }, } func init() { - rootCmd.AddCommand(generatorCmd) - generatorCmd.Flags().StringVarP(&install.GeneratorType, "type", "t", "kubeadm-config", "template type (Optional: kubeadm-config)") + rootCmd.AddCommand(configCmd) + configCmd.Flags().StringVarP(&install.ConfigType, "type", "t", "kubeadm", "template type (Optional: kubeadm-config)") + // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command // and all subcommands, e.g.: - // generatorCmd.PersistentFlags().String("foo", "", "A help for foo") + // configCmd.PersistentFlags().String("foo", "", "A help for foo") // Cobra supports local flags which will only run when this command // is called directly, e.g.: - // generatorCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/install/generator.go b/install/generator.go index 3649f7e23..ff40c5e28 100644 --- a/install/generator.go +++ b/install/generator.go @@ -2,8 +2,8 @@ package install import ( "bytes" + "fmt" "github.com/wonderivan/logger" - "io/ioutil" "text/template" ) @@ -29,24 +29,19 @@ ipvs: excludeCIDRs: - "{{.VIP}}/32"`) -var GeneratorType string +var ConfigType string -func Generator() { - switch GeneratorType { - case "kubeadm-config": - generatorKubeadmConfig() +func Config() { + switch ConfigType { + case "kubeadm": + kubeadmConfig() default: - generatorKubeadmConfig() + kubeadmConfig() } } -func generatorKubeadmConfig() { - const fileName = "kubeadm-config.yaml" - data := []byte(TemplateText) - if ioutil.WriteFile(fileName, data, 0755) == nil { - logger.Info("generator kubeadm-config.yaml success.") - logger.Info("\n" + TemplateText) - } +func kubeadmConfig() { + fmt.Print(TemplateText) } //Template is diff --git a/install/generator_test.go b/install/generator_test.go index e863de75a..b984d0961 100644 --- a/install/generator_test.go +++ b/install/generator_test.go @@ -3,7 +3,7 @@ package install import "testing" func Test_generatorKubeadmConfig(t *testing.T) { - generatorKubeadmConfig() + kubeadmConfig() } func TestTemplate(t *testing.T) { From 13967e98176d0c2948fec6089a939f3d682ea4d3 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 29 Jul 2019 14:59:53 +0800 Subject: [PATCH 4/4] fix #75 --- cmd/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/config.go b/cmd/config.go index fd1f5678c..dc8bcfd63 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -31,7 +31,7 @@ var configCmd = &cobra.Command{ func init() { rootCmd.AddCommand(configCmd) - configCmd.Flags().StringVarP(&install.ConfigType, "type", "t", "kubeadm", "template type (Optional: kubeadm-config)") + configCmd.Flags().StringVarP(&install.ConfigType, "type", "t", "kubeadm", "template type (Optional: kubeadm)") // Here you will define your flags and configuration settings.