Skip to content

Commit

Permalink
remove isCgroupV1 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AliDatadog committed Dec 19, 2023
1 parent bcf9464 commit 421ae2b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 135 deletions.
35 changes: 4 additions & 31 deletions statsd/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const (
// selfMountinfo is the path to the mountinfo path where we can find the container id in case cgroup namespace is preventing the use of /proc/self/cgroup
selfMountInfoPath = "/proc/self/mountinfo"

// mountsPath is the path to the file listing all the mount points
mountsPath = "/proc/mounts"

// defaultCgroupMountPath is the default path to the cgroup mount point.
defaultCgroupMountPath = "/sys/fs/cgroup"

Expand Down Expand Up @@ -113,30 +110,6 @@ func readMountinfo(path string) string {
return parseMountinfo(f)
}

// isCgroupV1 checks if Cgroup V1 is used
func isCgroupV1(mountsPath string) bool {
f, err := os.Open(mountsPath)
if err != nil {
return false
}
defer f.Close()

scn := bufio.NewScanner(f)
for scn.Scan() {
line := scn.Text()

tokens := strings.Fields(line)
if len(tokens) >= 3 {
fsType := tokens[2]
if fsType == "cgroup" {
return true
}
}
}

return false
}

func isHostCgroupNamespace() bool {
fi, err := os.Stat("/proc/self/ns/cgroup")
if err != nil {
Expand Down Expand Up @@ -214,12 +187,12 @@ func internalInitContainerID(userProvidedID string, cgroupFallback bool) {

if cgroupFallback {
isHostCgroupNs := isHostCgroupNamespace()
if isCgroupV1(mountsPath) || isHostCgroupNs {
if isHostCgroupNs {
containerID = readContainerID(cgroupPath)
} else {
containerID = readMountinfo(selfMountInfoPath)
return
}
if containerID != "" && !isHostCgroupNs {
containerID = readMountinfo(selfMountInfoPath)
if containerID != "" {
containerID = getCgroupInode(defaultCgroupMountPath, cgroupPath)
}
}
Expand Down
104 changes: 0 additions & 104 deletions statsd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,109 +263,6 @@ func TestReadMountinfo(t *testing.T) {
assert.Equal(t, cid, actualCID)
}

func TestIsCgroupV1(t *testing.T) {
for input, expectedResult := range map[string]bool{
`sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,nosuid,relatime,size=16229088k,nr_inodes=4057272,mode=755,inode64 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,noexec,relatime,size=3253732k,mode=755,inode64 0 0
/dev/mapper/vg-root / ext4 rw,relatime,errors=remount-ro 0 0
/dev/mapper/vg-root_usr /usr ext4 rw,nodev,relatime 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev,noexec,inode64 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k,inode64 0 0
cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
efivarfs /sys/firmware/efi/efivars efivarfs rw,nosuid,nodev,noexec,relatime 0 0
bpf /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=29681 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime,pagesize=2M 0 0
mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0
tracefs /sys/kernel/tracing tracefs rw,nosuid,nodev,noexec,relatime 0 0
fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
none /run/credentials/systemd-sysusers.service ramfs ro,nosuid,nodev,noexec,relatime,mode=700 0 0
/dev/nvme0n1p3 /boot ext4 rw,nosuid,nodev,noexec,relatime 0 0
/dev/nvme0n1p2 /boot/efi vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
tmpfs /run/qemu tmpfs rw,nosuid,nodev,relatime,mode=755,inode64 0 0
/dev/mapper/vg-root_home /home ext4 rw,nosuid,nodev,relatime 0 0
/dev/mapper/vg-root_tmp /tmp ext4 rw,nosuid,nodev,relatime 0 0
/dev/mapper/vg-root_var /var ext4 rw,nosuid,nodev,relatime 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
overlay /var/lib/docker/overlay2/d4cea28ac9d244e0d966e9298f2bfec6b51e3dbca19c63be5f25797145e7ae37/merged overlay rw,relatime,lowerdir=/var/lib/docker/overlay2/l/PSA4WM7MRV7OTYBH47EXU5XBNL:/var/lib/docker/overlay2/l/O47GWLQIRN2RDBHNBIT4MQSXHY,upperdir=/var/lib/docker/overlay2/d4cea28ac9d244e0d966e9298f2bfec6b51e3dbca19c63be5f25797145e7ae37/diff,workdir=/var/lib/docker/overlay2/d4cea28ac9d244e0d966e9298f2bfec6b51e3dbca19c63be5f25797145e7ae37/work 0 0
nsfs /run/docker/netns/f5019077c595 nsfs rw 0 0
tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=3253728k,nr_inodes=813432,mode=700,uid=1000,gid=1000,inode64 0 0
portal /run/user/1000/doc fuse.portal rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0`: false,
`/dev/root / ext4 rw,relatime,discard,errors=remount-ro 0 0
devtmpfs /dev devtmpfs rw,relatime,size=4065432k,nr_inodes=1016358,mode=755,inode64 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev,inode64 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,size=1627848k,nr_inodes=819200,mode=755,inode64 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k,inode64 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755,inode64 0 0
cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
efivarfs /sys/firmware/efi/efivars efivarfs rw,nosuid,nodev,noexec,relatime 0 0
bpf /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0
cgroup /sys/fs/cgroup/misc cgroup rw,nosuid,nodev,noexec,relatime,misc 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=14210 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime,pagesize=2M 0 0
mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0
tracefs /sys/kernel/tracing tracefs rw,nosuid,nodev,noexec,relatime 0 0
fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
none /run/credentials/systemd-sysusers.service ramfs ro,nosuid,nodev,noexec,relatime,mode=700 0 0
/dev/sda15 /boot/efi vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
sunrpc /run/rpc_pipefs rpc_pipefs rw,relatime 0 0
/dev/root /home/kubernetes/bin ext4 rw,relatime,discard,errors=remount-ro 0 0
/dev/root /home/kubernetes/flexvolume ext4 rw,relatime,discard,errors=remount-ro 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
/dev/root /var/lib/kubelet ext4 rw,relatime,discard,errors=remount-ro 0 0
tmpfs /var/lib/kubelet/pki tmpfs rw,relatime,inode64 0 0
tmpfs /var/lib/kubelet/pki tmpfs rw,relatime,inode64 0 0
/dev/root /home/containerd ext4 ro,relatime,discard,errors=remount-ro 0 0
/dev/root /home/kubernetes/containerized_mounter ext4 rw,relatime,discard,errors=remount-ro 0 0
/dev/root /home/kubernetes/containerized_mounter/rootfs/var/lib/kubelet ext4 rw,relatime,discard,errors=remount-ro 0 0
tmpfs /home/kubernetes/containerized_mounter/rootfs/var/lib/kubelet/pki tmpfs rw,relatime,inode64 0 0
/dev/root /home/kubernetes/containerized_mounter/rootfs/var/lib/kubelet ext4 rw,relatime,discard,errors=remount-ro 0 0
tmpfs /home/kubernetes/containerized_mounter/rootfs/var/lib/kubelet/pki tmpfs rw,relatime,inode64 0 0
proc /home/kubernetes/containerized_mounter/rootfs/proc proc ro,relatime 0 0
proc /home/kubernetes/containerized_mounter/rootfs/proc proc rw,nosuid,nodev,noexec,relatime 0 0
devtmpfs /home/kubernetes/containerized_mounter/rootfs/dev devtmpfs ro,relatime,size=4065432k,nr_inodes=1016358,mode=755,inode64 0 0
devtmpfs /home/kubernetes/containerized_mounter/rootfs/dev devtmpfs rw,relatime,size=4065432k,nr_inodes=1016358,mode=755,inode64 0 0`: true,
} {
tmpFile, err := ioutil.TempFile("", "mounts-")
assert.NoError(t, err)

defer os.Remove(tmpFile.Name())

_, err = tmpFile.WriteString(input)
assert.NoError(t, err)

assert.Equal(t, expectedResult, isCgroupV1(tmpFile.Name()))

err = tmpFile.Close()
assert.NoError(t, err)
}
}
func TestParsegroupControllerPath(t *testing.T) {
// Test cases
cases := []struct {
Expand Down Expand Up @@ -478,7 +375,6 @@ func TestGetCgroupInode(t *testing.T) {
t.Run(tc.description, func(t *testing.T) {
sysFsCgroupPath := path.Join(os.TempDir(), "sysfscgroup")
groupControllerPath := path.Join(sysFsCgroupPath, tc.controller, tc.cgroupNodeDir)
t.Log(groupControllerPath)
err := os.MkdirAll(groupControllerPath, 0755)
require.NoError(t, err)
defer os.RemoveAll(groupControllerPath)
Expand Down

0 comments on commit 421ae2b

Please sign in to comment.