VMware vSphere Integrated Containers provider (#206)

* Add Virtual Kubelet provider for VIC

Initial virtual kubelet provider for VMware VIC.  This provider currently
handles creating and starting of a pod VM via the VIC portlayer and persona
server.  Image store handling via the VIC persona server.  This provider
currently requires the feature/wolfpack branch of VIC.

* Added pod stop and delete.  Also added node capacity.

Added the ability to stop and delete pod VMs via VIC.  Also retrieve
node capacity information from the VCH.

* Cleanup and readme file

Some file clean up and added a Readme.md markdown file for the VIC
provider.

* Cleaned up errors, added function comments, moved operation code

1. Cleaned up error handling.  Set standard for creating errors.
2. Added method prototype comments for all interface functions.
3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder.

* Add mocking code and unit tests for podcache, podcreator, and podstarter

Used the unit test framework used in VIC to handle assertions in the provider's
unit test.  Mocking code generated using OSS project mockery, which is compatible
with the testify assertion framework.

* Vendored packages for the VIC provider

Requires feature/wolfpack branch of VIC and a few specific commit sha of
projects used within VIC.

* Implementation of POD Stopper and Deleter unit tests (#4)

* Updated files for initial PR
This commit is contained in:
Loc Nguyen
2018-06-04 15:41:32 -07:00
committed by Ria Bhatia
parent 98a111e8b7
commit 513cebe7b7
6296 changed files with 1123685 additions and 8 deletions

64
vendor/github.com/vmware/vic/lib/spec/disk.go generated vendored Normal file
View File

@@ -0,0 +1,64 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/trace"
)
// NewVirtualDisk returns a new disk attached to the controller
func NewVirtualDisk(controller types.BaseVirtualController) *types.VirtualDisk {
defer trace.End(trace.Begin(""))
unitNumber := int32(-1)
return &types.VirtualDisk{
VirtualDevice: types.VirtualDevice{
ControllerKey: controller.GetVirtualController().Key,
UnitNumber: &unitNumber,
},
}
}
// NewVirtualSCSIDisk returns a new disk attached to the SCSI controller
func NewVirtualSCSIDisk(controller types.VirtualSCSIController) *types.VirtualDisk {
defer trace.End(trace.Begin(""))
return NewVirtualDisk(&controller)
}
// NewVirtualIDEDisk returns a new disk attached to the IDE controller
func NewVirtualIDEDisk(controller types.VirtualIDEController) *types.VirtualDisk {
defer trace.End(trace.Begin(""))
return NewVirtualDisk(&controller)
}
// AddVirtualDisk adds a virtual disk to a virtual machine.
func (s *VirtualMachineConfigSpec) AddVirtualDisk(device *types.VirtualDisk) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
device.GetVirtualDevice().Key = s.generateNextKey()
return s.AddAndCreateVirtualDevice(device)
}
// RemoveVirtualDisk remvoes the virtual disk from a virtual machine.
func (s *VirtualMachineConfigSpec) RemoveVirtualDisk(device *types.VirtualDisk) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveAndDestroyVirtualDevice(device)
}

117
vendor/github.com/vmware/vic/lib/spec/ide.go generated vendored Normal file
View File

@@ -0,0 +1,117 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/trace"
)
// NewVirtualIDEController returns a VirtualIDEController spec with key.
func NewVirtualIDEController(key int32) *types.VirtualIDEController {
defer trace.End(trace.Begin(""))
return &types.VirtualIDEController{
VirtualController: types.VirtualController{
VirtualDevice: types.VirtualDevice{
Key: key,
},
},
}
}
// AddVirtualIDEController adds a virtual IDE controller.
func (s *VirtualMachineConfigSpec) AddVirtualIDEController(device *types.VirtualIDEController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.AddVirtualDevice(device)
}
// RemoveVirtualIDEController removes a virtual IDE controller.
func (s *VirtualMachineConfigSpec) RemoveVirtualIDEController(device *types.VirtualIDEController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// NewVirtualCdrom returns a virtual CDROM device.
func NewVirtualCdrom(device *types.VirtualIDEController) *types.VirtualCdrom {
defer trace.End(trace.Begin(""))
return &types.VirtualCdrom{
VirtualDevice: types.VirtualDevice{
ControllerKey: device.Key,
UnitNumber: new(int32),
},
}
}
// AddVirtualCdrom adds a CD-ROM device in a virtual machine.
func (s *VirtualMachineConfigSpec) AddVirtualCdrom(device *types.VirtualCdrom) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
device.GetVirtualDevice().Key = s.generateNextKey()
device.GetVirtualDevice().Backing = &types.VirtualCdromIsoBackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: s.BootMediaPath(),
},
}
return s.AddVirtualDevice(device)
}
// RemoveVirtualCdrom adds a CD-ROM device in a virtual machine.
func (s *VirtualMachineConfigSpec) RemoveVirtualCdrom(device *types.VirtualCdrom) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// NewVirtualFloppy adds a floppy device in a virtual machine.
func NewVirtualFloppy(device *types.VirtualIDEController) *types.VirtualFloppy {
defer trace.End(trace.Begin(""))
return &types.VirtualFloppy{
VirtualDevice: types.VirtualDevice{
ControllerKey: device.Key,
UnitNumber: new(int32),
},
}
}
// AddVirtualFloppy adds a floppy device in a virtual machine.
func (s *VirtualMachineConfigSpec) AddVirtualFloppy(device *types.VirtualFloppy) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
device.GetVirtualDevice().Key = s.generateNextKey()
device.GetVirtualDevice().Backing = &types.VirtualFloppyImageBackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: s.BootMediaPath(),
},
}
return s.AddVirtualDevice(device)
}
// RemoveVirtualFloppyDevice removes a floppy device from the virtual machine.
func (s *VirtualMachineConfigSpec) RemoveVirtualFloppyDevice(device *types.VirtualFloppy) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}

134
vendor/github.com/vmware/vic/lib/spec/network.go generated vendored Normal file
View File

@@ -0,0 +1,134 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"fmt"
"context"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/trace"
)
// NewVirtualVmxnet3 returns VirtualVmxnet3 spec.
func NewVirtualVmxnet3() *types.VirtualVmxnet3 {
defer trace.End(trace.Begin(""))
return &types.VirtualVmxnet3{
VirtualVmxnet: types.VirtualVmxnet{
VirtualEthernetCard: types.VirtualEthernetCard{
AddressType: string(types.VirtualEthernetCardMacTypeGenerated),
},
},
}
}
// NewVirtualPCNet32 returns VirtualPCNet32 spec.
func NewVirtualPCNet32() *types.VirtualPCNet32 {
defer trace.End(trace.Begin(""))
return &types.VirtualPCNet32{
VirtualEthernetCard: types.VirtualEthernetCard{
AddressType: string(types.VirtualEthernetCardMacTypeGenerated),
},
}
}
// NewVirtualE1000 returns VirtualE1000 spec.
func NewVirtualE1000() *types.VirtualE1000 {
defer trace.End(trace.Begin(""))
return &types.VirtualE1000{
VirtualEthernetCard: types.VirtualEthernetCard{
AddressType: string(types.VirtualEthernetCardMacTypeGenerated),
},
}
}
func (s *VirtualMachineConfigSpec) addVirtualNIC(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
device.GetVirtualDevice().Key = s.generateNextKey()
return s.AddVirtualDevice(device)
}
// AddVirtualVmxnet3 adds a VirtualVmxnet3 device.
func (s *VirtualMachineConfigSpec) AddVirtualVmxnet3(device *types.VirtualVmxnet3) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.addVirtualNIC(device)
}
// AddVirtualPCNet32 adds a VirtualPCNet32 device.
func (s *VirtualMachineConfigSpec) AddVirtualPCNet32(device *types.VirtualPCNet32) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.addVirtualNIC(device)
}
// AddVirtualE1000 adds a VirtualE1000 device.
func (s *VirtualMachineConfigSpec) AddVirtualE1000(device *types.VirtualE1000) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.addVirtualNIC(device)
}
// RemoveVirtualVmxnet3 adds a VirtualVmxnet3 device.
func (s *VirtualMachineConfigSpec) RemoveVirtualVmxnet3(device *types.VirtualVmxnet3) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// RemoveVirtualPCNet32 adds a VirtualPCNet32 device.
func (s *VirtualMachineConfigSpec) RemoveVirtualPCNet32(device *types.VirtualPCNet32) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// RemoveVirtualE1000 adds a VirtualE1000 device.
func (s *VirtualMachineConfigSpec) RemoveVirtualE1000(device *types.VirtualE1000) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
func (s *VirtualMachineConfigSpec) FindNICs(ctx context.Context, network object.NetworkReference) ([]types.BaseVirtualDeviceConfigSpec, error) {
if network == nil {
return nil, fmt.Errorf("no network provided")
}
backing, err := network.EthernetCardBackingInfo(ctx)
if err != nil {
return nil, err
}
var dcs []types.BaseVirtualDeviceConfigSpec
for _, d := range s.DeviceChange {
dev := d.GetVirtualDeviceConfigSpec().Device
if _, ok := dev.(types.BaseVirtualEthernetCard); ok {
var dl object.VirtualDeviceList
dl = append(dl, dev)
dl = dl.SelectByBackingInfo(backing)
if len(dl) > 0 {
dcs = append(dcs, d)
}
}
}
return dcs, nil
}

135
vendor/github.com/vmware/vic/lib/spec/scsi.go generated vendored Normal file
View File

@@ -0,0 +1,135 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"fmt"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/pkg/trace"
)
// NewVirtualSCSIController returns a VirtualSCSIController with bus number and key.
func NewVirtualSCSIController(bus int32, key int32) types.VirtualSCSIController {
defer trace.End(trace.Begin(fmt.Sprintf("%d - %d", bus, key)))
return types.VirtualSCSIController{
SharedBus: types.VirtualSCSISharingNoSharing,
VirtualController: types.VirtualController{
BusNumber: bus,
VirtualDevice: types.VirtualDevice{
Key: key,
},
},
}
}
// NewParaVirtualSCSIController returns ParaVirtualSCSIController spec.
func NewParaVirtualSCSIController(controller types.VirtualSCSIController) *types.ParaVirtualSCSIController {
defer trace.End(trace.Begin(""))
return &types.ParaVirtualSCSIController{
VirtualSCSIController: controller,
}
}
// NewVirtualBusLogicController returns VirtualBusLogicController spec.
func NewVirtualBusLogicController(controller types.VirtualSCSIController) *types.VirtualBusLogicController {
defer trace.End(trace.Begin(""))
return &types.VirtualBusLogicController{
VirtualSCSIController: controller,
}
}
// NewVirtualLsiLogicController returns a VirtualLsiLogicController spec
func NewVirtualLsiLogicController(controller types.VirtualSCSIController) *types.VirtualLsiLogicController {
defer trace.End(trace.Begin(""))
return &types.VirtualLsiLogicController{
VirtualSCSIController: controller,
}
}
// NewVirtualLsiLogicSASController returns VirtualLsiLogicSASController spec.
func NewVirtualLsiLogicSASController(controller types.VirtualSCSIController) *types.VirtualLsiLogicSASController {
defer trace.End(trace.Begin(""))
return &types.VirtualLsiLogicSASController{
VirtualSCSIController: controller,
}
}
// AddParaVirtualSCSIController adds a paravirtualized SCSI controller.
func (s *VirtualMachineConfigSpec) AddParaVirtualSCSIController(device *types.ParaVirtualSCSIController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.AddVirtualDevice(device)
}
// RemoveParaVirtualSCSIController removes a paravirtualized SCSI controller.
func (s *VirtualMachineConfigSpec) RemoveParaVirtualSCSIController(device *types.ParaVirtualSCSIController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// AddVirtualBusLogicController adds a BusLogic SCSI controller.
func (s *VirtualMachineConfigSpec) AddVirtualBusLogicController(device *types.VirtualBusLogicController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.AddVirtualDevice(device)
}
// RemoveVirtualBusLogicController removes a BusLogic SCSI controller.
func (s *VirtualMachineConfigSpec) RemoveVirtualBusLogicController(device *types.VirtualBusLogicController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// AddVirtualLsiLogicController adds a LSI Logic SCSI controller.
func (s *VirtualMachineConfigSpec) AddVirtualLsiLogicController(device *types.VirtualLsiLogicController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.AddVirtualDevice(device)
}
// RemoveVirtualLsiLogicController removes a LSI Logic SCSI controller.
func (s *VirtualMachineConfigSpec) RemoveVirtualLsiLogicController(device *types.VirtualLsiLogicController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}
// AddVirtualLsiLogicSASController add a LSI Logic SAS SCSI controller.
func (s *VirtualMachineConfigSpec) AddVirtualLsiLogicSASController(device *types.VirtualLsiLogicSASController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.AddVirtualDevice(device)
}
// RemoveVirtualLsiLogicSASController removes a LSI Logic SAS SCSI controller.
func (s *VirtualMachineConfigSpec) RemoveVirtualLsiLogicSASController(device *types.VirtualLsiLogicSASController) *VirtualMachineConfigSpec {
defer trace.End(trace.Begin(s.ID()))
return s.RemoveVirtualDevice(device)
}

305
vendor/github.com/vmware/vic/lib/spec/spec.go generated vendored Normal file
View File

@@ -0,0 +1,305 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"context"
"net/url"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/config/executor"
"github.com/vmware/vic/lib/constants"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/extraconfig/vmomi"
"github.com/vmware/vic/pkg/vsphere/session"
)
// VirtualMachineConfigSpecConfig holds the config values
type VirtualMachineConfigSpecConfig struct {
// ID of the VM
ID string
BiosUUID string
VMFullName string
// ParentImageID of the VM
ParentImageID string
// Name of the VM
Name string
// Number of CPUs
NumCPUs int32
// Memory - in MB
MemoryMB int64
// VMFork enabled
VMForkEnabled bool
// datastore path of the media file we boot from
BootMediaPath string
// datastore path of the VM
VMPathName string
// Name of the image store
ImageStoreName string
// url path to image store
ImageStorePath *url.URL
// Temporary
Metadata *executor.ExecutorConfig
}
// VirtualMachineConfigSpec type
type VirtualMachineConfigSpec struct {
*session.Session
*types.VirtualMachineConfigSpec
config *VirtualMachineConfigSpecConfig
// internal value to keep track of next ID
key int32
}
// NewVirtualMachineConfigSpec returns a VirtualMachineConfigSpec
func NewVirtualMachineConfigSpec(ctx context.Context, session *session.Session, config *VirtualMachineConfigSpecConfig) (*VirtualMachineConfigSpec, error) {
defer trace.End(trace.Begin(config.ID))
s := &types.VirtualMachineConfigSpec{
Name: config.VMFullName,
Uuid: config.BiosUUID,
Files: &types.VirtualMachineFileInfo{
VmPathName: config.VMPathName,
},
NumCPUs: config.NumCPUs,
CpuHotAddEnabled: &config.VMForkEnabled, // this disables vNUMA when true
MemoryMB: config.MemoryMB,
MemoryHotAddEnabled: &config.VMForkEnabled,
ExtraConfig: []types.BaseOptionValue{
// lets us see the UUID for the containerfs disk (hidden from daemon)
&types.OptionValue{Key: "disk.EnableUUID", Value: "true"},
// needed to avoid the questions that occur when attaching multiple disks with the same uuid (bugzilla 1362918)
&types.OptionValue{Key: "answer.msg.disk.duplicateUUID", Value: "Yes"},
// needed to avoid the question that occur when opening a file backed serial port
&types.OptionValue{Key: "answer.msg.serial.file.open", Value: "Append"},
&types.OptionValue{Key: "sched.mem.lpage.maxSharedPages", Value: "256"},
// seems to be needed to avoid children hanging shortly after fork
&types.OptionValue{Key: "vmotion.checkpointSVGAPrimarySize", Value: "4194304"},
// trying this out - if it works then we need to determine if we can rely on serial0 being the correct index.
&types.OptionValue{Key: "serial0.hardwareFlowControl", Value: "TRUE"},
// https://enatai-jira.eng.vmware.com/browse/BON-257
// Hotadd memory above 3 GB not working
&types.OptionValue{Key: "memory.noHotAddOver4GB", Value: "FALSE"},
&types.OptionValue{Key: "memory.maxGrow", Value: "512"},
// http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2030189
&types.OptionValue{Key: "tools.remindInstall", Value: "FALSE"},
&types.OptionValue{Key: "tools.upgrade.policy", Value: "manual"},
},
}
// encode the config as optionvalues
cfg := map[string]string{}
extraconfig.Encode(extraconfig.MapSink(cfg), config.Metadata)
metaCfg := vmomi.OptionValueFromMap(cfg, true)
// merge it with the sec
s.ExtraConfig = append(s.ExtraConfig, metaCfg...)
vmcs := &VirtualMachineConfigSpec{
Session: session,
VirtualMachineConfigSpec: s,
config: config,
}
return vmcs, nil
}
// AddVirtualDevice appends an Add operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) AddVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
Device: device,
},
)
return s
}
// AddAndCreateVirtualDevice appends an Add operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) AddAndCreateVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
FileOperation: types.VirtualDeviceConfigSpecFileOperationCreate,
Device: device,
},
)
return s
}
// RemoveVirtualDevice appends a Remove operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) RemoveVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationRemove,
Device: device,
},
)
return s
}
// RemoveAndDestroyVirtualDevice appends a Remove operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) RemoveAndDestroyVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationRemove,
FileOperation: types.VirtualDeviceConfigSpecFileOperationDestroy,
Device: device,
},
)
return s
}
// Name returns the name of the VM
func (s *VirtualMachineConfigSpec) Name() string {
defer trace.End(trace.Begin(s.config.Name))
return s.config.Name
}
// ID returns the ID of the VM
func (s *VirtualMachineConfigSpec) ID() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ID
}
// ParentImageID returns the ID of the image that VM is based on
func (s *VirtualMachineConfigSpec) ParentImageID() string {
defer trace.End(trace.Begin(s.config.ParentImageID))
return s.config.ParentImageID
}
// BootMediaPath returns the image path
func (s *VirtualMachineConfigSpec) BootMediaPath() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.BootMediaPath
}
// VMPathName returns the VM folder path
func (s *VirtualMachineConfigSpec) VMPathName() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.VMPathName
}
// ImageStoreName returns the image store name
func (s *VirtualMachineConfigSpec) ImageStoreName() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ImageStoreName
}
// ImageStorePath returns the image store url
func (s *VirtualMachineConfigSpec) ImageStorePath() *url.URL {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ImageStorePath
}
func (s *VirtualMachineConfigSpec) generateNextKey() int32 {
s.key -= 10
return s.key
}
// Spec returns the base types.VirtualMachineConfigSpec object
func (s *VirtualMachineConfigSpec) Spec() *types.VirtualMachineConfigSpec {
return s.VirtualMachineConfigSpec
}
// VirtualDeviceSlotNumber returns the PCI slot number of a device
func VirtualDeviceSlotNumber(d types.BaseVirtualDevice) int32 {
s := d.GetVirtualDevice().SlotInfo
if s == nil {
return constants.NilSlot
}
if i, ok := s.(*types.VirtualDevicePciBusSlotInfo); ok {
return i.PciSlotNumber
}
return constants.NilSlot
}
func findSlotNumber(slots map[int32]bool) int32 {
// see https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2047927
slot := constants.PCISlotNumberBegin
for _, ok := slots[slot]; ok && slot != constants.PCISlotNumberEnd; {
slot += constants.PCISlotNumberInc
_, ok = slots[slot]
}
if slot == constants.PCISlotNumberEnd {
return constants.NilSlot
}
return slot
}
// AssignSlotNumber assigns a specific PCI slot number to the specified device. This ensures that
// the slot is valid and not in use by anything else in the spec
func (s *VirtualMachineConfigSpec) AssignSlotNumber(dev types.BaseVirtualDevice, known map[int32]bool) int32 {
slot := VirtualDeviceSlotNumber(dev)
if slot != constants.NilSlot {
return slot
}
// build the slots in use from the spec
slots := s.CollectSlotNumbers(known)
slot = findSlotNumber(slots)
if slot != constants.NilSlot {
dev.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{PciSlotNumber: slot}
}
return slot
}
// CollectSlotNumbers returns a collection of all the PCI slot numbers for devices in the spec
// Can take a nil map as argument
func (s *VirtualMachineConfigSpec) CollectSlotNumbers(known map[int32]bool) map[int32]bool {
if known == nil {
known = make(map[int32]bool)
}
// collect all the already assigned slot numbers
for _, c := range s.DeviceChange {
if s := VirtualDeviceSlotNumber(c.GetVirtualDeviceConfigSpec().Device); s != constants.NilSlot {
known[s] = true
}
}
return known
}

186
vendor/github.com/vmware/vic/lib/spec/spec_test.go generated vendored Normal file
View File

@@ -0,0 +1,186 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"fmt"
"testing"
"time"
"context"
"github.com/stretchr/testify/assert"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/constants"
"github.com/vmware/vic/pkg/vsphere/session"
"github.com/vmware/vic/pkg/vsphere/test/env"
)
func TestVirtualMachineConfigSpec(t *testing.T) {
ctx := context.Background()
sessionconfig := &session.Config{
Service: env.URL(t),
Insecure: true,
Keepalive: time.Duration(5) * time.Minute,
DatacenterPath: "",
DatastorePath: "/ha-datacenter/datastore/*",
HostPath: "/ha-datacenter/host/*/*",
PoolPath: "/ha-datacenter/host/*/Resources",
}
s, err := session.NewSession(sessionconfig).Create(ctx)
if err != nil {
t.Logf("%+v", err.Error())
if _, ok := err.(*find.MultipleFoundError); !ok {
t.Errorf(err.Error())
} else {
t.SkipNow()
}
}
defer s.Logout(ctx)
specconfig := &VirtualMachineConfigSpecConfig{
NumCPUs: 2,
MemoryMB: 2048,
VMForkEnabled: true,
ID: "zombie_attack",
BootMediaPath: s.Datastore.Path("brainz.iso"),
VMPathName: fmt.Sprintf("[%s]", s.Datastore.Name()),
}
// FIXME: find a better way to pass those
var scsibus int32
var scsikey int32 = 100
var idekey int32 = 200
root, _ := NewVirtualMachineConfigSpec(ctx, s, specconfig)
scsi := NewVirtualSCSIController(scsibus, scsikey)
pv := NewParaVirtualSCSIController(scsi)
root.AddParaVirtualSCSIController(pv)
bl := NewVirtualBusLogicController(scsi)
root.AddVirtualBusLogicController(bl)
ll := NewVirtualLsiLogicController(scsi)
root.AddVirtualLsiLogicController(ll)
ls := NewVirtualLsiLogicSASController(scsi)
root.AddVirtualLsiLogicSASController(ls)
///
ide := NewVirtualIDEController(idekey)
root.AddVirtualIDEController(ide)
cdrom := NewVirtualCdrom(ide)
root.AddVirtualCdrom(cdrom)
floppy := NewVirtualFloppy(ide)
root.AddVirtualFloppy(floppy)
vmxnet3 := NewVirtualVmxnet3()
root.AddVirtualVmxnet3(vmxnet3)
pcnet32 := NewVirtualPCNet32()
root.AddVirtualPCNet32(pcnet32)
e1000 := NewVirtualE1000()
root.AddVirtualE1000(e1000)
for i := 0; i < len(root.DeviceChange); i++ {
t.Logf("%+v", root.DeviceChange[i].GetVirtualDeviceConfigSpec().Device)
}
}
func TestCollectSlotNumbers(t *testing.T) {
s := &VirtualMachineConfigSpec{
config: &VirtualMachineConfigSpecConfig{
ID: "foo",
},
VirtualMachineConfigSpec: &types.VirtualMachineConfigSpec{},
}
slots := s.CollectSlotNumbers(nil)
assert.Empty(t, slots)
s.AddVirtualVmxnet3(NewVirtualVmxnet3())
s.DeviceChange[0].GetVirtualDeviceConfigSpec().Device.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{PciSlotNumber: 32}
slots = s.CollectSlotNumbers(nil)
assert.EqualValues(t, map[int32]bool{32: true}, slots)
// add a device without a slot number
s.AddVirtualVmxnet3(NewVirtualVmxnet3())
slots = s.CollectSlotNumbers(nil)
assert.EqualValues(t, map[int32]bool{32: true}, slots)
// add another device with slot number
s.AddVirtualVmxnet3(NewVirtualVmxnet3())
s.DeviceChange[len(s.DeviceChange)-1].GetVirtualDeviceConfigSpec().Device.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{PciSlotNumber: 33}
slots = s.CollectSlotNumbers(slots)
assert.EqualValues(t, map[int32]bool{32: true, 33: true}, slots)
}
func TestFindSlotNumber(t *testing.T) {
allSlots := make(map[int32]bool)
for s := constants.PCISlotNumberBegin; s != constants.PCISlotNumberEnd; s += constants.PCISlotNumberInc {
allSlots[s] = true
}
// missing first slot
missingFirstSlot := make(map[int32]bool)
for s := constants.PCISlotNumberBegin + constants.PCISlotNumberInc; s != constants.PCISlotNumberEnd; s += constants.PCISlotNumberInc {
missingFirstSlot[s] = true
}
// missing last slot
missingLastSlot := make(map[int32]bool)
for s := constants.PCISlotNumberBegin; s != constants.PCISlotNumberEnd-constants.PCISlotNumberInc; s += constants.PCISlotNumberInc {
missingLastSlot[s] = true
}
// missing a slot in the middle
var missingSlot int32
missingMiddleSlot := make(map[int32]bool)
for s := constants.PCISlotNumberBegin; s != constants.PCISlotNumberEnd-constants.PCISlotNumberInc; s += constants.PCISlotNumberInc {
if constants.PCISlotNumberBegin+(2*constants.PCISlotNumberInc) == s {
missingSlot = s
continue
}
missingMiddleSlot[s] = true
}
var tests = []struct {
slots map[int32]bool
out int32
}{
{make(map[int32]bool), constants.PCISlotNumberBegin},
{allSlots, constants.NilSlot},
{missingFirstSlot, constants.PCISlotNumberBegin},
{missingLastSlot, constants.PCISlotNumberEnd - constants.PCISlotNumberInc},
{missingMiddleSlot, missingSlot},
}
for _, te := range tests {
if s := findSlotNumber(te.slots); s != te.out {
t.Fatalf("findSlotNumber(%v) => %d, want %d", te.slots, s, te.out)
}
}
}