Skip to content

Commit

Permalink
Use config in service.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <lantaol@google.com>
  • Loading branch information
Random-Liu committed Sep 17, 2017
1 parent da31647 commit 71b0d0a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 63 deletions.
13 changes: 1 addition & 12 deletions cmd/cri-containerd/cri_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/sandbox_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/server/sandbox_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/sandbox_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
55 changes: 17 additions & 38 deletions pkg/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down

0 comments on commit 71b0d0a

Please sign in to comment.