From c254933688ee2c9518cdcdb1c434f20fb9929f3d Mon Sep 17 00:00:00 2001 From: amrut-asm Date: Mon, 2 Oct 2023 19:00:54 +0530 Subject: [PATCH] Allow custom cgroup2 path --- felix/bpf/bpfdefs/defs.go | 14 ++++++++++++-- felix/bpf/utils/utils.go | 17 +++++++++-------- node/pkg/nodeinit/calico-init_linux.go | 11 ++++++----- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/felix/bpf/bpfdefs/defs.go b/felix/bpf/bpfdefs/defs.go index fbcc0731c4b..f9127a8899f 100644 --- a/felix/bpf/bpfdefs/defs.go +++ b/felix/bpf/bpfdefs/defs.go @@ -14,10 +14,20 @@ package bpfdefs +import "os" + const ( - DefaultBPFfsPath = "/sys/fs/bpf" - CgroupV2Path = "/run/calico/cgroup" + DefaultBPFfsPath = "/sys/fs/bpf" + DefaultCgroupV2Path = "/run/calico/cgroup" GlobalPinDir = DefaultBPFfsPath + "/tc/globals/" ObjectDir = "/usr/lib/calico/bpf" ) + +func GetCgroupV2Path() string { + cgroupV2CustomPath := os.Getenv("CALICO_CGROUP_PATH") + if cgroupV2CustomPath == "" { + return DefaultCgroupV2Path + } + return cgroupV2CustomPath +} diff --git a/felix/bpf/utils/utils.go b/felix/bpf/utils/utils.go index f3e1179423b..71a1e10db8b 100644 --- a/felix/bpf/utils/utils.go +++ b/felix/bpf/utils/utils.go @@ -74,27 +74,28 @@ func MaybeMountBPFfs() (string, error) { func MaybeMountCgroupV2() (string, error) { var err error - if err := os.MkdirAll(bpfdefs.CgroupV2Path, 0700); err != nil { + cgroupV2Path := bpfdefs.GetCgroupV2Path() + if err := os.MkdirAll(cgroupV2Path, 0700); err != nil { return "", err } - mnt, err := isMount(bpfdefs.CgroupV2Path) + mnt, err := isMount(cgroupV2Path) if err != nil { - return "", fmt.Errorf("error checking if %s is a mount: %v", bpfdefs.CgroupV2Path, err) + return "", fmt.Errorf("error checking if %s is a mount: %v", cgroupV2Path, err) } - fsCgroup, err := isCgroupV2(bpfdefs.CgroupV2Path) + fsCgroup, err := isCgroupV2(cgroupV2Path) if err != nil { - return "", fmt.Errorf("error checking if %s is CgroupV2: %v", bpfdefs.CgroupV2Path, err) + return "", fmt.Errorf("error checking if %s is CgroupV2: %v", cgroupV2Path, err) } if !mnt { - err = mountCgroupV2(bpfdefs.CgroupV2Path) + err = mountCgroupV2(cgroupV2Path) } else if !fsCgroup { - err = fmt.Errorf("something that's not cgroup v2 is already mounted in %s", bpfdefs.CgroupV2Path) + err = fmt.Errorf("something that's not cgroup v2 is already mounted in %s", cgroupV2Path) } - return bpfdefs.CgroupV2Path, err + return cgroupV2Path, err } func mountCgroupV2(path string) error { diff --git a/node/pkg/nodeinit/calico-init_linux.go b/node/pkg/nodeinit/calico-init_linux.go index 60af851b202..4c7a72611dc 100644 --- a/node/pkg/nodeinit/calico-init_linux.go +++ b/node/pkg/nodeinit/calico-init_linux.go @@ -96,6 +96,7 @@ func ensureCgroupV2Filesystem() error { return fmt.Errorf("failed to open %s. err: %w", mountInfoFile, err) } scanner := bufio.NewScanner(mounts) + cgroupV2Path := bpfdefs.GetCgroupV2Path() for scanner.Scan() { // An example line in mountinfo file: // 35 24 0:30 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime shared:9 - cgroup2 cgroup2 rw,nsdelegate,memory_recursiveprot @@ -106,7 +107,7 @@ func ensureCgroupV2Filesystem() error { if len(extraInfo) > 1 { fsType := strings.Split(extraInfo[1], " ")[0] // fsType is the first string after - - if mountPoint == bpfdefs.CgroupV2Path && fsType == "cgroup2" { + if mountPoint == cgroupV2Path && fsType == "cgroup2" { logrus.Info("Cgroup2 filesystem is mounted.") return nil } @@ -119,13 +120,13 @@ func ensureCgroupV2Filesystem() error { // If we get here, the Cgroup2 filesystem is not mounted. Try to mount it. logrus.Info("Cgroup2 filesystem is not mounted. Trying to mount it...") - err = os.MkdirAll(bpfdefs.CgroupV2Path, 0700) + err = os.MkdirAll(cgroupV2Path, 0700) if err != nil { - return fmt.Errorf("failed to prepare mount point: %v. err: %w.", bpfdefs.CgroupV2Path, err) + return fmt.Errorf("failed to prepare mount point: %v. err: %w", cgroupV2Path, err) } - logrus.Infof("Mount point %s is ready for mounting root cgroup2 fs.", bpfdefs.CgroupV2Path) + logrus.Infof("Mount point %s is ready for mounting root cgroup2 fs", cgroupV2Path) - mountCmd := exec.Command("mountns", bpfdefs.CgroupV2Path) + mountCmd := exec.Command("mountns", cgroupV2Path) out, err := mountCmd.Output() logrus.Debugf("Executed %v. err:%v out:\n%s", mountCmd, err, out) if err != nil {