Skip to content

Commit 5d31e4e

Browse files
committed
Add signal catch to stop the server gracefully
1 parent 0bfbc4b commit 5d31e4e

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

cmd/hostpathplugin/main.go

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

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

127-
if err := driver.Run(); err != nil {
127+
// Wait for signal
128+
stopCh := make(chan os.Signal, 1)
129+
sigs := []os.Signal{
130+
syscall.SIGTERM,
131+
syscall.SIGHUP,
132+
syscall.SIGINT,
133+
syscall.SIGQUIT,
134+
}
135+
signal.Notify(stopCh, sigs...)
136+
137+
if err := driver.Run(stopCh); err != nil {
128138
fmt.Printf("Failed to run driver: %s", err.Error())
129139
os.Exit(1)
130-
131140
}
132141
}

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
@@ -130,14 +130,16 @@ func NewHostPathDriver(cfg Config) (*hostPath, error) {
130130
return hp, nil
131131
}
132132

133-
func (hp *hostPath) Run() error {
133+
func (hp *hostPath) Run(stopCh <-chan os.Signal) error {
134134
s := NewNonBlockingGRPCServer()
135135
var sms csi.SnapshotMetadataServer
136136
if hp.config.EnableSnapshotMetadata {
137137
sms = hp
138138
}
139139
s.Start(hp.config.Endpoint, hp, hp, hp, hp, sms)
140-
s.Wait()
140+
141+
<-stopCh
142+
s.Stop()
141143

142144
return nil
143145
}

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
"golang.org/x/net/context"
2423
"google.golang.org/grpc"
@@ -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, gcs csi.GroupControllerServer, sms csi.SnapshotMetadataServer) {
4442

45-
s.wg.Add(1)
46-
4743
go s.serve(endpoint, ids, cs, ns, gcs, sms)
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()
@@ -95,7 +87,6 @@ func (s *nonBlockingGRPCServer) serve(ep string, ids csi.IdentityServer, cs csi.
9587
klog.Infof("Listening for connections on address: %#v", listener.Addr())
9688

9789
server.Serve(listener)
98-
9990
}
10091

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

0 commit comments

Comments
 (0)