diff --git a/engine/engine.go b/engine/engine.go index ba0b6d4..dc6f20b 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -30,8 +30,8 @@ const ( // annotation used on resources (deployments, statefulsets...) originalReplicas = "originalReplicas" - originalScaleTargetRefName = "originalScaleTargetRefName" + kedaAutoscalingPaused = "autoscaling.keda.sh/paused-replicas" ) type Engine struct { diff --git a/engine/scaledobjects.go b/engine/scaledobjects.go index 0650880..b9e9e1e 100644 --- a/engine/scaledobjects.go +++ b/engine/scaledobjects.go @@ -2,8 +2,8 @@ package engine import ( "context" - "fmt" + "github.com/rs/zerolog" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -13,26 +13,22 @@ import ( kedaclientset "github.com/kedacore/keda/v2/pkg/generated/clientset/versioned" ) -// checkRunningDeploymentsConformity verifies that all deployments within the namespace are -// currently running + func checkRunningScaledobjectsConformity(ctx context.Context, l zerolog.Logger, scaledobjects []kedav1alpha1.ScaledObject, cs *kedaclientset.Clientset, ns, prefix string) (bool, error) { hasBeenPatched := false for _, so := range scaledobjects { - // Check if the deployment is annotated and deployment name - origName := so.Annotations[prefix+originalScaleTargetRefName] - if origName != "" { - patchedName := fmt.Sprintf("%s-suspend", origName) - if so.Spec.ScaleTargetRef.Name != patchedName { - continue - } - l.Info().Str("scaledobject", so.Name).Msgf("changing scaleTargetRef name from %s to %s", so.Spec.ScaleTargetRef.Name, origName) - // patch the deployment - if err := patchScaledobjects(ctx, cs, ns, so.Name, prefix, origName); err != nil { + // Check if the ScaledObject is annotated + if _, exits := so.Annotations[kedaAutoscalingPaused]; exits { + l.Info().Str("scaledobjects", so.Name).Msgf("Removing annotations %s from the ScaledObject", kedaAutoscalingPaused) + origName := so.Annotations[prefix+originalScaleTargetRefName] + if err := patchScaledobjects(ctx, cs, ns, so.Name, prefix, origName, false); err != nil { return hasBeenPatched, err } + hasBeenPatched = true } } + return hasBeenPatched, nil } @@ -42,32 +38,36 @@ func checkSuspendedScaledobejctsConformity(ctx context.Context, l zerolog.Logger } for _, so := range scaledobjects { - origName := so.Annotations[prefix+originalScaleTargetRefName] - if origName == "" || so.Spec.ScaleTargetRef.Name == origName { - // TODO: what about fixing the annotation original Replicas here ? - patchedName := fmt.Sprintf("%s-suspend", so.Spec.ScaleTargetRef.Name) - l.Info().Str("scaledobjects", so.Name).Msgf("changing scaleTargetRef name from %s to %s", origName, patchedName) - // patch the deployment - if err := patchScaledobjects(ctx, cs, ns, so.Name, prefix, patchedName); err != nil { + if _, exists := so.Annotations[kedaAutoscalingPaused]; exists { + continue + } else { + l.Info().Str("scaledobject", so.Name).Msgf("Patching the ScaledObject with %s", kedaAutoscalingPaused) + if err := patchScaledobjects(ctx, cs, ns, so.Name, prefix, "", true); err != nil { return err } } } + return nil } // patchScaledobjects updates the minmum and maximum deployments -func patchScaledobjects(ctx context.Context, cs *kedaclientset.Clientset, ns, so, prefix string, patchedName string) error { +func patchScaledobjects(ctx context.Context, cs *kedaclientset.Clientset, ns, so, prefix string, patchedName string, suspend bool) error { return retry.RetryOnConflict(retry.DefaultRetry, func() error { result, err := cs.KedaV1alpha1().ScaledObjects(ns).Get(ctx, so, metav1.GetOptions{}) if err != nil { return err } - // If there's no annotation, set it up - if result.Annotations[prefix+originalScaleTargetRefName] == "" { - result.Annotations[prefix+originalScaleTargetRefName] = result.Spec.ScaleTargetRef.Name + if suspend { + result.Annotations[kedaAutoscalingPaused] = "0" + } else { + delete(result.Annotations, kedaAutoscalingPaused) + delete(result.Annotations, prefix+originalScaleTargetRefName) + if patchedName != "" { + result.Spec.ScaleTargetRef.Name = patchedName + } } - result.Spec.ScaleTargetRef.Name = patchedName + _, err = cs.KedaV1alpha1().ScaledObjects(ns).Update(ctx, result, v1.UpdateOptions{}) return err })