Use config in service.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2017-09-17 06:46:30 +00:00
parent da31647ef8
commit 71b0d0a043
9 changed files with 34 additions and 63 deletions

View File

@ -47,18 +47,7 @@ func main() {
} }
glog.V(2).Infof("Run cri-containerd grpc server on socket %q", o.SocketPath) glog.V(2).Infof("Run cri-containerd grpc server on socket %q", o.SocketPath)
s, err := server.NewCRIContainerdService( s, err := server.NewCRIContainerdService(o.Config)
o.SocketPath,
o.ContainerdEndpoint,
o.ContainerdSnapshotter,
o.RootDir,
o.NetworkPluginBinDir,
o.NetworkPluginConfDir,
o.StreamServerAddress,
o.StreamServerPort,
o.CgroupPath,
o.SandboxImage,
)
if err != nil { if err != nil {
glog.Exitf("Failed to create CRI containerd service %+v: %v", o, err) glog.Exitf("Failed to create CRI containerd service %+v: %v", o, err)
} }

View File

@ -104,7 +104,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C
} }
// Create container root directory. // Create container root directory.
containerRootDir := getContainerRootDir(c.rootDir, id) containerRootDir := getContainerRootDir(c.config.RootDir, id)
if err = c.os.MkdirAll(containerRootDir, 0755); err != nil { if err = c.os.MkdirAll(containerRootDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create container root directory %q: %v", return nil, fmt.Errorf("failed to create container root directory %q: %v",
containerRootDir, err) containerRootDir, err)
@ -124,7 +124,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C
volumeMounts := c.generateVolumeMounts(containerRootDir, config.GetMounts(), image.Config) volumeMounts := c.generateVolumeMounts(containerRootDir, config.GetMounts(), image.Config)
// Generate container runtime spec. // Generate container runtime spec.
mounts := c.generateContainerMounts(getSandboxRootDir(c.rootDir, sandboxID), config) mounts := c.generateContainerMounts(getSandboxRootDir(c.config.RootDir, sandboxID), config)
spec, err := c.generateContainerSpec(id, sandboxPid, config, sandboxConfig, image.Config, append(mounts, volumeMounts...)) spec, err := c.generateContainerSpec(id, sandboxPid, config, sandboxConfig, image.Config, append(mounts, volumeMounts...))
if err != nil { if err != nil {
@ -134,7 +134,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C
// Set snapshotter before any other options. // Set snapshotter before any other options.
opts := []containerd.NewContainerOpts{ opts := []containerd.NewContainerOpts{
containerd.WithSnapshotter(c.snapshotter), containerd.WithSnapshotter(c.config.ContainerdSnapshotter),
// Prepare container rootfs. This is always writeable even if // Prepare container rootfs. This is always writeable even if
// the container wants a readonly rootfs since we want to give // the container wants a readonly rootfs since we want to give
// the runtime (runc) a chance to modify (e.g. to create mount // the runtime (runc) a chance to modify (e.g. to create mount

View File

@ -63,7 +63,7 @@ func (c *criContainerdService) RemoveContainer(ctx context.Context, r *runtime.R
// kubelet implementation, we'll never start a container once we decide to remove it, // kubelet implementation, we'll never start a container once we decide to remove it,
// so we don't need the "Dead" state for now. // so we don't need the "Dead" state for now.
containerRootDir := getContainerRootDir(c.rootDir, id) containerRootDir := getContainerRootDir(c.config.RootDir, id)
if err := system.EnsureRemoveAll(containerRootDir); err != nil { if err := system.EnsureRemoveAll(containerRootDir); err != nil {
return nil, fmt.Errorf("failed to remove container root directory %q: %v", return nil, fmt.Errorf("failed to remove container root directory %q: %v",
containerRootDir, err) containerRootDir, err)

View File

@ -102,7 +102,7 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma
containerd.WithPullUnpack, containerd.WithPullUnpack,
containerd.WithSchema1Conversion, containerd.WithSchema1Conversion,
containerd.WithResolver(resolver), containerd.WithResolver(resolver),
containerd.WithPullSnapshotter(c.snapshotter), containerd.WithPullSnapshotter(c.config.ContainerdSnapshotter),
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to pull image %q: %v", ref, err) return nil, fmt.Errorf("failed to pull image %q: %v", ref, err)

View File

@ -73,7 +73,7 @@ func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.
} }
// Cleanup the sandbox root directory. // Cleanup the sandbox root directory.
sandboxRootDir := getSandboxRootDir(c.rootDir, id) sandboxRootDir := getSandboxRootDir(c.config.RootDir, id)
if err := system.EnsureRemoveAll(sandboxRootDir); err != nil { if err := system.EnsureRemoveAll(sandboxRootDir); err != nil {
return nil, fmt.Errorf("failed to remove sandbox root directory %q: %v", return nil, fmt.Errorf("failed to remove sandbox root directory %q: %v",
sandboxRootDir, err) sandboxRootDir, err)

View File

@ -65,9 +65,9 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run
} }
// Ensure sandbox container image snapshot. // Ensure sandbox container image snapshot.
image, err := c.ensureImageExists(ctx, c.sandboxImage) image, err := c.ensureImageExists(ctx, c.config.SandboxImage)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get sandbox image %q: %v", c.sandboxImage, err) return nil, fmt.Errorf("failed to get sandbox image %q: %v", c.config.SandboxImage, err)
} }
//Create Network Namespace if it is not in host network //Create Network Namespace if it is not in host network
hostNet := config.GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostNetwork() hostNet := config.GetLinux().GetSecurityContext().GetNamespaceOptions().GetHostNetwork()
@ -131,7 +131,7 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run
specOpts = append(specOpts, containerd.WithUserID(uint32(uid.GetValue()))) specOpts = append(specOpts, containerd.WithUserID(uint32(uid.GetValue())))
} }
opts := []containerd.NewContainerOpts{ opts := []containerd.NewContainerOpts{
containerd.WithSnapshotter(c.snapshotter), containerd.WithSnapshotter(c.config.ContainerdSnapshotter),
containerd.WithNewSnapshot(id, image.Image), containerd.WithNewSnapshot(id, image.Image),
containerd.WithSpec(spec, specOpts...), containerd.WithSpec(spec, specOpts...),
containerd.WithContainerLabels(labels), containerd.WithContainerLabels(labels),
@ -149,7 +149,7 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run
}() }()
// Create sandbox container root directory. // Create sandbox container root directory.
sandboxRootDir := getSandboxRootDir(c.rootDir, id) sandboxRootDir := getSandboxRootDir(c.config.RootDir, id)
if err := c.os.MkdirAll(sandboxRootDir, 0755); err != nil { if err := c.os.MkdirAll(sandboxRootDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create sandbox root directory %q: %v", return nil, fmt.Errorf("failed to create sandbox root directory %q: %v",
sandboxRootDir, err) sandboxRootDir, err)

View File

@ -84,7 +84,7 @@ func (c *criContainerdService) StopPodSandbox(ctx context.Context, r *runtime.St
glog.V(2).Infof("TearDown network for sandbox %q successfully", id) glog.V(2).Infof("TearDown network for sandbox %q successfully", id)
sandboxRoot := getSandboxRootDir(c.rootDir, id) sandboxRoot := getSandboxRootDir(c.config.RootDir, id)
if err := c.unmountSandboxFiles(sandboxRoot, sandbox.Config); err != nil { if err := c.unmountSandboxFiles(sandboxRoot, sandbox.Config); err != nil {
return nil, fmt.Errorf("failed to unmount sandbox files in %q: %v", sandboxRoot, err) return nil, fmt.Errorf("failed to unmount sandbox files in %q: %v", sandboxRoot, err)
} }

View File

@ -32,6 +32,7 @@ import (
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
"k8s.io/kubernetes/pkg/kubelet/server/streaming" "k8s.io/kubernetes/pkg/kubelet/server/streaming"
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
osinterface "github.com/kubernetes-incubator/cri-containerd/pkg/os" osinterface "github.com/kubernetes-incubator/cri-containerd/pkg/os"
"github.com/kubernetes-incubator/cri-containerd/pkg/registrar" "github.com/kubernetes-incubator/cri-containerd/pkg/registrar"
containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container" containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container"
@ -56,18 +57,12 @@ type CRIContainerdService interface {
// criContainerdService implements CRIContainerdService. // criContainerdService implements CRIContainerdService.
type criContainerdService struct { type criContainerdService struct {
// serverAddress is the grpc server unix path. // config contains all configurations.
serverAddress string config options.Config
// server is the grpc server. // server is the grpc server.
server *grpc.Server server *grpc.Server
// os is an interface for all required os operations. // os is an interface for all required os operations.
os osinterface.OS os osinterface.OS
// rootDir is the directory for managing cri-containerd files.
rootDir string
// sandboxImage is the image to use for sandbox container.
sandboxImage string
// snapshotter is the snapshotter to use in containerd.
snapshotter string
// sandboxStore stores all resources associated with sandboxes. // sandboxStore stores all resources associated with sandboxes.
sandboxStore *sandboxstore.Store sandboxStore *sandboxstore.Store
// sandboxNameIndex stores all sandbox names and make sure each name // sandboxNameIndex stores all sandbox names and make sure each name
@ -93,44 +88,29 @@ type criContainerdService struct {
client *containerd.Client client *containerd.Client
// streamServer is the streaming server serves container streaming request. // streamServer is the streaming server serves container streaming request.
streamServer streaming.Server streamServer streaming.Server
// cgroupPath in which the cri-containerd is placed in
cgroupPath string
// eventMonitor is the monitor monitors containerd events. // eventMonitor is the monitor monitors containerd events.
eventMonitor *eventMonitor eventMonitor *eventMonitor
} }
// NewCRIContainerdService returns a new instance of CRIContainerdService // NewCRIContainerdService returns a new instance of CRIContainerdService
// TODO(random-liu): Add cri-containerd server config to get rid of the long arg list. func NewCRIContainerdService(config options.Config) (CRIContainerdService, error) {
func NewCRIContainerdService(
serverAddress,
containerdEndpoint,
containerdSnapshotter,
rootDir,
networkPluginBinDir,
networkPluginConfDir,
streamAddress,
streamPort string,
cgroupPath string,
sandboxImage string) (CRIContainerdService, error) {
// TODO(random-liu): [P2] Recover from runtime state and checkpoint. // TODO(random-liu): [P2] Recover from runtime state and checkpoint.
client, err := containerd.New(containerdEndpoint, containerd.WithDefaultNamespace(k8sContainerdNamespace)) client, err := containerd.New(config.ContainerdEndpoint, containerd.WithDefaultNamespace(k8sContainerdNamespace))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize containerd client with endpoint %q: %v", containerdEndpoint, err) return nil, fmt.Errorf("failed to initialize containerd client with endpoint %q: %v",
config.ContainerdEndpoint, err)
} }
if cgroupPath != "" { if config.CgroupPath != "" {
_, err := loadCgroup(cgroupPath) _, err := loadCgroup(config.CgroupPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to load cgroup for cgroup path %v: %v", cgroupPath, err) return nil, fmt.Errorf("failed to load cgroup for cgroup path %v: %v", config.CgroupPath, err)
} }
} }
c := &criContainerdService{ c := &criContainerdService{
serverAddress: serverAddress, config: config,
os: osinterface.RealOS{}, os: osinterface.RealOS{},
rootDir: rootDir,
sandboxImage: sandboxImage,
snapshotter: containerdSnapshotter,
sandboxStore: sandboxstore.NewStore(), sandboxStore: sandboxstore.NewStore(),
containerStore: containerstore.NewStore(), containerStore: containerstore.NewStore(),
imageStore: imagestore.NewStore(), imageStore: imagestore.NewStore(),
@ -140,17 +120,16 @@ func NewCRIContainerdService(
imageStoreService: client.ImageService(), imageStoreService: client.ImageService(),
contentStoreService: client.ContentStore(), contentStoreService: client.ContentStore(),
client: client, client: client,
cgroupPath: cgroupPath,
} }
netPlugin, err := ocicni.InitCNI(networkPluginConfDir, networkPluginBinDir) netPlugin, err := ocicni.InitCNI(config.NetworkPluginConfDir, config.NetworkPluginBinDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize cni plugin: %v", err) return nil, fmt.Errorf("failed to initialize cni plugin: %v", err)
} }
c.netPlugin = netPlugin c.netPlugin = netPlugin
// prepare streaming server // prepare streaming server
c.streamServer, err = newStreamServer(c, streamAddress, streamPort) c.streamServer, err = newStreamServer(c, config.StreamServerAddress, config.StreamServerPort)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create stream server: %v", err) return nil, fmt.Errorf("failed to create stream server: %v", err)
} }
@ -187,13 +166,13 @@ func (c *criContainerdService) Run() error {
// Start grpc server. // Start grpc server.
// Unlink to cleanup the previous socket file. // Unlink to cleanup the previous socket file.
glog.V(2).Info("Start grpc server") glog.V(2).Info("Start grpc server")
err := syscall.Unlink(c.serverAddress) err := syscall.Unlink(c.config.SocketPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to unlink socket file %q: %v", c.serverAddress, err) return fmt.Errorf("failed to unlink socket file %q: %v", c.config.SocketPath, err)
} }
l, err := net.Listen(unixProtocol, c.serverAddress) l, err := net.Listen(unixProtocol, c.config.SocketPath)
if err != nil { if err != nil {
return fmt.Errorf("failed to listen on %q: %v", c.serverAddress, err) return fmt.Errorf("failed to listen on %q: %v", c.config.SocketPath, err)
} }
grpcServerCloseCh := make(chan struct{}) grpcServerCloseCh := make(chan struct{})
go func() { go func() {

View File

@ -17,6 +17,7 @@ limitations under the License.
package server package server
import ( import (
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing" ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing"
"github.com/kubernetes-incubator/cri-containerd/pkg/registrar" "github.com/kubernetes-incubator/cri-containerd/pkg/registrar"
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing" servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
@ -36,9 +37,11 @@ const (
// newTestCRIContainerdService creates a fake criContainerdService for test. // newTestCRIContainerdService creates a fake criContainerdService for test.
func newTestCRIContainerdService() *criContainerdService { func newTestCRIContainerdService() *criContainerdService {
return &criContainerdService{ return &criContainerdService{
config: options.Config{
RootDir: testRootDir,
SandboxImage: testSandboxImage,
},
os: ostesting.NewFakeOS(), os: ostesting.NewFakeOS(),
rootDir: testRootDir,
sandboxImage: testSandboxImage,
sandboxStore: sandboxstore.NewStore(), sandboxStore: sandboxstore.NewStore(),
imageStore: imagestore.NewStore(), imageStore: imagestore.NewStore(),
sandboxNameIndex: registrar.NewRegistrar(), sandboxNameIndex: registrar.NewRegistrar(),