@@ -43,12 +43,12 @@ var (
43
43
// CnsVolumeAttachment exposes an interface to support adding
44
44
// and removing information about attached VMs to a PVC.
45
45
type CnsVolumeAttachment interface {
46
- // Add adds the input VM UUID to the list of
46
+ // Add adds the input VM instance UUID to the list of
47
47
// attached VMs for the given volume.
48
- AddVmToAttachedList (ctx context.Context , volumeName , VmUUID string ) error
48
+ AddVmToAttachedList (ctx context.Context , volumeName , VmInstanceUUID string ) error
49
49
// RemoveVmFromAttachedList removes the input VM UUID from
50
50
// the list of attached VMs for the given volume.
51
- RemoveVmFromAttachedList (ctx context.Context , volumeName , VmUUID string ) error
51
+ RemoveVmFromAttachedList (ctx context.Context , volumeName , VmInstanceUUID string ) ( error , bool )
52
52
// CnsVolumeAttachmentExistsForPvc returns true if CnsVolumeAttachment for a PVC is found.
53
53
CnsVolumeAttachmentExistsForPvc (ctx context.Context , volumeName string ) (bool , error )
54
54
}
@@ -98,19 +98,19 @@ func GetCnsVolumeAttachmentInstance(ctx context.Context) (CnsVolumeAttachment, e
98
98
return cnsVolumeAttachmentInstance , nil
99
99
}
100
100
101
- // Add adds the input VM UUID to the list of
101
+ // Add adds the input VM InstanceUUID to the list of
102
102
// attached VMs for the given volume.
103
103
// Callers need to specify cnsVolumeAttachment as a combination of
104
104
// "<SV-namespace>/<SV-PVC-name>". This combination is used to uniquely
105
105
// identify CnsVolumeAttachment instances.
106
106
// The instance is created if it doesn't exist.
107
107
// Returns an error if the operation cannot be persisted on the API server.
108
108
func (f * cnsVolumeAttachment ) AddVmToAttachedList (ctx context.Context ,
109
- volumeName , VmUUID string ) error {
109
+ volumeName , VmInstanceUUID string ) error {
110
110
log := logger .GetLogger (ctx )
111
111
112
112
log .Infof ("Adding VM %s to cnsVolumeAttachment %s" ,
113
- VmUUID , volumeName )
113
+ VmInstanceUUID , volumeName )
114
114
actual , _ := f .volumeLock .LoadOrStore (volumeName , & sync.Mutex {})
115
115
instanceLock , ok := actual .(* sync.Mutex )
116
116
if ! ok {
@@ -146,7 +146,7 @@ func (f *cnsVolumeAttachment) AddVmToAttachedList(ctx context.Context,
146
146
},
147
147
Spec : v1alpha1.CnsVolumeAttachmentSpec {
148
148
AttachedVms : []string {
149
- VmUUID ,
149
+ VmInstanceUUID ,
150
150
},
151
151
},
152
152
}
@@ -162,17 +162,17 @@ func (f *cnsVolumeAttachment) AddVmToAttachedList(ctx context.Context,
162
162
return err
163
163
}
164
164
165
- // Verify if input VmUUID exists in existing AttachedVMs list.
165
+ // Verify if input VmInstanceUUID exists in existing AttachedVMs list.
166
166
log .Debugf ("Verifying if VM %s exists in current list of attached Vms. Current list: %+v" ,
167
- VmUUID , instance .Spec .AttachedVms )
167
+ VmInstanceUUID , instance .Spec .AttachedVms )
168
168
currentAttachedVmsList := instance .Spec .AttachedVms
169
169
for _ , currentAttachedVM := range currentAttachedVmsList {
170
- if currentAttachedVM == VmUUID {
171
- log .Debugf ("Found VM %s in list. Returning." , VmUUID )
170
+ if currentAttachedVM == VmInstanceUUID {
171
+ log .Debugf ("Found VM %s in list. Returning." , VmInstanceUUID )
172
172
return nil
173
173
}
174
174
}
175
- newAttachVmsList := append (currentAttachedVmsList , VmUUID )
175
+ newAttachVmsList := append (currentAttachedVmsList , VmInstanceUUID )
176
176
instance .Spec .AttachedVms = newAttachVmsList
177
177
log .Debugf ("Updating cnsVolumeAttachment instance %s with spec: %+v" , volumeName , instance )
178
178
err = f .client .Update (ctx , instance )
@@ -191,14 +191,15 @@ func (f *cnsVolumeAttachment) AddVmToAttachedList(ctx context.Context,
191
191
// deleted from the API server.
192
192
// Returns an error if the operation cannot be persisted on the API server.
193
193
func (f * cnsVolumeAttachment ) RemoveVmFromAttachedList (ctx context.Context ,
194
- volumeName , VmUUID string ) error {
194
+ volumeName , VmInstanceUUID string ) ( error , bool ) {
195
195
log := logger .GetLogger (ctx )
196
- log .Infof ("Removing VmUUID %s from cnsVolumeAttachment %s" ,
197
- VmUUID , volumeName )
196
+ log .Infof ("Removing VmInstanceUUID %s from cnsVolumeAttachment %s" ,
197
+ VmInstanceUUID , volumeName )
198
198
actual , _ := f .volumeLock .LoadOrStore (volumeName , & sync.Mutex {})
199
199
instanceLock , ok := actual .(* sync.Mutex )
200
200
if ! ok {
201
- return fmt .Errorf ("failed to cast lock for cnsVolumeAttachment instance: %s" , volumeName )
201
+ return fmt .Errorf ("failed to cast lock for cnsVolumeAttachment instance: %s" , volumeName ),
202
+ false
202
203
}
203
204
instanceLock .Lock ()
204
205
log .Infof ("Acquired lock for cnsVolumeAttachment instance %s" , volumeName )
@@ -211,7 +212,7 @@ func (f *cnsVolumeAttachment) RemoveVmFromAttachedList(ctx context.Context,
211
212
instanceNamespace , instanceName , err := cache .SplitMetaNamespaceKey (volumeName )
212
213
if err != nil {
213
214
log .Errorf ("failed to split key %s with error: %+v" , volumeName , err )
214
- return err
215
+ return err , false
215
216
}
216
217
instanceKey := types.NamespacedName {
217
218
Namespace : instanceNamespace ,
@@ -221,55 +222,57 @@ func (f *cnsVolumeAttachment) RemoveVmFromAttachedList(ctx context.Context,
221
222
if err != nil {
222
223
if errors .IsNotFound (err ) {
223
224
log .Infof ("cnsVolumeAttachment instance %s does not exist on API server" , volumeName )
224
- return nil
225
+ return nil , true
225
226
}
226
227
log .Errorf ("failed to get cnsVolumeAttachment instance %s with error: %+v" , volumeName , err )
227
- return err
228
+ return err , false
228
229
}
229
230
230
231
log .Debugf ("Verifying if VM UUID %s exists in list of already attached VMs. Current list: %+v" ,
231
232
volumeName , instance .Spec .AttachedVms )
232
233
for index , existingAttachedVM := range instance .Spec .AttachedVms {
233
- if VmUUID == existingAttachedVM {
234
- log .Infof ("Removing VmUUID %s from Attached VMs list" , VmUUID )
235
- instance .Spec .AttachedVms = append (
236
- instance .Spec .AttachedVms [:index ],
237
- instance .Spec .AttachedVms [index + 1 :]... )
238
- if len (instance .Spec .AttachedVms ) == 0 {
239
- log .Infof ("Deleting cnsVolumeAttachment instance %s from API server" , volumeName )
240
- // Remove finalizer from CnsVolumeAttachment instance
241
- err = removeFinalizer (ctx , f .client , instance )
242
- if err != nil {
243
- log .Errorf ("failed to remove finalizer from cnsVolumeAttachment instance %s with error: %+v" ,
244
- volumeName , err )
245
- }
246
- err = f .client .Delete (ctx , instance )
247
- if err != nil {
248
- // In case of namespace deletion, we will have deletion timestamp added on the
249
- // CnsVolumeAttachment instance. So, as soon as we delete finalizer, instance might
250
- // get deleted immediately. In such cases we will get NotFound error here, return success
251
- // if instance is already deleted.
252
- if errors .IsNotFound (err ) {
253
- log .Infof ("cnsVolumeAttachment instance %s seems to be already deleted." , volumeName )
254
- f .volumeLock .Delete (volumeName )
255
- return nil
256
- }
257
- log .Errorf ("failed to delete cnsVolumeAttachment instance %s with error: %+v" , volumeName , err )
258
- return err
259
- }
260
- f .volumeLock .Delete (volumeName )
261
- return nil
234
+ if VmInstanceUUID != existingAttachedVM {
235
+ continue
236
+ }
237
+ log .Infof ("Removing VmUUID %s from Attached VMs list" , VmInstanceUUID )
238
+ instance .Spec .AttachedVms = append (
239
+ instance .Spec .AttachedVms [:index ],
240
+ instance .Spec .AttachedVms [index + 1 :]... )
241
+ if len (instance .Spec .AttachedVms ) == 0 {
242
+ log .Infof ("Deleting cnsVolumeAttachment instance %s from API server" , volumeName )
243
+ // Remove finalizer from CnsVolumeAttachment instance
244
+ err = removeFinalizer (ctx , f .client , instance )
245
+ if err != nil {
246
+ log .Errorf ("failed to remove finalizer from cnsVolumeAttachment instance %s with error: %+v" ,
247
+ volumeName , err )
248
+ return err , false
262
249
}
263
- log .Debugf ("Updating cnsVolumeAttachment instance %s with spec: %+v" , volumeName , instance )
264
- err = f .client .Update (ctx , instance )
250
+ err = f .client .Delete (ctx , instance )
265
251
if err != nil {
266
- log .Errorf ("failed to update cnsVolumeAttachment instance %s with error: %+v" , volumeName , err )
252
+ // In case of namespace deletion, we will have deletion timestamp added on the
253
+ // CnsVolumeAttachment instance. So, as soon as we delete finalizer, instance might
254
+ // get deleted immediately. In such cases we will get NotFound error here, return success
255
+ // if instance is already deleted.
256
+ if errors .IsNotFound (err ) {
257
+ log .Infof ("cnsVolumeAttachment instance %s seems to be already deleted." , volumeName )
258
+ f .volumeLock .Delete (volumeName )
259
+ return nil , true
260
+ }
261
+ log .Errorf ("failed to delete cnsVolumeAttachment instance %s with error: %+v" , volumeName , err )
262
+ return err , false
267
263
}
268
- return err
264
+ f .volumeLock .Delete (volumeName )
265
+ return nil , true
266
+ }
267
+ log .Debugf ("Updating cnsVolumeAttachment instance %s with spec: %+v" , volumeName , instance )
268
+ err = f .client .Update (ctx , instance )
269
+ if err != nil {
270
+ log .Errorf ("failed to update cnsVolumeAttachment instance %s with error: %+v" , volumeName , err )
269
271
}
272
+ return err , false
270
273
}
271
- log .Infof ("Could not find VM %s in list. Returning." , VmUUID )
272
- return nil
274
+ log .Infof ("Could not find VM %s in list. Returning." , VmInstanceUUID )
275
+ return nil , false
273
276
}
274
277
275
278
// CnsVolumeAttachmentExistsForPvc returns true if CnsVolumeAttachment instance
0 commit comments