Skip to content

Commit 08b7479

Browse files
committed
sandbox: cleanup plugin and ensure options and netns are exposed
Signed-off-by: Derek McGowan <derek@mcg.dev>
1 parent 4991c6b commit 08b7479

4 files changed

Lines changed: 48 additions & 114 deletions

File tree

internal/shim/sandbox/service.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ func (s *SandboxService) RegisterStartOptions(fn StartOptionsFunc) {
131131
s.startOptsFn = fn
132132
}
133133

134-
// RegisterTTRPC registers the sandbox service on the TTRPC server.
135-
func (s *SandboxService) RegisterTTRPC(server *ttrpc.Server) error {
136-
sandboxAPI.RegisterTTRPCSandboxService(server, s)
137-
return nil
138-
}
139-
140134
// FS returns the SharedFS associated with this sandbox, or nil if the sandbox
141135
// has not been created yet. The task service uses this to share container
142136
// rootfses into the VM.
@@ -229,6 +223,16 @@ func (s *SandboxService) Options() *anypb.Any {
229223
return s.options
230224
}
231225

226+
// NetworkSandboxPath returns the host-side network sandbox path (e.g. a Linux netns)
227+
func (s *SandboxService) NetworkSandboxPath() string {
228+
s.mu.Lock()
229+
defer s.mu.Unlock()
230+
if s.networkSandbox != nil {
231+
return s.networkSandbox.Path()
232+
}
233+
return ""
234+
}
235+
232236
// StartSandbox boots the VM. It calls the registered StartOptionsFunc (if
233237
// any) to obtain bundle-derived options (networking, resources, init args),
234238
// then adds the shared filesystem share and starts the VM.

plugins/shim/sandbox/plugin.go

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package sandbox
1818

1919
import (
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
}

plugins/shim/sandbox/service_plugin.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

plugins/shim/task/plugin.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@ func init() {
5555
return nil, err
5656
}
5757

58-
// Unwrap the sandboxManager to get the underlying SandboxService.
59-
type sandboxManagerUnwrapper interface {
60-
Service() *intsandbox.SandboxService
61-
}
62-
unwrapper, ok := sbRaw.(sandboxManagerUnwrapper)
58+
svc, ok := sbRaw.(*intsandbox.SandboxService)
6359
if !ok {
6460
return nil, fmt.Errorf("unexpected SandboxPlugin implementation %T", sbRaw)
6561
}
66-
svc := unwrapper.Service()
6762

6863
// Determine debug flag from shim opts stored in context.
6964
debug := false

0 commit comments

Comments
 (0)