Skip to content

Commit b1fefcc

Browse files
committed
sandbox: store endpoint in cri sandboxStore
Signed-off-by: Abel Feng <[email protected]>
1 parent f6e0cf1 commit b1fefcc

File tree

8 files changed

+74
-9
lines changed

8 files changed

+74
-9
lines changed

core/runtime/v2/shim.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ type ShimInstance interface {
185185
Client() any
186186
// Delete will close the client and remove bundle from disk.
187187
Delete(ctx context.Context) error
188-
// Version returns shim's features compatibility version.
189-
Version() int
188+
// Endpoint returns shim's endpoint information,
189+
// including address, protocol and version.
190+
Endpoint() (string, string, int)
190191
}
191192

192193
func parseStartResponse(response []byte) (client.BootstrapParams, error) {
@@ -359,9 +360,11 @@ func (gc *grpcConn) UserOnCloseWait(ctx context.Context) error {
359360
}
360361

361362
type shim struct {
362-
bundle *Bundle
363-
client any
364-
version int
363+
bundle *Bundle
364+
client any
365+
address string
366+
protocol string
367+
version int
365368
}
366369

367370
var _ ShimInstance = (*shim)(nil)
@@ -371,8 +374,8 @@ func (s *shim) ID() string {
371374
return s.bundle.ID
372375
}
373376

374-
func (s *shim) Version() int {
375-
return s.version
377+
func (s *shim) Endpoint() (string, string, int) {
378+
return s.address, s.protocol, s.version
376379
}
377380

378381
func (s *shim) Namespace() string {
@@ -440,7 +443,8 @@ type shimTask struct {
440443
}
441444

442445
func newShimTask(shim ShimInstance) (*shimTask, error) {
443-
taskClient, err := NewTaskClient(shim.Client(), shim.Version())
446+
_, _, version := shim.Endpoint()
447+
taskClient, err := NewTaskClient(shim.Client(), version)
444448
if err != nil {
445449
return nil, err
446450
}

core/sandbox/controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ type ControllerInstance struct {
117117
SandboxID string
118118
Pid uint32
119119
CreatedAt time.Time
120+
Address string
121+
Protocol string
122+
Version uint32
120123
Labels map[string]string
121124
}
122125

@@ -133,4 +136,7 @@ type ControllerStatus struct {
133136
CreatedAt time.Time
134137
ExitedAt time.Time
135138
Extra typeurl.Any
139+
Address string
140+
Protocol string
141+
Version uint32
136142
}

core/sandbox/proxy/controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func (s *remoteSandboxController) Start(ctx context.Context, sandboxID string) (
7373
Pid: resp.GetPid(),
7474
CreatedAt: resp.GetCreatedAt().AsTime(),
7575
Labels: resp.GetLabels(),
76+
Address: resp.GetAddress(),
77+
Protocol: resp.GetProtocol(),
78+
Version: resp.GetVersion(),
7679
}, nil
7780
}
7881

@@ -141,6 +144,9 @@ func (s *remoteSandboxController) Status(ctx context.Context, sandboxID string,
141144
CreatedAt: resp.GetCreatedAt().AsTime(),
142145
ExitedAt: resp.GetExitedAt().AsTime(),
143146
Extra: resp.GetExtra(),
147+
Address: resp.GetAddress(),
148+
Protocol: resp.GetProtocol(),
149+
Version: resp.GetVersion(),
144150
}, nil
145151
}
146152

internal/cri/server/container_start.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ func (c *criService) StartContainer(ctx context.Context, r *runtime.StartContain
118118
if ociRuntime.Path != "" {
119119
taskOpts = append(taskOpts, containerd.WithRuntimePath(ociRuntime.Path))
120120
}
121+
122+
// append endpoint to the options so that task manager can get task api endpoint directly
123+
endpoint := sandbox.Endpoint
124+
if endpoint.IsValid() {
125+
taskOpts = append(taskOpts,
126+
containerd.WithTaskApiEndpoint(endpoint.Address, endpoint.Protocol, endpoint.Version))
127+
}
128+
121129
task, err := container.NewTask(ctx, ioCreation, taskOpts...)
122130
if err != nil {
123131
return nil, fmt.Errorf("failed to create containerd task: %w", err)

internal/cri/server/restart.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (c *criService) recover(ctx context.Context) error {
113113
var (
114114
state = sandboxstore.StateUnknown
115115
controller = c.client.SandboxController(sbx.Sandboxer)
116+
endpoint sandboxstore.Endpoint
116117
)
117118

118119
status, err := controller.Status(ctx, sbx.ID, false)
@@ -126,6 +127,9 @@ func (c *criService) recover(ctx context.Context) error {
126127
state = sandboxstore.StateNotReady
127128
}
128129
} else {
130+
endpoint.Protocol = status.Protocol
131+
endpoint.Version = status.Version
132+
endpoint.Address = status.Address
129133
if code, ok := runtime.PodSandboxState_value[status.State]; ok {
130134
if code == int32(runtime.PodSandboxState_SANDBOX_READY) {
131135
state = sandboxstore.StateReady
@@ -137,6 +141,7 @@ func (c *criService) recover(ctx context.Context) error {
137141

138142
sb := sandboxstore.NewSandbox(metadata, sandboxstore.Status{State: state})
139143
sb.Sandboxer = sbx.Sandboxer
144+
sb.Endpoint = endpoint
140145

141146
// Load network namespace.
142147
sb.NetNS = getNetNS(&metadata)

internal/cri/server/sandbox_run.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
265265
return nil, fmt.Errorf("failed to start sandbox %q: %w", id, err)
266266
}
267267

268+
if ctrl.Protocol != "" && ctrl.Address != "" {
269+
sandbox.Endpoint = sandboxstore.Endpoint{
270+
Protocol: ctrl.Protocol,
271+
Version: ctrl.Version,
272+
Address: ctrl.Address,
273+
}
274+
}
275+
276+
if sandboxInfo, err = c.client.SandboxStore().Update(ctx, sandboxInfo, "extensions"); err != nil {
277+
return nil, fmt.Errorf("unable to update extensions for sandbox %q: %w", id, err)
278+
}
279+
268280
if !hostNetwork(config) && userNsEnabled {
269281
// If userns is enabled, then the netns was created by the OCI runtime
270282
// on controller.Start(). The OCI runtime needs to create the netns

internal/cri/store/sandbox/sandbox.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ type Sandbox struct {
4747
*store.StopCh
4848
// Stats contains (mutable) stats for the (pause) sandbox container
4949
Stats *stats.ContainerStats
50+
// Endpoint is the sandbox endpoint, for task or streaming api connection
51+
Endpoint Endpoint
52+
}
53+
54+
type Endpoint struct {
55+
Address string
56+
Protocol string
57+
Version uint32
58+
}
59+
60+
func (e *Endpoint) IsValid() bool {
61+
return e.Protocol != "" && e.Address != ""
5062
}
5163

5264
// NewSandbox creates an internally used sandbox type. This functions reminds

plugins/sandbox/controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,13 @@ func (c *controllerLocal) Start(ctx context.Context, sandboxID string) (sandbox.
188188
c.cleanupShim(ctx, sandboxID, svc)
189189
return sandbox.ControllerInstance{}, fmt.Errorf("failed to start sandbox %s: %w", sandboxID, errdefs.FromGRPC(err))
190190
}
191-
191+
address, protocol, version := shim.Endpoint()
192192
return sandbox.ControllerInstance{
193193
SandboxID: sandboxID,
194194
Pid: resp.GetPid(),
195+
Address: address,
196+
Protocol: protocol,
197+
Version: uint32(version),
195198
CreatedAt: resp.GetCreatedAt().AsTime(),
196199
}, nil
197200
}
@@ -302,6 +305,12 @@ func (c *controllerLocal) Status(ctx context.Context, sandboxID string, verbose
302305
return sandbox.ControllerStatus{}, fmt.Errorf("failed to query sandbox %s status: %w", sandboxID, err)
303306
}
304307

308+
shim, err := c.shims.Get(ctx, sandboxID)
309+
if err != nil {
310+
return sandbox.ControllerStatus{}, fmt.Errorf("unable to find sandbox %q", sandboxID)
311+
}
312+
address, protocol, version := shim.Endpoint()
313+
305314
return sandbox.ControllerStatus{
306315
SandboxID: resp.GetSandboxID(),
307316
Pid: resp.GetPid(),
@@ -310,6 +319,9 @@ func (c *controllerLocal) Status(ctx context.Context, sandboxID string, verbose
310319
CreatedAt: resp.GetCreatedAt().AsTime(),
311320
ExitedAt: resp.GetExitedAt().AsTime(),
312321
Extra: resp.GetExtra(),
322+
Address: address,
323+
Protocol: protocol,
324+
Version: uint32(version),
313325
}, nil
314326
}
315327

0 commit comments

Comments
 (0)