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.

- Place os specific logic into util_{linux, windows} files.
  • Loading branch information
kkmsft committed Feb 6, 2020
1 parent 0bb1ff3 commit 5052986
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
12 changes: 2 additions & 10 deletions cmd/csi-node-driver-registrar/node_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions cmd/csi-node-driver-registrar/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions cmd/csi-node-driver-registrar/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5052986

Please sign in to comment.