Skip to content

Commit 3fcaebd

Browse files
committed
snapshotter: properly clean resources on exit
1 parent 135ae98 commit 3fcaebd

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

snapshotter/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ snapshotter plugin process as follows:
4242
```
4343
./snapshotter /var/run/firecracker-snapshotter.sock /var/lib/firecracker-snapshotter
4444
```
45+
46+
Now you can use snapshotter with containerd:
47+
48+
```
49+
$ CONTAINERD_SNAPSHOTTER=firecracker-snapshotter ctr images pull docker.io/library/alpine:latest
50+
```

snapshotter/cmd/snapshotter/main.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package main
22

33
import (
44
"context"
5-
"github.com/awslabs/containerd-firecracker/snapshotter"
6-
"google.golang.org/grpc"
75
"net"
86
"os"
7+
"os/signal"
8+
"syscall"
99

10+
"github.com/awslabs/containerd-firecracker/snapshotter"
1011
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
1112
"github.com/containerd/containerd/contrib/snapshotservice"
1213
"github.com/containerd/containerd/log"
14+
"golang.org/x/sync/errgroup"
15+
"google.golang.org/grpc"
1316
)
1417

1518
func main() {
@@ -19,7 +22,14 @@ func main() {
1922

2023
var unixAddr, rootPath = os.Args[1], os.Args[2]
2124

22-
ctx := context.Background()
25+
stop := make(chan os.Signal, 1)
26+
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
27+
28+
ctx, cancel := context.WithCancel(context.Background())
29+
defer cancel()
30+
31+
group, ctx := errgroup.WithContext(ctx)
32+
2333
rpc := grpc.NewServer()
2434

2535
snap, err := snapshotter.NewSnapshotter(ctx, rootPath)
@@ -37,9 +47,34 @@ func main() {
3747
log.G(ctx).WithError(err).Fatalf("failed to listen socket at %s", os.Args[1])
3848
}
3949

40-
defer listener.Close()
50+
group.Go(func() error {
51+
return rpc.Serve(listener)
52+
})
53+
54+
group.Go(func() error {
55+
defer func() {
56+
log.G(ctx).Info("stopping server")
57+
rpc.Stop()
58+
59+
if err := snap.Close(); err != nil {
60+
log.G(ctx).WithError(err).Error("failed to close snapshotter")
61+
}
62+
}()
4163

42-
if err := rpc.Serve(listener); err != nil {
43-
log.G(ctx).WithError(err).Fatal("failed to run gRPC server")
64+
for {
65+
select {
66+
case <-stop:
67+
cancel()
68+
return nil
69+
case <-ctx.Done():
70+
return ctx.Err()
71+
}
72+
}
73+
})
74+
75+
if err := group.Wait(); err != nil {
76+
log.G(ctx).WithError(err).Warn("snapshotter error")
4477
}
78+
79+
log.G(ctx).Info("done")
4580
}

0 commit comments

Comments
 (0)