|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package app |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "net" |
| 20 | + "os" |
| 21 | + "os/signal" |
| 22 | + "strconv" |
| 23 | + "syscall" |
| 24 | + |
| 25 | + snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1" |
| 26 | + "github.com/containerd/containerd/contrib/snapshotservice" |
| 27 | + "github.com/containerd/containerd/log" |
| 28 | + "github.com/containerd/containerd/snapshots" |
| 29 | + "github.com/firecracker-microvm/firecracker-go-sdk/vsock" |
| 30 | + "github.com/sirupsen/logrus" |
| 31 | + "golang.org/x/sync/errgroup" |
| 32 | + "google.golang.org/grpc" |
| 33 | + |
| 34 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/config" |
| 35 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux" |
| 36 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux/cache" |
| 37 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux/proxy" |
| 38 | + proxyaddress "github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux/proxy/address" |
| 39 | +) |
| 40 | + |
| 41 | +// Run the demultiplexing snapshotter service. |
| 42 | +// |
| 43 | +// The snapshotter server will be running on the |
| 44 | +// network address and port specified in listener config. |
| 45 | +func Run(config config.Config) error { |
| 46 | + stop := make(chan os.Signal, 1) |
| 47 | + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE, syscall.SIGHUP, syscall.SIGQUIT) |
| 48 | + |
| 49 | + ctx, cancel := context.WithCancel(context.Background()) |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + group, ctx := errgroup.WithContext(ctx) |
| 53 | + |
| 54 | + snapshotter, err := initSnapshotter(ctx, config) |
| 55 | + if err != nil { |
| 56 | + log.G(ctx).WithFields( |
| 57 | + logrus.Fields{"resolver": config.Snapshotter.Proxy.Address.Resolver.Type}, |
| 58 | + ).WithError(err).Fatal("failed creating socket resolver") |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + grpcServer := grpc.NewServer() |
| 63 | + service := snapshotservice.FromSnapshotter(snapshotter) |
| 64 | + snapshotsapi.RegisterSnapshotsServer(grpcServer, service) |
| 65 | + |
| 66 | + listenerConfig := config.Snapshotter.Listener |
| 67 | + listener, err := net.Listen(listenerConfig.Network, listenerConfig.Address) |
| 68 | + if err != nil { |
| 69 | + log.G(ctx).WithFields( |
| 70 | + logrus.Fields{ |
| 71 | + "network": listenerConfig.Network, |
| 72 | + "address": listenerConfig.Address, |
| 73 | + }, |
| 74 | + ).WithError(err).Fatal("failed creating listener") |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + group.Go(func() error { |
| 79 | + return grpcServer.Serve(listener) |
| 80 | + }) |
| 81 | + |
| 82 | + group.Go(func() error { |
| 83 | + defer func() { |
| 84 | + log.G(ctx).Info("stopping server") |
| 85 | + grpcServer.Stop() |
| 86 | + |
| 87 | + if err := snapshotter.Close(); err != nil { |
| 88 | + log.G(ctx).WithError(err).Error("failed to close snapshotter") |
| 89 | + } |
| 90 | + }() |
| 91 | + |
| 92 | + for { |
| 93 | + select { |
| 94 | + case <-stop: |
| 95 | + cancel() |
| 96 | + return nil |
| 97 | + case <-ctx.Done(): |
| 98 | + return ctx.Err() |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + if err := group.Wait(); err != nil { |
| 104 | + log.G(ctx).WithError(err).Error("demux snapshotter error") |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + log.G(ctx).Info("done") |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func initResolver(config config.Config) (proxyaddress.Resolver, error) { |
| 113 | + resolverConfig := config.Snapshotter.Proxy.Address.Resolver |
| 114 | + switch resolverConfig.Type { |
| 115 | + case "http": |
| 116 | + return proxyaddress.NewHTTPResolver(resolverConfig.Address), nil |
| 117 | + default: |
| 118 | + return nil, fmt.Errorf("invalid resolver type: %s", resolverConfig.Type) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +const base10 = 10 |
| 123 | +const bits32 = 32 |
| 124 | + |
| 125 | +func initSnapshotter(ctx context.Context, config config.Config) (snapshots.Snapshotter, error) { |
| 126 | + resolver, err := initResolver(config) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + newProxySnapshotterFunc := func(ctx context.Context, namespace string) (snapshots.Snapshotter, error) { |
| 132 | + r := resolver |
| 133 | + response, err := r.Get(namespace) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + host, portstr, err := net.SplitHostPort(response.Address) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + port, err := strconv.ParseUint(portstr, base10, bits32) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + snapshotterDialer := func(ctx context.Context, namespace string) (net.Conn, error) { |
| 146 | + return vsock.DialContext(ctx, host, uint32(port), vsock.WithLogger(log.G(ctx))) |
| 147 | + } |
| 148 | + return proxy.NewProxySnapshotter(ctx, host, snapshotterDialer) |
| 149 | + } |
| 150 | + |
| 151 | + return demux.NewSnapshotter(cache.NewSnapshotterCache(), newProxySnapshotterFunc), nil |
| 152 | +} |
0 commit comments