-
Notifications
You must be signed in to change notification settings - Fork 115
OCPBUGS-65896: controllers: Prevent Progressing=True when scaling only #836
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| package scaling | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| "k8s.io/utils/clock" | ||
| "k8s.io/utils/ptr" | ||
|
|
||
| operatorv1 "github.com/openshift/api/operator/v1" | ||
| applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| replicasChangedAnnotation = "authentication.operator.openshift.io/replicas-changed" | ||
| deploymentProgressedAnnotation = "authentication.operator.openshift.io/deployment-progressed" | ||
| scalingBeginTimeout = 1 * time.Minute | ||
| ) | ||
|
|
||
| // ProcessDeployment ensures the operator does not end up progressing on scaling. | ||
| // We define that scaling happens any time .spec.replicas is the only field that changes. | ||
| // The idea is then as follows: | ||
| // | ||
| // 1. When the replicas field is updated, store the change timestamp in a deployment annotation. | ||
| // 2. When the deployment eventually starts progressing, add another annotation so that we know it happened. | ||
| // 3. When the deployment hasn't progressing for too long, or it has finished progressing, remove all annotations. | ||
| // | ||
| // When the timestamp annotation is present, we should overwrite Progressing to be false. | ||
| // | ||
| // So, ProcessDeployment amends the expected deployment in place, also returning any conditions to set on the operator. | ||
| func ProcessDeployment(existing, expected *appsv1.Deployment, clock clock.Clock, conditionPrefix string) ([]*applyoperatorv1.OperatorConditionApplyConfiguration, error) { | ||
| if !specsEqualIgnoringReplicas(existing, expected) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if expected.Annotations == nil { | ||
| expected.Annotations = make(map[string]string) | ||
| } | ||
|
|
||
| if !ptr.Equal(existing.Spec.Replicas, expected.Spec.Replicas) { | ||
| expected.Annotations[replicasChangedAnnotation] = clock.Now().UTC().Format(time.RFC3339) | ||
| return cancelProgressing(conditionPrefix), nil | ||
| } | ||
|
|
||
| var replicasChangedAt time.Time | ||
| if v, ok := existing.Annotations[replicasChangedAnnotation]; ok { | ||
| var err error | ||
| replicasChangedAt, err = time.Parse(time.RFC3339, v) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to parse annotation %q = %q: %w", replicasChangedAnnotation, v, err) | ||
| } | ||
| } | ||
| if replicasChangedAt.IsZero() { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Cancel scaling if we are done, or the whole process has reached the specified timeout. | ||
| startedProgressing := existing.Annotations[deploymentProgressedAnnotation] == "true" | ||
| if !isDeploymentProgressing(existing.Status) && (startedProgressing || clock.Since(replicasChangedAt) > scalingBeginTimeout) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| expected.Annotations[replicasChangedAnnotation] = existing.Annotations[replicasChangedAnnotation] | ||
| if startedProgressing || isDeploymentProgressing(existing.Status) { | ||
| expected.Annotations[deploymentProgressedAnnotation] = "true" | ||
| } | ||
| return cancelProgressing(conditionPrefix), nil | ||
| } | ||
|
|
||
| // specsEqualIgnoringReplicas returns true when the deployment specs are the same or diff only in the replicas field. | ||
| // The function returns false automatically when one of the deployments is nil. | ||
| func specsEqualIgnoringReplicas(existing, expected *appsv1.Deployment) bool { | ||
| if existing == nil || expected == nil { | ||
| return false | ||
| } | ||
|
|
||
| s1 := &existing.Spec | ||
| s2 := &expected.Spec | ||
|
|
||
| if !ptr.Equal(s1.Replicas, s2.Replicas) { | ||
| s2 = s2.DeepCopy() | ||
| s2.Replicas = s1.Replicas | ||
| } | ||
| return equality.Semantic.DeepEqual(s1, s2) | ||
| } | ||
|
|
||
| // isDeploymentProgressing returns whether the given deployment is progressing. | ||
| func isDeploymentProgressing(status appsv1.DeploymentStatus) bool { | ||
| for _, cond := range status.Conditions { | ||
| if cond.Type == appsv1.DeploymentProgressing { | ||
| return !(cond.Status == corev1.ConditionTrue && cond.Reason == "NewReplicaSetAvailable") | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func cancelProgressing(conditionPrefix string) []*applyoperatorv1.OperatorConditionApplyConfiguration { | ||
| return []*applyoperatorv1.OperatorConditionApplyConfiguration{ | ||
| applyoperatorv1.OperatorCondition(). | ||
| WithType(fmt.Sprintf("%sDeploymentProgressing", conditionPrefix)). | ||
| WithStatus(operatorv1.ConditionFalse). | ||
| WithReason("AsExpected"). | ||
| WithMessage("Scaling replicas only"), | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
I understand what this is trying to solve, but the place is not correct. We cannot expect each deployment instance to have such a specific logic dedicated to progressing condition. This responsibility has to lie in the library-go or even better in the deployment controller.
The deployment controller should correctly report the progressing state via the Progressing condition. I think the best course of action should be to remove the Generation+ObservedGeneration check from library-go. As we can see in OCPBUGS-65896, not every spec update (e.g. scaling) should result in a progressing state, so generation tracking is not well equipped to do that.
Now there will be a small risk of reporting late the progressing state before the controller reacts to the real template change. This should be okay, unless the controller is down (this results in much bigger issues) or there is a long queue in the controller. Normally the reaction time should be similar to what we would have processing the annotation changes. I think it should be okay to live with this small delay.
Please also note openshift/cluster-image-registry-operator#1293.
Also, I think it might be useful to surface ProgressDeadlineExceeded reason in a degraded condition to report that the deployment controller timed out rolling out the new changes.