From 7420bef3eddcd53d1667972902274f5fef67b982 Mon Sep 17 00:00:00 2001 From: Zbynek Roubalik Date: Wed, 29 Jan 2020 20:30:04 +0100 Subject: [PATCH] ScaledObject.Status update should handle stale resource --- pkg/handler/scale_handler.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/handler/scale_handler.go b/pkg/handler/scale_handler.go index 3a1347b7823..ed756aa14e9 100644 --- a/pkg/handler/scale_handler.go +++ b/pkg/handler/scale_handler.go @@ -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 }