Move CRI image service into a separate plugin
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> Signed-off-by: Abel Feng <fshb1988@gmail.com>
This commit is contained in:
committed by
Abel Feng
parent
349c8d12c8
commit
e15c246550
121
pkg/cri/server/base/cri_base.go
Normal file
121
pkg/cri/server/base/cri_base.go
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
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 base
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/containerd/log"
|
||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
||||
"github.com/containerd/containerd/pkg/cri/constants"
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/containerd/containerd/plugin"
|
||||
"github.com/containerd/containerd/plugin/registry"
|
||||
"github.com/containerd/containerd/plugins"
|
||||
)
|
||||
|
||||
// CRIBase contains common dependencies for CRI's runtime, image, and podsandbox services.
|
||||
type CRIBase struct {
|
||||
// Config contains all configurations.
|
||||
Config criconfig.Config
|
||||
// Client is an instance of the containerd client
|
||||
Client *containerd.Client
|
||||
}
|
||||
|
||||
func init() {
|
||||
config := criconfig.DefaultConfig()
|
||||
|
||||
// Base plugin that other CRI services depend on.
|
||||
registry.Register(&plugin.Registration{
|
||||
Type: plugins.GRPCPlugin,
|
||||
ID: "cri",
|
||||
Config: &config,
|
||||
Requires: []plugin.Type{
|
||||
plugins.EventPlugin,
|
||||
plugins.ServicePlugin,
|
||||
plugins.NRIApiPlugin,
|
||||
},
|
||||
InitFn: initCRIBase,
|
||||
})
|
||||
}
|
||||
|
||||
func initCRIBase(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, fmt.Errorf("invalid plugin config: %w", err)
|
||||
}
|
||||
|
||||
c := criconfig.Config{
|
||||
PluginConfig: *pluginConfig,
|
||||
ContainerdRootDir: filepath.Dir(ic.Properties[plugins.PropertyRootDir]),
|
||||
ContainerdEndpoint: ic.Properties[plugins.PropertyGRPCAddress],
|
||||
RootDir: ic.Properties[plugins.PropertyRootDir],
|
||||
StateDir: ic.Properties[plugins.PropertyStateDir],
|
||||
}
|
||||
|
||||
log.G(ctx).Infof("Start cri plugin with config %+v", c)
|
||||
|
||||
if err := setGLogLevel(); err != nil {
|
||||
return nil, fmt.Errorf("failed to set glog level: %w", err)
|
||||
}
|
||||
|
||||
log.G(ctx).Info("Connect containerd service")
|
||||
client, err := containerd.New(
|
||||
"",
|
||||
containerd.WithDefaultNamespace(constants.K8sContainerdNamespace),
|
||||
containerd.WithDefaultPlatform(platforms.Default()),
|
||||
containerd.WithInMemoryServices(ic),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create containerd client: %w", err)
|
||||
}
|
||||
|
||||
return &CRIBase{
|
||||
Config: c,
|
||||
Client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Set glog level.
|
||||
func setGLogLevel() error {
|
||||
l := log.GetLevel()
|
||||
fs := flag.NewFlagSet("klog", flag.PanicOnError)
|
||||
klog.InitFlags(fs)
|
||||
if err := fs.Set("logtostderr", "true"); err != nil {
|
||||
return err
|
||||
}
|
||||
switch l {
|
||||
case log.TraceLevel:
|
||||
return fs.Set("v", "5")
|
||||
case log.DebugLevel:
|
||||
return fs.Set("v", "4")
|
||||
case log.InfoLevel:
|
||||
return fs.Set("v", "2")
|
||||
default:
|
||||
// glog doesn't support other filters. Defaults to v=0.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user