From e158356dd290dd5e7e0795c67f33bf69bd1f3856 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Fri, 4 Jul 2025 09:36:36 +0200 Subject: [PATCH] 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. --- .changelog/26194.txt | 3 +++ client/hostvolumemanager/host_volume_plugin.go | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 .changelog/26194.txt diff --git a/.changelog/26194.txt b/.changelog/26194.txt new file mode 100644 index 000000000..f9910f602 --- /dev/null +++ b/.changelog/26194.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Attempt to rollback directory creation when the `mkdir` plugin fails to perform ownership changes on it +``` diff --git a/client/hostvolumemanager/host_volume_plugin.go b/client/hostvolumemanager/host_volume_plugin.go index 92473e7f1..8e068d284 100644 --- a/client/hostvolumemanager/host_volume_plugin.go +++ b/client/hostvolumemanager/host_volume_plugin.go @@ -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) }