Skip to content

Commit

Permalink
fix(executor): Retrieve containerId from cri-containerd /proc/{pid}/c…
Browse files Browse the repository at this point in the history
…group. Fixes argoproj#4302 (argoproj#4309)
  • Loading branch information
cy-zheng authored Oct 18, 2020
1 parent e6b0249 commit 07b2ef6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
41 changes: 29 additions & 12 deletions workflow/executor/pns/pns.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,38 @@ func parseContainerID(pid int) (string, error) {
defer func() { _ = cgroupFile.Close() }()
sc := bufio.NewScanner(cgroupFile)
for sc.Scan() {
// See https://www.systutorials.com/docs/linux/man/5-proc/ for /proc/XX/cgroup format. e.g.:
// 5:cpuacct,cpu,cpuset:/daemons
line := sc.Text()
log.Debugf("pid %d: %s", pid, line)
parts := strings.Split(line, "/")
if len(parts) > 1 {
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")
return containerID, nil
}
containerID := parseContainerIDFromCgroupLine(line)
if containerID != "" {
return containerID, nil
}
}
return "", errors.InternalErrorf("Failed to parse container ID from %s", cgroupPath)
}

func parseContainerIDFromCgroupLine(line string) string {
// See https://www.systutorials.com/docs/linux/man/5-proc/ for /proc/XX/cgroup format. e.g.:
// 5:cpuacct,cpu,cpuset:/daemons
parts := strings.Split(line, "/")
if len(parts) > 1 {
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")

// 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-")
}
return containerID
}
}
return ""
}
40 changes: 40 additions & 0 deletions workflow/executor/pns/pns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pns

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPNSExecutor_parseContainerIDFromCgroupLine(t *testing.T) {
testCases := []struct {
line string
expected string
}{
{
line: "",
expected: "",
},
{
line: "5:rdma:/",
expected: "",
},
{
line: "8:cpu,cpuacct:/kubepods/besteffort/pod2fad8aad-dcd0-4fef-b45a-151630b9a4b5/b844ef90162af07ad3fb2961ffb2f528f8bf7c9edb8c45465fd8651fab8a78c1",
expected: "b844ef90162af07ad3fb2961ffb2f528f8bf7c9edb8c45465fd8651fab8a78c1",
},
{
line: "2:cpu,cpuacct:/system.slice/containerd.service/kubepods-burstable-podf61fae9b_d7a7_11ea_bb0c_12a065621d2b.slice:cri-containerd:b6b894a028b7ec8e0f98c57a5f7b9735ad792276c3ce52bc795fcd367d829de9",
expected: "b6b894a028b7ec8e0f98c57a5f7b9735ad792276c3ce52bc795fcd367d829de9",
},
{
line: "8:cpu,cpuacct:/kubepods/besteffort/pod2fad8aad-dcd0-4fef-b45a-151630b9a4b5/crio-7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42.scope",
expected: "7a92a067289f6197148912be1c15f20f0330c7f3c541473d3b9c4043ca137b42",
},
}

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

0 comments on commit 07b2ef6

Please sign in to comment.