forked from kubernetes/kops
/
attachiso.go
287 lines (237 loc) · 8.04 KB
/
attachiso.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright 2017 The Kubernetes Authors.
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 vspheretasks
// attachiso houses the task that creates cloud-init ISO file, uploads and attaches it to a VM on vSphere cloud.
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/golang/glog"
"github.com/pborman/uuid"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/vsphere"
)
// AttachISO represents the cloud-init ISO file attached to a VM on vSphere cloud.
//go:generate fitask -type=AttachISO
type AttachISO struct {
Name *string
VM *VirtualMachine
IG *kops.InstanceGroup
BootstrapScript *model.BootstrapScript
Cluster *kops.Cluster
}
var _ fi.HasName = &AttachISO{}
var _ fi.HasDependencies = &AttachISO{}
// GetDependencies returns map of tasks on which this task depends.
func (o *AttachISO) GetDependencies(tasks map[string]fi.Task) []fi.Task {
var deps []fi.Task
vmCreateTask := tasks["VirtualMachine/"+*o.VM.Name]
if vmCreateTask == nil {
glog.Fatalf("Unable to find create VM task %s dependency for AttachISO %s", *o.VM.Name, *o.Name)
}
deps = append(deps, vmCreateTask)
return deps
}
// GetName returns the Name of the object, implementing fi.HasName
func (o *AttachISO) GetName() *string {
return o.Name
}
// SetName sets the Name of the object, implementing fi.SetName
func (o *AttachISO) SetName(name string) {
o.Name = &name
}
// Run invokes DefaultDeltaRunMethod for this task.
func (e *AttachISO) Run(c *fi.Context) error {
glog.Info("AttachISO.Run invoked!")
return fi.DefaultDeltaRunMethod(e, c)
}
// Find is a no-op for this task.
func (e *AttachISO) Find(c *fi.Context) (*AttachISO, error) {
glog.Info("AttachISO.Find invoked!")
return nil, nil
}
// CheckChanges is a no-op for this task.
func (_ *AttachISO) CheckChanges(a, e, changes *AttachISO) error {
glog.Info("AttachISO.CheckChanges invoked!")
return nil
}
// RenderVSphere executes the actual task logic, for vSphere cloud.
func (_ *AttachISO) RenderVSphere(t *vsphere.VSphereAPITarget, a, e, changes *AttachISO) error {
startupScript, err := e.BootstrapScript.ResourceNodeUp(e.IG, e.Cluster)
if err != nil {
return fmt.Errorf("error on resource nodeup: %v", err)
}
startupStr, err := startupScript.AsString()
if err != nil {
return fmt.Errorf("error rendering startup script: %v", err)
}
dir, err := ioutil.TempDir("", *e.VM.Name)
if err != nil {
return fmt.Errorf("error creating tempdir: %v", err)
}
defer os.RemoveAll(dir)
// Need this in cloud config file for vSphere CloudProvider
vmUUID, err := t.Cloud.FindVMUUID(e.VM.Name)
if err != nil {
return err
}
isoFile, err := createISO(e, startupStr, dir, t.Cloud.CoreDNSServer, vmUUID)
if err != nil {
glog.Errorf("Failed to createISO for vspheretasks, err: %v", err)
return err
}
err = t.Cloud.UploadAndAttachISO(e.VM.Name, isoFile)
if err != nil {
return err
}
return nil
}
func createUserData(e *AttachISO, startupStr string, dir string, dnsServer string, vmUUID string) error {
// Populate nodeup initialization script.
// Update the startup script to add the extra spaces for
// indentation when copied to the user-data file.
strArray := strings.Split(startupStr, "\n")
for i, str := range strArray {
if len(str) > 0 {
strArray[i] = " " + str
}
}
startupStr = strings.Join(strArray, "\n")
data := strings.Replace(userDataTemplate, "$SCRIPT", startupStr, -1)
// Populate script to update nameserver for the VM.
dnsURL, err := url.Parse(dnsServer)
if err != nil {
return err
}
dnsHost, _, err := net.SplitHostPort(dnsURL.Host)
if err != nil {
return err
}
var lines []string
lines = append(lines, " echo \"nameserver "+dnsHost+"\" >> /etc/resolvconf/resolv.conf.d/head")
lines = append(lines, " resolvconf -u")
dnsUpdateStr := strings.Join(lines, "\n")
data = strings.Replace(data, "$DNS_SCRIPT", dnsUpdateStr, -1)
// Populate VM UUID information.
vmUUIDStr := " " + vmUUID + "\n"
data = strings.Replace(data, "$VM_UUID", vmUUIDStr, -1)
// Populate volume metadata.
data, err = createVolumeScript(e, data)
if err != nil {
return err
}
userDataFile := filepath.Join(dir, "user-data")
glog.V(4).Infof("User data file content: %s", data)
if err = ioutil.WriteFile(userDataFile, []byte(data), 0644); err != nil {
glog.Errorf("Unable to write user-data into file %s", userDataFile)
return err
}
return nil
}
func createVolumeScript(e *AttachISO, data string) (string, error) {
if e.IG.Spec.Role != kops.InstanceGroupRoleMaster {
return strings.Replace(data, "$VOLUME_SCRIPT", " No volume metadata needed for "+string(e.IG.Spec.Role)+".", -1), nil
}
volsString, err := getVolMetadata(e)
if err != nil {
return "", err
}
return strings.Replace(data, "$VOLUME_SCRIPT", " "+volsString, -1), nil
}
func getVolMetadata(e *AttachISO) (string, error) {
var volsMetadata []vsphere.VolumeMetadata
// Creating vsphere.VolumeMetadata using clusters EtcdClusterSpec
for i, etcd := range e.Cluster.Spec.EtcdClusters {
volMetadata := vsphere.VolumeMetadata{}
volMetadata.EtcdClusterName = etcd.Name
volMetadata.VolumeId = vsphere.GetVolumeId(i + 1)
var members []vsphere.EtcdMemberSpec
var thisNode string
for _, member := range etcd.Members {
if *member.InstanceGroup == e.IG.Name {
thisNode = member.Name
}
etcdMember := vsphere.EtcdMemberSpec{
Name: member.Name,
InstanceGroup: *member.InstanceGroup,
}
members = append(members, etcdMember)
}
if thisNode == "" {
return "", fmt.Errorf("Failed to construct volume metadata for %v InstanceGroup.", e.IG.Name)
}
volMetadata.EtcdNodeName = thisNode
volMetadata.Members = members
volsMetadata = append(volsMetadata, volMetadata)
}
glog.V(4).Infof("Marshaling master vol metadata : %v", volsMetadata)
volsString, err := vsphere.MarshalVolumeMetadata(volsMetadata)
glog.V(4).Infof("Marshaled master vol metadata: %v", volsString)
if err != nil {
return "", err
}
return volsString, nil
}
func createMetaData(dir string, vmName string) error {
data := strings.Replace(metaDataTemplate, "$INSTANCE_ID", uuid.NewUUID().String(), -1)
data = strings.Replace(data, "$LOCAL_HOST_NAME", vmName, -1)
glog.V(4).Infof("Meta data file content: %s", string(data))
metaDataFile := filepath.Join(dir, "meta-data")
if err := ioutil.WriteFile(metaDataFile, []byte(data), 0644); err != nil {
glog.Errorf("Unable to write meta-data into file %s", metaDataFile)
return err
}
return nil
}
func createISO(e *AttachISO, startupStr string, dir string, dnsServer, vmUUID string) (string, error) {
err := createUserData(e, startupStr, dir, dnsServer, vmUUID)
if err != nil {
return "", err
}
err = createMetaData(dir, *e.VM.Name)
if err != nil {
return "", err
}
isoFile := filepath.Join(dir, *e.VM.Name+".iso")
var commandName string
switch os := runtime.GOOS; os {
case "darwin":
commandName = "mkisofs"
case "linux":
commandName = "genisoimage"
default:
return "", fmt.Errorf("Cannot generate ISO file %s. Unsupported operation system (%s)!!!", isoFile, os)
}
cmd := exec.Command(commandName, "-o", isoFile, "-volid", "cidata", "-joliet", "-rock", dir)
var out bytes.Buffer
cmd.Stdout = &out
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
glog.Errorf("Error %s occurred while executing command %+v", err, cmd)
return "", err
}
glog.V(4).Infof("%s std output : %s\n", commandName, out.String())
glog.V(4).Infof("%s std error : %s\n", commandName, stderr.String())
return isoFile, nil
}