-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: container origin detection on UDP
- Loading branch information
Showing
4 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package statsd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
// cgroupPath is the path to the cgroup file where we can find the container id if one exists. | ||
cgroupPath = "/proc/self/cgroup" | ||
) | ||
|
||
const ( | ||
uuidSource = "[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}" | ||
containerSource = "[0-9a-f]{64}" | ||
taskSource = "[0-9a-f]{32}-\\d+" | ||
) | ||
|
||
var ( | ||
// expLine matches a line in the /proc/self/cgroup file. It has a submatch for the last element (path), which contains the container ID. | ||
expLine = regexp.MustCompile(`^\d+:[^:]*:(.+)$`) | ||
|
||
// expContainerID matches contained IDs and sources. Source: https://github.com/Qard/container-info/blob/master/index.js | ||
expContainerID = regexp.MustCompile(fmt.Sprintf(`(%s|%s|%s)(?:.scope)?$`, uuidSource, containerSource, taskSource)) | ||
) | ||
|
||
// parseContainerID finds the first container ID reading from r and returns it. | ||
func parseContainerID(r io.Reader) string { | ||
scn := bufio.NewScanner(r) | ||
for scn.Scan() { | ||
path := expLine.FindStringSubmatch(scn.Text()) | ||
if len(path) != 2 { | ||
// invalid entry, continue | ||
continue | ||
} | ||
if parts := expContainerID.FindStringSubmatch(path[1]); len(parts) == 2 { | ||
return parts[1] | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
// readContainerID attempts to return the container ID from the provided file path or empty on failure. | ||
func readContainerID(fpath string) string { | ||
f, err := os.Open(fpath) | ||
if err != nil { | ||
return "" | ||
} | ||
defer f.Close() | ||
return parseContainerID(f) | ||
} | ||
|
||
// ContainerID attempts to return the container ID from /proc/self/cgroup or empty on failure. | ||
var containerID func() string = func() string { | ||
return readContainerID(cgroupPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package statsd | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseContainerID(t *testing.T) { | ||
for in, out := range map[string]string{ | ||
`other_line | ||
10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
9:cpuset:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
8:pids:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
7:freezer:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
6:cpu,cpuacct:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
5:perf_event:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
4:blkio:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
3:devices:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa | ||
2:net_cls,net_prio:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa`: "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", | ||
"10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa": "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa", | ||
"10:hugetlb:/kubepods": "", | ||
"11:hugetlb:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da": "432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da", | ||
"1:name=systemd:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376": "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376", | ||
"1:name=systemd:/uuid/34dc0b5e-626f-2c5c-4c51-70e34b10e765": "34dc0b5e-626f-2c5c-4c51-70e34b10e765", | ||
"1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890": "34dc0b5e626f2c5c4c5170e34b10e765-1234567890", | ||
"1:name=systemd:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376.scope": "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376", | ||
`1:name=systemd:/nope | ||
2:pids:/docker/34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376 | ||
3:cpu:/invalid`: "34dc0b5e626f2c5c4c5170e34b10e7654ce36f0fcd532739f4445baabea03376", | ||
} { | ||
id := parseContainerID(strings.NewReader(in)) | ||
if id != out { | ||
t.Fatalf("%q -> %q: %q", in, out, id) | ||
} | ||
} | ||
} | ||
|
||
func TestReadContainerID(t *testing.T) { | ||
cid := "8c046cb0b72cd4c99f51b5591cd5b095967f58ee003710a45280c28ee1a9c7fa" | ||
cgroupContents := "10:hugetlb:/kubepods/burstable/podfd52ef25-a87d-11e9-9423-0800271a638e/" + cid | ||
|
||
tmpFile, err := ioutil.TempFile(os.TempDir(), "fake-cgroup-") | ||
if err != nil { | ||
t.Fatalf("failed to create fake cgroup file: %v", err) | ||
} | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
_, err = io.WriteString(tmpFile, cgroupContents) | ||
if err != nil { | ||
t.Fatalf("failed writing to fake cgroup file: %v", err) | ||
} | ||
|
||
err = tmpFile.Close() | ||
if err != nil { | ||
t.Fatalf("failed closing fake cgroup file: %v", err) | ||
} | ||
|
||
actualCID := readContainerID(tmpFile.Name()) | ||
assert.Equal(t, cid, actualCID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters