-
Notifications
You must be signed in to change notification settings - Fork 31
Limit HPA emergency mode to repeated failures #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,4 @@ testbin/* | |
| *.swp | ||
| *.swo | ||
| *~ | ||
| .vscode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/fields" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/client-go/tools/record" | ||
|
|
@@ -784,9 +785,43 @@ func (c *Service) IsHpaMetricAvailable(ctx context.Context, currenthpa *v2.Horiz | |
|
|
||
| for _, condition := range conditions { | ||
| if condition.Type == "ScalingActive" && condition.Status == "False" && condition.Reason == "FailedGetResourceMetric" { | ||
| // switch to Emergency mode since no metrics | ||
| logger.Info("HPA failed to get resource metrics, switch to emergency mode") | ||
| return false | ||
| // HPA failed to get resource metrics, but we need to check how many times it failed, because FailedGetResourceMetric is an informational message and can happen for various reasons (including a new pod from recent scale up). | ||
| logger.Info("HPA failed to get resource metrics, querying k8s events to see how many FailedGetResourceMetric events are there", "hpa", currenthpa.Name) | ||
|
|
||
| sel := fields.AndSelectors( | ||
| fields.OneTermEqualSelector("involvedObject.kind", "HorizontalPodAutoscaler"), | ||
| fields.OneTermEqualSelector("involvedObject.name", currenthpa.Name), | ||
| fields.OneTermEqualSelector("involvedObject.namespace", currenthpa.Namespace), | ||
| fields.OneTermEqualSelector("reason", "FailedGetResourceMetric"), | ||
| ) | ||
| var evList corev1.EventList | ||
| opts := &client.ListOptions{ | ||
| Namespace: currenthpa.Namespace, | ||
| Raw: &metav1.ListOptions{ | ||
| FieldSelector: sel.String(), | ||
| }, | ||
| } | ||
|
|
||
| if err := c.c.List(ctx, &evList, opts); err != nil { | ||
| logger.Info("Failed to get events for HPA to check for FailedGetResourceMetric, switch to emergency mode", "hpa", currenthpa.Name) | ||
| return false | ||
| } | ||
|
|
||
| // count number of FailedGetResourceMetric events that occured in the last 5 minutes | ||
| fiveMinutesAgo := time.Now().Add(-5 * time.Minute) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably at least move this into a constant |
||
| count := 0 | ||
| for _, ev := range evList.Items { | ||
| if ev.EventTime.Time.After(fiveMinutesAgo) { | ||
| count++ | ||
| } | ||
| } | ||
|
|
||
| if count > 5 { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this |
||
| logger.Info("HPA failed to get resource metrics over 5 times in the last 5 minutes, switch to emergency mode", "hpa", currenthpa.Name, "failCount", count) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this can be a Warn |
||
| return false | ||
| } | ||
|
|
||
| logger.Info("HPA did not fail to get resource metrics enough times to switch to emergency mode", "hpa", currenthpa.Name, "failCount", count) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This discards the
errobject, should probably log itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also maybe this should be Warn or Err instead of Info