@@ -53,7 +53,8 @@ func (r *AuthenticationReconciler) handleHPAs(ctx context.Context, req ctrl.Requ
53
53
// Build the HPA reconciler
54
54
builder := ctrlcommon .NewSecondaryReconcilerBuilder [* autoscalingv2.HorizontalPodAutoscaler ]().
55
55
WithName (deployName + "-hpa" ).
56
- WithGenerateFns (generateHPAObject (authCR , deployName ))
56
+ WithGenerateFns (generateHPAObject (authCR , deployName )).
57
+ WithModifyFns (modifyHPA (r .needsRollout ))
57
58
58
59
subRecs = append (subRecs , builder .
59
60
WithNamespace (authCRNS ).
@@ -119,8 +120,7 @@ func generateHPAObject(instance *operatorv1alpha1.Authentication, deploymentName
119
120
}
120
121
121
122
container := deploy .Spec .Template .Spec .Containers [0 ]
122
- currentReplicas := deploy .Spec .Replicas
123
- maxReplicas := 2 * (* currentReplicas ) + 1
123
+ maxReplicas := 2 * (instance .Spec .Replicas ) + 1
124
124
125
125
memRequest := container .Resources .Requests .Memory ().Value ()
126
126
memLimit := container .Resources .Limits .Memory ().Value ()
@@ -221,3 +221,32 @@ func (r *AuthenticationReconciler) UpdateDeploymentReplicas(ctx context.Context,
221
221
222
222
return nil
223
223
}
224
+
225
+ // modifyHPA looks for relevant differences between the observed and
226
+ // generated HPAs and makes modifications to the observed HPA when
227
+ // such differences are found. Returns a boolean representing whether a
228
+ // modification was made and an error if the operation could not be completed.
229
+ func modifyHPA (needsRollout bool ) ctrlcommon.ModifyFn [* autoscalingv2.HorizontalPodAutoscaler ] {
230
+ return func (s ctrlcommon.SecondaryReconciler , ctx context.Context , observed , generated * autoscalingv2.HorizontalPodAutoscaler ) (modified bool , err error ) {
231
+ authCR , ok := s .GetPrimary ().(* operatorv1alpha1.Authentication )
232
+ if ! ok {
233
+ return
234
+ }
235
+ desiredMax := 2 * (authCR .Spec .Replicas ) + 1
236
+ if * observed .Spec .MinReplicas != authCR .Spec .Replicas || observed .Spec .MaxReplicas != desiredMax {
237
+ observed .Spec = generated .Spec
238
+ modified = true
239
+ }
240
+ if ! ctrlcommon .IsControllerOf (s .GetClient ().Scheme (), s .GetPrimary (), observed ) {
241
+ if err = controllerutil .SetControllerReference (s .GetPrimary (), observed , s .GetClient ().Scheme ()); err != nil {
242
+ return false , err
243
+ }
244
+ modified = true
245
+ }
246
+
247
+ if ! needsRollout {
248
+ return
249
+ }
250
+ return
251
+ }
252
+ }
0 commit comments