mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Base CPUManager state reconciliation on container state, not pod state
This commit is contained in:
parent
f6cf9b8ce9
commit
f2acbf6607
@ -367,26 +367,39 @@ func (m *manager) reconcileState() (success []reconciledContainer, failure []rec
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether container is present in state, there may be 3 reasons why it's not present:
|
cstatus, err := findContainerStatusByName(&pstatus, container.Name)
|
||||||
// - policy does not want to track the container
|
if err != nil {
|
||||||
// - kubelet has just been restarted - and there is no previous state file
|
klog.Warningf("[cpumanager] reconcileState: skipping container; container status not found in pod status (pod: %s, container: %s, error: %v)", pod.Name, container.Name, err)
|
||||||
// - container has been removed from state by RemoveContainer call (DeletionTimestamp is set)
|
failure = append(failure, reconciledContainer{pod.Name, container.Name, ""})
|
||||||
if _, ok := m.state.GetCPUSet(string(pod.UID), container.Name); !ok {
|
continue
|
||||||
if pstatus.Phase == v1.PodRunning && pod.DeletionTimestamp == nil {
|
|
||||||
klog.V(4).Infof("[cpumanager] reconcileState: container is not present in state - trying to add (pod: %s, container: %s, container id: %s)", pod.Name, container.Name, containerID)
|
|
||||||
err := m.AddContainer(pod, &container, containerID)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("[cpumanager] reconcileState: failed to add container (pod: %s, container: %s, container id: %s, error: %v)", pod.Name, container.Name, containerID, err)
|
|
||||||
failure = append(failure, reconciledContainer{pod.Name, container.Name, containerID})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// if DeletionTimestamp is set, pod has already been removed from state
|
|
||||||
// skip the pod/container since it's not running and will be deleted soon
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cstatus.State.Waiting != nil ||
|
||||||
|
(cstatus.State.Waiting == nil && cstatus.State.Running == nil && cstatus.State.Terminated == nil) {
|
||||||
|
klog.Warningf("[cpumanager] reconcileState: skipping container; container still in the waiting state (pod: %s, container: %s)", pod.Name, container.Name)
|
||||||
|
failure = append(failure, reconciledContainer{pod.Name, container.Name, ""})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if cstatus.State.Terminated != nil {
|
||||||
|
// Since the container is terminated, we know it is safe to
|
||||||
|
// remove it without any reconciliation. Removing the container
|
||||||
|
// will also remove it from the `containerMap` so that this
|
||||||
|
// container will be skipped next time around the loop.
|
||||||
|
_, _, err := m.containerMap.GetContainerRef(containerID)
|
||||||
|
if err == nil {
|
||||||
|
klog.Warningf("[cpumanager] reconcileState: skipping container; already terminated (pod: %s, container id: %s)", pod.Name, containerID)
|
||||||
|
err := m.RemoveContainer(containerID)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("[cpumanager] reconcileState: failed to remove container (pod: %s, container id: %s, error: %v)", pod.Name, containerID, err)
|
||||||
|
failure = append(failure, reconciledContainer{pod.Name, container.Name, containerID})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
m.containerMap.Add(string(pod.UID), container.Name, containerID)
|
||||||
|
|
||||||
cset := m.state.GetCPUSetOrDefault(string(pod.UID), container.Name)
|
cset := m.state.GetCPUSetOrDefault(string(pod.UID), container.Name)
|
||||||
if cset.IsEmpty() {
|
if cset.IsEmpty() {
|
||||||
// NOTE: This should not happen outside of tests.
|
// NOTE: This should not happen outside of tests.
|
||||||
@ -424,6 +437,15 @@ func findContainerIDByName(status *v1.PodStatus, name string) (string, error) {
|
|||||||
return "", fmt.Errorf("unable to find ID for container with name %v in pod status (it may not be running)", name)
|
return "", fmt.Errorf("unable to find ID for container with name %v in pod status (it may not be running)", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findContainerStatusByName(status *v1.PodStatus, name string) (*v1.ContainerStatus, error) {
|
||||||
|
for _, status := range append(status.InitContainerStatuses, status.ContainerStatuses...) {
|
||||||
|
if status.Name == name {
|
||||||
|
return &status, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unable to find status for container with name %v in pod status (it may not be running)", name)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *manager) updateContainerCPUSet(containerID string, cpus cpuset.CPUSet) error {
|
func (m *manager) updateContainerCPUSet(containerID string, cpus cpuset.CPUSet) error {
|
||||||
// TODO: Consider adding a `ResourceConfigForContainer` helper in
|
// TODO: Consider adding a `ResourceConfigForContainer` helper in
|
||||||
// helpers_linux.go similar to what exists for pods.
|
// helpers_linux.go similar to what exists for pods.
|
||||||
|
@ -701,6 +701,9 @@ func TestReconcileState(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "fakeContainerName",
|
Name: "fakeContainerName",
|
||||||
ContainerID: "docker://fakeContainerID",
|
ContainerID: "docker://fakeContainerID",
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Running: &v1.ContainerStateRunning{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -737,6 +740,9 @@ func TestReconcileState(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "fakeContainerName",
|
Name: "fakeContainerName",
|
||||||
ContainerID: "docker://fakeContainerID",
|
ContainerID: "docker://fakeContainerID",
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Running: &v1.ContainerStateRunning{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -752,7 +758,7 @@ func TestReconcileState(t *testing.T) {
|
|||||||
expectFailedContainerName: "",
|
expectFailedContainerName: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "cpu manager reconclie - pod status not found",
|
description: "cpu manager reconcile - pod status not found",
|
||||||
activePods: []*v1.Pod{
|
activePods: []*v1.Pod{
|
||||||
{
|
{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
@ -777,7 +783,7 @@ func TestReconcileState(t *testing.T) {
|
|||||||
expectFailedContainerName: "",
|
expectFailedContainerName: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "cpu manager reconclie - container id not found",
|
description: "cpu manager reconcile - container state not found",
|
||||||
activePods: []*v1.Pod{
|
activePods: []*v1.Pod{
|
||||||
{
|
{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
@ -830,6 +836,9 @@ func TestReconcileState(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "fakeContainerName",
|
Name: "fakeContainerName",
|
||||||
ContainerID: "docker://fakeContainerID",
|
ContainerID: "docker://fakeContainerID",
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Running: &v1.ContainerStateRunning{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -866,6 +875,9 @@ func TestReconcileState(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "fakeContainerName",
|
Name: "fakeContainerName",
|
||||||
ContainerID: "docker://fakeContainerID",
|
ContainerID: "docker://fakeContainerID",
|
||||||
|
State: v1.ContainerState{
|
||||||
|
Running: &v1.ContainerStateRunning{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user