Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signal catch to stop the server gracefully #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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