diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 000000000..dc8bcfd63 --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,45 @@ +// 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" +) + +// 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.Config() + }, +} + +func init() { + rootCmd.AddCommand(configCmd) + configCmd.Flags().StringVarP(&install.ConfigType, "type", "t", "kubeadm", "template type (Optional: kubeadm)") + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // configCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} 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 new file mode 100644 index 000000000..ff40c5e28 --- /dev/null +++ b/install/generator.go @@ -0,0 +1,65 @@ +package install + +import ( + "bytes" + "fmt" + "github.com/wonderivan/logger" + "text/template" +) + +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 ConfigType string + +func Config() { + switch ConfigType { + case "kubeadm": + kubeadmConfig() + default: + kubeadmConfig() + } +} + +func kubeadmConfig() { + fmt.Print(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 new file mode 100644 index 000000000..b984d0961 --- /dev/null +++ b/install/generator_test.go @@ -0,0 +1,16 @@ +package install + +import "testing" + +func Test_generatorKubeadmConfig(t *testing.T) { + kubeadmConfig() +} + +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 4a1e3e455..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" @@ -28,7 +27,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) } @@ -228,40 +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 { - 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) - 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"