diff --git a/cmd/csi-node-driver-registrar/node_register.go b/cmd/csi-node-driver-registrar/node_register.go index 47efd349a..b110967bd 100644 --- a/cmd/csi-node-driver-registrar/node_register.go +++ b/cmd/csi-node-driver-registrar/node_register.go @@ -36,16 +36,8 @@ func nodeRegister( // pluginswatcher infrastructure. Node labeling is done by kubelet's csi code. registrar := newRegistrationServer(csiDriverName, *kubeletRegistrationPath, supportedVersions) socketPath := fmt.Sprintf("/registration/%s-reg.sock", csiDriverName) - fi, err := os.Stat(socketPath) - if err == nil && (fi.Mode()&os.ModeSocket) != 0 { - // Remove any socket, stale or not, but fall through for other files - if err := os.Remove(socketPath); err != nil { - klog.Errorf("failed to remove stale socket %s with error: %+v", socketPath, err) - os.Exit(1) - } - } - if err != nil && !os.IsNotExist(err) { - klog.Errorf("failed to stat the socket %s with error: %+v", socketPath, err) + if err := cleanupSocketFile(socketPath); err != nil { + klog.Errorf("%+v", err) os.Exit(1) } diff --git a/cmd/csi-node-driver-registrar/util_linux.go b/cmd/csi-node-driver-registrar/util_linux.go index bab7d62f6..203df6c76 100644 --- a/cmd/csi-node-driver-registrar/util_linux.go +++ b/cmd/csi-node-driver-registrar/util_linux.go @@ -19,9 +19,26 @@ limitations under the License. package main import ( + "fmt" + "os" + "golang.org/x/sys/unix" ) func umask(mask int) (int, error) { return unix.Umask(mask), nil } + +func cleanupSocketFile(socketPath string) error { + fi, err := os.Stat(socketPath) + if err == nil && (fi.Mode()&os.ModeSocket) != 0 { + // Remove any socket, stale or not, but fall through for other files + if err := os.Remove(socketPath); err != nil { + return fmt.Errorf("failed to remove stale socket %s with error: %+v", socketPath, err) + } + } + if err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to stat the socket %s with error: %+v", socketPath, err) + } + return nil +} diff --git a/cmd/csi-node-driver-registrar/util_windows.go b/cmd/csi-node-driver-registrar/util_windows.go index 7a65a9382..f245adfaa 100644 --- a/cmd/csi-node-driver-registrar/util_windows.go +++ b/cmd/csi-node-driver-registrar/util_windows.go @@ -20,8 +20,28 @@ package main import ( "errors" + "fmt" + "os" ) func umask(mask int) (int, error) { return -1, errors.New("umask not supported in Windows") } + +func cleanupSocketFile(socketPath string) error { + if _, err := os.Lstat(socketPath); err != nil { + // If the file does not exist, then the cleanup can be considered successful. + if os.IsNotExist(err) { + return nil + } + return fmt.Errorf("failed to lstat the socket %s with error: %+v", socketPath, err) + } + + // TODO: Until the bug - https://github.com/golang/go/issues/33357 is fixed, os.stat wouldn't return the + // right mode(socket) on windows. Hence deleting the file, without checking whether + // its a socket, on windows. + if err := os.Remove(socketPath); err != nil { + return fmt.Errorf("failed to remove stale socket %s with error: %+v", socketPath, err) + } + return nil +}