/
deploy_master.go
150 lines (125 loc) · 4.04 KB
/
deploy_master.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright © 2019 Jeff Wu <jeff.wu.junfei@gmail.com>
//
// 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 deployer
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/jeffwubj/kubev/pkg/kubev/constants"
"github.com/jeffwubj/kubev/pkg/kubev/model"
"github.com/jeffwubj/kubev/pkg/kubev/utils"
"github.com/pkg/sftp"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"k8s.io/minikube/pkg/util/kubeconfig"
)
func UpdateMasterNode(k8snodes *model.K8sNodes) error {
vmconfig := k8snodes.MasterNode
if err := PrepareVM(vmconfig); err != nil {
return err
}
k8sversion := viper.GetString("kubernetesversion")
runner, c, err := GetSSHRunner(vmconfig)
if err != nil {
return err
}
fmt.Println("Install Kubernetes...")
output, err := runner.CombinedOutput(constants.KubeAdmInit)
if err != nil {
return err
}
lines := strings.Split(output, "\n")
for _, line := range lines {
rawline := strings.TrimSpace(line)
if strings.Contains(rawline, "kubeadm join") {
k8snodes.JoinString = rawline
}
}
if err := PopuldateKubeConfig(c); err != nil {
fmt.Println("Failed to write Kuberntes config file")
return err
}
symlink := filepath.Join("/usr/local/bin/", constants.KubeCtlBinaryName)
os.Remove(symlink)
kubectlLocalPath := constants.GetLocalK8sKitFilePath(constants.KubeCtlBinaryName, k8sversion)
if err := os.Symlink(kubectlLocalPath, symlink); err != nil {
fmt.Println(err.Error())
fmt.Printf("Failed to link kubectl, please put %s into your path.\n", kubectlLocalPath)
}
// symlink = filepath.Join("/usr/local/bin/", constants.DockerBinaryName)
// os.Remove(symlink)
// dockerLocalPath := constants.GetLocalK8sKitFilePath(constants.DockerBinaryName, k8sversion)
// dockerAliasContent := fmt.Sprintf(constants.DockerAlias, dockerLocalPath, vmconfig.IP+":2375")
// content := []byte(dockerAliasContent)
// ioutil.WriteFile(symlink, content, os.ModePerm)
// err = ioutil.WriteFile(symlink, content, 0644)
// if err != nil {
// fmt.Println(err.Error())
// fmt.Printf("Failed to link docker, please put %s into your path.\n", dockerLocalPath)
// }
fmt.Println("Install master node finished")
return nil
}
func PopuldateKubeConfig(c *ssh.Client) error {
fmt.Println("Populate Kubernetes configure file")
oldKubeConfig, err := kubeconfig.ReadConfigOrNew(constants.GetK8sConfigPath())
if err != nil {
return err
}
client, err := sftp.NewClient(c)
if err != nil {
return err
}
defer client.Close()
srcFile, err := client.Open("/etc/kubernetes/admin.conf")
if err != nil {
return err
}
os.MkdirAll(constants.GetK8sConfigFolder(), os.ModePerm)
dstFile, err := os.OpenFile(constants.GetK8sTmpConfigPath(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
// flush in-memory copy
err = dstFile.Sync()
if err != nil {
return err
}
newKubeConfig, err := kubeconfig.ReadConfigOrNew(constants.GetK8sTmpConfigPath())
if err != nil {
return err
}
for clusterName, cluster := range newKubeConfig.Clusters {
oldKubeConfig.Clusters[clusterName] = cluster
}
for _, context := range newKubeConfig.Contexts {
oldKubeConfig.Contexts["kubev-cluster"] = context
oldKubeConfig.CurrentContext = "kubev-cluster"
}
for userName, authInfo := range newKubeConfig.AuthInfos {
oldKubeConfig.AuthInfos[userName] = authInfo
}
if err := kubeconfig.WriteConfig(oldKubeConfig, constants.GetK8sConfigPath()); err != nil {
return err
}
utils.DeleteFile(constants.GetK8sTmpConfigPath())
return nil
}