Skip to content

Commit 4e7e3a1

Browse files
authored
Add vSphere ResourcePool support (#726)
* Add vSphere resource pool support Signed-off-by: Maximilian Lampert <[email protected]> * Add resourcePool to docs Signed-off-by: Maximilian Lampert <[email protected]> * Remove VM template support Signed-off-by: Maximilian Lampert <[email protected]> * fix lint test Signed-off-by: Maximilian Lampert <[email protected]>
1 parent b71bc4f commit 4e7e3a1

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

docs/vsphere.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ templateVMName: ubuntu-template
235235
vmNetName: network1
236236
# Optional
237237
folder: folder1
238+
# Optional: Force VMs to be provisoned to the specified resourcePool
239+
# Default is to use the resourcePool of the template VM
240+
# example: kubeone or /DC/host/Cluster01/Resources/kubeone
241+
resourcePool: kubeone
238242
cluster: cluster1
239243
# either datastore or datastoreCluster have to be provided.
240244
datastore: datastore1
@@ -251,7 +255,7 @@ diskSizeGB: 10
251255

252256
### Datastore and DatastoreCluster
253257

254-
A `Datastore` is the basic unit of storage abstraction in VSphere storage (more details [here][datastore]).
258+
A `Datastore` is the basic unit of storage abstraction in vSphere storage (more details [here][datastore]).
255259

256260
A `DatastoreCluster` (sometimes referred to as StoragePod) is a logical grouping of `Datastores`, it provides some resource management capabilities (more details [here][datastore_cluster]).
257261

pkg/cloudprovider/provider/vsphere/helper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ func createClonedVM(ctx context.Context, vmName string, config *Config, session
8484
return nil, fmt.Errorf("failed to resolve datastore: %v", err)
8585
}
8686

87+
resourcepoolref, err := resolveResourcePoolRef(ctx, config, session, tpl)
88+
if err != nil {
89+
return nil, fmt.Errorf("failed to resolve resourcePool: %v", err)
90+
}
91+
8792
cloneSpec.Location.Datastore = datastoreref
93+
cloneSpec.Location.Pool = resourcepoolref
8894
// Create a cloned VM from the template VM's snapshot.
8995
// We split the cloning from the reconfiguring as those actions differ on the permission side.
9096
// It's nicer to tell which specific action failed due to lacking permissions.
@@ -427,3 +433,14 @@ func getDatastoreFromVM(ctx context.Context, session *Session, vmRef *object.Vir
427433
}
428434
return session.Finder.Datastore(ctx, datastorePathObj.Datastore)
429435
}
436+
437+
func resolveResourcePoolRef(ctx context.Context, config *Config, session *Session, vm *object.VirtualMachine) (*types.ManagedObjectReference, error) {
438+
if config.ResourcePool != "" {
439+
targetResourcePool, err := session.Finder.ResourcePool(ctx, config.ResourcePool)
440+
if err != nil {
441+
return nil, fmt.Errorf("failed to get target resourcepool: %v", err)
442+
}
443+
return types.NewReference(targetResourcePool.Reference()), nil
444+
}
445+
return nil, nil
446+
}

pkg/cloudprovider/provider/vsphere/provider.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Config struct {
6565
Datacenter string
6666
Cluster string
6767
Folder string
68+
ResourcePool string
6869
Datastore string
6970
DatastoreCluster string
7071
AllowInsecure bool
@@ -164,6 +165,11 @@ func (p *provider) getConfig(s v1alpha1.ProviderSpec) (*Config, *providerconfigt
164165
return nil, nil, nil, err
165166
}
166167

168+
c.ResourcePool, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.ResourcePool)
169+
if err != nil {
170+
return nil, nil, nil, err
171+
}
172+
167173
c.Datastore, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.Datastore)
168174
if err != nil {
169175
return nil, nil, nil, err
@@ -226,6 +232,12 @@ func (p *provider) Validate(spec v1alpha1.MachineSpec) error {
226232
return fmt.Errorf("a vm %s/%s already exists", config.Folder, spec.Name)
227233
}
228234

235+
if config.ResourcePool != "" {
236+
if _, err := session.Finder.ResourcePool(ctx, config.ResourcePool); err != nil {
237+
return fmt.Errorf("failed to get resourcepool %q: %v", config.ResourcePool, err)
238+
}
239+
}
240+
229241
templateVM, err := session.Finder.VirtualMachine(ctx, config.TemplateVMName)
230242
if err != nil {
231243
return fmt.Errorf("failed to get template vm %q: %v", config.TemplateVMName, err)

pkg/cloudprovider/provider/vsphere/provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (m machineSpecArgs) generateMachineSpec(user, password, url string) v1alpha
5656
"datacenter": "DC0",
5757
%s
5858
"folder": "/",
59+
"resourcePool": "/DC0/host/DC0_C0/Resources",
5960
"memoryMB": 2000,
6061
"password": "%s",
6162
"templateVMName": "DC0_H0_VM0",

pkg/cloudprovider/provider/vsphere/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type RawConfig struct {
3030
Datacenter providerconfigtypes.ConfigVarString `json:"datacenter"`
3131
Cluster providerconfigtypes.ConfigVarString `json:"cluster"`
3232
Folder providerconfigtypes.ConfigVarString `json:"folder"`
33+
ResourcePool providerconfigtypes.ConfigVarString `json:"resourcePool"`
3334

3435
// Either Datastore or DatastoreCluster have to be provided.
3536
DatastoreCluster providerconfigtypes.ConfigVarString `json:"datastoreCluster"`

0 commit comments

Comments
 (0)