refactor labels used in cri server
remove the duplication of labels used in cri/server and move them to a common package cri/labels Signed-off-by: Akhil Mohan <makhil@vmware.com>
This commit is contained in:
parent
45d7f2324d
commit
7e79225cec
@ -27,4 +27,12 @@ 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"
|
||||||
)
|
)
|
||||||
|
@ -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))
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user