Skip to content

Commit

Permalink
Base CPUManager state reconciliation on container state, not pod state
Browse files Browse the repository at this point in the history
  • Loading branch information
klueska committed Jan 20, 2020
1 parent f6cf9b8 commit f2acbf6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
50 changes: 36 additions & 14 deletions pkg/kubelet/cm/cpumanager/cpu_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,39 @@ func (m *manager) reconcileState() (success []reconciledContainer, failure []rec
continue
}

// Check whether container is present in state, there may be 3 reasons why it's not present:
// - policy does not want to track the container
// - kubelet has just been restarted - and there is no previous state file
// - container has been removed from state by RemoveContainer call (DeletionTimestamp is set)
if _, ok := m.state.GetCPUSet(string(pod.UID), container.Name); !ok {
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)
cstatus, err := findContainerStatusByName(&pstatus, container.Name)
if err != nil {
klog.Warningf("[cpumanager] reconcileState: skipping container; container status not found in pod status (pod: %s, container: %s, error: %v)", pod.Name, container.Name, err)
failure = append(failure, reconciledContainer{pod.Name, container.Name, ""})
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 add container (pod: %s, container: %s, container id: %s, error: %v)", pod.Name, container.Name, containerID, err)
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
}
} 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
}
continue
}

m.containerMap.Add(string(pod.UID), container.Name, containerID)

cset := m.state.GetCPUSetOrDefault(string(pod.UID), container.Name)
if cset.IsEmpty() {
// NOTE: This should not happen outside of tests.
Expand Down Expand Up @@ -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)
}

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 {
// TODO: Consider adding a `ResourceConfigForContainer` helper in
// helpers_linux.go similar to what exists for pods.
Expand Down
16 changes: 14 additions & 2 deletions pkg/kubelet/cm/cpumanager/cpu_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ func TestReconcileState(t *testing.T) {
{
Name: "fakeContainerName",
ContainerID: "docker://fakeContainerID",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
Expand Down Expand Up @@ -737,6 +740,9 @@ func TestReconcileState(t *testing.T) {
{
Name: "fakeContainerName",
ContainerID: "docker://fakeContainerID",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
Expand All @@ -752,7 +758,7 @@ func TestReconcileState(t *testing.T) {
expectFailedContainerName: "",
},
{
description: "cpu manager reconclie - pod status not found",
description: "cpu manager reconcile - pod status not found",
activePods: []*v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -777,7 +783,7 @@ func TestReconcileState(t *testing.T) {
expectFailedContainerName: "",
},
{
description: "cpu manager reconclie - container id not found",
description: "cpu manager reconcile - container state not found",
activePods: []*v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -830,6 +836,9 @@ func TestReconcileState(t *testing.T) {
{
Name: "fakeContainerName",
ContainerID: "docker://fakeContainerID",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
Expand Down Expand Up @@ -866,6 +875,9 @@ func TestReconcileState(t *testing.T) {
{
Name: "fakeContainerName",
ContainerID: "docker://fakeContainerID",
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
Expand Down

0 comments on commit f2acbf6

Please sign in to comment.