From d2757cb8f94bff588b0cdd9f9c6b0b5ec26c6204 Mon Sep 17 00:00:00 2001 From: yanxuean Date: Wed, 23 Aug 2017 15:07:19 +0800 Subject: [PATCH] Checkpoint and restart recovery fix part of #120 Signed-off-by: yanxuean --- pkg/server/container_create.go | 13 ++++++++++++- pkg/server/helpers.go | 7 +++++++ pkg/server/sandbox_run.go | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/server/container_create.go b/pkg/server/container_create.go index 4859e0435..45017e9b8 100644 --- a/pkg/server/container_create.go +++ b/pkg/server/container_create.go @@ -118,7 +118,18 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C } }() - opts = append(opts, containerd.WithSpec(spec), containerd.WithRuntime(defaultRuntime)) + metaBytes, err := meta.Encode() + if err != nil { + return nil, fmt.Errorf("failed to convert sandbox metadata: %+v, %v", meta, err) + } + labels := map[string]string{ + containerMetadataLabel: string(metaBytes), + } + + opts = append(opts, + containerd.WithSpec(spec), + containerd.WithRuntime(defaultRuntime), + containerd.WithContainerLabels(labels)) var cntr containerd.Container if cntr, err = c.client.NewContainer(ctx, id, opts...); err != nil { return nil, fmt.Errorf("failed to create containerd container: %v", err) diff --git a/pkg/server/helpers.go b/pkg/server/helpers.go index 465dcc4b5..d43851297 100644 --- a/pkg/server/helpers.go +++ b/pkg/server/helpers.go @@ -95,6 +95,13 @@ const ( resolvConfPath = "/etc/resolv.conf" ) +const ( + // sandboxMetadataLabel is label name that identify metadata of sandbox in CreateContainerRequest + sandboxMetadataLabel = "io.cri-containerd.sandbox.metadata" + // sandboxMetadataLabel is label name that identify metadata of container in CreateContainerRequest + containerMetadataLabel = "io.cri-containerd.container.metadata" +) + // generateID generates a random unique id. func generateID() string { return stringid.GenerateNonCryptoID() diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index 85531ffd2..d32b687bb 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -84,10 +84,26 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run return nil, fmt.Errorf("failed to generate sandbox container spec: %v", err) } glog.V(4).Infof("Sandbox container spec: %+v", spec) - // TODO(random-liu): Checkpoint metadata into container labels. + + // Checkpoint metadata into container + // TODO(random-liu): Switch to extensions(after merge containerd #1378). + // Actually the Pid, NetNS and CreatedAt underlying are not included here. + // I'm fine with this for now. After this PR is merged, we could: + // 1.Get Pid from containerd, so that we don't need to checkpoint it ourselves; + // 2.Use permanent NetNS after #138 is merged, so that we'll have network namespace here; + // 3.Get CreatedAt from containerd, which have been checkpointed by containerd + // https://github.com/containerd/containerd/blob/master/containers/containers.go#L14. + metaBytes, err := sandbox.Metadata.Encode() + if err != nil { + return nil, fmt.Errorf("failed to convert sandbox metadata: %+v, %v", sandbox.Metadata, err) + } + labels := map[string]string{ + sandboxMetadataLabel: string(metaBytes), + } opts := []containerd.NewContainerOpts{ containerd.WithSpec(spec), + containerd.WithContainerLabels(labels), containerd.WithRuntime(defaultRuntime), containerd.WithNewSnapshotView(id, image.Image)} container, err := c.client.NewContainer(ctx, id, opts...)