diff --git a/api/autoscaling/v2/horizontalpodautoscaler_webhook.go b/api/autoscaling/v2/horizontalpodautoscaler_webhook.go index 1d7b54d8..588a295f 100644 --- a/api/autoscaling/v2/horizontalpodautoscaler_webhook.go +++ b/api/autoscaling/v2/horizontalpodautoscaler_webhook.go @@ -64,6 +64,10 @@ var _ admission.CustomDefaulter = &HPAWebhook{} func (h *HPAWebhook) Default(ctx context.Context, obj runtime.Object) error { hpa := obj.(*v2.HorizontalPodAutoscaler) tortoiseName, ok := hpa.GetAnnotations()[annotation.TortoiseNameAnnotation] + if !ok { + //nolint // We use the deprecated annotation deliberately so that we don't break existing users. + tortoiseName, ok = hpa.GetAnnotations()[annotation.DeprecatedTortoiseNameAnnotation] + } if !ok { // not managed by tortoise return nil @@ -126,6 +130,10 @@ func (*HPAWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Ob func (h *HPAWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) { hpa := obj.(*v2.HorizontalPodAutoscaler) tortoiseName, ok := hpa.GetAnnotations()[annotation.TortoiseNameAnnotation] + if !ok { + //nolint // We use the deprecated annotation deliberately so that we don't break existing users. + tortoiseName, ok = hpa.GetAnnotations()[annotation.DeprecatedTortoiseNameAnnotation] + } if !ok { // not managed by tortoise return nil, nil diff --git a/pkg/annotation/annotation.go b/pkg/annotation/annotation.go index b4a8b2ee..8a40efd5 100644 --- a/pkg/annotation/annotation.go +++ b/pkg/annotation/annotation.go @@ -2,8 +2,11 @@ package annotation // annotation on HPA and VPA resource. const ( + // Deprecated: TortoiseNameAnnotation - VPA and HPA managed by tortoise have this label. + // If an existing HPA has this label, it's automatically replaced with TortoiseNameAnnotation by the controller. + DeprecatedTortoiseNameAnnotation = "tortoises.autoscaling.mercari.com/tortoise-name" // TortoiseNameAnnotation - VPA and HPA managed by tortoise have this label. - TortoiseNameAnnotation = "tortoises.autoscaling.mercari.com/tortoise-name" + TortoiseNameAnnotation = "tortoise.autoscaling.mercari.com/tortoise-name" // If this annotation is set to "true", it means that tortoise manages that resource, // and will be removed when the tortoise is deleted. diff --git a/pkg/hpa/service.go b/pkg/hpa/service.go index 00ad155d..013f78f8 100644 --- a/pkg/hpa/service.go +++ b/pkg/hpa/service.go @@ -668,6 +668,14 @@ func (c *Service) UpdateHPAFromTortoiseRecommendation(ctx context.Context, torto } hpa = c.excludeExternalMetric(ctx, hpa) + //nolint // We use the deprecated annotation deliberately so that we don't break existing users. + if tortoiseName, ok := hpa.Annotations[annotation.DeprecatedTortoiseNameAnnotation]; ok { + hpa.Annotations[annotation.TortoiseNameAnnotation] = tortoiseName + //nolint // We use the deprecated annotation deliberately so that we don't break existing users. + delete(hpa.Annotations, annotation.DeprecatedTortoiseNameAnnotation) + } + // If HPA has a deprecated annotation, we replace it with a new one. + // It allows us to remove a deprecated annotation completely later retHPA = hpa return c.c.Update(ctx, hpa) }