Skip to content

Commit

Permalink
Fix registration socket removal on windows.
Browse files Browse the repository at this point in the history
Until the bug - golang/go#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.
  • Loading branch information
kkmsft committed Feb 5, 2020
1 parent 0bb1ff3 commit 9397cc6
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions cmd/csi-node-driver-registrar/node_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,32 @@ 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)
os.Exit(1)
}

var oldmask int
if runtime.GOOS == "linux" {
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)
os.Exit(1)
}
// Default to only user accessible socket, caller can open up later if desired
oldmask, _ = umask(0077)
} else {
// 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.
klog.Warningf("Windows environment detected. Deleting the socket path: %s", socketPath)
if err := os.Remove(socketPath); err != nil {
klog.Errorf("failed to remove stale socket %s with error: %+v", socketPath, err)
os.Exit(1)
}
}

klog.Infof("Starting Registration Server at: %s\n", socketPath)
Expand Down

0 comments on commit 9397cc6

Please sign in to comment.