k3s/vendor/github.com/containerd/cri/cri.go

193 lines
6.1 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
/*
2020-08-10 17:43:49 +00:00
Copyright The containerd Authors.
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
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
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
http://www.apache.org/licenses/LICENSE-2.0
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
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.
2019-01-12 04:58:27 +00:00
*/
package cri
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"
2020-08-10 17:43:49 +00:00
introspectionapi "github.com/containerd/containerd/api/services/introspection/v1"
2019-01-12 04:58:27 +00:00
"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/leases"
"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"
2020-08-10 17:43:49 +00:00
"k8s.io/klog/v2"
2019-01-12 04:58:27 +00:00
criconfig "github.com/containerd/cri/pkg/config"
"github.com/containerd/cri/pkg/constants"
2020-08-10 17:43:49 +00:00
criplatforms "github.com/containerd/cri/pkg/containerd/platforms"
2019-01-12 04:58:27 +00:00
"github.com/containerd/cri/pkg/server"
)
// TODO(random-liu): Use github.com/pkg/errors for our errors.
// Register CRI service plugin
func init() {
config := criconfig.DefaultConfig()
plugin.Register(&plugin.Registration{
Type: plugin.GRPCPlugin,
ID: "cri",
Config: &config,
Requires: []plugin.Type{
plugin.ServicePlugin,
},
InitFn: initCRIService,
})
}
func initCRIService(ic *plugin.InitContext) (interface{}, error) {
ic.Meta.Platforms = []imagespec.Platform{platforms.DefaultSpec()}
ic.Meta.Exports = map[string]string{"CRIVersion": constants.CRIVersion}
ctx := ic.Context
pluginConfig := ic.Config.(*criconfig.PluginConfig)
if err := criconfig.ValidatePluginConfig(ctx, pluginConfig); err != nil {
return nil, errors.Wrap(err, "invalid plugin config")
}
2019-01-12 04:58:27 +00:00
c := criconfig.Config{
PluginConfig: *pluginConfig,
ContainerdRootDir: filepath.Dir(ic.Root),
ContainerdEndpoint: ic.Address,
RootDir: ic.Root,
StateDir: ic.State,
}
log.G(ctx).Infof("Start cri plugin with config %+v", c)
if err := setGLogLevel(); err != nil {
return nil, errors.Wrap(err, "failed to set glog level")
}
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),
2020-08-10 17:43:49 +00:00
containerd.WithDefaultPlatform(criplatforms.Default()),
2019-01-12 04:58:27 +00:00
containerd.WithServices(servicesOpts...),
)
if err != nil {
return nil, errors.Wrap(err, "failed to create containerd client")
}
s, err := server.NewCRIService(c, client)
if err != nil {
return nil, errors.Wrap(err, "failed to create CRI service")
}
go func() {
if err := s.Run(); err != nil {
log.G(ctx).WithError(err).Fatal("Failed to run CRI service")
}
// TODO(random-liu): Whether and how we can stop containerd.
}()
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.Manager))
},
2020-08-10 17:43:49 +00:00
services.IntrospectionService: func(s interface{}) containerd.ServicesOpt {
return containerd.WithIntrospectionService(s.(introspectionapi.IntrospectionClient))
},
2019-01-12 04:58:27 +00:00
} {
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()
2019-09-27 21:51:53 +00:00
fs := flag.NewFlagSet("klog", flag.PanicOnError)
klog.InitFlags(fs)
if err := fs.Set("logtostderr", "true"); err != nil {
2019-01-12 04:58:27 +00:00
return err
}
switch l {
2020-08-10 17:43:49 +00:00
case logrus.TraceLevel:
2019-09-27 21:51:53 +00:00
return fs.Set("v", "5")
2019-01-12 04:58:27 +00:00
case logrus.DebugLevel:
2019-09-27 21:51:53 +00:00
return fs.Set("v", "4")
2019-01-12 04:58:27 +00:00
case logrus.InfoLevel:
2019-09-27 21:51:53 +00:00
return fs.Set("v", "2")
2019-01-12 04:58:27 +00:00
// glog doesn't support following filters. Defaults to v=0.
case logrus.WarnLevel:
case logrus.ErrorLevel:
case logrus.FatalLevel:
case logrus.PanicLevel:
}
return nil
}