Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set annotations when creating sanbodx and containers #369

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (ds *dockerService) CreateContainer(
return nil, fmt.Errorf("sandbox config is nil for container %q", config.Metadata.Name)
}

// TODO: Docker supports annotations for now, so labels and annotations might be separated
// in the future. Keeping them merged is just for backward compatibility.
Comment on lines +50 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backward compatibility with what? AIUI no containers will be persisted across an upgrade (daemon restart) so there is no need for containers started by cri-dockerd v0.3.x to be understandable by v0.4.x, or vice versa.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two main considerations for backward compatibility:

  • I‘m not sure if some runtimes use the label to make some judgments
  • Docker ContainerList api doesn't return annotations, so keeping them merged is needed for later extraction.

labels := makeLabels(config.GetLabels(), config.GetAnnotations())
// Apply a the container type label.
labels[containerTypeLabelKey] = containerTypeLabelContainer
Expand All @@ -55,6 +57,12 @@ func (ds *dockerService) CreateContainer(
// Write the sandbox ID in the labels.
labels[sandboxIDLabelKey] = podSandboxID

// Set container annotations.
annotations := config.GetAnnotations()
annotations[containerTypeLabelKey] = containerTypeLabelContainer
annotations[containerLogPathLabelKey] = filepath.Join(sandboxConfig.LogDirectory, config.LogPath)
annotations[sandboxIDLabelKey] = podSandboxID

apiVersion, err := ds.getDockerAPIVersion()
if err != nil {
return nil, fmt.Errorf("unable to get the docker API version: %v", err)
Expand Down Expand Up @@ -96,7 +104,8 @@ func (ds *dockerService) CreateContainer(
RestartPolicy: container.RestartPolicy{
Name: "no",
},
Runtime: sandboxInfo.HostConfig.Runtime,
Runtime: sandboxInfo.HostConfig.Runtime,
Annotations: annotations,
},
}

Expand Down
5 changes: 3 additions & 2 deletions core/container_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package core
import (
"context"
"fmt"

"github.com/Mirantis/cri-dockerd/libdocker"
"github.com/sirupsen/logrus"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (ds *dockerService) ContainerStatus(
return nil, err
}

labels, annotations := extractLabels(r.Config.Labels)
labels, _ := extractLabels(r.Config.Labels)
imageName := r.Config.Image
if ir != nil && len(ir.RepoTags) > 0 {
imageName = ir.RepoTags[0]
Expand All @@ -133,7 +134,7 @@ func (ds *dockerService) ContainerStatus(
Reason: reason,
Message: message,
Labels: labels,
Annotations: annotations,
Annotations: r.HostConfig.Annotations,
LogPath: r.Config.Labels[containerLogPathLabelKey],
}
res := v1.ContainerStatusResponse{Status: status}
Expand Down
4 changes: 4 additions & 0 deletions core/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func toRuntimeAPIContainer(c *dockertypes.Container) (*runtimeapi.Container, err
if err != nil {
return nil, err
}
// TODO: Currently docker ContainerList api doesn't return annotations, so annotations
// still need to be extracted from labels here.
Comment on lines +205 to +206
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super comfortable with having different sources of truth for annotations depending on the CRI API endpoint queried. It would be straightforward to extend the ContainerList API to return container annotations; perhaps we should do that first so support can be added properly to cri-dockerd?

Copy link
Contributor Author

@cncal cncal May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm working on it moby/moby#47866. May I mark this PR a WIP util docker-side supports it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moby/moby#47866 has been merged into master branch, but released in Pre-release https://github.com/moby/moby/releases/tag/v27.0.0-rc.1. May I bump github.com/docker/docker from 26.1.2+incompatible to v27.0.0-rc.1+incompatible? @corhere

labels, annotations := extractLabels(c.Labels)
sandboxID := c.Labels[sandboxIDLabelKey]
// The timestamp in dockertypes.Container is in seconds.
Expand Down Expand Up @@ -269,6 +271,8 @@ func containerToRuntimeAPISandbox(c *dockertypes.Container) (*runtimeapi.PodSand
if err != nil {
return nil, err
}
// TODO: Currently docker ContainerList api doesn't return annotations, so annotations
// still need to be extracted from labels here.
labels, annotations := extractLabels(c.Labels)
// The timestamp in dockertypes.Container is in seconds.
createdAt := c.Created * int64(time.Second)
Expand Down
10 changes: 8 additions & 2 deletions core/sandbox_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,21 @@ func (ds *dockerService) makeSandboxDockerConfig(
c *runtimeapi.PodSandboxConfig,
image string,
) (*dockerbackend.ContainerCreateConfig, error) {
// Merge annotations and labels because docker supports only labels.
// TODO: Docker supports annotations for now, so labels and annotations might be separated
// in the future. Keeping them merged is just for backward compatibility.
labels := makeLabels(c.GetLabels(), c.GetAnnotations())
// Apply a label to distinguish sandboxes from regular containers.
labels[containerTypeLabelKey] = containerTypeLabelSandbox
// Apply a container name label for infra container. This is used in summary v1.
labels[config.KubernetesContainerNameLabel] = sandboxContainerName

// Set sandbox annotations.
annotations := c.GetAnnotations()
annotations[containerTypeLabelKey] = containerTypeLabelSandbox

hc := &dockercontainer.HostConfig{
IpcMode: dockercontainer.IpcMode("shareable"),
IpcMode: dockercontainer.IpcMode("shareable"),
Annotations: annotations,
}
createConfig := &dockerbackend.ContainerCreateConfig{
Name: makeSandboxName(c),
Expand Down
4 changes: 2 additions & 2 deletions core/sandbox_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ func (ds *dockerService) PodSandboxStatus(
ips = ips[1:]
}

labels, annotations := extractLabels(r.Config.Labels)
labels, _ := extractLabels(r.Config.Labels)
status := &v1.PodSandboxStatus{
Id: r.ID,
State: state,
CreatedAt: ct,
Metadata: metadata,
Labels: labels,
Annotations: annotations,
Annotations: r.HostConfig.Annotations,
Network: &v1.PodSandboxNetworkStatus{
Ip: ip,
},
Expand Down