use status.DesiredReplicas instead of status.CurrentReplicas of HPA to update resource scale

Signed-off-by: lxtywypc <lxtywypc@gmail.com>
This commit is contained in:
lxtywypc 2023-09-22 10:20:54 +08:00
parent 681515fbf6
commit 8a56a8dc34
3 changed files with 7 additions and 7 deletions

View File

@ -126,19 +126,19 @@ func (r *HPAReplicasSyncer) updateScaleIfNeed(ctx context.Context, workloadGR sc
return nil
}
if scale.Spec.Replicas != hpa.Status.CurrentReplicas {
if scale.Spec.Replicas != hpa.Status.DesiredReplicas {
oldReplicas := scale.Spec.Replicas
scale.Spec.Replicas = hpa.Status.CurrentReplicas
scale.Spec.Replicas = hpa.Status.DesiredReplicas
_, err := r.ScaleClient.Scales(hpa.Namespace).Update(ctx, workloadGR, scale, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("Failed to try to sync scale for resource(kind=%s, %s/%s) from %d to %d: %v",
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.CurrentReplicas, err)
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.DesiredReplicas, err)
return err
}
klog.V(4).Infof("Successfully synced scale for resource(kind=%s, %s/%s) from %d to %d",
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.CurrentReplicas)
hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name, oldReplicas, hpa.Status.DesiredReplicas)
}
return nil

View File

@ -147,7 +147,7 @@ func TestUpdateScaleIfNeed(t *testing.T) {
return
}
assert.Equal(t, tt.hpa.Status.CurrentReplicas, scale.Spec.Replicas)
assert.Equal(t, tt.hpa.Status.DesiredReplicas, scale.Spec.Replicas)
})
}
}
@ -307,7 +307,7 @@ func newHPA(apiVersion, kind, name string, replicas int32) *autoscalingv2.Horizo
},
},
Status: autoscalingv2.HorizontalPodAutoscalerStatus{
CurrentReplicas: replicas,
DesiredReplicas: replicas,
},
}
}

View File

@ -50,7 +50,7 @@ func (r *HPAReplicasSyncer) Update(e event.UpdateEvent) bool {
r.scaleRefWorker.Add(labelEvent{addLabelEvent, newHPA})
}
return oldHPA.Status.CurrentReplicas != newHPA.Status.CurrentReplicas
return oldHPA.Status.DesiredReplicas != newHPA.Status.DesiredReplicas
}
func (r *HPAReplicasSyncer) Delete(e event.DeleteEvent) bool {