-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdelete.go
More file actions
288 lines (252 loc) · 8.25 KB
/
delete.go
File metadata and controls
288 lines (252 loc) · 8.25 KB
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
// © Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: Apache-2.0
package virtualmachine
import (
"context"
"fmt"
"path"
"github.com/go-logr/logr"
"github.com/vmware/govmomi/cns"
cnstypes "github.com/vmware/govmomi/cns/types"
"github.com/vmware/govmomi/fault"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25"
vimtypes "github.com/vmware/govmomi/vim25/types"
"github.com/vmware/govmomi/vslm"
pkgconst "github.com/vmware-tanzu/vm-operator/pkg/constants"
pkgctx "github.com/vmware-tanzu/vm-operator/pkg/context"
pkglog "github.com/vmware-tanzu/vm-operator/pkg/log"
vmutil "github.com/vmware-tanzu/vm-operator/pkg/util/vsphere/vm"
)
// VMDeletePropertiesSelector is the set of VM properties fetched at the start
// of provider DeleteVirtualMachine.
var VMDeletePropertiesSelector = []string{
"recentTask",
"config.extraConfig",
"config.files",
"config.hardware.device",
"summary.runtime.connectionState",
}
// CleanupVMDir removes the VM directory on the datastore and, when non-TLD,
// the namespace mapping.
func CleanupVMDir(
ctx context.Context,
vimClient *vim25.Client,
dc *object.Datacenter,
vmDirPath, namespacePath string) error {
if vimClient == nil {
return nil
}
ctx = context.WithoutCancel(ctx)
fm := object.NewFileManager(vimClient)
if vmDirPath != "" {
task, err := fm.DeleteDatastoreFile(ctx, vmDirPath, dc)
if err != nil {
return err
}
if err := task.Wait(ctx); err != nil &&
!fault.Is(err, &vimtypes.FileNotFound{}) {
return err
}
}
if dc != nil && namespacePath != "" {
nm := object.NewDatastoreNamespaceManager(vimClient)
if err := nm.DeleteDirectory(ctx, dc, namespacePath); err != nil &&
!fault.Is(err, &vimtypes.FileNotFound{}) {
return err
}
}
return nil
}
func DeleteVirtualMachine(
vmCtx pkgctx.VirtualMachineContext,
vcVM *object.VirtualMachine,
dc *object.Datacenter) error {
if _, err := vmutil.SetAndWaitOnPowerState(
logr.NewContext(vmCtx, vmCtx.Logger),
vcVM.Client(),
vmutil.ManagedObjectFromObject(vcVM),
false,
vimtypes.VirtualMachinePowerStatePoweredOff,
vmutil.ParsePowerOpMode(string(vmCtx.VM.Spec.PowerOffMode))); err != nil {
return err
}
t, err := vcVM.Destroy(vmCtx)
if err != nil {
return err
}
if taskInfo, err := t.WaitForResult(vmCtx); err != nil {
if taskInfo != nil {
vmCtx.Logger.V(5).Error(err, "destroy VM task failed", "taskInfo", taskInfo)
}
return fmt.Errorf("destroy VM task failed: %w", err)
}
// After Destroy(), delete any FCD-backed disks left in the VM's directory
// (left behind because keepAfterDeleteVm=true) via the CNS DeleteVolume
// API. This MUST run after Destroy() so the disk is no longer attached
// and CNS permits the delete. Using the CNS API (not raw VSLM) keeps
// CNS's internal state consistent so the subsequent PVC cleanup path
// handles any duplicate delete gracefully with no VC UI error.
// See deleteFCDsInVMDir for full context.
deleteFCDsInVMDir(
vmCtx,
vcVM.Client(),
vmCtx.MoVM.Config)
// Best-effort cleanup of VM dir after Destroy();
// no error return since delete is still considered successful.
if dc == nil ||
vmCtx.MoVM.Config == nil ||
vmCtx.MoVM.Config.Files.VmPathName == "" {
return nil
}
namespacePath, _ := object.OptionValueList(
vmCtx.MoVM.Config.ExtraConfig).GetString(pkgconst.ExtraConfigVMDirNamespacePath)
_ = CleanupVMDir(vmCtx.Context,
vcVM.Client(),
dc,
path.Dir(vmCtx.MoVM.Config.Files.VmPathName),
namespacePath)
return nil
}
// deleteFCDsInVMDir finds all FCD-backed disks in the VM's own directory and
// deletes them via the CNS DeleteVolume API (with deleteDisk=true) before the
// VM is destroyed.
//
// The unmanaged volumes registration process (unmanagedvolumes_register)
// registers a VM's ephemeral disks as CNS FCDs via CnsRegisterVolume, which
// sets keepAfterDeleteVm=true. During VM deletion, vcVM.Destroy() skips such
// disks because of this flag. CleanupVMDir() then removes the directory via
// the raw FileManager API without updating the FCD catalog, leaving a stale
// catalog entry pointing to a now-deleted file.
//
// The CNS DeleteVolume API (rather than the lower-level VSLM
// DeleteVStorageObject) is used because CNS tracks the deletion in its own
// internal state. When CNS's PVC cleanup path subsequently also tries to
// delete the same volume, CNS recognises it is already gone and handles the
// duplicate call gracefully — avoiding "not found" errors visible in the
// vCenter task UI.
//
// Only disks whose backing file is in the VM's own directory are affected;
// user-attached PVC disks in separate directories are not touched.
//
// Note: after CSI registers a VMDK as an FCD, the VM hardware config's
// backingObjectId field is not updated. The VSLM catalog is queried directly
// to find FCDs whose backing file path matches the VM's disk paths.
func deleteFCDsInVMDir(
ctx context.Context,
vimClient *vim25.Client,
config *vimtypes.VirtualMachineConfigInfo) {
if config == nil {
return
}
if vimClient.ServiceContent.VStorageObjectManager == nil {
return
}
vmDir := path.Dir(config.Files.VmPathName)
logger := pkglog.FromContextOrDefault(ctx)
logger.Info(
"Scanning VM disks for FCD cleanup",
"vmDir", vmDir,
"numDevices", len(config.Hardware.Device))
// Collect the file paths of all disks in the VM's own directory, and
// capture one datastore MoRef to use for VSLM queries.
diskFilesInVMDir := map[string]struct{}{}
var datastoreMoRef *vimtypes.ManagedObjectReference
for _, dev := range config.Hardware.Device {
disk, ok := dev.(*vimtypes.VirtualDisk)
if !ok {
continue
}
bi := getVirtualDiskFileBackingInfo(disk)
if bi == nil || bi.FileName == "" || bi.Datastore == nil {
continue
}
logger.Info(
"Found disk in VM",
"fileName", bi.FileName,
"inVMDir", path.Dir(bi.FileName) == vmDir)
if path.Dir(bi.FileName) != vmDir {
continue
}
diskFilesInVMDir[bi.FileName] = struct{}{}
datastoreMoRef = bi.Datastore
}
if len(diskFilesInVMDir) == 0 || datastoreMoRef == nil {
logger.Info(
"No disks found in VM directory, skipping FCD cleanup",
"vmDir", vmDir)
return
}
// List all FCDs on the datastore and find those whose backing file
// matches a disk in the VM's directory.
mgr := vslm.NewObjectManager(vimClient)
ds := object.NewDatastore(vimClient, *datastoreMoRef)
ids, err := mgr.List(ctx, ds)
if err != nil {
logger.Error(err,
"Failed to list FCDs on datastore for cleanup")
return
}
// Build CNS client once; used to delete matched FCDs via CNS DeleteVolume
// so that CNS internal state is updated and its PVC cleanup path handles
// the subsequent duplicate delete gracefully (no VC UI errors).
cnsClient, err := cns.NewClient(ctx, vimClient)
if err != nil {
logger.Error(err, "Failed to create CNS client for FCD cleanup")
return
}
for _, id := range ids {
obj, err := mgr.Retrieve(ctx, ds, id.Id)
if err != nil {
logger.Info(
"Failed to retrieve FCD, skipping",
"fcdID", id.Id,
"err", err)
continue
}
// Extract the backing file path.
fb, ok := obj.Config.Backing.(
vimtypes.BaseBaseConfigInfoFileBackingInfo)
if !ok {
continue
}
filePath := fb.GetBaseConfigInfoFileBackingInfo().FilePath
if _, found := diskFilesInVMDir[filePath]; !found {
continue
}
logger.Info(
"Deleting FCD disk in VM directory via CNS",
"fcdID", id.Id,
"diskFile", filePath)
task, err := cnsClient.DeleteVolume(
ctx,
[]cnstypes.CnsVolumeId{{Id: id.Id}},
true)
if err != nil {
logger.Error(err,
"Failed to initiate CNS volume delete",
"fcdID", id.Id,
"diskFile", filePath)
continue
}
if _, err := task.WaitForResult(ctx); err != nil {
logger.Error(err,
"CNS volume delete task failed",
"fcdID", id.Id,
"diskFile", filePath)
}
}
}
// getVirtualDiskFileBackingInfo returns the VirtualDeviceFileBackingInfo for
// a virtual disk if the disk uses file-based backing, otherwise nil.
func getVirtualDiskFileBackingInfo(
disk *vimtypes.VirtualDisk) *vimtypes.VirtualDeviceFileBackingInfo {
type hasFileBackingInfo interface {
GetVirtualDeviceFileBackingInfo() *vimtypes.VirtualDeviceFileBackingInfo
}
if fb, ok := disk.Backing.(hasFileBackingInfo); ok {
return fb.GetVirtualDeviceFileBackingInfo()
}
return nil
}