Skip to content
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

OCPBUGS-23435: workloadctl: account for terminating pods #1732

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pkg/operator/apiserver/controller/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,21 @@ func (c *Controller) updateOperatorStatus(ctx context.Context, previousStatus *o
desiredReplicas = *(workload.Spec.Replicas)
}

selector, err := metav1.LabelSelectorAsSelector(workload.Spec.Selector)
if err != nil {
return fmt.Errorf("failed to construct label selector: %v", err)
}
matchingPods, err := c.podsLister.List(selector)
if err != nil {
return err
}
// Terminatig pods don't account for any of the other status fields but
// still can exist in a state when they are accepting connections and would
// contribute to unexpected behavior if we report Progressing=False.
// The case of too many pods might occur for example if `TerminationGracePeriodSeconds`
// is set.
tooManyMatchingPods := int32(len(matchingPods)) > desiredReplicas
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer would be to test if all the pods have the same hash (pod-template-hash label). This would work well in combination with workloadIsBeingUpdated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is pod-template-hash set on 100% of our deployments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the pod spec can be the same, only the underlying config changed. Would that still work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deployment rollout has to be triggered somehow and it seems that you are triggering it with the resource revisions of the dependencies. So yes it should work.

https://github.com/openshift/cluster-authentication-operator/blob/b415439ebab2829c8da1ea17c05f2ac75fe5dbe8/pkg/controllers/deployment/default_deployment.go#L54


// If the workload is up to date, then we are no longer progressing
workloadAtHighestGeneration := workload.ObjectMeta.Generation == workload.Status.ObservedGeneration
workloadIsBeingUpdated := workload.Status.UpdatedReplicas < desiredReplicas
Expand All @@ -274,6 +289,10 @@ func (c *Controller) updateOperatorStatus(ctx context.Context, previousStatus *o
deploymentProgressingCondition.Status = operatorv1.ConditionTrue
deploymentProgressingCondition.Reason = "PodsUpdating"
deploymentProgressingCondition.Message = fmt.Sprintf("deployment/%s.%s: %d/%d pods have been updated to the latest generation", workload.Name, c.targetNamespace, workload.Status.UpdatedReplicas, desiredReplicas)
} else if tooManyMatchingPods {
deploymentProgressingCondition.Status = operatorv1.ConditionTrue
deploymentProgressingCondition.Reason = "PreviousGenPodsPresent"
deploymentProgressingCondition.Message = fmt.Sprintf("deployment/%s.%s: %d pod(s) from the previous generation are still present", workload.Name, c.targetNamespace, len(matchingPods)-int(desiredReplicas))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not always be correct. There may be extra pods for different reasons. E.g. if they are disrupted/evicted for some reason, the deployment controller will create extra pods to account for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you word it, then? Just too many pods?

Copy link
Member

@atiratree atiratree May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In k8s, this is considered a complete deployment, so it depends on what you want to convey. There can also be extra pods during a rollout with maxSurge, but in that case the pods will have different revisions/hashes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the message should be about pods of a different revision still existing. Would you be able to think of any other case where extra pods might cause unexpected behavior?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the terminating pods which are a subject of this bug, no.

I guess, there might be some exotic cases where the pods can be owned by another controller, but that can be safely ignored here.

} else {
deploymentProgressingCondition.Status = operatorv1.ConditionFalse
deploymentProgressingCondition.Reason = "AsExpected"
Expand Down
102 changes: 102 additions & 0 deletions pkg/operator/apiserver/controller/workload/workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,108 @@ func TestUpdateOperatorStatus(t *testing.T) {
return areCondidtionsEqual(expectedConditions, actualStatus.Conditions)
},
},
{
name: "all pods rolled out but there is an old terminating pod",
workload: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver",
Namespace: "openshift-apiserver",
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
Template: corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}}},
Replicas: ptr.To[int32](2),
},
Status: appsv1.DeploymentStatus{
AvailableReplicas: 2,
ReadyReplicas: 2,
UpdatedReplicas: 2,
},
},
pods: []*corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-0", Namespace: "openshift-apiserver",
Labels: map[string]string{"foo": "bar"},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "test",
Ready: true,
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-1", Namespace: "openshift-apiserver",
Labels: map[string]string{"foo": "bar"},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "test",
Ready: true,
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-2", Namespace: "openshift-apiserver",
Labels: map[string]string{"foo": "bar"},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{
{
Name: "test",
Ready: true,
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
},
},
},
},
},
validateOperatorStatus: func(actualStatus *operatorv1.OperatorStatus) error {
expectedConditions := []operatorv1.OperatorCondition{
{
Type: fmt.Sprintf("%sDeployment%s", defaultControllerName, operatorv1.OperatorStatusTypeAvailable),
Status: operatorv1.ConditionTrue,
Reason: "AsExpected",
Message: "",
},
{
Type: fmt.Sprintf("%sWorkloadDegraded", defaultControllerName),
Status: operatorv1.ConditionFalse,
},
{
Type: fmt.Sprintf("%sDeploymentDegraded", defaultControllerName),
Status: operatorv1.ConditionFalse,
Reason: "AsExpected",
Message: "",
},
{
Type: fmt.Sprintf("%sDeployment%s", defaultControllerName, operatorv1.OperatorStatusTypeProgressing),
Status: operatorv1.ConditionTrue,
Reason: "PreviousGenPodsPresent",
Message: "deployment/apiserver.openshift-apiserver: 1 pod(s) from the previous generation are still present",
},
}
return areCondidtionsEqual(expectedConditions, actualStatus.Conditions)
},
},
}

for _, scenario := range scenarios {
Expand Down