1717package sandbox
1818
1919import (
20- "context"
21- "net"
20+ "fmt"
2221
22+ sandboxAPI "github.com/containerd/containerd/api/runtime/sandbox/v1"
2323 "github.com/containerd/plugin"
2424 "github.com/containerd/plugin/registry"
2525 "github.com/containerd/ttrpc"
2626
27- intsandbox "github.com/containerd/nerdbox/internal/shim/sandbox"
27+ "github.com/containerd/nerdbox/internal/shim/sandbox"
2828 vmsbox "github.com/containerd/nerdbox/internal/shim/sandbox/vm"
2929 "github.com/containerd/nerdbox/pkg/vm"
3030 "github.com/containerd/nerdbox/plugins"
@@ -44,59 +44,46 @@ func init() {
4444 return nil , err
4545 }
4646 sb := vmsbox .NewVMSandbox (vmm .(vm.Manager ))
47- // Wrap the raw Sandbox in a SandboxService that implements
48- // both the Sandbox interface and the containerd
49- // TTRPCSandboxService. The SandboxPlugin does NOT implement
50- // shim.TTRPCService — TTRPC registration is handled by the
51- // dedicated TTRPCPlugin "sandbox" in service_plugin.go. This
52- // prevents a double-registration panic when the shim framework
53- // iterates all plugins looking for TTRPCService implementors.
54- return & sandboxManager {svc : intsandbox .NewSandboxService (sb )}, nil
47+ return sandbox .NewSandboxService (sb ), nil
5548 },
5649 })
57- }
58-
59- // sandboxManager wraps *intsandbox.SandboxService and exposes the
60- // intsandbox.Sandbox interface to the plugin system while intentionally NOT
61- // implementing shim.TTRPCService. This prevents the shim framework from
62- // calling RegisterTTRPC on the SandboxPlugin instance, which would cause a
63- // duplicate registration panic (the TTRPCPlugin "sandbox" handles that).
64- type sandboxManager struct {
65- svc * intsandbox.SandboxService
66- }
67-
68- // Verify that sandboxManager satisfies the Sandbox interface.
69- var _ intsandbox.Sandbox = (* sandboxManager )(nil )
70-
71- // Service returns the underlying *intsandbox.SandboxService. The task and
72- // TTRPC-sandbox plugins use this to access sandbox-specific operations.
73- func (m * sandboxManager ) Service () * intsandbox.SandboxService {
74- return m .svc
75- }
7650
77- // Export NetNS
78- // Export Options
79-
80- // The following methods delegate to the underlying SandboxService so that
81- // sandboxManager satisfies intsandbox.Sandbox (required by the streaming
82- // plugin and any other consumer of the SandboxPlugin value).
83-
84- func (m * sandboxManager ) Start (ctx context.Context , opts ... intsandbox.Opt ) error {
85- return m .svc .Start (ctx , opts ... )
86- }
87-
88- func (m * sandboxManager ) Stop (ctx context.Context ) error {
89- return m .svc .Stop (ctx )
90- }
51+ registry .Register (& plugin.Registration {
52+ Type : plugins .TTRPCPlugin ,
53+ ID : "sandbox" ,
54+ Requires : []plugin.Type {
55+ plugins .SandboxPlugin ,
56+ },
57+ InitFn : func (ic * plugin.InitContext ) (interface {}, error ) {
58+ sbPlugin , err := ic .GetSingle (plugins .SandboxPlugin )
59+ if err != nil {
60+ return nil , err
61+ }
9162
92- func (m * sandboxManager ) Client () (* ttrpc.Client , error ) {
93- return m .svc .Client ()
63+ sm , ok := sbPlugin .(sandboxAPI.TTRPCSandboxService )
64+ if ! ok {
65+ return nil , fmt .Errorf ("unexpected sandbox plugin implementation %T" , sbPlugin )
66+ }
67+ return & sbService {srv : sm }, nil
68+ },
69+ })
9470}
9571
96- func (m * sandboxManager ) StartStream (ctx context.Context , id string ) (net.Conn , error ) {
97- return m .svc .StartStream (ctx , id )
72+ // sbService adapts a sandboxAPI.TTRPCSandboxService to shim.TTRPCService,
73+ // so that the "sandbox" TTRPCPlugin registration above (rather than the
74+ // SandboxPlugin "manager" registration, which other plugins such as
75+ // streaming/transfer depend on as a plain sandbox.Sandbox) is the one the
76+ // shim framework calls RegisterTTRPC on. Without this indirection, the
77+ // framework would either not find a RegisterTTRPC method at all, or (if
78+ // SandboxService implemented it directly) call it a second time when it
79+ // scans the "manager" plugin's own instance, double-registering the
80+ // service.
81+ type sbService struct {
82+ srv sandboxAPI.TTRPCSandboxService
9883}
9984
100- func (m * sandboxManager ) ReservedDisks () int {
101- return m .svc .ReservedDisks ()
85+ // RegisterTTRPC registers the sandbox service on the TTRPC server.
86+ func (s * sbService ) RegisterTTRPC (server * ttrpc.Server ) error {
87+ sandboxAPI .RegisterTTRPCSandboxService (server , s .srv )
88+ return nil
10289}
0 commit comments