Skip to content

Commit

Permalink
record events when the HPA is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Oct 11, 2023
1 parent a15be18 commit e40b3dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions controllers/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand Down

0 comments on commit e40b3dc

Please sign in to comment.