Skip to content

Commit

Permalink
automatic emergency mode trigger when kube metrics unavailable for hpa
Browse files Browse the repository at this point in the history
  • Loading branch information
randytqwjp committed Nov 26, 2024
1 parent 3725788 commit ba4351e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/controller/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, err
}
tortoise = tortoiseService.UpdateTortoiseAutoscalingPolicyInStatus(tortoise, hpa, now)
scalingActive := r.HpaService.checkHpaMetricStatus(ctx, hpa)
if scalingActive == false && tortoise.Spec.UpdateMode != autoscalingv1beta3.UpdateModeOff {
//switch to emergency mode
if tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeAuto {
tortoise.Spec.UpdateMode = autoscalingv1beta3.UpdateModeEmergency
}
}
if scalingActive == true && tortoise.Spec.UpdateMode == autoscalingv1beta3.UpdateModeEmergency {
tortoise.Spec.UpdateMode = autoscalingv1beta3.UpdateModeAuto
}

tortoise = r.TortoiseService.UpdateTortoisePhase(tortoise, now)
if tortoise.Status.TortoisePhase == autoscalingv1beta3.TortoisePhaseInitializing {
logger.Info("initializing tortoise", "tortoise", req.NamespacedName)
Expand Down
15 changes: 15 additions & 0 deletions pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,18 @@ func (c *Service) excludeExternalMetric(ctx context.Context, hpa *v2.HorizontalP

return newHPA
}

func (c *Service) checkHpaMetricStatus(ctx context.Context, currenthpa *v2.HorizontalPodAutoscaler, now time.Time) bool {
currenthpa = currenthpa.DeepCopy()
conditions := currenthpa.Status.Conditions

if conditions[1].Type == "ScalingActive" && conditions[1].Status == "True" {
//switch to Auto mode since metrics are back
return true
}

if conditions[1].Type == "ScalingActive" && conditions[1].Status == "False" && conditions[1].Reason == "FailedGetResourceMetric" {
//switch to Emergency mode since no metrics
return false
}
}

Check failure on line 782 in pkg/hpa/service.go

View workflow job for this annotation

GitHub Actions / Test

missing return

0 comments on commit ba4351e

Please sign in to comment.