From df234edeb4663c88d0cc96bc2bf4b7904cb06baa Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sun, 7 Nov 2021 16:40:28 -0800 Subject: [PATCH] [sandbox] Add controller service Signed-off-by: Maksym Pavlenko --- services/sandbox/controller_local.go | 101 +++++++++++++++++++++++++ services/sandbox/controller_service.go | 95 +++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 services/sandbox/controller_local.go create mode 100644 services/sandbox/controller_service.go diff --git a/services/sandbox/controller_local.go b/services/sandbox/controller_local.go new file mode 100644 index 000000000..3be92ff11 --- /dev/null +++ b/services/sandbox/controller_local.go @@ -0,0 +1,101 @@ +/* + 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 sandbox + +import ( + "context" + + api "github.com/containerd/containerd/api/services/sandbox/v1" + "github.com/containerd/containerd/events" + "github.com/containerd/containerd/events/exchange" + "github.com/containerd/containerd/metadata" + "github.com/containerd/containerd/plugin" + v2 "github.com/containerd/containerd/runtime/v2" + "github.com/containerd/containerd/sandbox" + "github.com/containerd/containerd/services" + "google.golang.org/grpc" +) + +func init() { + plugin.Register(&plugin.Registration{ + Type: plugin.ServicePlugin, + ID: services.SandboxControllerService, + Requires: []plugin.Type{ + plugin.RuntimeShimPlugin, + plugin.MetadataPlugin, + plugin.EventPlugin, + }, + InitFn: func(ic *plugin.InitContext) (interface{}, error) { + shimPlugin, err := ic.Get(plugin.RuntimeShimPlugin) + if err != nil { + return nil, err + } + + metadataPlugin, err := ic.Get(plugin.MetadataPlugin) + if err != nil { + return nil, err + } + + exchangePlugin, err := ic.GetByID(plugin.EventPlugin, "exchange") + if err != nil { + return nil, err + } + + var ( + shims = shimPlugin.(*v2.ShimManager) + publisher = exchangePlugin.(*exchange.Exchange) + db = metadataPlugin.(*metadata.DB) + store = metadata.NewSandboxStore(db) + ) + + return &controllerLocal{ + shims: shims, + store: store, + publisher: publisher, + }, nil + }, + }) +} + +type controllerLocal struct { + shims *v2.ShimManager + store sandbox.Store + publisher events.Publisher +} + +var _ api.ControllerClient = (*controllerLocal)(nil) + +func (c *controllerLocal) Start(ctx context.Context, in *api.ControllerStartRequest, opts ...grpc.CallOption) (*api.ControllerStartResponse, error) { + panic("implement me") +} + +func (c *controllerLocal) Shutdown(ctx context.Context, in *api.ControllerShutdownRequest, opts ...grpc.CallOption) (*api.ControllerShutdownResponse, error) { + panic("implement me") +} + +func (c *controllerLocal) Pause(ctx context.Context, in *api.ControllerPauseRequest, opts ...grpc.CallOption) (*api.ControllerPauseResponse, error) { + panic("implement me") +} + +func (c *controllerLocal) Resume(ctx context.Context, in *api.ControllerResumeRequest, opts ...grpc.CallOption) (*api.ControllerResumeResponse, error) { + panic("implement me") +} + +func (c *controllerLocal) Ping(ctx context.Context, in *api.ControllerPingRequest, opts ...grpc.CallOption) (*api.ControllerPingResponse, error) { + panic("implement me") +} + +func (c *controllerLocal) Status(ctx context.Context, in *api.ControllerStatusRequest, opts ...grpc.CallOption) (*api.ControllerStatusResponse, error) { + panic("implement me") +} diff --git a/services/sandbox/controller_service.go b/services/sandbox/controller_service.go new file mode 100644 index 000000000..b1df0a423 --- /dev/null +++ b/services/sandbox/controller_service.go @@ -0,0 +1,95 @@ +/* + 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 sandbox + +import ( + "context" + "errors" + + api "github.com/containerd/containerd/api/services/sandbox/v1" + "github.com/containerd/containerd/log" + "github.com/containerd/containerd/plugin" + "github.com/containerd/containerd/services" + "google.golang.org/grpc" +) + +func init() { + plugin.Register(&plugin.Registration{ + Type: plugin.GRPCPlugin, + ID: "controllers", + Requires: []plugin.Type{ + plugin.ServicePlugin, + }, + InitFn: func(ic *plugin.InitContext) (interface{}, error) { + plugins, err := ic.GetByType(plugin.ServicePlugin) + if err != nil { + return nil, err + } + + p, ok := plugins[services.SandboxControllerService] + if !ok { + return nil, errors.New("sandbox service not found") + } + + i, err := p.Instance() + if err != nil { + return nil, err + } + + return &controllerService{ + local: i.(api.ControllerClient), + }, nil + }, + }) +} + +type controllerService struct { + local api.ControllerClient +} + +var _ api.ControllerServer = (*controllerService)(nil) + +func (s *controllerService) Register(server *grpc.Server) error { + api.RegisterControllerServer(server, s) + return nil +} + +func (s *controllerService) Start(ctx context.Context, req *api.ControllerStartRequest) (*api.ControllerStartResponse, error) { + log.G(ctx).WithField("req", req).Debug("start sandbox") + return s.local.Start(ctx, req) +} + +func (s *controllerService) Shutdown(ctx context.Context, req *api.ControllerShutdownRequest) (*api.ControllerShutdownResponse, error) { + log.G(ctx).WithField("req", req).Debug("delete sandbox") + return s.local.Shutdown(ctx, req) +} + +func (s *controllerService) Pause(ctx context.Context, req *api.ControllerPauseRequest) (*api.ControllerPauseResponse, error) { + log.G(ctx).WithField("req", req).Debug("pause sandbox") + return s.local.Pause(ctx, req) +} + +func (s *controllerService) Resume(ctx context.Context, req *api.ControllerResumeRequest) (*api.ControllerResumeResponse, error) { + log.G(ctx).WithField("req", req).Debug("resume sandbox") + return s.local.Resume(ctx, req) +} + +func (s *controllerService) Ping(ctx context.Context, req *api.ControllerPingRequest) (*api.ControllerPingResponse, error) { + return s.local.Ping(ctx, req) +} + +func (s *controllerService) Status(ctx context.Context, req *api.ControllerStatusRequest) (*api.ControllerStatusResponse, error) { + log.G(ctx).WithField("req", req).Debug("sandbox status") + return s.local.Status(ctx, req) +}