From cd5886d64773f2d8b21b86fc4722d4f3a5165976 Mon Sep 17 00:00:00 2001 From: abhi Date: Sun, 5 Nov 2017 17:25:44 -0800 Subject: [PATCH] Adding kube pod and container labels to containerd Currently we have the pod and container labels part of containerd metadata extensions. However for third party users like cadvisor that depend on standard kube labels will need to be aware of the way metadata is stored in containerd to fetch the labels. Signed-off-by: abhi --- pkg/server/container_create.go | 3 ++- pkg/server/helpers.go | 10 ++++++++++ pkg/server/sandbox_run.go | 4 +++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/server/container_create.go b/pkg/server/container_create.go index f1ea31377..78e573cab 100644 --- a/pkg/server/container_create.go +++ b/pkg/server/container_create.go @@ -223,6 +223,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C if seccompSpecOpts != nil { specOpts = append(specOpts, seccompSpecOpts) } + containerLabels := buildLabels(config.Labels, containerKindContainer) opts = append(opts, containerd.WithSpec(spec, specOpts...), @@ -232,7 +233,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C Runtime: c.config.ContainerdConfig.RuntimeEngine, RuntimeRoot: c.config.ContainerdConfig.RuntimeRoot, SystemdCgroup: c.config.SystemdCgroup}), // TODO (mikebrow): add CriuPath when we add support for pause - containerd.WithContainerLabels(map[string]string{containerKindLabel: containerKindContainer}), + containerd.WithContainerLabels(containerLabels), containerd.WithContainerExtension(containerMetadataExtension, &meta)) var cntr containerd.Container if cntr, err = c.client.NewContainer(ctx, id, opts...); err != nil { diff --git a/pkg/server/helpers.go b/pkg/server/helpers.go index 6079df0fe..cbf3d887e 100644 --- a/pkg/server/helpers.go +++ b/pkg/server/helpers.go @@ -381,3 +381,13 @@ func isInCRIMounts(dst string, mounts []*runtime.Mount) bool { func filterLabel(k, v string) string { return fmt.Sprintf("labels.%q==%q", k, v) } + +// buildLabel builds the labels from config to be passed to containerd +func buildLabels(configLabels map[string]string, containerType string) map[string]string { + labels := make(map[string]string) + for k, v := range configLabels { + labels[k] = v + } + labels[containerKindLabel] = containerType + return labels +} diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index 7785487f6..9c7e977a6 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -143,12 +143,14 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run specOpts = append(specOpts, seccompSpecOpts) } + sandboxLabels := buildLabels(config.Labels, containerKindSandbox) + opts := []containerd.NewContainerOpts{ containerd.WithSnapshotter(c.config.ContainerdConfig.Snapshotter), customopts.WithImageUnpack(image.Image), containerd.WithNewSnapshot(id, image.Image), containerd.WithSpec(spec, specOpts...), - containerd.WithContainerLabels(map[string]string{containerKindLabel: containerKindSandbox}), + containerd.WithContainerLabels(sandboxLabels), containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata), containerd.WithRuntime( c.config.ContainerdConfig.Runtime,