From baa66debd8216e1bf39911fb1e6df44c36e2e726 Mon Sep 17 00:00:00 2001 From: "Krishnakumar R(KK)" <29471693+kkmsft@users.noreply.github.com> Date: Tue, 11 Feb 2020 13:46:56 -0800 Subject: [PATCH] Fix registration socket removal on windows. - 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. --- .../node_register.go | 17 ++----- cmd/csi-node-driver-registrar/util_windows.go | 27 ----------- .../util}/util_linux.go | 21 ++++++++- pkg/util/util_windows.go | 47 +++++++++++++++++++ 4 files changed, 71 insertions(+), 41 deletions(-) delete mode 100644 cmd/csi-node-driver-registrar/util_windows.go rename {cmd/csi-node-driver-registrar => pkg/util}/util_linux.go (54%) create mode 100644 pkg/util/util_windows.go diff --git a/cmd/csi-node-driver-registrar/node_register.go b/cmd/csi-node-driver-registrar/node_register.go index 47efd349..3fe2a395 100644 --- a/cmd/csi-node-driver-registrar/node_register.go +++ b/cmd/csi-node-driver-registrar/node_register.go @@ -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" ) @@ -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) @@ -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() diff --git a/cmd/csi-node-driver-registrar/util_windows.go b/cmd/csi-node-driver-registrar/util_windows.go deleted file mode 100644 index 7a65a938..00000000 --- a/cmd/csi-node-driver-registrar/util_windows.go +++ /dev/null @@ -1,27 +0,0 @@ -// +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 main - -import ( - "errors" -) - -func umask(mask int) (int, error) { - return -1, errors.New("umask not supported in Windows") -} diff --git a/cmd/csi-node-driver-registrar/util_linux.go b/pkg/util/util_linux.go similarity index 54% rename from cmd/csi-node-driver-registrar/util_linux.go rename to pkg/util/util_linux.go index bab7d62f..31795524 100644 --- a/cmd/csi-node-driver-registrar/util_linux.go +++ b/pkg/util/util_linux.go @@ -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 +} diff --git a/pkg/util/util_windows.go b/pkg/util/util_windows.go new file mode 100644 index 00000000..faa1e000 --- /dev/null +++ b/pkg/util/util_windows.go @@ -0,0 +1,47 @@ +// +build windows + +/* +Copyright 2020 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 +}