Skip to content

Commit

Permalink
Add signal catch to stop the server gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw99 committed Nov 29, 2023
1 parent a1a0031 commit 7ee30b6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bin
.idea
15 changes: 12 additions & 3 deletions cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
VendorVersion: version,
}

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

if err := driver.Run(); err != nil {
// Wait for signal
stopCh := make(chan os.Signal, 1)
sigs := []os.Signal{
syscall.SIGTERM,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
}
signal.Notify(stopCh, sigs...)

if err := driver.Run(stopCh); err != nil {
fmt.Printf("Failed to run driver: %s", err.Error())
os.Exit(1)

}
}
1 change: 0 additions & 1 deletion internal/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func Listen(endpoint string) (net.Listener, func(), error) {

cleanup := func() {}
if proto == "unix" {
addr = "/" + addr
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { //nolint: vetshadow
return nil, nil, fmt.Errorf("%s: %q", addr, err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ func NewHostPathDriver(cfg Config) (*hostPath, error) {
return hp, nil
}

func (hp *hostPath) Run() error {
func (hp *hostPath) Run(stopCh <-chan os.Signal) error {
s := NewNonBlockingGRPCServer()
// hp itself implements ControllerServer, NodeServer, and IdentityServer.
s.Start(hp.config.Endpoint, hp, hp, hp, hp)
s.Wait()

<-stopCh
s.Stop()

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostpath/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (hp *hostPath) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVol
switch m := info.Mode(); {
case m.IsDir():
if vol.VolAccessType != state.MountAccess {
return nil, status.Errorf(codes.InvalidArgument, "Volume %s is not a directory", volID)
return nil, status.Errorf(codes.InvalidArgument, "Volume %s is not a mounted filesystem", volID)
}
case m&os.ModeDevice != 0:
if vol.VolAccessType != state.BlockAccess {
Expand Down
9 changes: 0 additions & 9 deletions pkg/hostpath/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package hostpath

import (
"encoding/json"
"sync"

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

// NonBlocking server
type nonBlockingGRPCServer struct {
wg sync.WaitGroup
server *grpc.Server
cleanup func()
}

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

s.wg.Add(1)

go s.serve(endpoint, ids, cs, ns, gcs)

return
}

func (s *nonBlockingGRPCServer) Wait() {
s.wg.Wait()
}

func (s *nonBlockingGRPCServer) Stop() {
s.server.GracefulStop()
s.cleanup()
Expand Down Expand Up @@ -92,7 +84,6 @@ func (s *nonBlockingGRPCServer) serve(ep string, ids csi.IdentityServer, cs csi.
glog.Infof("Listening for connections on address: %#v", listener.Addr())

server.Serve(listener)

}

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

0 comments on commit 7ee30b6

Please sign in to comment.