@@ -36,13 +36,14 @@ import (
36
36
37
37
"golang.org/x/net/context"
38
38
39
- volumehelper "github.com/kubernetes-csi/csi-driver-smb/pkg/util"
39
+ "github.com/kubernetes-csi/csi-driver-smb/pkg/util"
40
40
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
41
41
)
42
42
43
43
// 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 {
46
47
return nil , status .Error (codes .InvalidArgument , "Volume capability missing in request" )
47
48
}
48
49
volumeID := req .GetVolumeId ()
@@ -55,6 +56,20 @@ func (d *Driver) NodePublishVolume(_ context.Context, req *csi.NodePublishVolume
55
56
return nil , status .Error (codes .InvalidArgument , "Target path not provided" )
56
57
}
57
58
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
+
58
73
source := req .GetStagingTargetPath ()
59
74
if len (source ) == 0 {
60
75
return nil , status .Error (codes .InvalidArgument , "Staging target not provided" )
@@ -110,7 +125,7 @@ func (d *Driver) NodeUnpublishVolume(_ context.Context, req *csi.NodeUnpublishVo
110
125
}
111
126
112
127
// 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 ) {
114
129
volumeID := req .GetVolumeId ()
115
130
if len (volumeID ) == 0 {
116
131
return nil , status .Error (codes .InvalidArgument , "Volume ID missing in request" )
@@ -132,7 +147,8 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
132
147
secrets := req .GetSecrets ()
133
148
gidPresent := checkGidPresentInMountFlags (mountFlags )
134
149
135
- var source , subDir string
150
+ var source , subDir , secretName , secretNamespace , ephemeralVolMountOptions string
151
+ var ephemeralVol bool
136
152
subDirReplaceMap := map [string ]string {}
137
153
for k , v := range context {
138
154
switch strings .ToLower (k ) {
@@ -146,6 +162,14 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
146
162
subDirReplaceMap [pvcNameMetadata ] = v
147
163
case pvNameKey :
148
164
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
149
173
}
150
174
}
151
175
@@ -171,8 +195,20 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
171
195
}
172
196
}
173
197
198
+ if ephemeralVol {
199
+ mountFlags = strings .Split (ephemeralVolMountOptions , "," )
200
+ }
201
+
174
202
// in guest login, username and password options are not needed
175
203
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
+ }
176
212
177
213
var mountOptions , sensitiveMountOptions []string
178
214
if runtime .GOOS == "windows" {
@@ -236,7 +272,7 @@ func (d *Driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
236
272
return Mount (d .mounter , source , targetPath , "cifs" , mountOptions , sensitiveMountOptions , volumeID )
237
273
}
238
274
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 {
240
276
return nil , status .Error (codes .Internal , fmt .Sprintf ("volume(%s) mount %q on %q failed with %v" , volumeID , source , targetPath , err ))
241
277
}
242
278
klog .V (2 ).Infof ("volume(%s) mount %q on %q succeeded" , volumeID , source , targetPath )
0 commit comments