Skip to content

Commit

Permalink
Sandbox: Add Metrics rpc for controller
Browse files Browse the repository at this point in the history
As a follow up change to adding a SandboxMetrics rpc to the core
sandbox service, the controller needed a corresponding rpc for CRI
and others to eventually implement.

This leaves the CRI (non-shim mode) controller unimplemented just to
have a change with the API addition to start.

Signed-off-by: Danny Canter <danny@dcantah.dev>
  • Loading branch information
dcantah committed Jun 13, 2023
1 parent d56722e commit d278d37
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 278 deletions.
27 changes: 27 additions & 0 deletions api/next.pb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5006,6 +5006,7 @@ file {
dependency: "github.com/containerd/containerd/api/types/sandbox.proto"
dependency: "github.com/containerd/containerd/api/types/mount.proto"
dependency: "github.com/containerd/containerd/api/types/platform.proto"
dependency: "github.com/containerd/containerd/api/types/metrics.proto"
message_type {
name: "StoreCreateRequest"
field {
Expand Down Expand Up @@ -5393,6 +5394,27 @@ file {
message_type {
name: "ControllerShutdownResponse"
}
message_type {
name: "ControllerMetricsRequest"
field {
name: "sandbox_id"
number: 1
label: LABEL_OPTIONAL
type: TYPE_STRING
json_name: "sandboxId"
}
}
message_type {
name: "ControllerMetricsResponse"
field {
name: "metrics"
number: 1
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: ".containerd.types.Metric"
json_name: "metrics"
}
}
service {
name: "Store"
method {
Expand Down Expand Up @@ -5458,6 +5480,11 @@ file {
input_type: ".containerd.services.sandbox.v1.ControllerShutdownRequest"
output_type: ".containerd.services.sandbox.v1.ControllerShutdownResponse"
}
method {
name: "Metrics"
input_type: ".containerd.services.sandbox.v1.ControllerMetricsRequest"
output_type: ".containerd.services.sandbox.v1.ControllerMetricsResponse"
}
}
options {
go_package: "github.com/containerd/containerd/api/services/sandbox/v1;sandbox"
Expand Down
700 changes: 422 additions & 278 deletions api/services/sandbox/v1/sandbox.pb.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions api/services/sandbox/v1/sandbox.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import "google/protobuf/timestamp.proto";
import "github.com/containerd/containerd/api/types/sandbox.proto";
import "github.com/containerd/containerd/api/types/mount.proto";
import "github.com/containerd/containerd/api/types/platform.proto";
import "github.com/containerd/containerd/api/types/metrics.proto";

option go_package = "github.com/containerd/containerd/api/services/sandbox/v1;sandbox";

Expand Down Expand Up @@ -93,6 +94,7 @@ service Controller {
rpc Wait(ControllerWaitRequest) returns (ControllerWaitResponse);
rpc Status(ControllerStatusRequest) returns (ControllerStatusResponse);
rpc Shutdown(ControllerShutdownRequest) returns (ControllerShutdownResponse);
rpc Metrics(ControllerMetricsRequest) returns (ControllerMetricsResponse);
}

message ControllerCreateRequest {
Expand Down Expand Up @@ -161,3 +163,11 @@ message ControllerShutdownRequest {
}

message ControllerShutdownResponse {}

message ControllerMetricsRequest {
string sandbox_id = 1;
}

message ControllerMetricsResponse {
types.Metric metrics = 1;
}
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.

29 changes: 29 additions & 0 deletions pkg/cri/sbserver/podsandbox/sandbox_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package podsandbox

import (
"context"

"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
)

// TODO(dcantah): Implement metrics to be used for SandboxStats rpc.
func (c *Controller) Metrics(ctx context.Context, sandboxID string) (*types.Metric, error) {
return nil, errdefs.ErrNotImplemented
}
14 changes: 14 additions & 0 deletions plugins/sandbox/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

runtimeAPI "github.com/containerd/containerd/api/runtime/sandbox/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/events"
"github.com/containerd/containerd/events/exchange"
Expand Down Expand Up @@ -283,6 +284,19 @@ func (c *controllerLocal) Status(ctx context.Context, sandboxID string, verbose
}, nil
}

func (c *controllerLocal) Metrics(ctx context.Context, sandboxID string) (*types.Metric, error) {
sb, err := c.getSandbox(ctx, sandboxID)
if err != nil {
return nil, err
}
req := &runtimeAPI.SandboxMetricsRequest{SandboxID: sandboxID}
resp, err := sb.SandboxMetrics(ctx, req)
if err != nil {
return nil, err
}
return resp.Metrics, 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
2 changes: 2 additions & 0 deletions sandbox/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type Controller interface {
Status(ctx context.Context, sandboxID string, verbose bool) (ControllerStatus, error)
// Shutdown deletes and cleans all tasks and sandbox instance.
Shutdown(ctx context.Context, sandboxID string) error
// Metrics queries the sandbox for metrics.
Metrics(ctx context.Context, sandboxID string) (*types.Metric, error)
}

type ControllerInstance struct {
Expand Down
9 changes: 9 additions & 0 deletions sandbox/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

api "github.com/containerd/containerd/api/services/sandbox/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/sandbox"
Expand Down Expand Up @@ -140,3 +141,11 @@ func (s *remoteSandboxController) Status(ctx context.Context, sandboxID string,
Extra: resp.GetExtra(),
}, nil
}

func (s *remoteSandboxController) Metrics(ctx context.Context, sandboxID string) (*types.Metric, error) {
resp, err := s.client.Metrics(ctx, &api.ControllerMetricsRequest{SandboxID: sandboxID})
if err != nil {
return nil, errdefs.FromGRPC(err)
}
return resp.Metrics, nil
}
12 changes: 12 additions & 0 deletions services/sandbox/controller_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,15 @@ func (s *controllerService) Shutdown(ctx context.Context, req *api.ControllerShu
log.G(ctx).WithField("req", req).Debug("shutdown sandbox")
return &api.ControllerShutdownResponse{}, errdefs.ToGRPC(s.local.Shutdown(ctx, req.GetSandboxID()))
}

func (s *controllerService) Metrics(ctx context.Context, req *api.ControllerMetricsRequest) (*api.ControllerMetricsResponse, error) {
log.G(ctx).WithField("req", req).Debug("sandbox metrics")

metrics, err := s.local.Metrics(ctx, req.GetSandboxID())
if err != nil {
return &api.ControllerMetricsResponse{}, errdefs.ToGRPC(err)
}
return &api.ControllerMetricsResponse{
Metrics: metrics,
}, nil
}

0 comments on commit d278d37

Please sign in to comment.