diff --git a/internal/cri/server/container_create.go b/internal/cri/server/container_create.go index 1a187b99c..e7e56f163 100644 --- a/internal/cri/server/container_create.go +++ b/internal/cri/server/container_create.go @@ -287,7 +287,7 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta return nil, fmt.Errorf("failed to get container spec opts: %w", err) } - containerLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindContainer) + containerLabels := util.BuildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindContainer) // TODO the sandbox in the cache should hold this info runtimeName, runtimeOption, err := c.runtimeInfo(ctx, sandboxID) diff --git a/internal/cri/server/helpers.go b/internal/cri/server/helpers.go index c53d6c9c8..afdd87c4c 100644 --- a/internal/cri/server/helpers.go +++ b/internal/cri/server/helpers.go @@ -33,10 +33,8 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/core/containers" - crilabels "github.com/containerd/containerd/v2/internal/cri/labels" containerstore "github.com/containerd/containerd/v2/internal/cri/store/container" imagestore "github.com/containerd/containerd/v2/internal/cri/store/image" - clabels "github.com/containerd/containerd/v2/pkg/labels" "github.com/containerd/errdefs" "github.com/containerd/log" ) @@ -220,27 +218,6 @@ 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, imageConfigLabels map[string]string, containerType string) map[string]string { - labels := make(map[string]string) - - for k, v := range imageConfigLabels { - if err := clabels.Validate(k, v); err == nil { - labels[k] = v - } else { - // In case the image label is invalid, we output a warning and skip adding it to the - // container. - log.L.WithError(err).Warnf("unable to add image label with key %s to the container", k) - } - } - // labels from the CRI request (config) will override labels in the image config - for k, v := range configLabels { - labels[k] = v - } - labels[crilabels.ContainerKindLabel] = containerType - return labels -} - // getRuntimeOptions get runtime options from container metadata. func getRuntimeOptions(c containers.Container) (interface{}, error) { from := c.Runtime.Options diff --git a/internal/cri/server/helpers_test.go b/internal/cri/server/helpers_test.go index 53865decd..b676e7588 100644 --- a/internal/cri/server/helpers_test.go +++ b/internal/cri/server/helpers_test.go @@ -20,7 +20,6 @@ import ( "context" "os" goruntime "runtime" - "strings" "testing" "time" @@ -29,7 +28,6 @@ import ( runcoptions "github.com/containerd/containerd/api/types/runc/options" "github.com/containerd/containerd/v2/core/containers" criconfig "github.com/containerd/containerd/v2/internal/cri/config" - crilabels "github.com/containerd/containerd/v2/internal/cri/labels" containerstore "github.com/containerd/containerd/v2/internal/cri/store/container" "github.com/containerd/containerd/v2/pkg/oci" "github.com/containerd/containerd/v2/pkg/protobuf/types" @@ -90,29 +88,6 @@ func TestGetUserFromImage(t *testing.T) { } } -func TestBuildLabels(t *testing.T) { - imageConfigLabels := map[string]string{ - "a": "z", - "d": "y", - "long-label": strings.Repeat("example", 10000), - } - configLabels := map[string]string{ - "a": "b", - "c": "d", - } - 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, crilabels.ContainerKindSandbox, newLabels[crilabels.ContainerKindLabel]) - assert.NotContains(t, newLabels, "long-label") - - newLabels["a"] = "e" - 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") -} - func TestGenerateRuntimeOptions(t *testing.T) { nilOpts := ` systemd_cgroup = true diff --git a/internal/cri/server/podsandbox/helpers.go b/internal/cri/server/podsandbox/helpers.go index 0a17b1aff..c960270b9 100644 --- a/internal/cri/server/podsandbox/helpers.go +++ b/internal/cri/server/podsandbox/helpers.go @@ -21,7 +21,6 @@ import ( "fmt" "path/filepath" - "github.com/containerd/log" "github.com/containerd/typeurl/v2" runtimespec "github.com/opencontainers/runtime-spec/specs-go" @@ -31,7 +30,6 @@ import ( imagestore "github.com/containerd/containerd/v2/internal/cri/store/image" sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox" ctrdutil "github.com/containerd/containerd/v2/internal/cri/util" - clabels "github.com/containerd/containerd/v2/pkg/labels" "github.com/containerd/containerd/v2/pkg/oci" ) @@ -71,27 +69,6 @@ func (c *Controller) toContainerdImage(ctx context.Context, image imagestore.Ima return c.client.GetImage(ctx, image.References[0]) } -// buildLabel builds the labels from config to be passed to containerd -func buildLabels(configLabels, imageConfigLabels map[string]string, containerType string) map[string]string { - labels := make(map[string]string) - - for k, v := range imageConfigLabels { - if err := clabels.Validate(k, v); err == nil { - labels[k] = v - } else { - // In case the image label is invalid, we output a warning and skip adding it to the - // container. - log.L.WithError(err).Warnf("unable to add image label with key %s to the container", k) - } - } - // labels from the CRI request (config) will override labels in the image config - for k, v := range configLabels { - labels[k] = v - } - labels[crilabels.ContainerKindLabel] = containerType - return labels -} - // runtimeSpec returns a default runtime spec used in cri-containerd. func (c *Controller) runtimeSpec(id string, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) { // GenerateSpec needs namespace. diff --git a/internal/cri/server/podsandbox/helpers_test.go b/internal/cri/server/podsandbox/helpers_test.go index 524153f02..2fa393666 100644 --- a/internal/cri/server/podsandbox/helpers_test.go +++ b/internal/cri/server/podsandbox/helpers_test.go @@ -19,38 +19,13 @@ package podsandbox import ( "context" "os" - "strings" "testing" - crilabels "github.com/containerd/containerd/v2/internal/cri/labels" "github.com/containerd/containerd/v2/pkg/oci" runtimespec "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/assert" ) -func TestBuildLabels(t *testing.T) { - imageConfigLabels := map[string]string{ - "a": "z", - "d": "y", - "long-label": strings.Repeat("example", 10000), - } - configLabels := map[string]string{ - "a": "b", - "c": "d", - } - 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, crilabels.ContainerKindSandbox, newLabels[crilabels.ContainerKindLabel]) - assert.NotContains(t, newLabels, "long-label") - - newLabels["a"] = "e" - 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") -} - func TestEnvDeduplication(t *testing.T) { for _, test := range []struct { desc string diff --git a/internal/cri/server/podsandbox/sandbox_run.go b/internal/cri/server/podsandbox/sandbox_run.go index 702b56801..53d949fdb 100644 --- a/internal/cri/server/podsandbox/sandbox_run.go +++ b/internal/cri/server/podsandbox/sandbox_run.go @@ -163,7 +163,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, crilabels.ContainerKindSandbox) + sandboxLabels := ctrdutil.BuildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindSandbox) snapshotterOpt := []snapshots.Opt{snapshots.WithLabels(snapshots.FilterInheritedLabels(config.Annotations))} extraSOpts, err := sandboxSnapshotterOpts(config) diff --git a/internal/cri/util/util.go b/internal/cri/util/util.go index ecc2823a6..e5ca181b1 100644 --- a/internal/cri/util/util.go +++ b/internal/cri/util/util.go @@ -21,9 +21,11 @@ import ( "path" "time" - "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/internal/cri/constants" + crilabels "github.com/containerd/containerd/v2/internal/cri/labels" + clabels "github.com/containerd/containerd/v2/pkg/labels" + "github.com/containerd/containerd/v2/pkg/namespaces" + "github.com/containerd/log" ) // deferCleanupTimeout is the default timeout for containerd cleanup operations @@ -64,3 +66,24 @@ func GetPassthroughAnnotations(podAnnotations map[string]string, } return passthroughAnnotations } + +// BuildLabel builds the labels from config to be passed to containerd +func BuildLabels(configLabels, imageConfigLabels map[string]string, containerType string) map[string]string { + labels := make(map[string]string) + + for k, v := range imageConfigLabels { + if err := clabels.Validate(k, v); err == nil { + labels[k] = v + } else { + // In case the image label is invalid, we output a warning and skip adding it to the + // container. + log.L.WithError(err).Warnf("unable to add image label with key %s to the container", k) + } + } + // labels from the CRI request (config) will override labels in the image config + for k, v := range configLabels { + labels[k] = v + } + labels[crilabels.ContainerKindLabel] = containerType + return labels +} diff --git a/internal/cri/util/util_test.go b/internal/cri/util/util_test.go index 2a1263b8f..6bd558542 100644 --- a/internal/cri/util/util_test.go +++ b/internal/cri/util/util_test.go @@ -17,8 +17,10 @@ package util import ( + "strings" "testing" + crilabels "github.com/containerd/containerd/v2/internal/cri/labels" "github.com/stretchr/testify/assert" ) @@ -136,3 +138,26 @@ func TestPassThroughAnnotationsFilter(t *testing.T) { }) } } + +func TestBuildLabels(t *testing.T) { + imageConfigLabels := map[string]string{ + "a": "z", + "d": "y", + "long-label": strings.Repeat("example", 10000), + } + configLabels := map[string]string{ + "a": "b", + "c": "d", + } + 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, crilabels.ContainerKindSandbox, newLabels[crilabels.ContainerKindLabel]) + assert.NotContains(t, newLabels, "long-label") + + newLabels["a"] = "e" + 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") +}