Skip to content

Commit

Permalink
Fix the first alias of container reference in containerd handler
Browse files Browse the repository at this point in the history
Signed-off-by: Qiutong Song <songqt01@gmail.com>
  • Loading branch information
qiutongs authored and bobbypage committed Oct 11, 2021
1 parent 1fb8237 commit 56f032e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion container/containerd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *containerdClientMock) TaskPid(ctx context.Context, id string) (uint32,
}

func (c *containerdClientMock) ContainerStatus(ctx context.Context, id string) (*criapi.ContainerStatus, error) {
return &criapi.ContainerStatus{}, nil
return c.status, nil
}

func mockcontainerdClient(cntrs map[string]*containers.Container, status *criapi.ContainerStatus, returnErr error) ContainerdClient {
Expand Down
22 changes: 21 additions & 1 deletion container/containerd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,31 @@ func newContainerdContainerHandler(
rootfs = "/rootfs"
}

var restart uint32 = 0
containerName := "POD"
if cntr.Labels["io.cri-containerd.kind"] != "sandbox" {
status, err := client.ContainerStatus(ctx, id)
if err != nil {
return nil, err
}
restart = status.Metadata.Attempt
containerName = cntr.Labels["io.kubernetes.container.name"]
}

// The "io.kubernetes" labels are defined in https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/types/labels.go
containerNameConcat := strings.Join([]string{
"k8s",
containerName,
cntr.Labels["io.kubernetes.pod.name"],
cntr.Labels["io.kubernetes.pod.namespace"],
cntr.Labels["io.kubernetes.pod.uid"],
fmt.Sprint(restart)}, "_")

containerReference := info.ContainerReference{
Id: id,
Name: name,
Namespace: k8sContainerdNamespace,
Aliases: []string{id, name},
Aliases: []string{containerNameConcat, id, name},
}

libcontainerHandler := containerlibcontainer.NewHandler(cgroupManager, rootfs, int(taskPid), includedMetrics)
Expand Down
48 changes: 43 additions & 5 deletions container/containerd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
info "github.com/google/cadvisor/info/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

func init() {
Expand Down Expand Up @@ -60,13 +61,31 @@ func TestHandler(t *testing.T) {
checkEnvVars map[string]string
}
testContainers := make(map[string]*containers.Container)
testContainerSandbox := &containers.Container{
ID: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Labels: map[string]string{
"io.cri-containerd.kind": "sandbox",
"io.kubernetes.container.name": "pause",
"io.kubernetes.pod.name": "some-pod",
"io.kubernetes.pod.namespace": "some-ns",
"io.kubernetes.pod.uid": "some-uid"},
}
testContainer := &containers.Container{
ID: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Labels: map[string]string{"io.cri-containerd.kind": "sandbox"},
ID: "c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086",
Labels: map[string]string{
"io.cri-containerd.kind": "container",
"io.kubernetes.container.name": "some-container",
"io.kubernetes.pod.name": "some-pod",
"io.kubernetes.pod.namespace": "some-ns",
"io.kubernetes.pod.uid": "some-uid"},
}
spec := &specs.Spec{Root: &specs.Root{Path: "/test/"}, Process: &specs.Process{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "HELLO=WORLD"}}}
testContainerSandbox.Spec, _ = typeurl.MarshalAny(spec)
testContainer.Spec, _ = typeurl.MarshalAny(spec)
testContainers["40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"] = testContainer
testContainers["40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"] = testContainerSandbox
testContainers["c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086"] = testContainer
status := &criapi.ContainerStatus{Metadata: &criapi.ContainerMetadata{Attempt: 2}}

for _, ts := range []testCase{
{
mockcontainerdClient(nil, nil, nil),
Expand Down Expand Up @@ -96,7 +115,7 @@ func TestHandler(t *testing.T) {
&info.ContainerReference{
Id: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Aliases: []string{"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"},
Aliases: []string{"k8s_POD_some-pod_some-ns_some-uid_0", "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"},
Namespace: k8sContainerdNamespace,
},
map[string]string{},
Expand All @@ -115,11 +134,30 @@ func TestHandler(t *testing.T) {
&info.ContainerReference{
Id: "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9",
Aliases: []string{"40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"},
Aliases: []string{"k8s_POD_some-pod_some-ns_some-uid_0", "40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/40af7cdcbe507acad47a5a62025743ad3ddc6ab93b77b21363aa1c1d641047c9"},
Namespace: k8sContainerdNamespace,
},
map[string]string{"TEST_REGION": "FRA", "TEST_ZONE": "A"},
},
{
mockcontainerdClient(testContainers, status, nil),
"/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086",
&mockedMachineInfo{},
nil,
&containerlibcontainer.CgroupSubsystems{},
false,
nil,
nil,
false,
"",
&info.ContainerReference{
Id: "c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086",
Name: "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086",
Aliases: []string{"k8s_some-container_some-pod_some-ns_some-uid_2", "c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086", "/kubepods/pod068e8fa0-9213-11e7-a01f-507b9d4141fa/c6a1aa99f14d3e57417e145b897e34961145f6b6f14216a176a34bfabbf79086"},
Namespace: k8sContainerdNamespace,
},
map[string]string{},
},
} {
handler, err := newContainerdContainerHandler(ts.client, ts.name, ts.machineInfoFactory, ts.fsInfo, ts.cgroupSubsystems, ts.inHostNamespace, ts.metadataEnvAllowList, ts.includedMetrics)
if ts.hasErr {
Expand Down

0 comments on commit 56f032e

Please sign in to comment.