Skip to content

Commit

Permalink
Extract functions to lower cyclic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Apr 24, 2022
1 parent f63cb3d commit 8bce297
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions pkg/drivers/kic/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ func hasMemorySwapCgroup() bool {
return memcgSwap
}

func hasCPUCfsPeriod() bool {
cpuCfsPeriod := true
if runtime.GOOS == "linux" {
if _, err := os.Stat("/sys/fs/cgroup/cpu/cpu.cfs_period_us"); os.IsNotExist(err) {
cpuCfsPeriod = false
}
if !cpuCfsPeriod {
// requires CONFIG_CFS_BANDWIDTH
klog.Warning("Your kernel does not support CPU cfs period or the cgroup is not mounted.")
}
}
return cpuCfsPeriod
}

func hasCPUCfsQuota() bool {
cpuCfsQuota := true
if runtime.GOOS == "linux" {
if _, err := os.Stat("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"); os.IsNotExist(err) {
cpuCfsQuota = false
}
if !cpuCfsQuota {
// requires CONFIG_CFS_BANDWIDTH
klog.Warning("Your kernel does not support CPU cfs quota or the cgroup is not mounted.")
}
}
return cpuCfsQuota
}

// CreateContainerNode creates a new container node
func CreateContainerNode(p CreateParams) error {
// on windows os, if docker desktop is using Windows Containers. Exit early with error
Expand Down Expand Up @@ -196,9 +224,6 @@ func CreateContainerNode(p CreateParams) error {
runArgs = append(runArgs, "-v", fmt.Sprintf("%s:/cache", detect.ImageCacheDir()))
}

memcgSwap := hasMemorySwapCgroup()
memcg := HasMemoryCgroup()

// https://www.freedesktop.org/wiki/Software/systemd/ContainerInterface/
var virtualization string
if p.OCIBinary == Podman { // enable execing in /var
Expand All @@ -215,6 +240,8 @@ func CreateContainerNode(p CreateParams) error {
virtualization = "docker" // VIRTUALIZATION_DOCKER
}

memcgSwap := hasMemorySwapCgroup()
memcg := HasMemoryCgroup()
if memcg {
runArgs = append(runArgs, fmt.Sprintf("--memory=%s", p.Memory))
}
Expand All @@ -223,21 +250,8 @@ func CreateContainerNode(p CreateParams) error {
runArgs = append(runArgs, fmt.Sprintf("--memory-swap=%s", p.Memory))
}

cpuCfsPeriod := true
cpuCfsQuota := true
if runtime.GOOS == "linux" {
if _, err := os.Stat("/sys/fs/cgroup/cpu/cpu.cfs_period_us"); os.IsNotExist(err) {
cpuCfsPeriod = false
}
if _, err := os.Stat("/sys/fs/cgroup/cpu/cpu.cfs_quota_us"); os.IsNotExist(err) {
cpuCfsQuota = false
}
if !cpuCfsPeriod || !cpuCfsQuota {
// requires CONFIG_CFS_BANDWIDTH
klog.Warning("Your kernel does not support CPU cfs period/quota or the cgroup is not mounted.")
}
}

cpuCfsPeriod := hasCPUCfsPeriod()
cpuCfsQuota := hasCPUCfsQuota()
if cpuCfsPeriod && cpuCfsQuota {
runArgs = append(runArgs, fmt.Sprintf("--cpus=%s", p.CPUs))
}
Expand Down

0 comments on commit 8bce297

Please sign in to comment.