diff --git a/README.md b/README.md index e837c9bb..ed44e8fc 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,6 @@ the actual driver's name. args: - "--csi-address=/csi/csi.sock" - "--kubelet-registration-path=/var/lib/kubelet/plugins//csi.sock" - lifecycle: - preStop: - exec: - command: ["/bin/sh", "-c", "rm -rf /registration/ /registration/-reg.sock"] volumeMounts: - name: plugin-dir mountPath: /csi diff --git a/cmd/csi-node-driver-registrar/node_register.go b/cmd/csi-node-driver-registrar/node_register.go index 47efd349..246f6748 100644 --- a/cmd/csi-node-driver-registrar/node_register.go +++ b/cmd/csi-node-driver-registrar/node_register.go @@ -20,7 +20,9 @@ import ( "fmt" "net" "os" + "os/signal" "runtime" + "syscall" "google.golang.org/grpc" @@ -28,6 +30,10 @@ import ( registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1alpha1" ) +const ( + sockerDirPath = "/registration" +) + func nodeRegister( csiDriverName string, ) { @@ -35,7 +41,7 @@ func nodeRegister( // as gRPC server which replies to registration requests initiated by kubelet's // pluginswatcher infrastructure. Node labeling is done by kubelet's csi code. registrar := newRegistrationServer(csiDriverName, *kubeletRegistrationPath, supportedVersions) - socketPath := fmt.Sprintf("/registration/%s-reg.sock", csiDriverName) + socketPath := fmt.Sprintf("%s/%s-reg.sock", sockerDirPath, 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 @@ -69,6 +75,7 @@ func nodeRegister( // Registers kubelet plugin watcher api. registerapi.RegisterRegistrationServer(grpcServer, registrar) + go removeRegSocket(csiDriverName) // Starts service if err := grpcServer.Serve(lis); err != nil { klog.Errorf("Registration Server stopped serving: %v", err) @@ -77,3 +84,16 @@ func nodeRegister( // If gRPC server is gracefully shutdown, exit os.Exit(0) } + +func removeRegSocket(csiDriverName string) { + sigc := make(chan os.Signal, 1) + signal.Notify(sigc, syscall.SIGTERM) + <-sigc + socketPath := fmt.Sprintf("%s/%s-reg.sock", sockerDirPath, csiDriverName) + err := os.Remove(socketPath) + if err != nil && os.IsNotExist(err) { + klog.Errorf("failed to remove socket: %s with error: %+v", socketPath, err) + os.Exit(1) + } + os.Exit(0) +}