Skip to content

Commit

Permalink
Annotation: migrate to tortoise.autoscaling.mercari.com/tortoise-name (
Browse files Browse the repository at this point in the history
…#326)

* Annotation: migrate to tortoise.autoscaling.mercari.com/tortoise-name

Signed-off-by: sadath-12 <[email protected]>

* delete old annotation during update

Signed-off-by: sadath-12 <[email protected]>

* minorfix

Signed-off-by: sadath-12 <[email protected]>

* deprication labeled

Signed-off-by: sadath-12 <[email protected]>

* extra comments

Signed-off-by: sadath-12 <[email protected]>

* nolint

Signed-off-by: sadath-12 <[email protected]>

---------

Signed-off-by: sadath-12 <[email protected]>
  • Loading branch information
sadath-12 authored Feb 14, 2024
1 parent a03ae13 commit 3a46f26
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions api/autoscaling/v2/horizontalpodautoscaler_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3a46f26

Please sign in to comment.