Skip to content

Commit 7dd17ca

Browse files
committed
add API for storage policy support
Signed-off-by: Yassine TIJANI <[email protected]>
1 parent c18ca88 commit 7dd17ca

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

api/v1alpha3/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ type VirtualMachineCloneSpec struct {
100100
// +optional
101101
Datastore string `json:"datastore,omitempty"`
102102

103+
// StoragePolicyName of the storage policy to use with this
104+
// Virtual Machine
105+
// +optional
106+
StoragePolicyName string `json:"storagePolicyName,omitempty"`
107+
103108
// ResourcePool is the name or inventory path of the resource pool in which
104109
// the virtual machine is created/located.
105110
// +optional

config/crd/bases/infrastructure.cluster.x-k8s.io_haproxyloadbalancers.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ spec:
250250
create a linked clone. This field is ignored if LinkedClone
251251
is not enabled. Defaults to the source's current snapshot.
252252
type: string
253+
storagePolicyName:
254+
description: StoragePolicyName of the storage policy to use with
255+
this Virtual Machine
256+
type: string
253257
template:
254258
description: Template is the name or inventory path of the template
255259
used to clone the virtual machine.

config/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ spec:
514514
a linked clone. This field is ignored if LinkedClone is not enabled.
515515
Defaults to the source's current snapshot.
516516
type: string
517+
storagePolicyName:
518+
description: StoragePolicyName of the storage policy to use with this
519+
Virtual Machine
520+
type: string
517521
template:
518522
description: Template is the name or inventory path of the template
519523
used to clone the virtual machine.

config/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ spec:
574574
to create a linked clone. This field is ignored if LinkedClone
575575
is not enabled. Defaults to the source's current snapshot.
576576
type: string
577+
storagePolicyName:
578+
description: StoragePolicyName of the storage policy to use
579+
with this Virtual Machine
580+
type: string
577581
template:
578582
description: Template is the name or inventory path of the
579583
template used to clone the virtual machine.

config/crd/bases/infrastructure.cluster.x-k8s.io_vspherevms.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ spec:
272272
a linked clone. This field is ignored if LinkedClone is not enabled.
273273
Defaults to the source's current snapshot.
274274
type: string
275+
storagePolicyName:
276+
description: StoragePolicyName of the storage policy to use with this
277+
Virtual Machine
278+
type: string
275279
template:
276280
description: Template is the name or inventory path of the template
277281
used to clone the virtual machine.

pkg/services/govmomi/vcenter/clone.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package vcenter
1919
import (
2020
"github.com/pkg/errors"
2121
"github.com/vmware/govmomi/object"
22+
"github.com/vmware/govmomi/pbm"
2223
"github.com/vmware/govmomi/vim25/mo"
2324
"github.com/vmware/govmomi/vim25/types"
2425

@@ -107,11 +108,6 @@ func Clone(ctx *context.VMContext, bootstrapData []byte) error {
107108
return errors.Wrapf(err, "unable to get folder for %q", ctx)
108109
}
109110

110-
datastore, err := ctx.Session.Finder.DatastoreOrDefault(ctx, ctx.VSphereVM.Spec.Datastore)
111-
if err != nil {
112-
return errors.Wrapf(err, "unable to get datastore for %q", ctx)
113-
}
114-
115111
pool, err := ctx.Session.Finder.ResourcePoolOrDefault(ctx, ctx.VSphereVM.Spec.ResourcePool)
116112
if err != nil {
117113
return errors.Wrapf(err, "unable to get resource pool for %q", ctx)
@@ -167,7 +163,7 @@ func Clone(ctx *context.VMContext, bootstrapData []byte) error {
167163
MemoryMB: memMiB,
168164
},
169165
Location: types.VirtualMachineRelocateSpec{
170-
Datastore: types.NewReference(datastore.Reference()),
166+
// Datastore: types.NewReference(datastore.Reference()),
171167
DiskMoveType: string(diskMoveType),
172168
Folder: types.NewReference(folder.Reference()),
173169
Pool: types.NewReference(pool.Reference()),
@@ -180,12 +176,32 @@ func Clone(ctx *context.VMContext, bootstrapData []byte) error {
180176
Snapshot: snapshotRef,
181177
}
182178

179+
if ctx.VSphereVM.Spec.StoragePolicyName != "" {
180+
pbmClient, err := pbm.NewClient(ctx, ctx.Session.Client.Client)
181+
if err != nil {
182+
return errors.Wrapf(err, "unable to create pbm client for %q", ctx)
183+
}
184+
185+
storageProfileID, err := pbmClient.ProfileIDByName(ctx, ctx.VSphereVM.Spec.StoragePolicyName)
186+
if err != nil {
187+
return errors.Wrapf(err, "unable to get storageProfileID from name %s for %q", ctx.VSphereVM.Spec.StoragePolicyName, ctx)
188+
}
189+
spec.Location.Profile = []types.BaseVirtualMachineProfileSpec{
190+
&types.VirtualMachineDefinedProfileSpec{ProfileId: storageProfileID},
191+
}
192+
} else {
193+
datastore, err := ctx.Session.Finder.DatastoreOrDefault(ctx, ctx.VSphereVM.Spec.Datastore)
194+
if err != nil {
195+
return errors.Wrapf(err, "unable to get datastore for %q", ctx)
196+
}
197+
spec.Location.Datastore = types.NewReference(datastore.Reference())
198+
}
199+
183200
ctx.Logger.Info("cloning machine", "namespace", ctx.VSphereVM.Namespace, "name", ctx.VSphereVM.Name, "cloneType", ctx.VSphereVM.Status.CloneMode)
184201
task, err := tpl.Clone(ctx, folder, ctx.VSphereVM.Name, spec)
185202
if err != nil {
186203
return errors.Wrapf(err, "error trigging clone op for machine %s", ctx)
187204
}
188-
189205
ctx.VSphereVM.Status.TaskRef = task.Reference().Value
190206

191207
// patch the vsphereVM early to ensure that the task is

0 commit comments

Comments
 (0)