Skip to content

Commit 3d879f7

Browse files
authored
updateHPA when profile size change (#1041)
1 parent 6667fdc commit 3d879f7

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

controllers/operator/hpa.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (r *AuthenticationReconciler) handleHPAs(ctx context.Context, req ctrl.Requ
5353
// Build the HPA reconciler
5454
builder := ctrlcommon.NewSecondaryReconcilerBuilder[*autoscalingv2.HorizontalPodAutoscaler]().
5555
WithName(deployName + "-hpa").
56-
WithGenerateFns(generateHPAObject(authCR, deployName))
56+
WithGenerateFns(generateHPAObject(authCR, deployName)).
57+
WithModifyFns(modifyHPA(r.needsRollout))
5758

5859
subRecs = append(subRecs, builder.
5960
WithNamespace(authCRNS).
@@ -119,8 +120,7 @@ func generateHPAObject(instance *operatorv1alpha1.Authentication, deploymentName
119120
}
120121

121122
container := deploy.Spec.Template.Spec.Containers[0]
122-
currentReplicas := deploy.Spec.Replicas
123-
maxReplicas := 2*(*currentReplicas) + 1
123+
maxReplicas := 2*(instance.Spec.Replicas) + 1
124124

125125
memRequest := container.Resources.Requests.Memory().Value()
126126
memLimit := container.Resources.Limits.Memory().Value()
@@ -221,3 +221,32 @@ func (r *AuthenticationReconciler) UpdateDeploymentReplicas(ctx context.Context,
221221

222222
return nil
223223
}
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

Comments
 (0)