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

fix: executor/pns containerid prefix fix #4555

Merged
merged 6 commits into from
Nov 19, 2020
Merged
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
12 changes: 8 additions & 4 deletions workflow/executor/pns/pns.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,22 @@ func parseContainerIDFromCgroupLine(line string) string {
if containerID := parts[len(parts)-1]; containerID != "" {
// need to check for empty string because the line may look like: 5:rdma:/

// for crio we need to get rid of "crio-" prefix and ".scope" suffix
// e.g. crio-7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42.scope
containerID := strings.TrimSuffix(strings.TrimPrefix(containerID, "crio-"), ".scope")
// remove possible ".scope" suffix
containerID := strings.TrimSuffix(containerID, ".scope")

// for compatibility with cri-containerd record format when using systemd cgroup path
// example record in /proc/{pid}/cgroup:
// 9:cpuset:/kubepods-besteffort-pod30556cce_0f92_11eb_b36d_02623cf324c8.slice:cri-containerd:c688c856b21cfb29c1dbf6c14793435e44a1299dfc12add33283239bffed2620
if strings.Contains(containerID, "cri-containerd") {
strList := strings.Split(containerID, ":")
containerID = strList[len(strList)-1]
containerID = strings.TrimPrefix(containerID, "cri-containerd-")
}

// remove possible "*-" prefix
// e.g. crio-7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42.scope
parts := strings.Split(containerID, "-")
Copy link
Contributor

Choose a reason for hiding this comment

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

this could panic in len(parts) == 0?

Copy link

Choose a reason for hiding this comment

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

strings.Split() always returns a slice of length ≥ 1, as even the empty string, split on any "split char" would result in []string{""}.

Please refer to the documentation where it says:

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

Copy link
Contributor Author

@L3Nerd L3Nerd Nov 19, 2020

Choose a reason for hiding this comment

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

And it's already tested here, here and here.

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad - I actually meant line 435 - but I don't think it can panic on closer inspecting

containerID = parts[len(parts)-1]

return containerID
}
}
Expand Down
6 changes: 5 additions & 1 deletion workflow/executor/pns/pns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ func TestPNSExecutor_parseContainerIDFromCgroupLine(t *testing.T) {
line: "8:cpu,cpuacct:/kubepods/besteffort/pod2fad8aad-dcd0-4fef-b45a-151630b9a4b5/crio-7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42.scope",
expected: "7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42",
},
{
line: "2:cpuacct,cpu:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod1cd87fe8_8ea0_11ea_8d51_566f300c000a.slice/docker-6b40fc7f75fe3210621a287412ac056e43554b1026a01625b48ba7d136d8a125.scope",
expected: "6b40fc7f75fe3210621a287412ac056e43554b1026a01625b48ba7d136d8a125",
},
}

for _, testCase := range testCases {
containerID := parseContainerIDFromCgroupLine(testCase.line)
assert.Equal(t, containerID, testCase.expected)
assert.Equal(t, testCase.expected, containerID)
}
}