Skip to content

Commit

Permalink
Merge pull request #8512 from tomastigera/auto-pick-of-#8085-release-…
Browse files Browse the repository at this point in the history
…v3.27

[release-v3.27] Auto pick #8085: Allow custom cgroup2 path
  • Loading branch information
tomastigera committed Feb 15, 2024
2 parents 396de4f + c254933 commit e06e0c9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
14 changes: 12 additions & 2 deletions felix/bpf/bpfdefs/defs.go
Expand Up @@ -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
}
17 changes: 9 additions & 8 deletions felix/bpf/utils/utils.go
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions node/pkg/nodeinit/calico-init_linux.go
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit e06e0c9

Please sign in to comment.