Skip to content

Commit

Permalink
match only cgroup2 and use dummy initcontainerid in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AliDatadog committed Nov 29, 2023
1 parent de331d0 commit f106b04
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
27 changes: 23 additions & 4 deletions statsd/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const (
// Currently, host namespace inode number are hardcoded, which can be used to detect
// if we're running in host namespace or not (does not work when running in DinD)
hostCgroupNamespaceInode = 0xEFFFFFFB

// From https://github.com/torvalds/linux/blob/5859a2b1991101d6b978f3feb5325dad39421f29/include/linux/proc_ns.h#L41-L49
// Currently, host namespace inode number are hardcoded, which can be used to detect
// if we're running in host namespace or not (does not work when running in DinD)
hostMntNamespaceInode = 0xEFFFFFFB
)

var (
Expand All @@ -46,6 +51,9 @@ var (
expContainerID = regexp.MustCompile(fmt.Sprintf(`(%s|%s|%s)(?:.scope)?$`, uuidSource, containerSource, taskSource))

cIDMountInfoRegexp = regexp.MustCompile(cIDRegexpStr)

// initContainerID initializes the container ID.
initContainerID = internalInitContainerID
)

// parseContainerID finds the first container ID reading from r and returns it.
Expand Down Expand Up @@ -135,7 +143,7 @@ func parseCgroupMountPath(r io.Reader) string {
tokens := strings.Fields(line)
if len(tokens) >= 3 {
fsType := tokens[2]
if strings.HasPrefix(fsType, "cgroup") {
if fsType == "cgroup2" {
return tokens[1] // line is formatted as "cgroup /sys/fs/cgroup/... cgroup...""
}
}
Expand All @@ -156,7 +164,7 @@ func parseCgroupNodePath(r io.Reader) string {

// getCgroupInode returns the cgroup inode prefixed by "in-" and is used by the agent to retrieve the container ID
func getCgroupInode(mountsPath, cgroupPath string) string {
// Retrieve a cgroup mount point from /proc/self/mounts
// Retrieve a cgroup mount point from /proc/mounts
f, err := os.Open(mountsPath)
if err != nil {
return ""
Expand Down Expand Up @@ -199,9 +207,20 @@ func isHostCgroupNamespace() bool {
return inode == hostCgroupNamespaceInode
}

// initContainerID initializes the container ID.
func isHostMountNamespace() bool {
fi, err := os.Stat("/proc/self/ns/mnt")
if err != nil {
return false
}

inode := fi.Sys().(*syscall.Stat_t).Ino

return inode == hostMntNamespaceInode
}

// internalInitContainerID initializes the container ID.
// It can either be provided by the user or read from cgroups.
func initContainerID(userProvidedID string, cgroupFallback bool) {
func internalInitContainerID(userProvidedID string, cgroupFallback bool) {
initOnce.Do(func() {
if userProvidedID != "" {
containerID = userProvidedID
Expand Down
2 changes: 1 addition & 1 deletion statsd/container_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package statsd

func initContainerID(userProvidedID string, cgroupFallback bool) {
var initContainerID = func(userProvidedID string, cgroupFallback bool) {
initOnce.Do(func() {
if userProvidedID != "" {
containerID = userProvidedID
Expand Down
26 changes: 24 additions & 2 deletions statsd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import (
"github.com/stretchr/testify/require"
)

// initContainerID initializes the container ID.
func init() {
initContainerID = dummyInitContainerId
}

// dummyInitContainerId initializes the container ID if provided
func dummyInitContainerId(userProvidedID string, _ bool) {
initOnce.Do(func() {
if userProvidedID != "" {
containerID = userProvidedID
return
}
})
}

func TestParseContainerID(t *testing.T) {
for input, expectedResult := range map[string]string{
`other_line
Expand Down Expand Up @@ -478,14 +493,21 @@ func TestGetCgroupInode(t *testing.T) {
expectedResult: "in-%d", // Will be formatted with inode number
},
{
description: "hybrid cgroup - should match the first which does not exist",
description: "should not match cgroup",
procMountsContent: "cgroup %s cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0\n",
cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope",
procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n",
expectedResult: "",
},
{
description: "hybrid cgroup - should match only cgroup2",
procMountsContent: `other_line
cgroup /sys/fs/cgroup/memory cgroup foo,bar 0 0
cgroup2 %s cgroup2 rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
`,
cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope",
procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n",
expectedResult: "", // Will be formatted with inode number
expectedResult: "in-%d", // Will be formatted with inode number
},
{
description: "Non-matching entry in /proc/self/cgroup",
Expand Down

0 comments on commit f106b04

Please sign in to comment.