Merge pull request #9266 from akhilerm/refactor-cri-labels

refactor labels used in cri server
This commit is contained in:
Samuel Karp 2023-11-14 04:20:00 +00:00 committed by GitHub
commit 7deb68fbf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 39 deletions

View File

@ -27,4 +27,14 @@ const (
PinnedImageLabelKey = criContainerdPrefix + ".pinned" PinnedImageLabelKey = criContainerdPrefix + ".pinned"
// PinnedImageLabelValue is the label value indicating the image is pinned. // PinnedImageLabelValue is the label value indicating the image is pinned.
PinnedImageLabelValue = "pinned" PinnedImageLabelValue = "pinned"
// 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"
// ContainerKindContainer is a label value indicating container is application container
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

@ -40,6 +40,7 @@ import (
"github.com/containerd/containerd/v2/pkg/cri/annotations" "github.com/containerd/containerd/v2/pkg/cri/annotations"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
cio "github.com/containerd/containerd/v2/pkg/cri/io" cio "github.com/containerd/containerd/v2/pkg/cri/io"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
customopts "github.com/containerd/containerd/v2/pkg/cri/opts" customopts "github.com/containerd/containerd/v2/pkg/cri/opts"
containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
"github.com/containerd/containerd/v2/pkg/cri/util" "github.com/containerd/containerd/v2/pkg/cri/util"
@ -260,7 +261,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, containerKindContainer) containerLabels := buildLabels(config.Labels, image.ImageSpec.Config.Labels, crilabels.ContainerKindContainer)
sandboxInfo, err := c.client.SandboxStore().Get(ctx, sandboxID) sandboxInfo, err := c.client.SandboxStore().Get(ctx, sandboxID)
if err != nil { if err != nil {
@ -271,7 +272,7 @@ func (c *criService) CreateContainer(ctx context.Context, r *runtime.CreateConta
containerd.WithSpec(spec, specOpts...), containerd.WithSpec(spec, specOpts...),
containerd.WithRuntime(sandboxInfo.Runtime.Name, sandboxInfo.Runtime.Options), containerd.WithRuntime(sandboxInfo.Runtime.Name, sandboxInfo.Runtime.Options),
containerd.WithContainerLabels(containerLabels), containerd.WithContainerLabels(containerLabels),
containerd.WithContainerExtension(containerMetadataExtension, &meta), containerd.WithContainerExtension(crilabels.ContainerMetadataExtension, &meta),
) )
opts = append(opts, containerd.WithSandbox(sandboxID)) opts = append(opts, containerd.WithSandbox(sandboxID))

View File

@ -38,6 +38,7 @@ import (
"github.com/containerd/containerd/v2/errdefs" "github.com/containerd/containerd/v2/errdefs"
clabels "github.com/containerd/containerd/v2/labels" clabels "github.com/containerd/containerd/v2/labels"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
runtimeoptions "github.com/containerd/containerd/v2/pkg/runtimeoptions/v1" runtimeoptions "github.com/containerd/containerd/v2/pkg/runtimeoptions/v1"
@ -71,18 +72,6 @@ const (
// Delimiter used to construct container/sandbox names. // Delimiter used to construct container/sandbox names.
nameDelimiter = "_" nameDelimiter = "_"
// 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"
// containerKindContainer is a label value indicating container is application container
containerKindContainer = "container"
// containerMetadataExtension is an extension name that identify metadata of container in CreateContainerRequest
containerMetadataExtension = criContainerdPrefix + ".container.metadata"
// defaultIfName is the default network interface for the pods // defaultIfName is the default network interface for the pods
defaultIfName = "eth0" defaultIfName = "eth0"
@ -257,7 +246,7 @@ func buildLabels(configLabels, imageConfigLabels map[string]string, containerTyp
for k, v := range configLabels { for k, v := range configLabels {
labels[k] = v labels[k] = v
} }
labels[containerKindLabel] = containerType labels[crilabels.ContainerKindLabel] = containerType
return labels return labels
} }

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd/v2/containers" "github.com/containerd/containerd/v2/containers"
"github.com/containerd/containerd/v2/oci" "github.com/containerd/containerd/v2/oci"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
"github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/protobuf/types" "github.com/containerd/containerd/v2/protobuf/types"
@ -99,16 +100,16 @@ func TestBuildLabels(t *testing.T) {
"a": "b", "a": "b",
"c": "d", "c": "d",
} }
newLabels := buildLabels(configLabels, imageConfigLabels, containerKindSandbox) newLabels := buildLabels(configLabels, imageConfigLabels, crilabels.ContainerKindSandbox)
assert.Len(t, newLabels, 4) assert.Len(t, newLabels, 4)
assert.Equal(t, "b", newLabels["a"]) assert.Equal(t, "b", newLabels["a"])
assert.Equal(t, "d", newLabels["c"]) assert.Equal(t, "d", newLabels["c"])
assert.Equal(t, "y", newLabels["d"]) 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") assert.NotContains(t, newLabels, "long-label")
newLabels["a"] = "e" 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") assert.Equal(t, "b", configLabels["a"], "change in new labels should not affect original label")
} }

View File

@ -18,6 +18,7 @@ package server
import ( import (
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
cstore "github.com/containerd/containerd/v2/pkg/cri/store/container" cstore "github.com/containerd/containerd/v2/pkg/cri/store/container"
sstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox" sstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
) )
@ -39,5 +40,5 @@ func (i *criImplementation) ContainerStore() *cstore.Store {
} }
func (i *criImplementation) ContainerMetadataExtensionKey() string { func (i *criImplementation) ContainerMetadataExtensionKey() string {
return containerMetadataExtension return crilabels.ContainerMetadataExtension
} }

View File

@ -28,6 +28,7 @@ import (
clabels "github.com/containerd/containerd/v2/labels" clabels "github.com/containerd/containerd/v2/labels"
"github.com/containerd/containerd/v2/oci" "github.com/containerd/containerd/v2/oci"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" 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" imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util" ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
"github.com/containerd/log" "github.com/containerd/log"
@ -43,14 +44,6 @@ const (
// directory of the sandbox, all files created for the sandbox will be // directory of the sandbox, all files created for the sandbox will be
// placed under this directory. // placed under this directory.
sandboxesDir = "sandboxes" 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 is the key used for storing metadata in the sandbox extensions
MetadataKey = "metadata" MetadataKey = "metadata"
) )
@ -117,7 +110,7 @@ func buildLabels(configLabels, imageConfigLabels map[string]string, containerTyp
for k, v := range configLabels { for k, v := range configLabels {
labels[k] = v labels[k] = v
} }
labels[containerKindLabel] = containerType labels[crilabels.ContainerKindLabel] = containerType
return labels return labels
} }

View File

@ -23,6 +23,7 @@ import (
"testing" "testing"
"github.com/containerd/containerd/v2/oci" "github.com/containerd/containerd/v2/oci"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
docker "github.com/distribution/reference" docker "github.com/distribution/reference"
imagedigest "github.com/opencontainers/go-digest" imagedigest "github.com/opencontainers/go-digest"
runtimespec "github.com/opencontainers/runtime-spec/specs-go" runtimespec "github.com/opencontainers/runtime-spec/specs-go"
@ -85,16 +86,16 @@ func TestBuildLabels(t *testing.T) {
"a": "b", "a": "b",
"c": "d", "c": "d",
} }
newLabels := buildLabels(configLabels, imageConfigLabels, containerKindSandbox) newLabels := buildLabels(configLabels, imageConfigLabels, crilabels.ContainerKindSandbox)
assert.Len(t, newLabels, 4) assert.Len(t, newLabels, 4)
assert.Equal(t, "b", newLabels["a"]) assert.Equal(t, "b", newLabels["a"])
assert.Equal(t, "d", newLabels["c"]) assert.Equal(t, "d", newLabels["c"])
assert.Equal(t, "y", newLabels["d"]) 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") assert.NotContains(t, newLabels, "long-label")
newLabels["a"] = "e" 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") 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" containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/errdefs" "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" sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util" ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util"
"github.com/containerd/log" "github.com/containerd/log"
@ -55,9 +56,9 @@ func (c *Controller) RecoverContainer(ctx context.Context, cntr containerd.Conta
if err != nil { if err != nil {
return sandbox, fmt.Errorf("failed to get sandbox container extensions: %w", err) return sandbox, fmt.Errorf("failed to get sandbox container extensions: %w", err)
} }
ext, ok := exts[sandboxMetadataExtension] ext, ok := exts[crilabels.SandboxMetadataExtension]
if !ok { 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) data, err := typeurl.UnmarshalAny(ext)
if err != nil { if err != nil {

View File

@ -34,6 +34,7 @@ import (
"github.com/containerd/containerd/v2/errdefs" "github.com/containerd/containerd/v2/errdefs"
"github.com/containerd/containerd/v2/pkg/cri/annotations" "github.com/containerd/containerd/v2/pkg/cri/annotations"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" 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" customopts "github.com/containerd/containerd/v2/pkg/cri/opts"
sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox" sandboxstore "github.com/containerd/containerd/v2/pkg/cri/store/sandbox"
ctrdutil "github.com/containerd/containerd/v2/pkg/cri/util" 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) 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))} snapshotterOpt := []snapshots.Opt{snapshots.WithLabels(snapshots.FilterInheritedLabels(config.Annotations))}
extraSOpts, err := sandboxSnapshotterOpts(config) 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...), customopts.WithNewSnapshot(id, containerdImage, snapshotterOpt...),
containerd.WithSpec(spec, specOpts...), containerd.WithSpec(spec, specOpts...),
containerd.WithContainerLabels(sandboxLabels), containerd.WithContainerLabels(sandboxLabels),
containerd.WithContainerExtension(sandboxMetadataExtension, &metadata), containerd.WithContainerExtension(crilabels.SandboxMetadataExtension, &metadata),
containerd.WithRuntime(ociRuntime.Type, sandboxInfo.Runtime.Options), containerd.WithRuntime(ociRuntime.Type, sandboxInfo.Runtime.Options),
} }

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd/v2/errdefs" "github.com/containerd/containerd/v2/errdefs"
containerdimages "github.com/containerd/containerd/v2/images" containerdimages "github.com/containerd/containerd/v2/images"
criconfig "github.com/containerd/containerd/v2/pkg/cri/config" criconfig "github.com/containerd/containerd/v2/pkg/cri/config"
crilabels "github.com/containerd/containerd/v2/pkg/cri/labels"
"github.com/containerd/containerd/v2/pkg/cri/server/podsandbox" "github.com/containerd/containerd/v2/pkg/cri/server/podsandbox"
"github.com/containerd/containerd/v2/pkg/netns" "github.com/containerd/containerd/v2/pkg/netns"
"github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/platforms"
@ -55,7 +56,7 @@ import (
// recover recovers system state from containerd and status checkpoint. // recover recovers system state from containerd and status checkpoint.
func (c *criService) recover(ctx context.Context) error { func (c *criService) recover(ctx context.Context) error {
// Recover all sandboxes. // Recover all sandboxes.
sandboxes, err := c.client.Containers(ctx, filterLabel(containerKindLabel, containerKindSandbox)) sandboxes, err := c.client.Containers(ctx, filterLabel(crilabels.ContainerKindLabel, crilabels.ContainerKindSandbox))
if err != nil { if err != nil {
return fmt.Errorf("failed to list sandbox containers: %w", err) return fmt.Errorf("failed to list sandbox containers: %w", err)
} }
@ -146,7 +147,7 @@ func (c *criService) recover(ctx context.Context) error {
} }
// Recover all containers. // Recover all containers.
containers, err := c.client.Containers(ctx, filterLabel(containerKindLabel, containerKindContainer)) containers, err := c.client.Containers(ctx, filterLabel(crilabels.ContainerKindLabel, crilabels.ContainerKindContainer))
if err != nil { if err != nil {
return fmt.Errorf("failed to list containers: %w", err) return fmt.Errorf("failed to list containers: %w", err)
} }
@ -248,9 +249,9 @@ func (c *criService) loadContainer(ctx context.Context, cntr containerd.Containe
if err != nil { if err != nil {
return container, fmt.Errorf("failed to get container extensions: %w", err) return container, fmt.Errorf("failed to get container extensions: %w", err)
} }
ext, ok := exts[containerMetadataExtension] ext, ok := exts[crilabels.ContainerMetadataExtension]
if !ok { if !ok {
return container, fmt.Errorf("metadata extension %q not found", containerMetadataExtension) return container, fmt.Errorf("metadata extension %q not found", crilabels.ContainerMetadataExtension)
} }
data, err := typeurl.UnmarshalAny(ext) data, err := typeurl.UnmarshalAny(ext)
if err != nil { if err != nil {