github.com/cilium/cilium@v1.16.2/pkg/cgroups/cgroups.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package cgroups
     5  
     6  import (
     7  	"sync"
     8  
     9  	"github.com/cilium/cilium/pkg/defaults"
    10  	"github.com/cilium/cilium/pkg/logging"
    11  	"github.com/cilium/cilium/pkg/logging/logfields"
    12  )
    13  
    14  var (
    15  	// Path to where cgroup is mounted
    16  	cgroupRoot = defaults.DefaultCgroupRoot
    17  
    18  	// Only mount a single instance
    19  	cgrpMountOnce sync.Once
    20  )
    21  
    22  var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cgroups")
    23  
    24  // setCgroupRoot will set the path to mount cgroupv2
    25  func setCgroupRoot(path string) {
    26  	cgroupRoot = path
    27  }
    28  
    29  // GetCgroupRoot returns the path for the cgroupv2 mount
    30  func GetCgroupRoot() string {
    31  	return cgroupRoot
    32  }
    33  
    34  // CheckOrMountCgrpFS this checks if the cilium cgroup2 root mount point is
    35  // mounted and if not mounts it. If mapRoot is "" it will mount the default
    36  // location. It is harmless to have multiple cgroupv2 root mounts so unlike
    37  // BPFFS case we simply mount at the cilium default regardless if the system
    38  // has another mount created by systemd or otherwise.
    39  func CheckOrMountCgrpFS(mapRoot string) {
    40  	cgrpMountOnce.Do(func() {
    41  		if mapRoot == "" {
    42  			mapRoot = cgroupRoot
    43  		}
    44  
    45  		if err := cgrpCheckOrMountLocation(mapRoot); err != nil {
    46  			log.WithError(err).
    47  				Warn("Failed to mount cgroupv2. Any functionality that needs cgroup (e.g.: socket-based LB) will not work.")
    48  		} else {
    49  			log.Infof("Mounted cgroupv2 filesystem at %s", mapRoot)
    50  		}
    51  	})
    52  }