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 and move util under pkg.
  • Loading branch information
kkmsft committed Feb 11, 2020
1 parent 0bb1ff3 commit d366c53
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 41 deletions.
17 changes: 5 additions & 12 deletions cmd/csi-node-driver-registrar/node_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"google.golang.org/grpc"

"github.com/kubernetes-csi/node-driver-registrar/pkg/util"
"k8s.io/klog"
registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1alpha1"
)
Expand All @@ -36,23 +37,15 @@ 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 := util.CleanupSocketFile(socketPath); err != nil {
klog.Errorf("%+v", err)
os.Exit(1)
}

var oldmask int
if runtime.GOOS == "linux" {
// Default to only user accessible socket, caller can open up later if desired
oldmask, _ = umask(0077)
oldmask, _ = util.Umask(0077)
}

klog.Infof("Starting Registration Server at: %s\n", socketPath)
Expand All @@ -62,7 +55,7 @@ func nodeRegister(
os.Exit(1)
}
if runtime.GOOS == "linux" {
umask(oldmask)
util.Umask(oldmask)
}
klog.Infof("Registration Server started at: %s\n", socketPath)
grpcServer := grpc.NewServer()
Expand Down
27 changes: 0 additions & 27 deletions cmd/csi-node-driver-registrar/util_windows.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package util

import (
"fmt"
"os"

"golang.org/x/sys/unix"
)

func umask(mask int) (int, error) {
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
}
47 changes: 47 additions & 0 deletions pkg/util/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// +build windows

/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

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 d366c53

Please sign in to comment.