Skip to content

Commit e39d94c

Browse files
committed
Add signal catch to stop the server gracefully
1 parent 3348ad0 commit e39d94c

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/bin
2+
.idea

cmd/hostpathplugin/main.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
VendorVersion: version,
4545
}
4646

47-
flag.StringVar(&cfg.Endpoint, "endpoint", "unix://tmp/csi.sock", "CSI endpoint")
47+
flag.StringVar(&cfg.Endpoint, "endpoint", "unix:///tmp/csi.sock", "CSI endpoint")
4848
flag.StringVar(&cfg.DriverName, "drivername", "hostpath.csi.k8s.io", "name of the driver")
4949
flag.StringVar(&cfg.StateDir, "statedir", "/csi-data-dir", "directory for storing state information across driver restarts, volumes and snapshots")
5050
flag.StringVar(&cfg.NodeID, "nodeid", "", "node id")
@@ -113,9 +113,18 @@ func main() {
113113
os.Exit(1)
114114
}
115115

116-
if err := driver.Run(); err != nil {
116+
// Wait for signal
117+
stopCh := make(chan os.Signal, 1)
118+
sigs := []os.Signal{
119+
syscall.SIGTERM,
120+
syscall.SIGHUP,
121+
syscall.SIGINT,
122+
syscall.SIGQUIT,
123+
}
124+
signal.Notify(stopCh, sigs...)
125+
126+
if err := driver.Run(stopCh); err != nil {
117127
fmt.Printf("Failed to run driver: %s", err.Error())
118128
os.Exit(1)
119-
120129
}
121130
}

internal/endpoint/endpoint.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func Listen(endpoint string) (net.Listener, func(), error) {
4343

4444
cleanup := func() {}
4545
if proto == "unix" {
46-
addr = "/" + addr
4746
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { //nolint: vetshadow
4847
return nil, nil, fmt.Errorf("%s: %q", addr, err)
4948
}

pkg/hostpath/hostpath.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ func NewHostPathDriver(cfg Config) (*hostPath, error) {
120120
return hp, nil
121121
}
122122

123-
func (hp *hostPath) Run() error {
123+
func (hp *hostPath) Run(stopCh <-chan os.Signal) error {
124124
s := NewNonBlockingGRPCServer()
125125
// hp itself implements ControllerServer, NodeServer, and IdentityServer.
126126
s.Start(hp.config.Endpoint, hp, hp, hp)
127-
s.Wait()
127+
128+
<-stopCh
129+
s.Stop()
128130

129131
return nil
130132
}

pkg/hostpath/nodeserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func (hp *hostPath) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVol
503503
switch m := info.Mode(); {
504504
case m.IsDir():
505505
if vol.VolAccessType != state.MountAccess {
506-
return nil, status.Errorf(codes.InvalidArgument, "Volume %s is not a directory", volID)
506+
return nil, status.Errorf(codes.InvalidArgument, "Volume %s is not a mounted filesystem", volID)
507507
}
508508
case m&os.ModeDevice != 0:
509509
if vol.VolAccessType != state.BlockAccess {

pkg/hostpath/server.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package hostpath
1818

1919
import (
2020
"encoding/json"
21-
"sync"
2221

2322
"github.com/golang/glog"
2423
"golang.org/x/net/context"
@@ -35,24 +34,17 @@ func NewNonBlockingGRPCServer() *nonBlockingGRPCServer {
3534

3635
// NonBlocking server
3736
type nonBlockingGRPCServer struct {
38-
wg sync.WaitGroup
3937
server *grpc.Server
4038
cleanup func()
4139
}
4240

4341
func (s *nonBlockingGRPCServer) Start(endpoint string, ids csi.IdentityServer, cs csi.ControllerServer, ns csi.NodeServer) {
4442

45-
s.wg.Add(1)
46-
4743
go s.serve(endpoint, ids, cs, ns)
4844

4945
return
5046
}
5147

52-
func (s *nonBlockingGRPCServer) Wait() {
53-
s.wg.Wait()
54-
}
55-
5648
func (s *nonBlockingGRPCServer) Stop() {
5749
s.server.GracefulStop()
5850
s.cleanup()
@@ -89,7 +81,6 @@ func (s *nonBlockingGRPCServer) serve(ep string, ids csi.IdentityServer, cs csi.
8981
glog.Infof("Listening for connections on address: %#v", listener.Addr())
9082

9183
server.Serve(listener)
92-
9384
}
9485

9586
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {

0 commit comments

Comments
 (0)