forked from kubernetes/kops
/
vsphere_cloud.go
439 lines (370 loc) · 13.1 KB
/
vsphere_cloud.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
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 vsphere
// vsphere_cloud is the entry point to vSphere. All operations that need access to vSphere should be housed here.
import (
"bytes"
"context"
"fmt"
"net/url"
"os"
"strings"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"k8s.io/api/core/v1"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
k8scoredns "k8s.io/kops/dnsprovider/pkg/dnsprovider/providers/coredns"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
)
// VSphereCloud represents a vSphere cloud instance.
type VSphereCloud struct {
Server string
Datacenter string
Cluster string
Username string
Password string
Client *govmomi.Client
CoreDNSServer string
DNSZone string
}
const (
snapshotName string = "LinkCloneSnapshotPoint"
snapshotDesc string = "Snapshot created by kops"
cloudInitFile string = "cloud-init.iso"
)
var _ fi.Cloud = &VSphereCloud{}
// ProviderID returns ID for vSphere type cloud provider.
func (c *VSphereCloud) ProviderID() kops.CloudProviderID {
return kops.CloudProviderVSphere
}
// NewVSphereCloud returns VSphereCloud instance for given ClusterSpec.
func NewVSphereCloud(spec *kops.ClusterSpec) (*VSphereCloud, error) {
server := *spec.CloudConfig.VSphereServer
datacenter := *spec.CloudConfig.VSphereDatacenter
cluster := *spec.CloudConfig.VSphereResourcePool
glog.V(2).Infof("Creating vSphere Cloud with server(%s), datacenter(%s), cluster(%s)", server, datacenter, cluster)
dns_server := *spec.CloudConfig.VSphereCoreDNSServer
dns_zone := spec.DNSZone
username := os.Getenv("VSPHERE_USERNAME")
password := os.Getenv("VSPHERE_PASSWORD")
if username == "" || password == "" {
return nil, fmt.Errorf("Failed to detect vSphere username and password. Please set env variables: VSPHERE_USERNAME and VSPHERE_PASSWORD accordingly.")
}
u, err := url.Parse(fmt.Sprintf("https://%s/sdk", server))
if err != nil {
return nil, err
}
glog.V(2).Infof("Creating vSphere Cloud URL is %s", u)
// set username and password in URL
u.User = url.UserPassword(username, password)
c, err := govmomi.NewClient(context.TODO(), u, true)
if err != nil {
return nil, err
}
// Add retry functionality
c.RoundTripper = vim25.Retry(c.RoundTripper, vim25.TemporaryNetworkError(5))
vsphereCloud := &VSphereCloud{Server: server, Datacenter: datacenter, Cluster: cluster, Username: username, Password: password, Client: c, CoreDNSServer: dns_server, DNSZone: dns_zone}
spec.CloudConfig.VSphereUsername = fi.String(username)
spec.CloudConfig.VSpherePassword = fi.String(password)
glog.V(2).Infof("Created vSphere Cloud successfully: %+v", vsphereCloud)
return vsphereCloud, nil
}
// GetCloudGroups is not implemented yet, that needs to return the instances and groups that back a kops cluster.
func (c *VSphereCloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
glog.V(8).Infof("vSphere cloud provider GetCloudGroups not implemented yet")
return nil, fmt.Errorf("vSphere cloud provider does not support getting cloud groups at this time")
}
// DeleteGroup is not implemented yet, is a func that needs to delete a vSphere instance group.
func (c *VSphereCloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
glog.V(8).Infof("vSphere cloud provider DeleteGroup not implemented yet")
return fmt.Errorf("vSphere cloud provider does not support deleting cloud groups at this time.")
}
// DeleteInstance is not implemented yet, is func needs to delete a vSphereCloud instance.
func (c *VSphereCloud) DeleteInstance(i *cloudinstances.CloudInstanceGroupMember) error {
glog.V(8).Infof("vSphere cloud provider DeleteInstance not implemented yet")
return fmt.Errorf("vSphere cloud provider does not support deleting cloud instances at this time.")
}
// DNS returns dnsprovider interface for this vSphere cloud.
func (c *VSphereCloud) DNS() (dnsprovider.Interface, error) {
var provider dnsprovider.Interface
var err error
var lines []string
lines = append(lines, "etcd-endpoints = "+c.CoreDNSServer)
lines = append(lines, "zones = "+c.DNSZone)
config := "[global]\n" + strings.Join(lines, "\n") + "\n"
file := bytes.NewReader([]byte(config))
provider, err = dnsprovider.GetDnsProvider(k8scoredns.ProviderName, file)
if err != nil {
return nil, fmt.Errorf("Error building (k8s) DNS provider: %v", err)
}
return provider, nil
}
// FindVPCInfo doesn't perform any operation for now. No VPC is present for vSphere.
func (c *VSphereCloud) FindVPCInfo(id string) (*fi.VPCInfo, error) {
glog.Warning("FindVPCInfo not (yet) implemented on VSphere")
return nil, nil
}
// CreateLinkClonedVm creates linked clone of given VM image. This method will perform all necessary steps, like creating snapshot if it's not already present.
func (c *VSphereCloud) CreateLinkClonedVm(vmName, vmImage *string) (string, error) {
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return "", err
}
f.SetDatacenter(dc)
templateVm, err := f.VirtualMachine(ctx, *vmImage)
if err != nil {
return "", err
}
glog.V(2).Infof("Template VM ref is %+v", templateVm)
datacenterFolders, err := dc.Folders(ctx)
if err != nil {
return "", err
}
// Create snapshot of the template VM if not already snapshotted.
snapshot, err := createSnapshot(ctx, templateVm, snapshotName, snapshotDesc)
if err != nil {
return "", err
}
clsComputeRes, err := f.ClusterComputeResource(ctx, c.Cluster)
glog.V(4).Infof("Cluster compute resource is %+v", clsComputeRes)
if err != nil {
return "", err
}
resPool, err := clsComputeRes.ResourcePool(ctx)
glog.V(4).Infof("Cluster resource pool is %+v", resPool)
if err != nil {
return "", err
}
if resPool == nil {
return "", errors.New(fmt.Sprintf("No resource pool found for cluster %s", c.Cluster))
}
resPoolRef := resPool.Reference()
snapshotRef := snapshot.Reference()
cloneSpec := &types.VirtualMachineCloneSpec{
Config: &types.VirtualMachineConfigSpec{
Flags: &types.VirtualMachineFlagInfo{
DiskUuidEnabled: fi.Bool(true),
},
},
Location: types.VirtualMachineRelocateSpec{
Pool: &resPoolRef,
DiskMoveType: "createNewChildDiskBacking",
},
Snapshot: &snapshotRef,
}
// Create a link cloned VM from the template VM's snapshot
clonedVmTask, err := templateVm.Clone(ctx, datacenterFolders.VmFolder, *vmName, *cloneSpec)
if err != nil {
return "", err
}
clonedVmTaskInfo, err := clonedVmTask.WaitForResult(ctx, nil)
if err != nil {
return "", err
}
clonedVm := clonedVmTaskInfo.Result.(object.Reference)
glog.V(2).Infof("Created VM %s successfully", clonedVm)
return clonedVm.Reference().Value, nil
}
// PowerOn powers on given VM.
func (c *VSphereCloud) PowerOn(vm string) error {
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return err
}
f.SetDatacenter(dc)
vmRef, err := f.VirtualMachine(ctx, vm)
if err != nil {
return err
}
task, err := vmRef.PowerOn(ctx)
if err != nil {
return err
}
task.Wait(ctx)
return nil
}
// UploadAndAttachISO uploads the ISO to datastore and attaches it to the given VM.
func (c *VSphereCloud) UploadAndAttachISO(vm *string, isoFile string) error {
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return err
}
f.SetDatacenter(dc)
vmRef, err := f.VirtualMachine(ctx, *vm)
if err != nil {
return err
}
var vmResult mo.VirtualMachine
pc := property.DefaultCollector(c.Client.Client)
err = pc.RetrieveOne(ctx, vmRef.Reference(), []string{"datastore"}, &vmResult)
if err != nil {
glog.Fatalf("Unable to retrieve VM summary for VM %s", *vm)
}
glog.V(4).Infof("vm property collector result :%+v\n", vmResult)
// We expect the VM to be on only 1 datastore
dsRef := vmResult.Datastore[0].Reference()
var dsResult mo.Datastore
err = pc.RetrieveOne(ctx, dsRef, []string{"summary"}, &dsResult)
if err != nil {
glog.Fatalf("Unable to retrieve datastore summary for datastore %s", dsRef)
}
glog.V(4).Infof("datastore property collector result :%+v\n", dsResult)
dsObj, err := f.Datastore(ctx, dsResult.Summary.Name)
if err != nil {
return err
}
p := soap.DefaultUpload
dstIsoFile := getCloudInitFileName(*vm)
glog.V(2).Infof("Uploading ISO file %s to datastore %+v, destination iso is %s\n", isoFile, dsObj, dstIsoFile)
err = dsObj.UploadFile(ctx, isoFile, dstIsoFile, &p)
if err != nil {
return err
}
glog.V(2).Infof("Uploaded ISO file %s", isoFile)
// Find the cd-rom device and insert the cloud init iso file into it.
devices, err := vmRef.Device(ctx)
if err != nil {
return err
}
// passing empty cd-rom name so that the first one gets returned
cdrom, err := devices.FindCdrom("")
cdrom.Connectable.StartConnected = true
if err != nil {
return err
}
iso := dsObj.Path(dstIsoFile)
glog.V(2).Infof("Inserting ISO file %s into cd-rom", iso)
return vmRef.EditDevice(ctx, devices.InsertIso(cdrom, iso))
}
// Returns VM's instance uuid
func (c *VSphereCloud) FindVMUUID(vm *string) (string, error) {
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return "", err
}
f.SetDatacenter(dc)
vmRef, err := f.VirtualMachine(ctx, *vm)
if err != nil {
return "", err
}
var vmResult mo.VirtualMachine
pc := property.DefaultCollector(c.Client.Client)
err = pc.RetrieveOne(ctx, vmRef.Reference(), []string{"config.uuid"}, &vmResult)
if err != nil {
return "", err
}
glog.V(4).Infof("vm property collector result :%+v\n", vmResult)
glog.V(3).Infof("retrieved vm uuid as %q for vm %q", vmResult.Config.Uuid, *vm)
return vmResult.Config.Uuid, nil
}
// GetVirtualMachines returns the VMs where the VM name matches the strings in the argument
func (c *VSphereCloud) GetVirtualMachines(args []string) ([]*object.VirtualMachine, error) {
var out []*object.VirtualMachine
// List virtual machines
if len(args) == 0 {
return nil, errors.New("no argument")
}
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return nil, err
}
f.SetDatacenter(dc)
var nfe error
// List virtual machines for every argument
for _, arg := range args {
vms, err := f.VirtualMachineList(context.TODO(), arg)
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
// Let caller decide how to handle NotFoundError
nfe = err
continue
}
return nil, err
}
out = append(out, vms...)
}
return out, nfe
}
func (c *VSphereCloud) DeleteCloudInitISO(vm *string) error {
f := find.NewFinder(c.Client.Client, true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dc, err := f.Datacenter(ctx, c.Datacenter)
if err != nil {
return err
}
f.SetDatacenter(dc)
vmRef, err := f.VirtualMachine(ctx, *vm)
if err != nil {
return err
}
var vmResult mo.VirtualMachine
pc := property.DefaultCollector(c.Client.Client)
err = pc.RetrieveOne(ctx, vmRef.Reference(), []string{"datastore"}, &vmResult)
if err != nil {
glog.Fatalf("Unable to retrieve VM summary for VM %s", *vm)
}
glog.V(4).Infof("vm property collector result :%+v\n", vmResult)
// We expect the VM to be on only 1 datastore
dsRef := vmResult.Datastore[0].Reference()
var dsResult mo.Datastore
err = pc.RetrieveOne(ctx, dsRef, []string{"summary"}, &dsResult)
if err != nil {
glog.Fatalf("Unable to retrieve datastore summary for datastore %s", dsRef)
}
glog.V(4).Infof("datastore property collector result :%+v\n", dsResult)
dsObj, err := f.Datastore(ctx, dsResult.Summary.Name)
if err != nil {
return err
}
isoFileName := getCloudInitFileName(*vm)
fileManager := dsObj.NewFileManager(dc, false)
err = fileManager.DeleteFile(ctx, isoFileName)
if err != nil {
if types.IsFileNotFound(err) {
glog.Warningf("ISO file not found: %q", isoFileName)
return nil
}
return err
}
glog.V(2).Infof("Deleted ISO file %q", isoFileName)
return nil
}
func getCloudInitFileName(vmName string) string {
return vmName + "/" + cloudInitFile
}