diff --git a/pkg/controller/scaledobject/scaledobject_controller.go b/pkg/controller/scaledobject/scaledobject_controller.go index 44489c086c3..a2632b2bbab 100644 --- a/pkg/controller/scaledobject/scaledobject_controller.go +++ b/pkg/controller/scaledobject/scaledobject_controller.go @@ -216,6 +216,13 @@ func (r *ReconcileScaledObject) reconcileDeploymentType(logger logr.Logger, scal return reconcile.Result{}, err } + // add deploymentName label if needed + err = r.checkScaledObjectLabel(logger, scaledObject) + if err != nil { + logger.Error(err, "Failed to update ScaledObject with deploymentName label") + return reconcile.Result{}, err + } + hpaName := getHpaName(deploymentName) hpaNamespace := scaledObject.Namespace @@ -300,6 +307,22 @@ func checkDeploymentTypeScaledObject(scaledObject *kedav1alpha1.ScaledObject) (s return deploymentName, err } +func (r *ReconcileScaledObject) checkScaledObjectLabel(logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) error { + + if scaledObject.Labels == nil { + scaledObject.Labels = map[string]string{"deploymentName": scaledObject.Spec.ScaleTargetRef.DeploymentName} + } else { + value, found := scaledObject.Labels["deploymentName"] + if found && value == scaledObject.Spec.ScaleTargetRef.DeploymentName { + return nil + } + scaledObject.Labels["deploymentName"] = scaledObject.Spec.ScaleTargetRef.DeploymentName + } + + logger.V(1).Info("Adding deploymentName label on ScaledObject") + return r.client.Update(context.TODO(), scaledObject) +} + // startScaleLoop starts ScaleLoop handler for the respective ScaledObject func (r *ReconcileScaledObject) startScaleLoop(logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) error {