Skip to content

Commit

Permalink
Remove registratation socket file on shutdown
Browse files Browse the repository at this point in the history
when terminating, remove the *-reg.socket file
created by node-driver-registrar
  • Loading branch information
Madhu-1 committed Feb 6, 2020
1 parent 0bb1ff3 commit d278cf0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ the CSI calls on.

This information reflects the head of this branch.

| Compatible with CSI Version | Container Image | Min K8s Version |
| ------------------------------------------------------------------------------------------ | ----------------------------------------------- | --------------- |
| [CSI Spec v1.0.0](https://github.com/container-storage-interface/spec/releases/tag/v1.0.0) | quay.io/k8scsi/csi-node-driver-registrar | 1.13 |
| Compatible with CSI Version | Container Image | Min K8s Version |
| ------------------------------------------------------------------------------------------ | ---------------------------------------- | --------------- |
| [CSI Spec v1.0.0](https://github.com/container-storage-interface/spec/releases/tag/v1.0.0) | quay.io/k8scsi/csi-node-driver-registrar | 1.13 |

For release-0.4 and below, please refer to the [driver-registrar
repository](https://github.com/kubernetes-csi/driver-registrar).
Expand Down Expand Up @@ -76,10 +76,6 @@ the actual driver's name.
args:
- "--csi-address=/csi/csi.sock"
- "--kubelet-registration-path=/var/lib/kubelet/plugins/<drivername.example.com>/csi.sock"
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "rm -rf /registration/<plugin> /registration/<drivername.example.com>-reg.sock"]
volumeMounts:
- name: plugin-dir
mountPath: /csi
Expand Down
22 changes: 21 additions & 1 deletion cmd/csi-node-driver-registrar/node_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"fmt"
"net"
"os"
"os/signal"
"runtime"
"syscall"

"google.golang.org/grpc"

Expand All @@ -35,7 +37,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 := buildSocketPath(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
Expand Down Expand Up @@ -69,6 +71,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)
Expand All @@ -77,3 +80,20 @@ func nodeRegister(
// If gRPC server is gracefully shutdown, exit
os.Exit(0)
}

func buildSocketPath(csiDriverName string) string {
return fmt.Sprintf("/registration/%s-reg.sock", csiDriverName)
}

func removeRegSocket(csiDriverName string) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGTERM)
<-sigc
socketPath := buildSocketPath(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)
}

0 comments on commit d278cf0

Please sign in to comment.