dedup BuildLabels
Signed-off-by: Jin Dong <djdongjin95@gmail.com>
This commit is contained in:
parent
a5cd0d0a5c
commit
38ba7f2f7e
@ -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)
|
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
|
// TODO the sandbox in the cache should hold this info
|
||||||
runtimeName, runtimeOption, err := c.runtimeInfo(ctx, sandboxID)
|
runtimeName, runtimeOption, err := c.runtimeInfo(ctx, sandboxID)
|
||||||
|
@ -33,10 +33,8 @@ import (
|
|||||||
|
|
||||||
containerd "github.com/containerd/containerd/v2/client"
|
containerd "github.com/containerd/containerd/v2/client"
|
||||||
"github.com/containerd/containerd/v2/core/containers"
|
"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"
|
containerstore "github.com/containerd/containerd/v2/internal/cri/store/container"
|
||||||
imagestore "github.com/containerd/containerd/v2/internal/cri/store/image"
|
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/errdefs"
|
||||||
"github.com/containerd/log"
|
"github.com/containerd/log"
|
||||||
)
|
)
|
||||||
@ -220,27 +218,6 @@ func filterLabel(k, v string) string {
|
|||||||
return fmt.Sprintf("labels.%q==%q", k, v)
|
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.
|
// getRuntimeOptions get runtime options from container metadata.
|
||||||
func getRuntimeOptions(c containers.Container) (interface{}, error) {
|
func getRuntimeOptions(c containers.Container) (interface{}, error) {
|
||||||
from := c.Runtime.Options
|
from := c.Runtime.Options
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -29,7 +28,6 @@ import (
|
|||||||
runcoptions "github.com/containerd/containerd/api/types/runc/options"
|
runcoptions "github.com/containerd/containerd/api/types/runc/options"
|
||||||
"github.com/containerd/containerd/v2/core/containers"
|
"github.com/containerd/containerd/v2/core/containers"
|
||||||
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
|
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"
|
containerstore "github.com/containerd/containerd/v2/internal/cri/store/container"
|
||||||
"github.com/containerd/containerd/v2/pkg/oci"
|
"github.com/containerd/containerd/v2/pkg/oci"
|
||||||
"github.com/containerd/containerd/v2/pkg/protobuf/types"
|
"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) {
|
func TestGenerateRuntimeOptions(t *testing.T) {
|
||||||
nilOpts := `
|
nilOpts := `
|
||||||
systemd_cgroup = true
|
systemd_cgroup = true
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/containerd/log"
|
|
||||||
"github.com/containerd/typeurl/v2"
|
"github.com/containerd/typeurl/v2"
|
||||||
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
|
||||||
@ -31,7 +30,6 @@ import (
|
|||||||
imagestore "github.com/containerd/containerd/v2/internal/cri/store/image"
|
imagestore "github.com/containerd/containerd/v2/internal/cri/store/image"
|
||||||
sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox"
|
sandboxstore "github.com/containerd/containerd/v2/internal/cri/store/sandbox"
|
||||||
ctrdutil "github.com/containerd/containerd/v2/internal/cri/util"
|
ctrdutil "github.com/containerd/containerd/v2/internal/cri/util"
|
||||||
clabels "github.com/containerd/containerd/v2/pkg/labels"
|
|
||||||
"github.com/containerd/containerd/v2/pkg/oci"
|
"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])
|
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.
|
// runtimeSpec returns a default runtime spec used in cri-containerd.
|
||||||
func (c *Controller) runtimeSpec(id string, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
|
func (c *Controller) runtimeSpec(id string, baseSpecFile string, opts ...oci.SpecOpts) (*runtimespec.Spec, error) {
|
||||||
// GenerateSpec needs namespace.
|
// GenerateSpec needs namespace.
|
||||||
|
@ -19,38 +19,13 @@ package podsandbox
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
crilabels "github.com/containerd/containerd/v2/internal/cri/labels"
|
|
||||||
"github.com/containerd/containerd/v2/pkg/oci"
|
"github.com/containerd/containerd/v2/pkg/oci"
|
||||||
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestEnvDeduplication(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
desc string
|
desc string
|
||||||
|
@ -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)
|
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))}
|
snapshotterOpt := []snapshots.Opt{snapshots.WithLabels(snapshots.FilterInheritedLabels(config.Annotations))}
|
||||||
extraSOpts, err := sandboxSnapshotterOpts(config)
|
extraSOpts, err := sandboxSnapshotterOpts(config)
|
||||||
|
@ -21,9 +21,11 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/containerd/v2/pkg/namespaces"
|
|
||||||
|
|
||||||
"github.com/containerd/containerd/v2/internal/cri/constants"
|
"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
|
// deferCleanupTimeout is the default timeout for containerd cleanup operations
|
||||||
@ -64,3 +66,24 @@ func GetPassthroughAnnotations(podAnnotations map[string]string,
|
|||||||
}
|
}
|
||||||
return passthroughAnnotations
|
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
|
||||||
|
}
|
||||||
|
@ -17,8 +17,10 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
crilabels "github.com/containerd/containerd/v2/internal/cri/labels"
|
||||||
"github.com/stretchr/testify/assert"
|
"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")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user