@@ -36,13 +36,14 @@ import (
3636
3737 "golang.org/x/net/context"
3838
39- volumehelper "github.com/kubernetes-csi/csi-driver-smb/pkg/util"
39+ "github.com/kubernetes-csi/csi-driver-smb/pkg/util"
4040 azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
4141)
4242
4343// NodePublishVolume mount the volume from staging to target path
44- func (d * Driver ) NodePublishVolume (_ context.Context , req * csi.NodePublishVolumeRequest ) (* csi.NodePublishVolumeResponse , error ) {
45- if req .GetVolumeCapability () == nil {
44+ func (d * Driver ) NodePublishVolume (ctx context.Context , req * csi.NodePublishVolumeRequest ) (* csi.NodePublishVolumeResponse , error ) {
45+ volCap := req .GetVolumeCapability ()
46+ if volCap == nil {
4647 return nil , status .Error (codes .InvalidArgument , "Volume capability missing in request" )
4748 }
4849 volumeID := req .GetVolumeId ()
@@ -55,6 +56,20 @@ func (d *Driver) NodePublishVolume(_ context.Context, req *csi.NodePublishVolume
5556 return nil , status .Error (codes .InvalidArgument , "Target path not provided" )
5657 }
5758
59+ context := req .GetVolumeContext ()
60+ if context != nil && strings .EqualFold (context [ephemeralField ], trueValue ) {
61+ // ephemeral volume
62+ util .SetKeyValueInMap (context , secretNamespaceField , context [podNamespaceField ])
63+ klog .V (2 ).Infof ("NodePublishVolume: ephemeral volume(%s) mount on %s" , volumeID , target )
64+ _ , err := d .NodeStageVolume (ctx , & csi.NodeStageVolumeRequest {
65+ StagingTargetPath : target ,
66+ VolumeContext : context ,
67+ VolumeCapability : volCap ,
68+ VolumeId : volumeID ,
69+ })
70+ return & csi.NodePublishVolumeResponse {}, err
71+ }
72+
5873 source := req .GetStagingTargetPath ()
5974 if len (source ) == 0 {
6075 return nil , status .Error (codes .InvalidArgument , "Staging target not provided" )
@@ -110,7 +125,7 @@ func (d *Driver) NodeUnpublishVolume(_ context.Context, req *csi.NodeUnpublishVo
110125}
111126
112127// NodeStageVolume mount the volume to a staging path
113- func (d * Driver ) NodeStageVolume (_ context.Context , req * csi.NodeStageVolumeRequest ) (* csi.NodeStageVolumeResponse , error ) {
128+ func (d * Driver ) NodeStageVolume (ctx context.Context , req * csi.NodeStageVolumeRequest ) (* csi.NodeStageVolumeResponse , error ) {
114129 volumeID := req .GetVolumeId ()
115130 if len (volumeID ) == 0 {
116131 return nil , status .Error (codes .InvalidArgument , "Volume ID missing in request" )
@@ -132,7 +147,8 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
132147 secrets := req .GetSecrets ()
133148 gidPresent := checkGidPresentInMountFlags (mountFlags )
134149
135- var source , subDir string
150+ var source , subDir , secretName , secretNamespace , ephemeralVolMountOptions string
151+ var ephemeralVol bool
136152 subDirReplaceMap := map [string ]string {}
137153 for k , v := range context {
138154 switch strings .ToLower (k ) {
@@ -146,6 +162,14 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
146162 subDirReplaceMap [pvcNameMetadata ] = v
147163 case pvNameKey :
148164 subDirReplaceMap [pvNameMetadata ] = v
165+ case secretNameField :
166+ secretName = v
167+ case secretNamespaceField :
168+ secretNamespace = v
169+ case ephemeralField :
170+ ephemeralVol = strings .EqualFold (v , trueValue )
171+ case mountOptionsField :
172+ ephemeralVolMountOptions = v
149173 }
150174 }
151175
@@ -171,8 +195,20 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
171195 }
172196 }
173197
198+ if ephemeralVol {
199+ mountFlags = strings .Split (ephemeralVolMountOptions , "," )
200+ }
201+
174202 // in guest login, username and password options are not needed
175203 requireUsernamePwdOption := ! hasGuestMountOptions (mountFlags )
204+ if ephemeralVol && requireUsernamePwdOption {
205+ klog .V (2 ).Infof ("NodeStageVolume: getting username and password from secret %s in namespace %s" , secretName , secretNamespace )
206+ var err error
207+ username , password , domain , err = d .GetUserNamePasswordFromSecret (ctx , secretName , secretNamespace )
208+ if err != nil {
209+ return nil , status .Error (codes .Internal , fmt .Sprintf ("Error getting username and password from secret %s in namespace %s: %v" , secretName , secretNamespace , err ))
210+ }
211+ }
176212
177213 var mountOptions , sensitiveMountOptions []string
178214 if runtime .GOOS == "windows" {
@@ -236,7 +272,7 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
236272 return Mount (d .mounter , source , targetPath , "cifs" , mountOptions , sensitiveMountOptions , volumeID )
237273 }
238274 timeoutFunc := func () error { return fmt .Errorf ("time out" ) }
239- if err := volumehelper .WaitUntilTimeout (90 * time .Second , execFunc , timeoutFunc ); err != nil {
275+ if err := util .WaitUntilTimeout (90 * time .Second , execFunc , timeoutFunc ); err != nil {
240276 return nil , status .Error (codes .Internal , fmt .Sprintf ("volume(%s) mount %q on %q failed with %v" , volumeID , source , targetPath , err ))
241277 }
242278 klog .V (2 ).Infof ("volume(%s) mount %q on %q succeeded" , volumeID , source , targetPath )
0 commit comments