Skip to content

Commit

Permalink
sandbox: add update api for controller
Browse files Browse the repository at this point in the history
Signed-off-by: Abel Feng <[email protected]>
  • Loading branch information
abel-von authored and f00589305 committed Jun 14, 2024
1 parent e49d3fd commit 15887d7
Show file tree
Hide file tree
Showing 20 changed files with 1,200 additions and 736 deletions.
48 changes: 48 additions & 0 deletions api/next.pb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5419,6 +5419,14 @@ file {
type_name: ".containerd.services.sandbox.v1.ControllerCreateRequest.AnnotationsEntry"
json_name: "annotations"
}
field {
name: "sandbox"
number: 6
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: ".containerd.types.Sandbox"
json_name: "sandbox"
}
field {
name: "sandboxer"
number: 10
Expand Down Expand Up @@ -5793,6 +5801,41 @@ file {
json_name: "metrics"
}
}
message_type {
name: "ControllerUpdateRequest"
field {
name: "sandbox_id"
number: 1
label: LABEL_OPTIONAL
type: TYPE_STRING
json_name: "sandboxId"
}
field {
name: "sandboxer"
number: 2
label: LABEL_OPTIONAL
type: TYPE_STRING
json_name: "sandboxer"
}
field {
name: "sandbox"
number: 3
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: ".containerd.types.Sandbox"
json_name: "sandbox"
}
field {
name: "fields"
number: 4
label: LABEL_REPEATED
type: TYPE_STRING
json_name: "fields"
}
}
message_type {
name: "ControllerUpdateResponse"
}
service {
name: "Store"
method {
Expand Down Expand Up @@ -5863,6 +5906,11 @@ file {
input_type: ".containerd.services.sandbox.v1.ControllerMetricsRequest"
output_type: ".containerd.services.sandbox.v1.ControllerMetricsResponse"
}
method {
name: "Update"
input_type: ".containerd.services.sandbox.v1.ControllerUpdateRequest"
output_type: ".containerd.services.sandbox.v1.ControllerUpdateResponse"
}
}
options {
go_package: "github.com/containerd/containerd/api/services/sandbox/v1;sandbox"
Expand Down
709 changes: 440 additions & 269 deletions api/services/sandbox/v1/sandbox.pb.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions api/services/sandbox/v1/sandbox.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ service Controller {
rpc Status(ControllerStatusRequest) returns (ControllerStatusResponse);
rpc Shutdown(ControllerShutdownRequest) returns (ControllerShutdownResponse);
rpc Metrics(ControllerMetricsRequest) returns (ControllerMetricsResponse);
rpc Update(ControllerUpdateRequest) returns (ControllerUpdateResponse);
}

message ControllerCreateRequest {
Expand All @@ -103,6 +104,7 @@ message ControllerCreateRequest {
google.protobuf.Any options = 3;
string netns_path = 4;
map<string, string> annotations = 5;
containerd.types.Sandbox sandbox = 6;
string sandboxer = 10;
}

Expand Down Expand Up @@ -190,3 +192,13 @@ message ControllerMetricsRequest {
message ControllerMetricsResponse {
types.Metric metrics = 1;
}

message ControllerUpdateRequest {
string sandbox_id = 1;
string sandboxer = 2;
containerd.types.Sandbox sandbox = 3;
repeated string fields = 4;
}

message ControllerUpdateResponse {
}
36 changes: 36 additions & 0 deletions api/services/sandbox/v1/sandbox_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions api/services/sandbox/v1/sandbox_ttrpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions client/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"fmt"
"time"

"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"

"github.com/containerd/containerd/v2/core/containers"
api "github.com/containerd/containerd/v2/core/sandbox"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/errdefs"
"github.com/containerd/typeurl/v2"
)

// Sandbox is a high level client to containerd's sandboxes.
Expand Down
3 changes: 3 additions & 0 deletions core/sandbox/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type Controller interface {
Shutdown(ctx context.Context, sandboxID string) error
// Metrics queries the sandbox for metrics.
Metrics(ctx context.Context, sandboxID string) (*types.Metric, error)
// Update changes a part of sandbox, such as extensions/annotations/labels/spec of
// Sandbox object, controllers may have to update the running sandbox according to the changes.
Update(ctx context.Context, sandboxID string, sandbox Sandbox, fields ...string) error
}

type ControllerInstance struct {
Expand Down
60 changes: 59 additions & 1 deletion core/sandbox/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/core/sandbox"
"github.com/containerd/containerd/v2/pkg/protobuf"
"github.com/containerd/errdefs"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"google.golang.org/protobuf/types/known/anypb"
Expand All @@ -46,7 +47,11 @@ func (s *remoteSandboxController) Create(ctx context.Context, sandboxInfo sandbo
for _, opt := range opts {
opt(&options)
}
_, err := s.client.Create(ctx, &api.ControllerCreateRequest{
apiSandbox, err := toAPISandbox(sandboxInfo)
if err != nil {
return err
}
_, err = s.client.Create(ctx, &api.ControllerCreateRequest{
SandboxID: sandboxInfo.ID,
Rootfs: mount.ToProto(options.Rootfs),
Options: &anypb.Any{
Expand All @@ -55,6 +60,7 @@ func (s *remoteSandboxController) Create(ctx context.Context, sandboxInfo sandbo
},
NetnsPath: options.NetNSPath,
Annotations: options.Annotations,
Sandbox: &apiSandbox,
})
if err != nil {
return errdefs.FromGRPC(err)
Expand Down Expand Up @@ -178,3 +184,55 @@ func (s *remoteSandboxController) Metrics(ctx context.Context, sandboxID string)
}
return resp.Metrics, nil
}

func (s *remoteSandboxController) Update(
ctx context.Context,
sandboxID string,
sandbox sandbox.Sandbox,
fields ...string) error {
apiSandbox, err := toAPISandbox(sandbox)
if err != nil {
return err
}
_, err = s.client.Update(ctx, &api.ControllerUpdateRequest{
SandboxID: sandboxID,
Sandbox: &apiSandbox,
Fields: fields,
})
if err != nil {
return errdefs.FromGRPC(err)
}
return nil
}

func toAPISandbox(sb sandbox.Sandbox) (types.Sandbox, error) {
options, err := protobuf.MarshalAnyToProto(sb.Runtime.Options)
if err != nil {
return types.Sandbox{}, err
}
spec, err := protobuf.MarshalAnyToProto(sb.Spec)
if err != nil {
return types.Sandbox{}, err
}
extensions := make(map[string]*anypb.Any)
for k, v := range sb.Extensions {
pb, err := protobuf.MarshalAnyToProto(v)
if err != nil {
return types.Sandbox{}, err
}
extensions[k] = pb
}
return types.Sandbox{
SandboxID: sb.ID,
Runtime: &types.Sandbox_Runtime{
Name: sb.Runtime.Name,
Options: options,
},
Spec: spec,
Labels: sb.Labels,
CreatedAt: protobuf.ToTimestamp(sb.CreatedAt),
UpdatedAt: protobuf.ToTimestamp(sb.UpdatedAt),
Extensions: extensions,
Sandboxer: sb.Sandboxer,
}, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
tags.cncf.io/container-device-interface/specs-go v0.7.0 // indirect
)

replace github.com/containerd/containerd/api => ./api
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ github.com/containerd/cgroups/v3 v3.0.3 h1:S5ByHZ/h9PMe5IOQoN7E+nMc2UcLEM/V48DGD
github.com/containerd/cgroups/v3 v3.0.3/go.mod h1:8HBe7V3aWGLFPd/k03swSIsGjZhHI2WzJmticMgVuz0=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/containerd/containerd/api v1.8.0-rc.2 h1:EnWLDKWWbIRzuy71L20P3VF/DhxSaDEocsovKPdW5Oo=
github.com/containerd/containerd/api v1.8.0-rc.2/go.mod h1:VgMSK19YOLolP4a1/b5vlVkTo8MzMoLPZnvD1PNWeGg=
github.com/containerd/continuity v0.4.3 h1:6HVkalIp+2u1ZLH1J/pYX2oBVXlJZvh1X1A7bEZ9Su8=
github.com/containerd/continuity v0.4.3/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5ZURM=
Expand Down
8 changes: 8 additions & 0 deletions internal/cri/server/podsandbox/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func (c *Controller) Wait(ctx context.Context, sandboxID string) (sandbox.ExitSt

}

func (c *Controller) Update(
ctx context.Context,
sandboxID string,
sandbox sandbox.Sandbox,
fields ...string) error {
return nil
}

func (c *Controller) waitSandboxExit(ctx context.Context, p *types.PodSandbox, exitCh <-chan containerd.ExitStatus) error {
select {
case e := <-exitCh:
Expand Down
8 changes: 8 additions & 0 deletions internal/cri/server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ func (f fakeSandboxController) Metrics(ctx context.Context, sandboxID string) (*
return &types.Metric{}, errdefs.ErrNotImplemented
}

func (f *fakeSandboxController) Update(
ctx context.Context,
sandboxID string,
sandbox sandbox.Sandbox,
fields ...string) error {
return errdefs.ErrNotImplemented
}

type fakeRuntimeService struct {
ocispecs map[string]*oci.Spec
}
Expand Down
8 changes: 8 additions & 0 deletions plugins/sandbox/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ func (c *controllerLocal) Metrics(ctx context.Context, sandboxID string) (*types
return resp.Metrics, nil
}

func (c *controllerLocal) Update(
ctx context.Context,
sandboxID string,
sandbox sandbox.Sandbox,
fields ...string) error {
return nil
}

func (c *controllerLocal) getSandbox(ctx context.Context, id string) (runtimeAPI.TTRPCSandboxService, error) {
shim, err := c.shims.Get(ctx, id)
if err != nil {
Expand Down
Loading

0 comments on commit 15887d7

Please sign in to comment.