Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
ScaledObject.Status update should handle stale resource (kedacore#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
zroubalik authored and Mel Cone committed Jan 31, 2020
1 parent d12b1be commit cd55d30
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/handler/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cd55d30

Please sign in to comment.