ScaledObject.Status update should handle stale resource
@@ -10,6 +10,7 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
+ "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -44,9 +45,27 @@ func NewScaleHandler(client client.Client, reconcilerScheme *runtime.Scheme) *Sc
func (h *ScaleHandler) updateScaledObjectStatus(scaledObject *kedav1alpha1.ScaledObject) error {
err := h.client.Status().Update(context.TODO(), scaledObject)
if err != nil {
+ if errors.IsConflict(err) {
+ // ScaledObject's metadata that are not necessary to restart the ScaleLoop were updated (eg. labels)
+ // we should try to fetch the scaledObject again and process the update once again
+ h.logger.V(1).Info("Trying to fetch updated version of ScaledObject to properly update it's Status")
+ updatedScaledObject := &kedav1alpha1.ScaledObject{}
+ err2 := h.client.Get(context.TODO(), types.NamespacedName{Name: scaledObject.Name, Namespace: scaledObject.Namespace}, updatedScaledObject)
+ if err2 != nil {
+ h.logger.Error(err2, "Error getting updated version of ScaledObject before updating it's Status")
+ } else {
+ scaledObject = updatedScaledObject
+ if h.client.Status().Update(context.TODO(), scaledObject) == nil {
+ h.logger.V(1).Info("ScaledObject's Status was properly updated on re-fetched ScaledObject")
+ return nil
+ }
+ }
+ }
+ // we got another type of error
h.logger.Error(err, "Error updating scaledObject status")
return err
}
+ h.logger.V(1).Info("ScaledObject's Status was properly updated")
return nil
}
