client: Remove created directory when mkdir plugin fails to chown. (#26194)

The mkdir plugin creates the directory and then chowns it. In the
event the chown command fails, we should attempt to remove the
directory. Without this, we leave directories on the client in
partial failure situations.
This commit is contained in:
James Rasell
2025-07-04 09:36:36 +02:00
committed by GitHub
parent 004fa6132b
commit e158356dd2
2 changed files with 12 additions and 0 deletions

3
.changelog/26194.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
client: Attempt to rollback directory creation when the `mkdir` plugin fails to perform ownership changes on it
```

View File

@@ -138,6 +138,15 @@ func (p *HostVolumePluginMkdir) Create(_ context.Context,
// Chown note: A uid or gid of -1 means to not change that value.
if err = os.Chown(path, params.Uid, params.Gid); err != nil {
log.Error("error changing owner/group", "error", err, "uid", params.Uid, "gid", params.Gid)
// Failing to change ownership is fatal for this plugin. Since we have
// already created the directory, we should attempt to clean it.
// Otherwise, the operator must do this manually.
if err := os.RemoveAll(path); err != nil {
log.Error("failed to remove directory after create failure",
"error", err)
}
return nil, fmt.Errorf("error changing owner/group: %w", err)
}