From bd9e9ce56eee03900b0b7f1fa29c380008a17688 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Thu, 4 Jan 2018 02:43:00 +0000 Subject: [PATCH] Add containerd plugin mode. Signed-off-by: Lantao Liu --- cmd/cri-containerd/options/options.go | 10 ++-- cri.go | 70 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 cri.go diff --git a/cmd/cri-containerd/options/options.go b/cmd/cri-containerd/options/options.go index f9df285e3..c7d328e8a 100644 --- a/cmd/cri-containerd/options/options.go +++ b/cmd/cri-containerd/options/options.go @@ -116,7 +116,7 @@ func NewCRIContainerdOptions() *CRIContainerdOptions { // AddFlags adds cri-containerd command line options to pflag. func (c *CRIContainerdOptions) AddFlags(fs *pflag.FlagSet) { - defaults := defaultConfig() + defaults := DefaultConfig() fs.StringVar(&c.ConfigFilePath, configFilePathArgName, defaultConfigFilePath, "Path to the config file.") fs.StringVar(&c.SocketPath, "socket-path", @@ -192,7 +192,7 @@ func (c *CRIContainerdOptions) InitFlags(fs *pflag.FlagSet) error { // PrintDefaultTomlConfig print default toml config of cri-containerd. func PrintDefaultTomlConfig() { - if err := toml.NewEncoder(os.Stdout).Encode(defaultConfig()); err != nil { + if err := toml.NewEncoder(os.Stdout).Encode(DefaultConfig()); err != nil { fmt.Println(err) return } @@ -200,13 +200,13 @@ func PrintDefaultTomlConfig() { // AddGRPCFlags add flags for grpc connection. func AddGRPCFlags(fs *pflag.FlagSet) (*string, *time.Duration) { - endpoint := fs.String("endpoint", defaultConfig().SocketPath, "cri-containerd endpoint.") + endpoint := fs.String("endpoint", DefaultConfig().SocketPath, "cri-containerd endpoint.") timeout := fs.Duration("timeout", connectionTimeout, "cri-containerd connection timeout.") return endpoint, timeout } -// defaultConfig returns default configurations of cri-containerd. -func defaultConfig() Config { +// DefaultConfig returns default configurations of cri-containerd. +func DefaultConfig() Config { return Config{ ContainerdConfig: ContainerdConfig{ RootDir: "/var/lib/containerd", diff --git a/cri.go b/cri.go new file mode 100644 index 000000000..dbe853f55 --- /dev/null +++ b/cri.go @@ -0,0 +1,70 @@ +/* +Copyright 2018 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 cri + +import ( + "github.com/containerd/containerd/plugin" + "github.com/golang/glog" + + "github.com/containerd/cri-containerd/cmd/cri-containerd/options" + "github.com/containerd/cri-containerd/pkg/server" +) + +// Register CRI service plugin +func init() { + plugin.Register(&plugin.Registration{ + // In fact, cri is not strictly a GRPC plugin now. + Type: plugin.GRPCPlugin, + ID: "cri", + Requires: []plugin.Type{ + plugin.RuntimePlugin, + plugin.SnapshotPlugin, + plugin.TaskMonitorPlugin, + plugin.DiffPlugin, + plugin.MetadataPlugin, + plugin.ContentPlugin, + plugin.GCPlugin, + }, + InitFn: initCRIService, + }) +} + +func initCRIService(_ *plugin.InitContext) (interface{}, error) { + // TODO(random-liu): Support Config through Registration.Config. + // TODO(random-liu): Validate the configuration. + // TODO(random-liu): Leverage other fields in InitContext, such as Root. + // TODO(random-liu): Register GRPC service onto containerd GRPC server. + // TODO(random-liu): Separate cri plugin config from cri-containerd server config, + // because many options only make sense to cri-containerd server. + // TODO(random-liu): Change all glog to logrus. + // TODO(random-liu): Handle graceful stop. + c := options.DefaultConfig() + glog.V(0).Infof("Start cri plugin with config %+v", c) + // Use a goroutine to start cri service. The reason is that currently + // cri service requires containerd to be running. + // TODO(random-liu): Resolve the circular dependency. + go func() { + s, err := server.NewCRIContainerdService(c) + if err != nil { + glog.Exitf("Failed to create CRI service: %v", err) + } + if err := s.Run(); err != nil { + glog.Exitf("Failed to run CRI grpc server: %v", err) + } + }() + return nil, nil +}