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

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
  • Loading branch information
Madhu-1 committed Dec 18, 2019
1 parent 0bb1ff3 commit 7394f62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
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,22 +20,28 @@ import (
"fmt"
"net"
"os"
"os/signal"
"runtime"
"syscall"

"google.golang.org/grpc"

"k8s.io/klog"
registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1alpha1"
)

const (
sockerDirPath = "/registration"
)

func nodeRegister(
csiDriverName string,
) {
// When kubeletRegistrationPath is specified then driver-registrar ONLY acts
// 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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 7394f62

Please sign in to comment.