-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
entrypoint: fix regression for cgroup v2 #2014
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2021 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. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
if [[ ! -f "/sys/fs/cgroup/cgroup.controllers" ]]; then | ||
echo 'ERROR: this script should not be called on cgroup v1 hosts' >&2 | ||
exit 1 | ||
fi | ||
|
||
# NOTE: we can't use `test -s` because cgroup.procs is not a regular file. | ||
if [[ -n "$(cat /sys/fs/cgroup/cgroup.procs)" ]]; then | ||
echo 'ERROR: this script needs /sys/fs/cgroup/cgroup.procs to be empty (for writing the top-level cgroup.subtree_control)' >&2 | ||
# So, this script needs to be called after launching systemd. | ||
# This script cannot be called from /usr/local/bin/entrypoint. | ||
exit 1 | ||
fi | ||
|
||
ensure_subtree_control() { | ||
local group=$1 | ||
# When cgroup.controllers is like "cpu cpuset memory io pids", | ||
# cgroup.subtree_control is written with "+cpu +cpuset +memory +io +pids" . | ||
sed -e 's/ / +/g' -e 's/^/+/' <"/sys/fs/cgroup/$group/cgroup.controllers" >"/sys/fs/cgroup/$group/cgroup.subtree_control" | ||
} | ||
|
||
# kubelet requires all the controllers (including hugetlb) in /sys/fs/cgroup/cgroup.controllers to be available in | ||
# /sys/fs/cgroup/kubelet/cgroup.subtree_control. | ||
# | ||
# We need to update the top-level cgroup.subtree_controllers as well, because hugetlb is not present in the file by default. | ||
ensure_subtree_control / | ||
mkdir -p /sys/fs/cgroup/kubelet | ||
ensure_subtree_control /kubelet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,26 @@ mount_kubelet_cgroup_root() { | |
} | ||
|
||
fix_cgroup() { | ||
if [[ -f "/sys/fs/cgroup/cgroup.controllers" ]]; then | ||
echo 'INFO: detected cgroup v2' | ||
# Both Docker and Podman enable CgroupNS on cgroup v2 hosts by default. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for leaving the detailed comments! |
||
# | ||
# So mostly we do not need to mess around with the cgroup path stuff, | ||
# however, we still need to create the "/kubelet" cgroup at least. | ||
# (Otherwise kubelet fails with `cgroup-root ["kubelet"] doesn't exist` error, see #1969) | ||
# | ||
# The "/kubelet" cgroup is created in ExecStartPre of the kubeadm service. | ||
# | ||
# [FAQ: Why not create "/kubelet" cgroup here?] | ||
# We can't create the cgroup with controllers here, because /sys/fs/cgroup/cgroup.subtree_control is empty. | ||
# And yet we can't write controllers to /sys/fs/cgroup/cgroup.subtree_control by ourselves either, because | ||
# /sys/fs/cgroup/cgroup.procs is not empty at this moment. | ||
# | ||
# After switching from this entrypoint script to systemd, systemd evacuates the processes in the root | ||
# group to "/init.scope" group, so we can write the root subtree_control and create "/kubelet" cgroup. | ||
return | ||
fi | ||
echo 'INFO: detected cgroup v1' | ||
echo 'INFO: fix cgroup mounts for all subsystems' | ||
# see: https://d2iq.com/blog/running-kind-inside-a-kubernetes-cluster-for-continuous-integration | ||
# capture initial state before modifying | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this script re-entrant? it tentatively looks like it is not.
under kubeadm kubelet will crash and restart many times before the config is written and it finally starts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the first execution of
create-kubelet-cgroup-v2
is meaningful. So, I don't see any issue about restarting.