diff --git a/cmd/cri-containerd/cri_containerd.go b/cmd/cri-containerd/cri_containerd.go index 782b1a188..002acb1ef 100644 --- a/cmd/cri-containerd/cri_containerd.go +++ b/cmd/cri-containerd/cri_containerd.go @@ -47,18 +47,7 @@ func main() { } glog.V(2).Infof("Run cri-containerd grpc server on socket %q", o.SocketPath) - s, err := server.NewCRIContainerdService( - o.SocketPath, - o.ContainerdEndpoint, - o.ContainerdSnapshotter, - o.RootDir, - o.NetworkPluginBinDir, - o.NetworkPluginConfDir, - o.StreamServerAddress, - o.StreamServerPort, - o.CgroupPath, - o.SandboxImage, - ) + s, err := server.NewCRIContainerdService(o.Config) if err != nil { glog.Exitf("Failed to create CRI containerd service %+v: %v", o, err) } diff --git a/pkg/server/container_create.go b/pkg/server/container_create.go index 427501339..e980e21e5 100644 --- a/pkg/server/container_create.go +++ b/pkg/server/container_create.go @@ -104,7 +104,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C } // Create container root directory. - containerRootDir := getContainerRootDir(c.rootDir, id) + containerRootDir := getContainerRootDir(c.config.RootDir, id) if err = c.os.MkdirAll(containerRootDir, 0755); err != nil { return nil, fmt.Errorf("failed to create container root directory %q: %v", containerRootDir, err) @@ -124,7 +124,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C volumeMounts := c.generateVolumeMounts(containerRootDir, config.GetMounts(), image.Config) // 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...)) if err != nil { @@ -134,7 +134,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C // Set snapshotter before any other options. opts := []containerd.NewContainerOpts{ - containerd.WithSnapshotter(c.snapshotter), + containerd.WithSnapshotter(c.config.ContainerdSnapshotter), // Prepare container rootfs. This is always writeable even if // the container wants a readonly rootfs since we want to give // the runtime (runc) a chance to modify (e.g. to create mount diff --git a/pkg/server/container_remove.go b/pkg/server/container_remove.go index e6d737378..7caa54bda 100644 --- a/pkg/server/container_remove.go +++ b/pkg/server/container_remove.go @@ -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, // 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 { return nil, fmt.Errorf("failed to remove container root directory %q: %v", containerRootDir, err) diff --git a/pkg/server/image_pull.go b/pkg/server/image_pull.go index 345a31643..78bb541c0 100644 --- a/pkg/server/image_pull.go +++ b/pkg/server/image_pull.go @@ -102,7 +102,7 @@ func (c *criContainerdService) PullImage(ctx context.Context, r *runtime.PullIma containerd.WithPullUnpack, containerd.WithSchema1Conversion, containerd.WithResolver(resolver), - containerd.WithPullSnapshotter(c.snapshotter), + containerd.WithPullSnapshotter(c.config.ContainerdSnapshotter), ) if err != nil { return nil, fmt.Errorf("failed to pull image %q: %v", ref, err) diff --git a/pkg/server/sandbox_remove.go b/pkg/server/sandbox_remove.go index 23057957e..fb21a723f 100644 --- a/pkg/server/sandbox_remove.go +++ b/pkg/server/sandbox_remove.go @@ -73,7 +73,7 @@ func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime. } // Cleanup the sandbox root directory. - sandboxRootDir := getSandboxRootDir(c.rootDir, id) + sandboxRootDir := getSandboxRootDir(c.config.RootDir, id) if err := system.EnsureRemoveAll(sandboxRootDir); err != nil { return nil, fmt.Errorf("failed to remove sandbox root directory %q: %v", sandboxRootDir, err) diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index c36d15121..4c13710a9 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -65,9 +65,9 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run } // Ensure sandbox container image snapshot. - image, err := c.ensureImageExists(ctx, c.sandboxImage) + image, err := c.ensureImageExists(ctx, c.config.SandboxImage) 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 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()))) } opts := []containerd.NewContainerOpts{ - containerd.WithSnapshotter(c.snapshotter), + containerd.WithSnapshotter(c.config.ContainerdSnapshotter), containerd.WithNewSnapshot(id, image.Image), containerd.WithSpec(spec, specOpts...), containerd.WithContainerLabels(labels), @@ -149,7 +149,7 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run }() // 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 { return nil, fmt.Errorf("failed to create sandbox root directory %q: %v", sandboxRootDir, err) diff --git a/pkg/server/sandbox_stop.go b/pkg/server/sandbox_stop.go index cd56aebe7..fc9865b23 100644 --- a/pkg/server/sandbox_stop.go +++ b/pkg/server/sandbox_stop.go @@ -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) - sandboxRoot := getSandboxRootDir(c.rootDir, id) + sandboxRoot := getSandboxRootDir(c.config.RootDir, id) if err := c.unmountSandboxFiles(sandboxRoot, sandbox.Config); err != nil { return nil, fmt.Errorf("failed to unmount sandbox files in %q: %v", sandboxRoot, err) } diff --git a/pkg/server/service.go b/pkg/server/service.go index d9c5820d9..0a40d29d2 100644 --- a/pkg/server/service.go +++ b/pkg/server/service.go @@ -32,6 +32,7 @@ import ( "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" "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" "github.com/kubernetes-incubator/cri-containerd/pkg/registrar" containerstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/container" @@ -56,18 +57,12 @@ type CRIContainerdService interface { // criContainerdService implements CRIContainerdService. type criContainerdService struct { - // serverAddress is the grpc server unix path. - serverAddress string + // config contains all configurations. + config options.Config // server is the grpc server. server *grpc.Server // os is an interface for all required os operations. 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 *sandboxstore.Store // sandboxNameIndex stores all sandbox names and make sure each name @@ -93,44 +88,29 @@ type criContainerdService struct { client *containerd.Client // streamServer is the streaming server serves container streaming request. streamServer streaming.Server - // cgroupPath in which the cri-containerd is placed in - cgroupPath string // eventMonitor is the monitor monitors containerd events. eventMonitor *eventMonitor } // 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( - serverAddress, - containerdEndpoint, - containerdSnapshotter, - rootDir, - networkPluginBinDir, - networkPluginConfDir, - streamAddress, - streamPort string, - cgroupPath string, - sandboxImage string) (CRIContainerdService, error) { +func NewCRIContainerdService(config options.Config) (CRIContainerdService, error) { // 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 { - 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 != "" { - _, err := loadCgroup(cgroupPath) + if config.CgroupPath != "" { + _, err := loadCgroup(config.CgroupPath) 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{ - serverAddress: serverAddress, + config: config, os: osinterface.RealOS{}, - rootDir: rootDir, - sandboxImage: sandboxImage, - snapshotter: containerdSnapshotter, sandboxStore: sandboxstore.NewStore(), containerStore: containerstore.NewStore(), imageStore: imagestore.NewStore(), @@ -140,17 +120,16 @@ func NewCRIContainerdService( imageStoreService: client.ImageService(), contentStoreService: client.ContentStore(), client: client, - cgroupPath: cgroupPath, } - netPlugin, err := ocicni.InitCNI(networkPluginConfDir, networkPluginBinDir) + netPlugin, err := ocicni.InitCNI(config.NetworkPluginConfDir, config.NetworkPluginBinDir) if err != nil { return nil, fmt.Errorf("failed to initialize cni plugin: %v", err) } c.netPlugin = netPlugin // prepare streaming server - c.streamServer, err = newStreamServer(c, streamAddress, streamPort) + c.streamServer, err = newStreamServer(c, config.StreamServerAddress, config.StreamServerPort) if err != nil { return nil, fmt.Errorf("failed to create stream server: %v", err) } @@ -187,13 +166,13 @@ func (c *criContainerdService) Run() error { // Start grpc server. // Unlink to cleanup the previous socket file. glog.V(2).Info("Start grpc server") - err := syscall.Unlink(c.serverAddress) + err := syscall.Unlink(c.config.SocketPath) 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 { - 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{}) go func() { diff --git a/pkg/server/service_test.go b/pkg/server/service_test.go index 5ad55b88d..75cff1d23 100644 --- a/pkg/server/service_test.go +++ b/pkg/server/service_test.go @@ -17,6 +17,7 @@ limitations under the License. package server import ( + "github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options" ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing" "github.com/kubernetes-incubator/cri-containerd/pkg/registrar" servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing" @@ -36,9 +37,11 @@ const ( // newTestCRIContainerdService creates a fake criContainerdService for test. func newTestCRIContainerdService() *criContainerdService { return &criContainerdService{ + config: options.Config{ + RootDir: testRootDir, + SandboxImage: testSandboxImage, + }, os: ostesting.NewFakeOS(), - rootDir: testRootDir, - sandboxImage: testSandboxImage, sandboxStore: sandboxstore.NewStore(), imageStore: imagestore.NewStore(), sandboxNameIndex: registrar.NewRegistrar(),