From e40b3dcdb724b340fbf3b042cedd9d6eb0da083f Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Wed, 11 Oct 2023 18:15:28 +0900 Subject: [PATCH] record events when the HPA is modified --- controllers/tortoise_controller.go | 5 +++++ pkg/hpa/service.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/controllers/tortoise_controller.go b/controllers/tortoise_controller.go index 2ad3af0a..f82a9cb2 100644 --- a/controllers/tortoise_controller.go +++ b/controllers/tortoise_controller.go @@ -99,6 +99,11 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ } defer func() { + if tortoise == nil { + logger.Error(reterr, "get error during the reconciliation, but cannot record the event because tortoise object is nil", "tortoise", req.NamespacedName) + return + } + tortoise = r.TortoiseService.RecordReconciliationFailure(tortoise, reterr, now) _, err = r.TortoiseService.UpdateTortoiseStatus(ctx, tortoise, now, false) if err != nil { diff --git a/pkg/hpa/service.go b/pkg/hpa/service.go index a875e9bb..299026c7 100644 --- a/pkg/hpa/service.go +++ b/pkg/hpa/service.go @@ -72,7 +72,7 @@ func (c *Service) InitializeHPA(ctx context.Context, tortoise *autoscalingv1beta return tortoise, fmt.Errorf("create hpa: %w", err) } - c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler)) + c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s for a created tortoise", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler)) return tortoise, nil } @@ -347,6 +347,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context } // No need to edit container resource phase. + c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPADeleted", fmt.Sprintf("Deleted a HPA %s/%s because tortoise has no resource to scale horizontally", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler)) + return tortoise, nil } @@ -361,11 +363,18 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context if err != nil { return tortoise, fmt.Errorf("initialize hpa: %w", err) } + + c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPACreated", fmt.Sprintf("Initialized a HPA %s/%s because tortoise has resource to scale horizontally", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler)) return tortoise, nil } return tortoise, fmt.Errorf("failed to get hpa on tortoise: %w", err) } + // make sure it's managed by tortoise + if v, ok := hpa.Annotations[annotation.ManagedByTortoiseAnnotation]; !ok || v != "true" { + return tortoise, fmt.Errorf("the HPA %s/%s is specified in tortoise, but not managed by tortoise", hpa.Namespace, hpa.Name) + } + var newhpa *v2.HorizontalPodAutoscaler var isHpaEdited bool newhpa, tortoise, isHpaEdited = c.addHPAMetricsFromTortoiseAutoscalingPolicy(ctx, tortoise, hpa) @@ -389,6 +398,8 @@ func (c *Service) UpdateHPASpecFromTortoiseAutoscalingPolicy(ctx context.Context return tortoise, err } + c.recorder.Event(tortoise, corev1.EventTypeNormal, "HPAUpdated", fmt.Sprintf("Updated a HPA %s/%s because the autoscaling policy is changed in the tortoise", tortoise.Namespace, tortoise.Status.Targets.HorizontalPodAutoscaler)) + return tortoise, nil }