@@ -284,6 +284,7 @@ func (r *PodAutoscalerReconciler) reconcileCustomPA(ctx context.Context, pa auto
284284 paStatusOriginal := pa .Status .DeepCopy ()
285285 paType := pa .Spec .ScalingStrategy
286286 scaleReference := fmt .Sprintf ("%s/%s/%s" , pa .Spec .ScaleTargetRef .Kind , pa .Namespace , pa .Spec .ScaleTargetRef .Name )
287+ metricKey := metrics .NewNamespaceNameMetric (& pa )
287288
288289 targetGV , err := schema .ParseGroupVersion (pa .Spec .ScaleTargetRef .APIVersion )
289290 if err != nil {
@@ -324,7 +325,7 @@ func (r *PodAutoscalerReconciler) reconcileCustomPA(ctx context.Context, pa auto
324325 setCondition (& pa , "AbleToScale" , metav1 .ConditionTrue , "SucceededGetScale" , "the %s controller was able to get the target's current scale" , paType )
325326
326327 // Update the scale required metrics periodically
327- err = r .updateMetricsForScale (ctx , pa , scale )
328+ err = r .updateMetricsForScale (ctx , pa , scale , metricKey )
328329 if err != nil {
329330 r .EventRecorder .Event (& pa , corev1 .EventTypeWarning , "FailedUpdateMetrics" , err .Error ())
330331 return ctrl.Result {}, fmt .Errorf ("failed to update metrics for scale target reference: %v" , err )
@@ -366,7 +367,7 @@ func (r *PodAutoscalerReconciler) reconcileCustomPA(ctx context.Context, pa auto
366367 // if the currentReplicas is within the range, we should
367368 // computeReplicasForMetrics gives
368369 // TODO: check why it return the metrics name here?
369- metricDesiredReplicas , metricName , metricTimestamp , err := r .computeReplicasForMetrics (ctx , pa , scale )
370+ metricDesiredReplicas , metricName , metricTimestamp , err := r .computeReplicasForMetrics (ctx , pa , scale , metricKey )
370371 if err != nil && metricDesiredReplicas == - 1 {
371372 r .setCurrentReplicasAndMetricsInStatus (& pa , currentReplicas )
372373 if err := r .updateStatusIfNeeded (ctx , paStatusOriginal , & pa ); err != nil {
@@ -556,7 +557,7 @@ func (r *PodAutoscalerReconciler) updateStatus(ctx context.Context, pa *autoscal
556557// It may return both valid metricDesiredReplicas and an error,
557558// when some metrics still work and PA should perform scaling based on them.
558559// If PodAutoscaler cannot do anything due to error, it returns -1 in metricDesiredReplicas as a failure signal.
559- func (r * PodAutoscalerReconciler ) computeReplicasForMetrics (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler , scale * unstructured.Unstructured ) (replicas int32 , relatedMetrics string , timestamp time.Time , err error ) {
560+ func (r * PodAutoscalerReconciler ) computeReplicasForMetrics (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler , scale * unstructured.Unstructured , metricKey metrics. NamespaceNameMetric ) (replicas int32 , relatedMetrics string , timestamp time.Time , err error ) {
560561 logger := klog .FromContext (ctx )
561562 currentTimestamp := time .Now ()
562563
@@ -574,19 +575,13 @@ func (r *PodAutoscalerReconciler) computeReplicasForMetrics(ctx context.Context,
574575 }
575576
576577 // TODO UpdateScalingContext (in updateScalerSpec) is duplicate invoked in computeReplicasForMetrics and updateMetricsForScale
577- err = r .updateScalerSpec (ctx , pa )
578+ err = r .updateScalerSpec (ctx , pa , metricKey )
578579 if err != nil {
579580 klog .ErrorS (err , "Failed to update scaler spec from pa_types" )
580581 return 0 , "" , currentTimestamp , fmt .Errorf ("error update scaler spec: %w" , err )
581582 }
582583
583584 logger .V (4 ).Info ("Obtained selector and get ReadyPodsCount" , "selector" , labelsSelector , "originalReadyPodsCount" , originalReadyPodsCount )
584- var metricKey metrics.NamespaceNameMetric
585- if len (pa .Spec .MetricsSources ) > 0 {
586- metricKey = metrics .NewNamespaceNameMetric (pa .Namespace , pa .Spec .ScaleTargetRef .Name , pa .Spec .MetricsSources [0 ].Name )
587- } else {
588- metricKey = metrics .NewNamespaceNameMetric (pa .Namespace , pa .Spec .ScaleTargetRef .Name , pa .Spec .TargetMetric )
589- }
590585
591586 // Calculate the desired number of pods using the autoscaler logic.
592587 autoScaler , ok := r .AutoscalerMap [metricKey ]
@@ -605,18 +600,16 @@ func (r *PodAutoscalerReconciler) computeReplicasForMetrics(ctx context.Context,
605600// refer to knative-serving.
606601// In pkg/reconciler/autoscaling/kpa/kpa.go:198, kpa maintains a list of deciders into multi-scaler, each of them corresponds to a pa (PodAutoscaler).
607602// We create or update the scaler instance according to the pa passed in
608- func (r * PodAutoscalerReconciler ) updateScalerSpec (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler ) error {
609- metricKey := NewNamespaceNameMetricByPa (pa )
603+ func (r * PodAutoscalerReconciler ) updateScalerSpec (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler , metricKey metrics.NamespaceNameMetric ) error {
610604 autoScaler , ok := r .AutoscalerMap [metricKey ]
611605 if ! ok {
612606 return fmt .Errorf ("unsupported scaling strategy: %s" , pa .Spec .ScalingStrategy )
613607 }
614608 return autoScaler .UpdateScalingContext (pa )
615609}
616610
617- func (r * PodAutoscalerReconciler ) updateMetricsForScale (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler , scale * unstructured.Unstructured ) (err error ) {
611+ func (r * PodAutoscalerReconciler ) updateMetricsForScale (ctx context.Context , pa autoscalingv1alpha1.PodAutoscaler , scale * unstructured.Unstructured , metricKey metrics. NamespaceNameMetric ) (err error ) {
618612 currentTimestamp := time .Now ()
619- metricKey := NewNamespaceNameMetricByPa (pa )
620613 var autoScaler scaler.Scaler
621614 autoScaler , exists := r .AutoscalerMap [metricKey ]
622615 if ! exists {
@@ -649,7 +642,6 @@ func (r *PodAutoscalerReconciler) updateMetricsForScale(ctx context.Context, pa
649642
650643 // update metrics
651644 for _ , source := range pa .Spec .MetricsSources {
652- metricKey := metrics .NewNamespaceNameMetric (pa .Namespace , pa .Spec .ScaleTargetRef .Name , source .Name )
653645 return autoScaler .UpdateSourceMetrics (ctx , metricKey , source , currentTimestamp )
654646 }
655647
0 commit comments