Skip to content

Commit da0de40

Browse files
authored
Merge pull request #102 from cuisongliu/optimization/vars
Optimization/vars
2 parents 3aa5e63 + 6773d22 commit da0de40

File tree

16 files changed

+90
-111
lines changed

16 files changed

+90
-111
lines changed

cmd/clean.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var cleanCmd = &cobra.Command{
2525
Short: "Simplest way to clean your kubernets HA cluster",
2626
Long: `sealos clean --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 --node 192.168.0.5 --user root --passwd your-server-password`,
2727
Run: func(cmd *cobra.Command, args []string) {
28-
install.BuildClean(masters, nodes)
28+
install.BuildClean()
2929
},
3030
}
3131

@@ -46,6 +46,6 @@ func init() {
4646
cleanCmd.Flags().StringVar(&install.Passwd, "passwd", "", "password for ssh")
4747
cleanCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")
4848

49-
cleanCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
50-
cleanCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
49+
cleanCmd.Flags().StringSliceVar(&install.Masters, "master", []string{}, "kubernetes masters")
50+
cleanCmd.Flags().StringSliceVar(&install.Nodes, "node", []string{}, "kubernetes nodes")
5151
}

cmd/init.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,13 @@ import (
1919
"github.com/spf13/cobra"
2020
)
2121

22-
var (
23-
masters []string
24-
nodes []string
25-
vip string
26-
27-
pkgURL string
28-
)
29-
3022
// initCmd represents the init command
3123
var initCmd = &cobra.Command{
3224
Use: "init",
3325
Short: "Simplest way to init your kubernets HA cluster",
3426
Long: `sealos init --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 --node 192.168.0.5 --user root --passwd your-server-password`,
3527
Run: func(cmd *cobra.Command, args []string) {
36-
install.BuildInit(masters, nodes, vip, pkgURL)
28+
install.BuildInit()
3729
},
3830
}
3931

@@ -47,10 +39,10 @@ func init() {
4739

4840
initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml template file")
4941

50-
initCmd.Flags().StringVar(&vip, "vip", "10.103.97.2", "virtual ip")
51-
initCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
52-
initCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
42+
initCmd.Flags().StringVar(&install.VIP, "vip", "10.103.97.2", "virtual ip")
43+
initCmd.Flags().StringSliceVar(&install.Masters, "master", []string{}, "kubernetes masters")
44+
initCmd.Flags().StringSliceVar(&install.Nodes, "node", []string{}, "kubernetes nodes")
5345

54-
initCmd.Flags().StringVar(&pkgURL, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file localtion ex. /root/kube1.14.1.tar.gz")
46+
initCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file localtion ex. /root/kube1.14.1.tar.gz")
5547
initCmd.Flags().StringVar(&install.Version, "version", "v1.14.1", "version is kubernetes version")
5648
}

cmd/install.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var installCmd = &cobra.Command{
2525
Short: "kubernetes cluster install plugin,ex dashboard",
2626
Long: `sealos install --master 172.16.3.254 --user root --passwd admin --name dashboard --pkg-url /root/dashboard.tar.gz`,
2727
Run: func(cmd *cobra.Command, args []string) {
28-
install.BuildInstall(masters, nodes, pkgURL, name)
28+
install.BuildInstall(name)
2929
},
3030
}
3131
var name string
@@ -40,9 +40,9 @@ func init() {
4040
installCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")
4141
installCmd.Flags().BoolVar(&install.Kustomize, "kust", false, "kustomize type deploy")
4242

43-
installCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
44-
installCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
43+
installCmd.Flags().StringSliceVar(&install.Masters, "master", []string{}, "kubernetes masters")
44+
installCmd.Flags().StringSliceVar(&install.Nodes, "node", []string{}, "kubernetes nodes")
4545
installCmd.Flags().StringVar(&name, "name", "", "kubernetes plugins name")
46-
installCmd.Flags().StringVar(&pkgURL, "pkg-url", "", "http://store.lameleg.com/prometheus.tar.gz download offline plugins package url, or file localtion ex. /root/prometheus.tar.gz")
46+
installCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "http://store.lameleg.com/prometheus.tar.gz download offline plugins package url, or file localtion ex. /root/prometheus.tar.gz")
4747

4848
}

cmd/join.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var joinCmd = &cobra.Command{
2626
Short: "Simplest way to join your kubernets HA cluster",
2727
Long: `sealos join --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 --node 192.168.0.5 --vip 192.168.0.1 --user root --passwd your-server-password --pkg-url /root/kube1.14.1.tar.gz`,
2828
Run: func(cmd *cobra.Command, args []string) {
29-
install.BuildJoin(masters, nodes, vip, pkgURL)
29+
install.BuildJoin()
3030
},
3131
}
3232

@@ -36,9 +36,9 @@ func init() {
3636
joinCmd.Flags().StringVar(&install.Passwd, "passwd", "", "password for ssh")
3737
joinCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")
3838

39-
joinCmd.Flags().StringVar(&vip, "vip", "10.103.97.2", "virtual ip")
40-
joinCmd.Flags().StringSliceVar(&masters, "master", []string{}, "kubernetes masters")
41-
joinCmd.Flags().StringSliceVar(&nodes, "node", []string{}, "kubernetes nodes")
39+
joinCmd.Flags().StringVar(&install.VIP, "vip", "10.103.97.2", "virtual ip")
40+
joinCmd.Flags().StringSliceVar(&install.Masters, "master", []string{}, "kubernetes masters")
41+
joinCmd.Flags().StringSliceVar(&install.Nodes, "node", []string{}, "kubernetes nodes")
4242

43-
joinCmd.Flags().StringVar(&pkgURL, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline pakage url, or file localtion ex. /root/kube1.14.1.tar.gz")
43+
joinCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline pakage url, or file localtion ex. /root/kube1.14.1.tar.gz")
4444
}

install/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
//CheckValid is
99
func (s *SealosInstaller) CheckValid() {
10-
hosts := append(s.Masters, s.Nodes...)
10+
hosts := append(Masters, Nodes...)
1111
var session *ssh.Session
1212
for _, h := range hosts {
1313
session, err := Connect(User, Passwd, PrivateKeyFile, h)

install/clean.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package install
33
import "sync"
44

55
//BuildClean is
6-
func BuildClean(masters []string, nodes []string) {
7-
hosts := append(masters, nodes...)
6+
func BuildClean() {
7+
hosts := append(Masters, Nodes...)
88
i := &SealosInstaller{
99
Hosts: hosts,
1010
}

install/generator.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func printlnKubeadmConfig() {
5252
}
5353

5454
//Template is
55-
func Template(masters []string, vip string, version string) []byte {
56-
return TemplateFromTemplateContent(masters, vip, version, kubeadmConfig())
55+
func Template() []byte {
56+
return TemplateFromTemplateContent(kubeadmConfig())
5757
}
5858

59-
func TemplateFromTemplateContent(masters []string, vip, version, templateContent string) []byte {
59+
func TemplateFromTemplateContent(templateContent string) []byte {
6060
tmpl, err := template.New("text").Parse(templateContent)
6161
defer func() {
6262
if r := recover(); r != nil {
@@ -66,10 +66,14 @@ func TemplateFromTemplateContent(masters []string, vip, version, templateContent
6666
if err != nil {
6767
panic(1)
6868
}
69+
var masters []string
70+
for _, h := range Masters {
71+
masters = append(masters, IpFormat(h))
72+
}
6973
var envMap = make(map[string]interface{})
70-
envMap["VIP"] = vip
74+
envMap["VIP"] = VIP
7175
envMap["Masters"] = masters
72-
envMap["Version"] = version
76+
envMap["Version"] = Version
7377
var buffer bytes.Buffer
7478
_ = tmpl.Execute(&buffer, envMap)
7579
return buffer.Bytes()

install/generator_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ func TestTemplate(t *testing.T) {
1111
var vip = "10.103.97.1"
1212
User = "cuisongliu"
1313
Passwd = "admin"
14-
Cmd("127.0.0.1", "echo \""+string(Template(masters, vip, "v1.14.1"))+"\" > ~/aa")
15-
t.Log(string(Template(masters, vip, "v1.14.0")))
14+
Masters = masters
15+
VIP = vip
16+
Cmd("127.0.0.1", "echo \""+string(Template())+"\" > ~/aa")
17+
t.Log(string(Template()))
1618
}

install/init.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import (
77
)
88

99
//BuildInit is
10-
func BuildInit(masters []string, nodes []string, vip, pkgUrl string) {
11-
hosts := append(masters, nodes...)
10+
func BuildInit() {
11+
hosts := append(Masters, Nodes...)
1212
i := &SealosInstaller{
13-
Masters: masters,
14-
Nodes: nodes,
15-
VIP: vip,
16-
PkgUrl: pkgUrl,
17-
Hosts: hosts,
13+
Hosts: hosts,
1814
}
1915
i.CheckValid()
2016
i.Print()
@@ -24,25 +20,21 @@ func BuildInit(masters []string, nodes []string, vip, pkgUrl string) {
2420
i.Print("SendPackage", "KubeadmConfigInstall")
2521
i.InstallMaster0()
2622
i.Print("SendPackage", "KubeadmConfigInstall", "InstallMaster0")
27-
if len(i.Masters) > 1 {
23+
if len(Masters) > 1 {
2824
i.JoinMasters()
2925
i.Print("SendPackage", "KubeadmConfigInstall", "InstallMaster0", "JoinMasters")
3026
}
31-
if len(i.Nodes) > 0 {
27+
if len(Nodes) > 0 {
3228
i.JoinNodes()
3329
i.Print("SendPackage", "KubeadmConfigInstall", "InstallMaster0", "JoinMasters", "JoinNodes")
3430
}
3531
}
3632

3733
//KubeadmConfigInstall is
3834
func (s *SealosInstaller) KubeadmConfigInstall() {
39-
var masters []string
40-
for _, h := range s.Masters {
41-
masters = append(masters, IpFormat(h))
42-
}
4335
var templateData string
4436
if KubeadmFile == "" {
45-
templateData = string(Template(masters, s.VIP, Version))
37+
templateData = string(Template())
4638
} else {
4739
fileData, err := ioutil.ReadFile(KubeadmFile)
4840
defer func() {
@@ -53,24 +45,24 @@ func (s *SealosInstaller) KubeadmConfigInstall() {
5345
if err != nil {
5446
panic(1)
5547
}
56-
templateData = string(TemplateFromTemplateContent(masters, s.VIP, Version, string(fileData)))
48+
templateData = string(TemplateFromTemplateContent(string(fileData)))
5749
}
5850
cmd := "echo \"" + templateData + "\" > /root/kubeadm-config.yaml"
59-
Cmd(s.Masters[0], cmd)
51+
Cmd(Masters[0], cmd)
6052
}
6153

6254
//InstallMaster0 is
6355
func (s *SealosInstaller) InstallMaster0() {
64-
cmd := fmt.Sprintf("echo %s apiserver.cluster.local >> /etc/hosts", IpFormat(s.Masters[0]))
65-
Cmd(s.Masters[0], cmd)
56+
cmd := fmt.Sprintf("echo %s apiserver.cluster.local >> /etc/hosts", IpFormat(Masters[0]))
57+
Cmd(Masters[0], cmd)
6658

6759
cmd = `kubeadm init --config=/root/kubeadm-config.yaml --experimental-upload-certs`
68-
output := Cmd(s.Masters[0], cmd)
60+
output := Cmd(Masters[0], cmd)
6961
decodeOutput(output)
7062

7163
cmd = `mkdir -p /root/.kube && cp /etc/kubernetes/admin.conf /root/.kube/config`
72-
output = Cmd(s.Masters[0], cmd)
64+
output = Cmd(Masters[0], cmd)
7365

7466
cmd = `kubectl apply -f /root/kube/conf/net/calico.yaml || true`
75-
output = Cmd(s.Masters[0], cmd)
67+
output = Cmd(Masters[0], cmd)
7668
}

install/install.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ package install
33
import "fmt"
44

55
//BuildJoin is
6-
func BuildInstall(masters []string, nodes []string, pkgUrl, name string) {
7-
hosts := append(masters, nodes...)
6+
func BuildInstall(name string) {
7+
hosts := append(Masters, Nodes...)
88
i := &SealosInstaller{
9-
Masters: masters,
10-
Nodes: nodes,
11-
PkgUrl: pkgUrl,
12-
Hosts: hosts,
9+
Hosts: hosts,
1310
}
1411
i.CheckValid()
1512
i.SendPackage(name)
@@ -22,5 +19,5 @@ func (s *SealosInstaller) KubeApply(name string) {
2219
args = "-k"
2320
}
2421
kubeCmd := fmt.Sprintf("cd /root/%s/conf && kubectl apply %s .", name, args)
25-
Cmd(s.Masters[0], kubeCmd)
22+
Cmd(Masters[0], kubeCmd)
2623
}

0 commit comments

Comments
 (0)