forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
273 lines (242 loc) · 7.1 KB
/
utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package node
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var regExHyphen = regexp.MustCompile("([a-z])([A-Z])")
var (
RegExNodeDirEnv = regexp.MustCompile("^" + nodeDirEnvKey + ".*")
RegExNodePluginToken = regexp.MustCompile("^" + "MACHINE_PLUGIN_TOKEN=" + ".*")
RegExNodeDriverName = regexp.MustCompile("^" + "MACHINE_PLUGIN_DRIVER_NAME=" + ".*")
)
const (
errorCreatingNode = "Error creating machine: "
nodeDirEnvKey = "MACHINE_STORAGE_PATH="
nodeCmd = "docker-machine"
ec2TagFlag = "tags"
)
func buildAgentCommand(node *v3.Node, dockerRun string) []string {
drun := strings.Fields(dockerRun)
cmd := []string{"--native-ssh", "ssh", node.Spec.RequestedHostname}
cmd = append(cmd, drun...)
cmd = append(cmd, "-r", "-n", node.Name)
return cmd
}
func buildCreateCommand(node *v3.Node, configMap map[string]interface{}) []string {
sDriver := strings.ToLower(node.Status.NodeTemplateSpec.Driver)
cmd := []string{"create", "-d", sDriver}
cmd = append(cmd, buildEngineOpts("--engine-install-url", []string{node.Status.NodeTemplateSpec.EngineInstallURL})...)
cmd = append(cmd, buildEngineOpts("--engine-opt", mapToSlice(node.Status.NodeTemplateSpec.EngineOpt))...)
cmd = append(cmd, buildEngineOpts("--engine-env", mapToSlice(node.Status.NodeTemplateSpec.EngineEnv))...)
cmd = append(cmd, buildEngineOpts("--engine-insecure-registry", node.Status.NodeTemplateSpec.EngineInsecureRegistry)...)
cmd = append(cmd, buildEngineOpts("--engine-label", mapToSlice(node.Status.NodeTemplateSpec.EngineLabel))...)
cmd = append(cmd, buildEngineOpts("--engine-registry-mirror", node.Status.NodeTemplateSpec.EngineRegistryMirror)...)
cmd = append(cmd, buildEngineOpts("--engine-storage-driver", []string{node.Status.NodeTemplateSpec.EngineStorageDriver})...)
for k, v := range configMap {
dmField := "--" + sDriver + "-" + strings.ToLower(regExHyphen.ReplaceAllString(k, "${1}-${2}"))
switch v.(type) {
case int64:
cmd = append(cmd, dmField, strconv.FormatInt(v.(int64), 10))
case string:
if v.(string) != "" {
cmd = append(cmd, dmField, v.(string))
}
case bool:
if v.(bool) {
cmd = append(cmd, dmField)
}
case []interface{}:
for _, s := range v.([]interface{}) {
if _, ok := s.(string); ok {
cmd = append(cmd, dmField, s.(string))
}
}
}
}
logrus.Debugf("create cmd %v", cmd)
cmd = append(cmd, node.Spec.RequestedHostname)
return cmd
}
func buildEngineOpts(name string, values []string) []string {
var opts []string
for _, value := range values {
if value == "" {
continue
}
opts = append(opts, name, value)
}
return opts
}
func mapToSlice(m map[string]string) []string {
var ret []string
for k, v := range m {
ret = append(ret, fmt.Sprintf("%s=%s", k, v))
}
return ret
}
func buildCommand(nodeDir string, cmdArgs []string) *exec.Cmd {
command := exec.Command(nodeCmd, cmdArgs...)
env := initEnviron(nodeDir)
command.Env = env
return command
}
func initEnviron(nodeDir string) []string {
env := os.Environ()
found := false
for idx, ev := range env {
if RegExNodeDirEnv.MatchString(ev) {
env[idx] = nodeDirEnvKey + nodeDir
found = true
}
if RegExNodePluginToken.MatchString(ev) {
env[idx] = ""
}
if RegExNodeDriverName.MatchString(ev) {
env[idx] = ""
}
}
if !found {
env = append(env, nodeDirEnvKey+nodeDir)
}
return env
}
func startReturnOutput(command *exec.Cmd) (io.ReadCloser, io.ReadCloser, error) {
readerStdout, err := command.StdoutPipe()
if err != nil {
return nil, nil, err
}
readerStderr, err := command.StderrPipe()
if err != nil {
return nil, nil, err
}
if err := command.Start(); err != nil {
readerStdout.Close()
readerStderr.Close()
return nil, nil, err
}
return readerStdout, readerStderr, nil
}
func getSSHKey(nodeDir string, obj *v3.Node) (string, error) {
if err := waitUntilSSHKey(nodeDir, obj); err != nil {
return "", err
}
return getSSHPrivateKey(nodeDir, obj)
}
func (m *Lifecycle) reportStatus(stdoutReader io.Reader, stderrReader io.Reader, node *v3.Node) (*v3.Node, error) {
scanner := bufio.NewScanner(stdoutReader)
for scanner.Scan() {
msg := scanner.Text()
if strings.Contains(msg, "To see how to connect") {
continue
}
logrus.Infof("stdout: %s", msg)
_, err := filterDockerMessage(msg, node)
if err != nil {
return node, err
}
logrus.Info(msg)
v3.NodeConditionProvisioned.Message(node, msg)
// ignore update errors
if newObj, err := m.nodeClient.Update(node); err == nil {
node = newObj
} else {
node, _ = m.nodeClient.Get(node.Name, metav1.GetOptions{})
}
}
scanner = bufio.NewScanner(stderrReader)
for scanner.Scan() {
msg := scanner.Text()
return node, errors.New(msg)
}
return node, nil
}
func filterDockerMessage(msg string, node *v3.Node) (string, error) {
if strings.Contains(msg, errorCreatingNode) {
return "", errors.New(msg)
}
if strings.Contains(msg, node.Spec.RequestedHostname) {
return "", nil
}
return msg, nil
}
func nodeExists(nodeDir string, name string) (bool, error) {
command := buildCommand(nodeDir, []string{"ls", "-q"})
r, err := command.StdoutPipe()
if err != nil {
return false, err
}
if err = command.Start(); err != nil {
return false, err
}
defer command.Wait()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
foundName := scanner.Text()
if foundName == name {
return true, nil
}
}
if err = scanner.Err(); err != nil {
return false, err
}
if err := command.Wait(); err != nil {
return false, err
}
return false, nil
}
func deleteNode(nodeDir string, node *v3.Node) error {
command := buildCommand(nodeDir, []string{"rm", "-f", node.Spec.RequestedHostname})
if err := command.Start(); err != nil {
return err
}
return command.Wait()
}
func getSSHPrivateKey(nodeDir string, node *v3.Node) (string, error) {
keyPath := filepath.Join(nodeDir, "machines", node.Spec.RequestedHostname, "id_rsa")
data, err := ioutil.ReadFile(keyPath)
if err != nil {
return "", nil
}
return string(data), nil
}
func waitUntilSSHKey(nodeDir string, node *v3.Node) error {
keyPath := filepath.Join(nodeDir, "machines", node.Spec.RequestedHostname, "id_rsa")
startTime := time.Now()
increments := 1
for {
if time.Now().After(startTime.Add(15 * time.Second)) {
return errors.New("Timeout waiting for ssh key")
}
if _, err := os.Stat(keyPath); err != nil {
logrus.Debugf("keyPath not found. The node is probably still provisioning. Sleep %v second", increments)
time.Sleep(time.Duration(increments) * time.Second)
increments = increments * 2
continue
}
return nil
}
}
func setEc2ClusterIDTag(data interface{}, clusterID string) {
if m, ok := data.(map[string]interface{}); ok {
tagValue := fmt.Sprintf("kubernetes.io/cluster/%s,owned", clusterID)
if tags, ok := m[ec2TagFlag]; !ok || convert.ToString(tags) == "" {
m[ec2TagFlag] = tagValue
} else {
m[ec2TagFlag] = convert.ToString(tags) + "," + tagValue
}
}
}