Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add a timeout to attach and detach calls #52

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/csi-attacher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
dummy = flag.Bool("dummy", false, "Run in dummy mode, i.e. not connecting to CSI driver and marking everything as attached. Expected CSI driver name is \"csi/dummy\".")
showVersion = flag.Bool("version", false, "Show version.")
timeout = flag.Duration("timeout", 15*time.Second, "Timeout for waiting for attaching or detaching the volume.")

enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.")
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace where this attacher runs.")
Expand Down Expand Up @@ -138,7 +139,7 @@ func main() {
pvLister := factory.Core().V1().PersistentVolumes().Lister()
nodeLister := factory.Core().V1().Nodes().Lister()
vaLister := factory.Storage().V1beta1().VolumeAttachments().Lister()
handler = controller.NewCSIHandler(clientset, attacher, csiConn, pvLister, nodeLister, vaLister)
handler = controller.NewCSIHandler(clientset, attacher, csiConn, pvLister, nodeLister, vaLister, timeout)
glog.V(2).Infof("CSI driver supports ControllerPublishUnpublish, using real CSI handler")
} else {
handler = controller.NewTrivialHandler(clientset)
Expand Down
13 changes: 10 additions & 3 deletions pkg/controller/csi_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"fmt"
"time"

"github.com/golang/glog"

Expand All @@ -45,17 +46,20 @@ type csiHandler struct {
nodeLister corelisters.NodeLister
vaLister storagelisters.VolumeAttachmentLister
vaQueue, pvQueue workqueue.RateLimitingInterface
timeout time.Duration
}

var _ Handler = &csiHandler{}

// NewCSIHandler creates a new CSIHandler.
func NewCSIHandler(
client kubernetes.Interface,
attacherName string,
csiConnection connection.CSIConnection,
pvLister corelisters.PersistentVolumeLister,
nodeLister corelisters.NodeLister,
vaLister storagelisters.VolumeAttachmentLister) Handler {
vaLister storagelisters.VolumeAttachmentLister,
timeout *time.Duration) Handler {

return &csiHandler{
client: client,
Expand All @@ -64,6 +68,7 @@ func NewCSIHandler(
pvLister: pvLister,
nodeLister: nodeLister,
vaLister: vaLister,
timeout: *timeout,
}
}

Expand Down Expand Up @@ -258,7 +263,8 @@ func (h *csiHandler) csiAttach(va *storage.VolumeAttachment) (*storage.VolumeAtt
return va, nil, fmt.Errorf("could not add VolumeAttachment finalizer: %s", err)
}

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
defer cancel()
// We're not interested in `detached` return value, the controller will
// issue Detach to be sure the volume is really detached.
publishInfo, _, err := h.csiConnection.Attach(ctx, volumeHandle, readOnly, nodeID, volumeCapabilities, attributes, secrets)
Expand Down Expand Up @@ -296,7 +302,8 @@ func (h *csiHandler) csiDetach(va *storage.VolumeAttachment) (*storage.VolumeAtt
return va, err
}

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
defer cancel()
detached, err := h.csiConnection.Detach(ctx, volumeHandle, nodeID, secrets)
if err != nil && !detached {
// The volume may not be fully detached. Save the error and try again
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/csi_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/kubernetes-csi/external-attacher/pkg/connection"

Expand All @@ -39,14 +40,18 @@ const (
fin = "external-attacher/csi-test"
)

var timeout = 1 * time.Minute

func csiHandlerFactory(client kubernetes.Interface, informerFactory informers.SharedInformerFactory, csi connection.CSIConnection) Handler {
return NewCSIHandler(
client,
testAttacherName,
csi,
informerFactory.Core().V1().PersistentVolumes().Lister(),
informerFactory.Core().V1().Nodes().Lister(),
informerFactory.Storage().V1beta1().VolumeAttachments().Lister())
informerFactory.Storage().V1beta1().VolumeAttachments().Lister(),
&timeout,
)
}

func pv() *v1.PersistentVolume {
Expand Down