@@ -32,7 +32,6 @@ import (
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
33
"k8s.io/apimachinery/pkg/types"
34
34
"k8s.io/apimachinery/pkg/util/wait"
35
- "k8s.io/apimachinery/pkg/watch"
36
35
utilfeature "k8s.io/apiserver/pkg/util/feature"
37
36
"k8s.io/client-go/kubernetes"
38
37
"k8s.io/klog/v2"
@@ -54,8 +53,6 @@ type csiAttacher struct {
54
53
csiClient csiClient
55
54
}
56
55
57
- type verifyAttachDetachStatus func (attach * storage.VolumeAttachment , volumeHandle string ) (bool , error )
58
-
59
56
// volume.Attacher methods
60
57
var _ volume.Attacher = & csiAttacher {}
61
58
@@ -141,38 +138,34 @@ func (c *csiAttacher) Attach(spec *volume.Spec, nodeName types.NodeName) (string
141
138
return "" , nil
142
139
}
143
140
144
- func (c * csiAttacher ) WaitForAttach (spec * volume.Spec , _ string , pod * v1.Pod , timeout time.Duration ) (string , error ) {
141
+ // WaitForAttach waits for the attach operation to complete and returns the device path when it is done.
142
+ // But in this case, there should be no waiting. The device is found by the CSI driver later, in NodeStage / NodePublish calls.
143
+ // so it should just return device metadata, in this case it is VolumeAttachment name. If the target VolumeAttachment does not
144
+ // exist or is not attached, the function will return an error. And then the caller (kubelet) should retry it.
145
+ // We can get rid of watching it that serves no purpose. More details in https://issues.k8s.io/124398
146
+ func (c * csiAttacher ) WaitForAttach (spec * volume.Spec , _ string , pod * v1.Pod , _ time.Duration ) (string , error ) {
145
147
source , err := getPVSourceFromSpec (spec )
146
148
if err != nil {
147
149
return "" , errors .New (log ("attacher.WaitForAttach failed to extract CSI volume source: %v" , err ))
148
150
}
149
151
152
+ volumeHandle := source .VolumeHandle
150
153
attachID := getAttachmentName (source .VolumeHandle , source .Driver , string (c .plugin .host .GetNodeName ()))
151
154
152
- return c .waitForVolumeAttachment (source .VolumeHandle , attachID , timeout )
153
- }
154
-
155
- func (c * csiAttacher ) waitForVolumeAttachment (volumeHandle , attachID string , timeout time.Duration ) (string , error ) {
156
- klog .V (4 ).Info (log ("probing for updates from CSI driver for [attachment.ID=%v]" , attachID ))
157
-
158
- timer := time .NewTimer (timeout ) // TODO (vladimirvivien) investigate making this configurable
159
- defer timer .Stop ()
160
-
161
- return c .waitForVolumeAttachmentInternal (volumeHandle , attachID , timer , timeout )
162
- }
163
-
164
- func (c * csiAttacher ) waitForVolumeAttachmentInternal (volumeHandle , attachID string , timer * time.Timer , timeout time.Duration ) (string , error ) {
165
-
166
- klog .V (4 ).Info (log ("probing VolumeAttachment [id=%v]" , attachID ))
167
155
attach , err := c .k8s .StorageV1 ().VolumeAttachments ().Get (context .TODO (), attachID , metav1.GetOptions {})
168
156
if err != nil {
169
157
klog .Error (log ("attacher.WaitForAttach failed for volume [%s] (will continue to try): %v" , volumeHandle , err ))
170
158
return "" , fmt .Errorf ("volume %v has GET error for volume attachment %v: %v" , volumeHandle , attachID , err )
171
159
}
172
- err = c .waitForVolumeAttachDetachStatus (attach , volumeHandle , attachID , timer , timeout , verifyAttachmentStatus )
160
+
161
+ successful , err := verifyAttachmentStatus (attach , volumeHandle )
173
162
if err != nil {
174
163
return "" , err
175
164
}
165
+ if ! successful {
166
+ klog .Error (log ("attacher.WaitForAttach failed for volume [%s] attached (will continue to try)" , volumeHandle ))
167
+ return "" , fmt .Errorf ("volume %v is not attached for volume attachment %v" , volumeHandle , attachID )
168
+ }
176
169
return attach .Name , nil
177
170
}
178
171
@@ -532,62 +525,6 @@ func (c *csiAttacher) waitForVolumeAttachDetachStatusWithLister(spec *volume.Spe
532
525
}
533
526
}
534
527
535
- func (c * csiAttacher ) waitForVolumeAttachDetachStatus (attach * storage.VolumeAttachment , volumeHandle , attachID string ,
536
- timer * time.Timer , timeout time.Duration , verifyStatus verifyAttachDetachStatus ) error {
537
- successful , err := verifyStatus (attach , volumeHandle )
538
- if err != nil {
539
- return err
540
- }
541
- if successful {
542
- return nil
543
- }
544
-
545
- watcher , err := c .k8s .StorageV1 ().VolumeAttachments ().Watch (context .TODO (), metav1 .SingleObject (metav1.ObjectMeta {Name : attachID , ResourceVersion : attach .ResourceVersion }))
546
- if err != nil {
547
- return fmt .Errorf ("watch error:%v for volume %v" , err , volumeHandle )
548
- }
549
-
550
- ch := watcher .ResultChan ()
551
- defer watcher .Stop ()
552
- for {
553
- select {
554
- case event , ok := <- ch :
555
- if ! ok {
556
- klog .Errorf ("[attachment.ID=%v] watch channel had been closed" , attachID )
557
- return errors .New ("volume attachment watch channel had been closed" )
558
- }
559
-
560
- switch event .Type {
561
- case watch .Added , watch .Modified :
562
- attach , _ := event .Object .(* storage.VolumeAttachment )
563
- successful , err := verifyStatus (attach , volumeHandle )
564
- if err != nil {
565
- return err
566
- }
567
- if successful {
568
- return nil
569
- }
570
- case watch .Deleted :
571
- // set attach nil to get different results
572
- // for detachment, a deleted event means successful detachment, should return success
573
- // for attachment, should return fail
574
- if successful , err := verifyStatus (nil , volumeHandle ); ! successful {
575
- return err
576
- }
577
- klog .V (4 ).Info (log ("VolumeAttachment object [%v] for volume [%v] has been deleted" , attachID , volumeHandle ))
578
- return nil
579
-
580
- case watch .Error :
581
- klog .Warningf ("waitForVolumeAttachDetachInternal received watch error: %v" , event )
582
- }
583
-
584
- case <- timer .C :
585
- klog .Error (log ("attachdetacher.WaitForDetach timeout after %v [volume=%v; attachment.ID=%v]" , timeout , volumeHandle , attachID ))
586
- return fmt .Errorf ("attachdetachment timeout for volume %v" , volumeHandle )
587
- }
588
- }
589
- }
590
-
591
528
func (c * csiAttacher ) UnmountDevice (deviceMountPath string ) error {
592
529
klog .V (4 ).Info (log ("attacher.UnmountDevice(%s)" , deviceMountPath ))
593
530
0 commit comments