From 010fd816ddca781503d537236a861544ebfb1af1 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 18 Mar 2022 10:49:03 +0100 Subject: [PATCH] nfs: store the calling Context in NFSVolume NFSVolume instances are short lived, they only extist for a certain gRPC procedure. It is easier to store the calling Context in the NFSVolume struct, than to pass it to some of the functions that require it. Signed-off-by: Niels de Vos --- internal/nfs/controller/controllerserver.go | 4 ++-- internal/nfs/controller/volume.go | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/nfs/controller/controllerserver.go b/internal/nfs/controller/controllerserver.go index ed31dde7c..65b2c3b02 100644 --- a/internal/nfs/controller/controllerserver.go +++ b/internal/nfs/controller/controllerserver.go @@ -91,7 +91,7 @@ func (cs *Server) CreateVolume( } defer cr.DeleteCredentials() - nfsVolume, err := NewNFSVolume(backend.VolumeId) + nfsVolume, err := NewNFSVolume(ctx, backend.VolumeId) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } @@ -128,7 +128,7 @@ func (cs *Server) DeleteVolume( } defer cr.DeleteCredentials() - nfsVolume, err := NewNFSVolume(req.GetVolumeId()) + nfsVolume, err := NewNFSVolume(ctx, req.GetVolumeId()) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } diff --git a/internal/nfs/controller/volume.go b/internal/nfs/controller/volume.go index 8b22a7a28..95e744b8b 100644 --- a/internal/nfs/controller/volume.go +++ b/internal/nfs/controller/volume.go @@ -30,6 +30,9 @@ import ( // modify and delete the NFS-exported CephFS volume. Instances of this struct // are short lived, they only exist as long as a CSI-procedure is active. type NFSVolume struct { + // ctx is the context for this short living volume object + ctx context.Context + volumeID string clusterID string mons string @@ -41,7 +44,7 @@ type NFSVolume struct { // NewNFSVolume create a new NFSVolume instance for the currently executing // CSI-procedure. -func NewNFSVolume(volumeID string) (*NFSVolume, error) { +func NewNFSVolume(ctx context.Context, volumeID string) (*NFSVolume, error) { // TODO: validate volume.VolumeContext parameters vi := util.CSIIdentifier{} @@ -51,6 +54,7 @@ func NewNFSVolume(volumeID string) (*NFSVolume, error) { } return &NFSVolume{ + ctx: ctx, volumeID: volumeID, }, nil } @@ -117,7 +121,7 @@ func (nv *NFSVolume) CreateExport(backend *csi.Volume) error { } // TODO: use new go-ceph API - _, stderr, err := util.ExecCommand(context.TODO(), "ceph", args...) + _, stderr, err := util.ExecCommand(nv.ctx, "ceph", args...) if err != nil { return fmt.Errorf("executing ceph export command failed (%w): %s", err, stderr) } @@ -142,7 +146,7 @@ func (nv *NFSVolume) getNFSCluster() (string, error) { "ls", } - nfsCluster, _, err := util.ExecCommand(context.TODO(), "ceph", args...) + nfsCluster, _, err := util.ExecCommand(nv.ctx, "ceph", args...) if err != nil { return "", fmt.Errorf("executing ceph export command failed: %w", err) } @@ -174,7 +178,7 @@ func (nv *NFSVolume) DeleteExport() error { } // TODO: use new go-ceph API - _, stderr, err := util.ExecCommand(context.TODO(), "ceph", args...) + _, stderr, err := util.ExecCommand(nv.ctx, "ceph", args...) if err != nil { return fmt.Errorf("executing ceph export command failed (%w): %s", err, stderr) }