mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	check root cpu.stat instead of kernel version for cgroup v2
This commit is contained in:
		@@ -45,10 +45,13 @@ import (
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	// Warning message for the users still using cgroup v1
 | 
			
		||||
	CgroupV1MaintenanceModeWarning = "Cgroup v1 support is in maintenance mode, please migrate to Cgroup v2."
 | 
			
		||||
	CgroupV1MaintenanceModeWarning = "cgroup v1 support is in maintenance mode, please migrate to cgroup v2"
 | 
			
		||||
 | 
			
		||||
	// Warning message for the users using cgroup v2 on kernel older than 5.8
 | 
			
		||||
	CgroupV2KernelWarning = "Cgroup v2 is being used on a kernel older than 5.8, which may have limited functionality."
 | 
			
		||||
	// Warning message for the users using cgroup v2 on kernel doesn't support root `cpu.stat`.
 | 
			
		||||
	// `cpu.stat` was added to root cgroup in kernel 5.8.
 | 
			
		||||
	// (ref: https://github.com/torvalds/linux/commit/936f2a70f2077f64fab1dcb3eca71879e82ecd3f)
 | 
			
		||||
	CgroupV2KernelWarning = "cgroup v2 is being used on a kernel, which doesn't support root `cpu.stat`." +
 | 
			
		||||
		"Kubelet will continue, but may experience instability or wrong behavior"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ActivePodsFunc func() []*v1.Pod
 | 
			
		||||
 
 | 
			
		||||
@@ -77,7 +77,6 @@ const (
 | 
			
		||||
	FailedPrepareDynamicResources        = "FailedPrepareDynamicResources"
 | 
			
		||||
	PossibleMemoryBackedVolumesOnDisk    = "PossibleMemoryBackedVolumesOnDisk"
 | 
			
		||||
	CgroupV1                             = "CgroupV1"
 | 
			
		||||
	CgroupV2                             = "CgroupV2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Image manager event reason list
 | 
			
		||||
 
 | 
			
		||||
@@ -52,7 +52,6 @@ import (
 | 
			
		||||
	"k8s.io/apimachinery/pkg/types"
 | 
			
		||||
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/sets"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/version"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/wait"
 | 
			
		||||
	utilfeature "k8s.io/apiserver/pkg/util/feature"
 | 
			
		||||
	clientset "k8s.io/client-go/kubernetes"
 | 
			
		||||
@@ -121,7 +120,6 @@ import (
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/volumemanager"
 | 
			
		||||
	httpprobe "k8s.io/kubernetes/pkg/probe/http"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/security/apparmor"
 | 
			
		||||
	utilkernel "k8s.io/kubernetes/pkg/util/kernel"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/util/oom"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/volume"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/volume/csi"
 | 
			
		||||
@@ -1647,7 +1645,9 @@ func (kl *Kubelet) Run(updates <-chan kubetypes.PodUpdate) {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	kl.cgroupVersionCheck()
 | 
			
		||||
	if err := kl.cgroupVersionCheck(); err != nil {
 | 
			
		||||
		klog.V(2).InfoS("Warning: cgroup check", "error", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Start volume manager
 | 
			
		||||
	go kl.volumeManager.Run(kl.sourcesReady, wait.NeverStop)
 | 
			
		||||
@@ -3068,22 +3068,3 @@ func (kl *Kubelet) PrepareDynamicResources(pod *v1.Pod) error {
 | 
			
		||||
func (kl *Kubelet) UnprepareDynamicResources(pod *v1.Pod) error {
 | 
			
		||||
	return kl.containerManager.UnprepareDynamicResources(pod)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (kl *Kubelet) cgroupVersionCheck() {
 | 
			
		||||
	cgroupVersion := kl.containerManager.GetNodeConfig().CgroupVersion
 | 
			
		||||
	if cgroupVersion == 1 {
 | 
			
		||||
		kl.recorder.Eventf(kl.nodeRef, v1.EventTypeWarning, events.CgroupV1, cm.CgroupV1MaintenanceModeWarning)
 | 
			
		||||
		klog.V(2).InfoS("Warning: cgroup v1", "message", cm.CgroupV1MaintenanceModeWarning)
 | 
			
		||||
	} else {
 | 
			
		||||
		kernelVersion, err := utilkernel.GetVersion()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			klog.V(2).ErrorS(err, "Warning: cannot determine kernel version, unable to determine if cgroup v2 is supported", "message", cm.CgroupV1MaintenanceModeWarning)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if kernelVersion.LessThan(version.MustParseGeneric(utilkernel.CgroupV2KernelVersion)) {
 | 
			
		||||
			kl.recorder.Eventf(kl.nodeRef, v1.EventTypeWarning, events.CgroupV2, cm.CgroupV2KernelWarning)
 | 
			
		||||
			klog.V(2).InfoS("Warning: cgroup v2", "message", cm.CgroupV2KernelWarning)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	metrics.CgroupVersion.Set(float64(cgroupVersion))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										51
									
								
								pkg/kubelet/kubelet_linux.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								pkg/kubelet/kubelet_linux.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
//go:build linux
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Copyright 2024 The Kubernetes Authors.
 | 
			
		||||
 | 
			
		||||
Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
you may not use this file except in compliance with the License.
 | 
			
		||||
You may obtain a copy of the License at
 | 
			
		||||
 | 
			
		||||
    http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 | 
			
		||||
Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package kubelet
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	v1 "k8s.io/api/core/v1"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/cm"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/cm/util"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/events"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/metrics"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (kl *Kubelet) cgroupVersionCheck() error {
 | 
			
		||||
	cgroupVersion := kl.containerManager.GetNodeConfig().CgroupVersion
 | 
			
		||||
	metrics.CgroupVersion.Set(float64(cgroupVersion))
 | 
			
		||||
	switch cgroupVersion {
 | 
			
		||||
	case 1:
 | 
			
		||||
		kl.recorder.Eventf(kl.nodeRef, v1.EventTypeWarning, events.CgroupV1, cm.CgroupV1MaintenanceModeWarning)
 | 
			
		||||
		return errors.New(cm.CgroupV1MaintenanceModeWarning)
 | 
			
		||||
	case 2:
 | 
			
		||||
		cpustat := filepath.Join(util.CgroupRoot, "cpu.stat")
 | 
			
		||||
		if _, err := os.Stat(cpustat); os.IsNotExist(err) {
 | 
			
		||||
			// if `/sys/fs/cgroup/cpu.stat` does not exist, log a warning
 | 
			
		||||
			return errors.New(cm.CgroupV2KernelWarning)
 | 
			
		||||
		}
 | 
			
		||||
	default:
 | 
			
		||||
		return fmt.Errorf("unsupported cgroup version: %d", cgroupVersion)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								pkg/kubelet/kubelet_others.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/kubelet/kubelet_others.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
//go:build !linux
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Copyright 2024 The Kubernetes Authors.
 | 
			
		||||
 | 
			
		||||
Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
you may not use this file except in compliance with the License.
 | 
			
		||||
You may obtain a copy of the License at
 | 
			
		||||
 | 
			
		||||
    http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 | 
			
		||||
Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package kubelet
 | 
			
		||||
 | 
			
		||||
// cgroupVersionCheck performs a version check for the cgroup.
 | 
			
		||||
// This method is not applicable for non-Linux operating systems.
 | 
			
		||||
func (kl *Kubelet) cgroupVersionCheck() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
@@ -54,8 +54,3 @@ const TmpfsNoswapSupportKernelVersion = "6.4"
 | 
			
		||||
// nftables mode with by default. This is not directly related to any specific kernel
 | 
			
		||||
// commit; see https://issues.k8s.io/122743#issuecomment-1893922424
 | 
			
		||||
const NFTablesKubeProxyKernelVersion = "5.13"
 | 
			
		||||
 | 
			
		||||
// CgroupV2KernelVersion is the minimal kernel version required for cgroup v2 support.
 | 
			
		||||
// `cpu.stat` file was added to root cgroup.
 | 
			
		||||
// (ref: https://github.com/torvalds/linux/commit/936f2a70f2077f64fab1dcb3eca71879e82ecd3f)
 | 
			
		||||
const CgroupV2KernelVersion = "5.8"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user