Use direct function call.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
95
cri.go
95
cri.go
@@ -20,20 +20,28 @@ import (
|
||||
"flag"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/api/services/containers/v1"
|
||||
"github.com/containerd/containerd/api/services/diff/v1"
|
||||
"github.com/containerd/containerd/api/services/images/v1"
|
||||
"github.com/containerd/containerd/api/services/leases/v1"
|
||||
"github.com/containerd/containerd/api/services/namespaces/v1"
|
||||
"github.com/containerd/containerd/api/services/tasks/v1"
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/containerd/containerd/plugin"
|
||||
"github.com/containerd/containerd/services"
|
||||
"github.com/containerd/containerd/snapshots"
|
||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
criconfig "github.com/containerd/cri-containerd/pkg/config"
|
||||
"github.com/containerd/cri-containerd/pkg/constants"
|
||||
"github.com/containerd/cri-containerd/pkg/server"
|
||||
)
|
||||
|
||||
// criVersion is the CRI version supported by the CRI plugin.
|
||||
const criVersion = "v1alpha2"
|
||||
|
||||
// TODO(random-liu): Use github.com/pkg/errors for our errors.
|
||||
// Register CRI service plugin
|
||||
func init() {
|
||||
@@ -43,13 +51,7 @@ func init() {
|
||||
ID: "cri",
|
||||
Config: &config,
|
||||
Requires: []plugin.Type{
|
||||
plugin.RuntimePlugin,
|
||||
plugin.SnapshotPlugin,
|
||||
plugin.TaskMonitorPlugin,
|
||||
plugin.DiffPlugin,
|
||||
plugin.MetadataPlugin,
|
||||
plugin.ContentPlugin,
|
||||
plugin.GCPlugin,
|
||||
plugin.ServicePlugin,
|
||||
},
|
||||
InitFn: initCRIService,
|
||||
})
|
||||
@@ -57,7 +59,7 @@ func init() {
|
||||
|
||||
func initCRIService(ic *plugin.InitContext) (interface{}, error) {
|
||||
ic.Meta.Platforms = []imagespec.Platform{platforms.DefaultSpec()}
|
||||
ic.Meta.Exports = map[string]string{"CRIVersion": criVersion}
|
||||
ic.Meta.Exports = map[string]string{"CRIVersion": constants.CRIVersion}
|
||||
ctx := ic.Context
|
||||
pluginConfig := ic.Config.(*criconfig.PluginConfig)
|
||||
c := criconfig.Config{
|
||||
@@ -75,13 +77,26 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
|
||||
return nil, errors.Wrap(err, "failed to set glog level")
|
||||
}
|
||||
|
||||
s, err := server.NewCRIContainerdService(c)
|
||||
servicesOpts, err := getServicesOpts(ic)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get services")
|
||||
}
|
||||
|
||||
log.G(ctx).Info("Connect containerd service")
|
||||
client, err := containerd.New(
|
||||
"",
|
||||
containerd.WithDefaultNamespace(constants.K8sContainerdNamespace),
|
||||
containerd.WithServices(servicesOpts...),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create containerd client")
|
||||
}
|
||||
|
||||
s, err := server.NewCRIContainerdService(c, client)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create CRI service")
|
||||
}
|
||||
|
||||
// Use a goroutine to initialize cri service. The reason is that currently
|
||||
// cri service requires containerd to be initialize.
|
||||
go func() {
|
||||
if err := s.Run(); err != nil {
|
||||
log.G(ctx).WithError(err).Fatal("Failed to run CRI service")
|
||||
@@ -91,6 +106,58 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// getServicesOpts get service options from plugin context.
|
||||
func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
|
||||
plugins, err := ic.GetByType(plugin.ServicePlugin)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get service plugin")
|
||||
}
|
||||
|
||||
opts := []containerd.ServicesOpt{
|
||||
containerd.WithEventService(ic.Events),
|
||||
}
|
||||
for s, fn := range map[string]func(interface{}) containerd.ServicesOpt{
|
||||
services.ContentService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithContentStore(s.(content.Store))
|
||||
},
|
||||
services.ImagesService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithImageService(s.(images.ImagesClient))
|
||||
},
|
||||
services.SnapshotsService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithSnapshotters(s.(map[string]snapshots.Snapshotter))
|
||||
},
|
||||
services.ContainersService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithContainerService(s.(containers.ContainersClient))
|
||||
},
|
||||
services.TasksService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithTaskService(s.(tasks.TasksClient))
|
||||
},
|
||||
services.DiffService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithDiffService(s.(diff.DiffClient))
|
||||
},
|
||||
services.NamespacesService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithNamespaceService(s.(namespaces.NamespacesClient))
|
||||
},
|
||||
services.LeasesService: func(s interface{}) containerd.ServicesOpt {
|
||||
return containerd.WithLeasesService(s.(leases.LeasesClient))
|
||||
},
|
||||
} {
|
||||
p := plugins[s]
|
||||
if p == nil {
|
||||
return nil, errors.Errorf("service %q not found", s)
|
||||
}
|
||||
i, err := p.Instance()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to get instance of service %q", s)
|
||||
}
|
||||
if i == nil {
|
||||
return nil, errors.Errorf("instance of service %q not found", s)
|
||||
}
|
||||
opts = append(opts, fn(i))
|
||||
}
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
// Set glog level.
|
||||
func setGLogLevel() error {
|
||||
l := logrus.GetLevel()
|
||||
|
Reference in New Issue
Block a user