fix labels in pod sandbox

Signed-off-by: Akhil Mohan <makhil@vmware.com>
This commit is contained in:
Akhil Mohan 2023-10-19 00:06:40 +05:30 committed by Akhil Mohan
parent 64c41162c3
commit e682da76ce
No known key found for this signature in database
GPG Key ID: CE237415BA2A2E78
5 changed files with 14 additions and 16 deletions

View File

@ -35,4 +35,6 @@ const (
ContainerKindContainer = "container"
// ContainerMetadataExtension is an extension name that identify metadata of container in CreateContainerRequest
ContainerMetadataExtension = criContainerdPrefix + ".container.metadata"
// SandboxMetadataExtension is an extension name that identify metadata of sandbox in CreateContainerRequest
SandboxMetadataExtension = criContainerdPrefix + ".sandbox.metadata"
)

View File

@ -28,6 +28,7 @@ import (
clabels "github.com/containerd/containerd/v2/labels"
"github.com/containerd/containerd/v2/oci"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
"github.com/containerd/log"
@ -43,14 +44,6 @@ const (
// directory of the sandbox, all files created for the sandbox will be
// placed under this directory.
sandboxesDir = "sandboxes"
// criContainerdPrefix is common prefix for cri-containerd
criContainerdPrefix = "io.cri-containerd"
// containerKindLabel is a label key indicating container is sandbox container or application container
containerKindLabel = criContainerdPrefix + ".kind"
// containerKindSandbox is a label value indicating container is sandbox container
containerKindSandbox = "sandbox"
// sandboxMetadataExtension is an extension name that identify metadata of sandbox in CreateContainerRequest
sandboxMetadataExtension = criContainerdPrefix + ".sandbox.metadata"
// MetadataKey is the key used for storing metadata in the sandbox extensions
MetadataKey = "metadata"
)
@ -117,7 +110,7 @@ func buildLabels(configLabels, imageConfigLabels map[string]string, containerTyp
for k, v := range configLabels {
labels[k] = v
}
labels[containerKindLabel] = containerType
labels[crilabels.ContainerKindLabel] = containerType
return labels
}

View File

@ -23,6 +23,7 @@ import (
"testing"
"github.com/containerd/containerd/v2/oci"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
docker "github.com/distribution/reference"
imagedigest "github.com/opencontainers/go-digest"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
@ -85,16 +86,16 @@ func TestBuildLabels(t *testing.T) {
"a": "b",
"c": "d",
}
newLabels := buildLabels(configLabels, imageConfigLabels, containerKindSandbox)
newLabels := buildLabels(configLabels, imageConfigLabels, crilabels.ContainerKindSandbox)
assert.Len(t, newLabels, 4)
assert.Equal(t, "b", newLabels["a"])
assert.Equal(t, "d", newLabels["c"])
assert.Equal(t, "y", newLabels["d"])
assert.Equal(t, containerKindSandbox, newLabels[containerKindLabel])
assert.Equal(t, crilabels.ContainerKindSandbox, newLabels[crilabels.ContainerKindLabel])
assert.NotContains(t, newLabels, "long-label")
newLabels["a"] = "e"
assert.Empty(t, configLabels[containerKindLabel], "should not add new labels into original label")
assert.Empty(t, configLabels[crilabels.ContainerKindLabel], "should not add new labels into original label")
assert.Equal(t, "b", configLabels["a"], "change in new labels should not affect original label")
}

View File

@ -28,6 +28,7 @@ import (
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/errdefs"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
"github.com/containerd/log"
@ -55,9 +56,9 @@ func (c *Controller) RecoverContainer(ctx context.Context, cntr containerd.Conta
if err != nil {
return sandbox, fmt.Errorf("failed to get sandbox container extensions: %w", err)
}
ext, ok := exts[sandboxMetadataExtension]
ext, ok := exts[crilabels.SandboxMetadataExtension]
if !ok {
return sandbox, fmt.Errorf("metadata extension %q not found", sandboxMetadataExtension)
return sandbox, fmt.Errorf("metadata extension %q not found", crilabels.SandboxMetadataExtension)
}
data, err := typeurl.UnmarshalAny(ext)
if err != nil {

View File

@ -34,6 +34,7 @@ import (
"github.com/containerd/containerd/v2/errdefs"
"github.com/containerd/containerd/v2/pkg/cri/annotations"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
customopts "github.com/containerd/containerd/v2/pkg/cri/opts"
sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
@ -133,7 +134,7 @@ func (c *Controller) Start(ctx context.Context, id string) (cin sandbox.Controll
return cin, fmt.Errorf("failed to generate sandbox container spec options: %w", err)
}
sandboxLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, containerKindSandbox)
sandboxLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindSandbox)
snapshotterOpt := []snapshots.Opt{snapshots.WithLabels(snapshots.FilterInheritedLabels(config.Annotations))}
extraSOpts, err := sandboxSnapshotterOpts(config)
@ -147,7 +148,7 @@ func (c *Controller) Start(ctx context.Context, id string) (cin sandbox.Controll
customopts.WithNewSnapshot(id, containerdImage, snapshotterOpt...),
containerd.WithSpec(spec, specOpts...),
containerd.WithContainerLabels(sandboxLabels),
containerd.WithContainerExtension(sandboxMetadataExtension, &metadata),
containerd.WithContainerExtension(crilabels.SandboxMetadataExtension, &metadata),
containerd.WithRuntime(ociRuntime.Type, sandboxInfo.Runtime.Options),
}