|
17 | 17 | package plugin
|
18 | 18 |
|
19 | 19 | import (
|
20 |
| - "errors" |
21 |
| - "fmt" |
22 |
| - "net" |
23 |
| - "os" |
24 |
| - "path/filepath" |
| 20 | + "context" |
25 | 21 | "time"
|
26 | 22 |
|
27 | 23 | "github.com/containerd/containerd/defaults"
|
28 |
| - "github.com/containerd/containerd/log" |
29 | 24 | "github.com/containerd/containerd/pkg/dialer"
|
30 |
| - "github.com/containerd/containerd/platforms" |
31 |
| - ctdplugin "github.com/containerd/containerd/plugin" |
32 |
| - "github.com/containerd/stargz-snapshotter/service" |
33 |
| - "github.com/containerd/stargz-snapshotter/service/keychain/cri" |
34 |
| - "github.com/containerd/stargz-snapshotter/service/keychain/dockerconfig" |
35 |
| - "github.com/containerd/stargz-snapshotter/service/keychain/kubeconfig" |
36 |
| - "github.com/containerd/stargz-snapshotter/service/resolver" |
37 | 25 | grpc "google.golang.org/grpc"
|
38 | 26 | "google.golang.org/grpc/backoff"
|
39 | 27 | "google.golang.org/grpc/credentials/insecure"
|
40 |
| - runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" |
41 |
| -) |
42 |
| - |
43 |
| -// Config represents configuration for the stargz snapshotter plugin. |
44 |
| -type Config struct { |
45 |
| - service.Config |
46 | 28 |
|
47 |
| - // RootPath is the directory for the plugin |
48 |
| - RootPath string `toml:"root_path"` |
| 29 | + runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2" |
| 30 | + "github.com/containerd/stargz-snapshotter/service/keychain/crialpha" |
| 31 | + "github.com/containerd/stargz-snapshotter/service/plugincore" |
| 32 | + "github.com/containerd/stargz-snapshotter/service/resolver" |
| 33 | +) |
49 | 34 |
|
50 |
| - // CRIKeychainImageServicePath is the path to expose CRI service wrapped by CRI keychain |
51 |
| - CRIKeychainImageServicePath string `toml:"cri_keychain_image_service_path"` |
52 |
| - |
53 |
| - // Registry is CRI-plugin-compatible registry configuration |
54 |
| - Registry resolver.Registry `toml:"registry"` |
55 |
| -} |
| 35 | +// This plugin requires containerd newer than 234bf990dca4e81e89f549448aa6b555286eaa7a. |
| 36 | +// If not, use "github.com/containerd/stargz-snapshotter/service/pluginforked" instead. |
56 | 37 |
|
57 | 38 | func init() {
|
58 |
| - ctdplugin.Register(&ctdplugin.Registration{ |
59 |
| - Type: ctdplugin.SnapshotPlugin, |
60 |
| - ID: "stargz", |
61 |
| - Config: &Config{}, |
62 |
| - InitFn: func(ic *ctdplugin.InitContext) (interface{}, error) { |
63 |
| - ic.Meta.Platforms = append(ic.Meta.Platforms, platforms.DefaultSpec()) |
64 |
| - ctx := ic.Context |
65 |
| - |
66 |
| - config, ok := ic.Config.(*Config) |
67 |
| - if !ok { |
68 |
| - return nil, errors.New("invalid stargz snapshotter configuration") |
69 |
| - } |
70 |
| - |
71 |
| - root := ic.Root |
72 |
| - if config.RootPath != "" { |
73 |
| - root = config.RootPath |
74 |
| - } |
75 |
| - ic.Meta.Exports["root"] = root |
| 39 | + plugincore.RegisterPlugin(registerCRIAlphaServer) |
| 40 | +} |
76 | 41 |
|
77 |
| - // Configure keychain |
78 |
| - credsFuncs := []resolver.Credential{dockerconfig.NewDockerconfigKeychain(ctx)} |
79 |
| - if config.Config.KubeconfigKeychainConfig.EnableKeychain { |
80 |
| - var opts []kubeconfig.Option |
81 |
| - if kcp := config.Config.KubeconfigKeychainConfig.KubeconfigPath; kcp != "" { |
82 |
| - opts = append(opts, kubeconfig.WithKubeconfigPath(kcp)) |
83 |
| - } |
84 |
| - credsFuncs = append(credsFuncs, kubeconfig.NewKubeconfigKeychain(ctx, opts...)) |
85 |
| - } |
86 |
| - if addr := config.CRIKeychainImageServicePath; config.Config.CRIKeychainConfig.EnableKeychain && addr != "" { |
87 |
| - // connects to the backend CRI service (defaults to containerd socket) |
88 |
| - criAddr := ic.Address |
89 |
| - if cp := config.Config.CRIKeychainConfig.ImageServicePath; cp != "" { |
90 |
| - criAddr = cp |
91 |
| - } |
92 |
| - if criAddr == "" { |
93 |
| - return nil, errors.New("backend CRI service address is not specified") |
94 |
| - } |
95 |
| - connectCRI := func() (runtime.ImageServiceClient, error) { |
96 |
| - // TODO: make gRPC options configurable from config.toml |
97 |
| - backoffConfig := backoff.DefaultConfig |
98 |
| - backoffConfig.MaxDelay = 3 * time.Second |
99 |
| - connParams := grpc.ConnectParams{ |
100 |
| - Backoff: backoffConfig, |
101 |
| - } |
102 |
| - gopts := []grpc.DialOption{ |
103 |
| - grpc.WithTransportCredentials(insecure.NewCredentials()), |
104 |
| - grpc.WithConnectParams(connParams), |
105 |
| - grpc.WithContextDialer(dialer.ContextDialer), |
106 |
| - grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)), |
107 |
| - grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)), |
108 |
| - } |
109 |
| - conn, err := grpc.Dial(dialer.DialAddress(criAddr), gopts...) |
110 |
| - if err != nil { |
111 |
| - return nil, err |
112 |
| - } |
113 |
| - return runtime.NewImageServiceClient(conn), nil |
114 |
| - } |
115 |
| - criCreds, criServer := cri.NewCRIKeychain(ctx, connectCRI) |
116 |
| - // Create a gRPC server |
117 |
| - rpc := grpc.NewServer() |
118 |
| - runtime.RegisterImageServiceServer(rpc, criServer) |
119 |
| - // Prepare the directory for the socket |
120 |
| - if err := os.MkdirAll(filepath.Dir(addr), 0700); err != nil { |
121 |
| - return nil, fmt.Errorf("failed to create directory %q: %w", filepath.Dir(addr), err) |
122 |
| - } |
123 |
| - // Try to remove the socket file to avoid EADDRINUSE |
124 |
| - if err := os.RemoveAll(addr); err != nil { |
125 |
| - return nil, fmt.Errorf("failed to remove %q: %w", addr, err) |
126 |
| - } |
127 |
| - // Listen and serve |
128 |
| - l, err := net.Listen("unix", addr) |
129 |
| - if err != nil { |
130 |
| - return nil, fmt.Errorf("error on listen socket %q: %w", addr, err) |
131 |
| - } |
132 |
| - go func() { |
133 |
| - if err := rpc.Serve(l); err != nil { |
134 |
| - log.G(ctx).WithError(err).Warnf("error on serving via socket %q", addr) |
135 |
| - } |
136 |
| - }() |
137 |
| - credsFuncs = append(credsFuncs, criCreds) |
138 |
| - } |
| 42 | +func registerCRIAlphaServer(ctx context.Context, criAddr string, rpc *grpc.Server) resolver.Credential { |
| 43 | + connectAlphaCRI := func() (runtime_alpha.ImageServiceClient, error) { |
| 44 | + conn, err := newCRIConn(criAddr) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + return runtime_alpha.NewImageServiceClient(conn), nil |
| 49 | + } |
| 50 | + criAlphaCreds, criAlphaServer := crialpha.NewCRIAlphaKeychain(ctx, connectAlphaCRI) |
| 51 | + runtime_alpha.RegisterImageServiceServer(rpc, criAlphaServer) |
| 52 | + return criAlphaCreds |
| 53 | +} |
139 | 54 |
|
140 |
| - // TODO(ktock): print warn if old configuration is specified. |
141 |
| - // TODO(ktock): should we respect old configuration? |
142 |
| - return service.NewStargzSnapshotterService(ctx, root, &config.Config, |
143 |
| - service.WithCustomRegistryHosts(resolver.RegistryHostsFromCRIConfig(ctx, config.Registry, credsFuncs...))) |
144 |
| - }, |
145 |
| - }) |
| 55 | +func newCRIConn(criAddr string) (*grpc.ClientConn, error) { |
| 56 | + // TODO: make gRPC options configurable from config.toml |
| 57 | + backoffConfig := backoff.DefaultConfig |
| 58 | + backoffConfig.MaxDelay = 3 * time.Second |
| 59 | + connParams := grpc.ConnectParams{ |
| 60 | + Backoff: backoffConfig, |
| 61 | + } |
| 62 | + gopts := []grpc.DialOption{ |
| 63 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 64 | + grpc.WithConnectParams(connParams), |
| 65 | + grpc.WithContextDialer(dialer.ContextDialer), |
| 66 | + grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)), |
| 67 | + grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)), |
| 68 | + } |
| 69 | + return grpc.Dial(dialer.DialAddress(criAddr), gopts...) |
146 | 70 | }
|
0 commit comments