diff --git a/cmd/containerd/builtins/builtins.go b/cmd/containerd/builtins/builtins.go index bb64c1904..6ed9f3b0a 100644 --- a/cmd/containerd/builtins/builtins.go +++ b/cmd/containerd/builtins/builtins.go @@ -24,6 +24,7 @@ import ( _ "github.com/containerd/containerd/leases/plugin" _ "github.com/containerd/containerd/metadata/plugin" _ "github.com/containerd/containerd/pkg/nri/plugin" + _ "github.com/containerd/containerd/plugins/sandbox" _ "github.com/containerd/containerd/plugins/streaming" _ "github.com/containerd/containerd/plugins/transfer" _ "github.com/containerd/containerd/runtime/restart/monitor" diff --git a/integration/build_local_containerd_helper_test_linux.go b/integration/build_local_containerd_helper_test_linux.go index 956744cb1..6286c4bc6 100644 --- a/integration/build_local_containerd_helper_test_linux.go +++ b/integration/build_local_containerd_helper_test_linux.go @@ -18,6 +18,7 @@ package integration import ( // Register for linux platforms + _ "github.com/containerd/containerd/plugins/sandbox" // WithInMemoryServices will fail otherwise _ "github.com/containerd/containerd/runtime/v1/linux" _ "github.com/containerd/containerd/services/sandbox" // WithInMemoryServices will fail otherwise _ "github.com/containerd/containerd/snapshots/overlay/plugin" diff --git a/plugin/plugin.go b/plugin/plugin.go index 80dd2a263..0bc59d76f 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -86,6 +86,8 @@ const ( NRIApiPlugin Type = "io.containerd.nri.v1" // TransferPlugin implements a transfer service TransferPlugin Type = "io.containerd.transfer.v1" + // SandboxStorePlugin implements a sandbox store + SandboxStorePlugin Type = "io.containerd.sandbox.store.v1" ) const ( diff --git a/plugins/sandbox/store.go b/plugins/sandbox/store.go new file mode 100644 index 000000000..8b3df117f --- /dev/null +++ b/plugins/sandbox/store.go @@ -0,0 +1,40 @@ +/* + 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 ( + "github.com/containerd/containerd/metadata" + "github.com/containerd/containerd/plugin" +) + +func init() { + plugin.Register(&plugin.Registration{ + Type: plugin.SandboxStorePlugin, + ID: "local", + Requires: []plugin.Type{ + plugin.MetadataPlugin, + }, + InitFn: func(ic *plugin.InitContext) (interface{}, error) { + m, err := ic.Get(plugin.MetadataPlugin) + if err != nil { + return nil, err + } + + return metadata.NewSandboxStore(m.(*metadata.DB)), nil + }, + }) +} diff --git a/services.go b/services.go index 5b15ebc20..1c8efec15 100644 --- a/services.go +++ b/services.go @@ -166,9 +166,9 @@ func WithIntrospectionService(in introspection.Service) ServicesOpt { } // WithSandboxStore sets the sandbox store. -func WithSandboxStore(client sandboxapi.StoreClient) ServicesOpt { +func WithSandboxStore(client sandbox.Store) ServicesOpt { return func(s *services) { - s.sandboxStore = proxy.NewSandboxStore(client) + s.sandboxStore = client } } @@ -191,6 +191,9 @@ func WithInMemoryServices(ic *plugin.InitContext) ClientOpt { plugin.LeasePlugin: func(i interface{}) ServicesOpt { return WithLeasesService(i.(leases.Manager)) }, + plugin.SandboxStorePlugin: func(i interface{}) ServicesOpt { + return WithSandboxStore(i.(sandbox.Store)) + }, } { i, err := ic.Get(t) if err != nil { @@ -228,9 +231,6 @@ func WithInMemoryServices(ic *plugin.InitContext) ClientOpt { srv.IntrospectionService: func(s interface{}) ServicesOpt { return WithIntrospectionClient(s.(introspectionapi.IntrospectionClient)) }, - srv.SandboxStoreService: func(s interface{}) ServicesOpt { - return WithSandboxStore(s.(sandboxapi.StoreClient)) - }, srv.SandboxControllerService: func(s interface{}) ServicesOpt { return WithSandboxController(s.(sandboxapi.ControllerClient)) }, diff --git a/services/sandbox/controller_local.go b/services/sandbox/controller_local.go index 3a33aa5a9..506048acd 100644 --- a/services/sandbox/controller_local.go +++ b/services/sandbox/controller_local.go @@ -25,7 +25,6 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events" "github.com/containerd/containerd/events/exchange" - "github.com/containerd/containerd/metadata" "github.com/containerd/containerd/plugin" "github.com/containerd/containerd/runtime" v2 "github.com/containerd/containerd/runtime/v2" @@ -40,8 +39,8 @@ func init() { ID: services.SandboxControllerService, Requires: []plugin.Type{ plugin.RuntimePluginV2, - plugin.MetadataPlugin, plugin.EventPlugin, + plugin.SandboxStorePlugin, }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { shimPlugin, err := ic.GetByID(plugin.RuntimePluginV2, "shim") @@ -49,21 +48,19 @@ func init() { 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 } + storePlugin, err := ic.GetByID(plugin.SandboxStorePlugin, "local") + if err != nil { + return nil, err + } var ( shims = shimPlugin.(*v2.ShimManager) publisher = exchangePlugin.(*exchange.Exchange) - db = metadataPlugin.(*metadata.DB) - store = metadata.NewSandboxStore(db) + store = storePlugin.(sandbox.Store) ) return &controllerLocal{ diff --git a/services/sandbox/store_local.go b/services/sandbox/store_local.go deleted file mode 100644 index 553b47ebe..000000000 --- a/services/sandbox/store_local.go +++ /dev/null @@ -1,111 +0,0 @@ -/* - 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" - - "github.com/containerd/containerd/services" - "google.golang.org/grpc" - - api "github.com/containerd/containerd/api/services/sandbox/v1" - "github.com/containerd/containerd/api/types" - "github.com/containerd/containerd/errdefs" - "github.com/containerd/containerd/events" - "github.com/containerd/containerd/metadata" - "github.com/containerd/containerd/plugin" - "github.com/containerd/containerd/sandbox" -) - -func init() { - plugin.Register(&plugin.Registration{ - Type: plugin.ServicePlugin, - ID: services.SandboxStoreService, - Requires: []plugin.Type{ - plugin.MetadataPlugin, - }, - InitFn: func(ic *plugin.InitContext) (interface{}, error) { - m, err := ic.Get(plugin.MetadataPlugin) - if err != nil { - return nil, err - } - - db := m.(*metadata.DB) - return &sandboxLocal{ - store: metadata.NewSandboxStore(db), - publisher: ic.Events, - }, nil - }, - }) -} - -type sandboxLocal struct { - store sandbox.Store - publisher events.Publisher -} - -var _ = (api.StoreClient)(&sandboxLocal{}) - -func (s *sandboxLocal) Create(ctx context.Context, in *api.StoreCreateRequest, _ ...grpc.CallOption) (*api.StoreCreateResponse, error) { - sb, err := s.store.Create(ctx, sandbox.FromProto(in.Sandbox)) - if err != nil { - return nil, errdefs.ToGRPC(err) - } - - return &api.StoreCreateResponse{Sandbox: sandbox.ToProto(&sb)}, nil -} - -func (s *sandboxLocal) Update(ctx context.Context, in *api.StoreUpdateRequest, _ ...grpc.CallOption) (*api.StoreUpdateResponse, error) { - sb, err := s.store.Update(ctx, sandbox.FromProto(in.Sandbox), in.Fields...) - if err != nil { - return nil, errdefs.ToGRPC(err) - } - - return &api.StoreUpdateResponse{Sandbox: sandbox.ToProto(&sb)}, nil -} - -func (s *sandboxLocal) Get(ctx context.Context, in *api.StoreGetRequest, _ ...grpc.CallOption) (*api.StoreGetResponse, error) { - resp, err := s.store.Get(ctx, in.SandboxID) - if err != nil { - return nil, errdefs.ToGRPC(err) - } - - desc := sandbox.ToProto(&resp) - return &api.StoreGetResponse{Sandbox: desc}, nil -} - -func (s *sandboxLocal) List(ctx context.Context, in *api.StoreListRequest, _ ...grpc.CallOption) (*api.StoreListResponse, error) { - resp, err := s.store.List(ctx, in.Filters...) - if err != nil { - return nil, errdefs.ToGRPC(err) - } - - list := make([]*types.Sandbox, len(resp)) - for i := range resp { - list[i] = sandbox.ToProto(&resp[i]) - } - - return &api.StoreListResponse{List: list}, nil -} - -func (s *sandboxLocal) Delete(ctx context.Context, in *api.StoreDeleteRequest, _ ...grpc.CallOption) (*api.StoreDeleteResponse, error) { - if err := s.store.Delete(ctx, in.SandboxID); err != nil { - return nil, errdefs.ToGRPC(err) - } - - return &api.StoreDeleteResponse{}, nil -} diff --git a/services/sandbox/store_service.go b/services/sandbox/store_service.go index 00212c2dd..a5a8a9c3f 100644 --- a/services/sandbox/store_service.go +++ b/services/sandbox/store_service.go @@ -18,14 +18,15 @@ package sandbox import ( "context" - "errors" "google.golang.org/grpc" api "github.com/containerd/containerd/api/services/sandbox/v1" + "github.com/containerd/containerd/api/types" + "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" "github.com/containerd/containerd/plugin" - "github.com/containerd/containerd/services" + "github.com/containerd/containerd/sandbox" ) func init() { @@ -33,28 +34,21 @@ func init() { Type: plugin.GRPCPlugin, ID: "sandboxes", Requires: []plugin.Type{ - plugin.ServicePlugin, + plugin.SandboxStorePlugin, }, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - plugins, err := ic.GetByType(plugin.ServicePlugin) + sp, err := ic.GetByID(plugin.SandboxStorePlugin, "local") if err != nil { return nil, err } - p, ok := plugins[services.SandboxStoreService] - if !ok { - return nil, errors.New("sandbox store service not found") - } - i, err := p.Instance() - if err != nil { - return nil, err - } - return &sandboxService{local: i.(api.StoreClient)}, nil + + return &sandboxService{store: sp.(sandbox.Store)}, nil }, }) } type sandboxService struct { - local api.StoreClient + store sandbox.Store api.UnimplementedStoreServer } @@ -67,25 +61,57 @@ func (s *sandboxService) Register(server *grpc.Server) error { func (s *sandboxService) Create(ctx context.Context, req *api.StoreCreateRequest) (*api.StoreCreateResponse, error) { log.G(ctx).WithField("req", req).Debug("create sandbox") - return s.local.Create(ctx, req) + sb, err := s.store.Create(ctx, sandbox.FromProto(req.Sandbox)) + if err != nil { + return nil, errdefs.ToGRPC(err) + } + + return &api.StoreCreateResponse{Sandbox: sandbox.ToProto(&sb)}, nil } func (s *sandboxService) Update(ctx context.Context, req *api.StoreUpdateRequest) (*api.StoreUpdateResponse, error) { log.G(ctx).WithField("req", req).Debug("update sandbox") - return s.local.Update(ctx, req) + + sb, err := s.store.Update(ctx, sandbox.FromProto(req.Sandbox), req.Fields...) + if err != nil { + return nil, errdefs.ToGRPC(err) + } + + return &api.StoreUpdateResponse{Sandbox: sandbox.ToProto(&sb)}, nil } func (s *sandboxService) List(ctx context.Context, req *api.StoreListRequest) (*api.StoreListResponse, error) { log.G(ctx).WithField("req", req).Debug("list sandboxes") - return s.local.List(ctx, req) + + resp, err := s.store.List(ctx, req.Filters...) + if err != nil { + return nil, errdefs.ToGRPC(err) + } + + list := make([]*types.Sandbox, len(resp)) + for i := range resp { + list[i] = sandbox.ToProto(&resp[i]) + } + + return &api.StoreListResponse{List: list}, nil } func (s *sandboxService) Get(ctx context.Context, req *api.StoreGetRequest) (*api.StoreGetResponse, error) { log.G(ctx).WithField("req", req).Debug("get sandbox") - return s.local.Get(ctx, req) + resp, err := s.store.Get(ctx, req.SandboxID) + if err != nil { + return nil, errdefs.ToGRPC(err) + } + + desc := sandbox.ToProto(&resp) + return &api.StoreGetResponse{Sandbox: desc}, nil } func (s *sandboxService) Delete(ctx context.Context, req *api.StoreDeleteRequest) (*api.StoreDeleteResponse, error) { log.G(ctx).WithField("req", req).Debug("delete sandbox") - return s.local.Delete(ctx, req) + if err := s.store.Delete(ctx, req.SandboxID); err != nil { + return nil, errdefs.ToGRPC(err) + } + + return &api.StoreDeleteResponse{}, nil }