@@ -172,29 +172,66 @@ func (f *Filesystem) WriteMetadata(volumeID string, meta metadata.Metadata) erro
172
172
func (f * Filesystem ) RegisterMetadata (meta metadata.Metadata ) (bool , error ) {
173
173
existingMeta , err := f .ReadMetadata (meta .VolumeID )
174
174
if errors .Is (err , ErrNotFound ) {
175
- if err := os .MkdirAll (f .volumePath (meta .VolumeID ), 0644 ); err != nil {
175
+ // Ensure directory structure for the volume exists
176
+ if err := f .ensureVolumeDirectory (meta .VolumeID ); err != nil {
176
177
return false , err
177
178
}
178
179
179
- return true , f .WriteMetadata (meta .VolumeID , meta )
180
+ if err := f .WriteMetadata (meta .VolumeID , meta ); err != nil {
181
+ return false , err
182
+ }
183
+
184
+ return true , nil
180
185
}
181
186
182
187
// If the volume context has changed, should write updated metadata
183
188
if ! apiequality .Semantic .DeepEqual (existingMeta .VolumeContext , meta .VolumeContext ) {
189
+ // Ensure directory structure for the volume exists - this will probably do
190
+ // nothing, but it helps avoid any weird edge cases we could find ourselves in &
191
+ // is an inexpensive operation.
192
+ if err := f .ensureVolumeDirectory (meta .VolumeID ); err != nil {
193
+ return false , err
194
+ }
195
+
184
196
f .log .WithValues ("volume_id" , meta .VolumeID ).Info ("volume context changed, updating file system metadata" )
185
197
existingMeta .VolumeContext = meta .VolumeContext
186
- return true , f .WriteMetadata (existingMeta .VolumeID , existingMeta )
198
+ if err := f .WriteMetadata (existingMeta .VolumeID , existingMeta ); err != nil {
199
+ return false , err
200
+ }
201
+
202
+ return true , nil
187
203
}
188
204
189
205
return false , nil
190
206
}
191
207
208
+ // ensureVolumeDirectory ensures the directory structure for the volume exists.
209
+ // If the directories already exist, it will do nothing.
210
+ func (f * Filesystem ) ensureVolumeDirectory (volumeID string ) error {
211
+ if err := os .MkdirAll (f .volumePath (volumeID ), 0644 ); err != nil {
212
+ return err
213
+ }
214
+
215
+ // Data directory should be read and execute only to the fs user and group.
216
+ if err := os .MkdirAll (f .dataPathForVolumeID (volumeID ), 0550 ); err != nil {
217
+ return err
218
+ }
219
+
220
+ return nil
221
+ }
222
+
192
223
// WriteFiles writes the given data to filesystem files within the volume's
193
224
// data directory. Filesystem supports changing ownership of the data directory
194
225
// to a custom gid.
195
226
func (f * Filesystem ) WriteFiles (meta metadata.Metadata , files map [string ][]byte ) error {
196
- // Data directory should be read and execute only to the fs user and group.
197
- if err := os .MkdirAll (f .dataPathForVolumeID (meta .VolumeID ), 0550 ); err != nil {
227
+ // Ensure the full directory structure for the volume exists.
228
+ // This already happens in RegisterMetadata, however, when a driver starts up and reads
229
+ // the metadata files from the existing tmpfs to re-populate the manager, RegisterMetadata
230
+ // is not called again (it is only invoked by driver/nodeserver.go when a pod is first processed
231
+ // during NodePublishVolume).
232
+ // There is a very slim chance we could end out in a weird situation where the metadata
233
+ // file exists but the data directory does not, so re-run ensureVolumeDirectory just to be safe.
234
+ if err := f .ensureVolumeDirectory (meta .VolumeID ); err != nil {
198
235
return err
199
236
}
200
237
0 commit comments