Skip to content

Commit 24a74f8

Browse files
authored
Merge pull request kubernetes#126595 from pacoxu/kubelet-cgroup-v2-kernel-version
[1.32]kubelet: add log and event for cgroup v2 running on kernel < 5.8
2 parents 30226e6 + 259671b commit 24a74f8

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

pkg/kubelet/cm/container_manager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ import (
4646

4747
const (
4848
// Warning message for the users still using cgroup v1
49-
CgroupV1MaintenanceModeWarning = "Cgroup v1 support is in maintenance mode, please migrate to Cgroup v2."
49+
CgroupV1MaintenanceModeWarning = "cgroup v1 support is in maintenance mode, please migrate to cgroup v2"
50+
51+
// Warning message for the users using cgroup v2 on kernel doesn't support root `cpu.stat`.
52+
// `cpu.stat` was added to root cgroup in kernel 5.8.
53+
// (ref: https://github.com/torvalds/linux/commit/936f2a70f2077f64fab1dcb3eca71879e82ecd3f)
54+
CgroupV2KernelWarning = "cgroup v2 is being used on a kernel, which doesn't support root `cpu.stat`." +
55+
"Kubelet will continue, but may experience instability or wrong behavior"
5056
)
5157

5258
type ActivePodsFunc func() []*v1.Pod

pkg/kubelet/kubelet.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,9 @@ func (kl *Kubelet) Run(updates <-chan kubetypes.PodUpdate) {
16451645
os.Exit(1)
16461646
}
16471647

1648-
kl.warnCgroupV1Usage()
1648+
if err := kl.cgroupVersionCheck(); err != nil {
1649+
klog.V(2).InfoS("Warning: cgroup check", "error", err)
1650+
}
16491651

16501652
// Start volume manager
16511653
go kl.volumeManager.Run(ctx, kl.sourcesReady)
@@ -3067,12 +3069,3 @@ func (kl *Kubelet) PrepareDynamicResources(ctx context.Context, pod *v1.Pod) err
30673069
func (kl *Kubelet) UnprepareDynamicResources(ctx context.Context, pod *v1.Pod) error {
30683070
return kl.containerManager.UnprepareDynamicResources(ctx, pod)
30693071
}
3070-
3071-
func (kl *Kubelet) warnCgroupV1Usage() {
3072-
cgroupVersion := kl.containerManager.GetNodeConfig().CgroupVersion
3073-
if cgroupVersion == 1 {
3074-
kl.recorder.Eventf(kl.nodeRef, v1.EventTypeWarning, events.CgroupV1, cm.CgroupV1MaintenanceModeWarning)
3075-
klog.V(2).InfoS("Warning: cgroup v1", "message", cm.CgroupV1MaintenanceModeWarning)
3076-
}
3077-
metrics.CgroupVersion.Set(float64(cgroupVersion))
3078-
}

pkg/kubelet/kubelet_linux.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//go:build linux
2+
3+
/*
4+
Copyright 2024 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kubelet
20+
21+
import (
22+
"errors"
23+
"fmt"
24+
"os"
25+
"path/filepath"
26+
27+
v1 "k8s.io/api/core/v1"
28+
"k8s.io/kubernetes/pkg/kubelet/cm"
29+
"k8s.io/kubernetes/pkg/kubelet/cm/util"
30+
"k8s.io/kubernetes/pkg/kubelet/events"
31+
"k8s.io/kubernetes/pkg/kubelet/metrics"
32+
)
33+
34+
func (kl *Kubelet) cgroupVersionCheck() error {
35+
cgroupVersion := kl.containerManager.GetNodeConfig().CgroupVersion
36+
metrics.CgroupVersion.Set(float64(cgroupVersion))
37+
switch cgroupVersion {
38+
case 1:
39+
kl.recorder.Eventf(kl.nodeRef, v1.EventTypeWarning, events.CgroupV1, cm.CgroupV1MaintenanceModeWarning)
40+
return errors.New(cm.CgroupV1MaintenanceModeWarning)
41+
case 2:
42+
cpustat := filepath.Join(util.CgroupRoot, "cpu.stat")
43+
if _, err := os.Stat(cpustat); os.IsNotExist(err) {
44+
// if `/sys/fs/cgroup/cpu.stat` does not exist, log a warning
45+
return errors.New(cm.CgroupV2KernelWarning)
46+
}
47+
default:
48+
return fmt.Errorf("unsupported cgroup version: %d", cgroupVersion)
49+
}
50+
return nil
51+
}

pkg/kubelet/kubelet_others.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build !linux
2+
3+
/*
4+
Copyright 2024 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package kubelet
20+
21+
// cgroupVersionCheck performs a version check for the cgroup.
22+
// This method is not applicable for non-Linux operating systems.
23+
func (kl *Kubelet) cgroupVersionCheck() error {
24+
return nil
25+
}

0 commit comments

Comments
 (0)