Merge pull request #76 from cuisongliu/bugfix/75

Bugfix/75
This commit is contained in:
steven
2019-07-29 15:06:42 +08:00
committed by GitHub
7 changed files with 129 additions and 53 deletions
+45
View File
@@ -0,0 +1,45 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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")
}
+1 -1
View File
@@ -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")
+65
View File
@@ -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()
}
+16
View File
@@ -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")))
}
+1 -4
View File
@@ -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)
+1 -39
View File
@@ -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()
}
-9
View File
@@ -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"