Add sandbox store plugin type

Moves the sandbox store plugin under the plugins packages and adds a
unique plugin type for other plugins to depend on it.
Updates the sandbox controller plugin to depend on the sandbox store
plugin.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2022-12-20 17:46:38 -08:00
parent dd5605e444
commit 47fee791f6
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
8 changed files with 100 additions and 144 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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 (

40
plugins/sandbox/store.go Normal file
View File

@ -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
},
})
}

View File

@ -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))
},

View File

@ -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{

View File

@ -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
}

View File

@ -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
}