From e4602073c13221f060b8ae712e3fc0821c16155f Mon Sep 17 00:00:00 2001 From: Arnaud Farbos Date: Thu, 5 Sep 2024 16:40:44 -0700 Subject: [PATCH 1/3] fix: health hpa when only second conditions is False Signed-off-by: Arnaud Farbos --- pkg/health/health_hpa.go | 11 +++-- .../testdata/hpa-v2-degraded-partially.yaml | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 pkg/health/testdata/hpa-v2-degraded-partially.yaml diff --git a/pkg/health/health_hpa.go b/pkg/health/health_hpa.go index 95580ebb0..5f31661dc 100644 --- a/pkg/health/health_hpa.go +++ b/pkg/health/health_hpa.go @@ -3,6 +3,7 @@ package health import ( "encoding/json" "fmt" + "strings" autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" @@ -132,6 +133,7 @@ func getAutoScalingV1HPAHealth(hpa *autoscalingv1.HorizontalPodAutoscaler) (*Hea } func checkConditions(conditions []hpaCondition, progressingStatus *HealthStatus) (*HealthStatus, error) { + healthyMessages := []string{} for _, condition := range conditions { if isDegraded(&condition) { return &HealthStatus{ @@ -141,13 +143,14 @@ func checkConditions(conditions []hpaCondition, progressingStatus *HealthStatus) } if isHealthy(&condition) { - return &HealthStatus{ - Status: HealthStatusHealthy, - Message: condition.Message, - }, nil + healthyMessages = append(healthyMessages, condition.Message) } } + if len(conditions) == len(healthyMessages) { + return &HealthStatus{Status: HealthStatusHealthy, Message: strings.Join(healthyMessages, ",")}, nil + } + return progressingStatus, nil } diff --git a/pkg/health/testdata/hpa-v2-degraded-partially.yaml b/pkg/health/testdata/hpa-v2-degraded-partially.yaml new file mode 100644 index 000000000..4bd74b1b2 --- /dev/null +++ b/pkg/health/testdata/hpa-v2-degraded-partially.yaml @@ -0,0 +1,40 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + creationTimestamp: "2022-01-17T14:22:27Z" + name: sample + uid: 0e6d855e-83ed-4ed5-b80a-461a750f14db +spec: + maxReplicas: 2 + minReplicas: 1 + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: argocd-server + targetCPUUtilizationPercentage: 80 +status: + conditions: + - lastTransitionTime: '2024-09-05T20:20:41Z' + message: the HPA controller was able to get the target's current scale + reason: SucceededGetScale + status: 'True' + type: AbleToScale + - lastTransitionTime: '2024-09-05T20:20:56Z' + message: >- + the HPA was unable to compute the replica count: failed to get memory + utilization: unable to get metrics for resource memory: unable to fetch + metrics from resource metrics API: the server could not find the + requested resource (get pods.metrics.k8s.io) + reason: FailedGetResourceMetric + status: 'False' + type: ScalingActive + type: ScalingLimited + currentMetrics: + - resource: + current: + averageUtilization: 6 + averageValue: 12m + name: cpu + type: Resource + currentReplicas: 1 + desiredReplicas: 1 From 4d25c5a784febe4eaae68944d97c9d3b15199be9 Mon Sep 17 00:00:00 2001 From: Arnaud Farbos Date: Thu, 5 Sep 2024 16:46:32 -0700 Subject: [PATCH 2/3] add test in pkg/health/health_test.go Signed-off-by: Arnaud Farbos --- pkg/health/health_hpa.go | 2 +- pkg/health/health_test.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/health/health_hpa.go b/pkg/health/health_hpa.go index 5f31661dc..0cc5379a1 100644 --- a/pkg/health/health_hpa.go +++ b/pkg/health/health_hpa.go @@ -147,7 +147,7 @@ func checkConditions(conditions []hpaCondition, progressingStatus *HealthStatus) } } - if len(conditions) == len(healthyMessages) { + if len(healthyMessages) > 0 { return &HealthStatus{Status: HealthStatusHealthy, Message: strings.Join(healthyMessages, ",")}, nil } diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index 45ff74941..13118be55 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -81,6 +81,7 @@ func TestJob(t *testing.T) { func TestHPA(t *testing.T) { assertAppHealth(t, "./testdata/hpa-v2-healthy.yaml", HealthStatusHealthy) assertAppHealth(t, "./testdata/hpa-v2-degraded.yaml", HealthStatusDegraded) + assertAppHealth(t, "./testdata/hpa-v2-degraded-partially.yaml", HealthStatusDegraded) assertAppHealth(t, "./testdata/hpa-v2-progressing.yaml", HealthStatusProgressing) assertAppHealth(t, "./testdata/hpa-v2beta2-healthy.yaml", HealthStatusHealthy) assertAppHealth(t, "./testdata/hpa-v2beta1-healthy-disabled.yaml", HealthStatusHealthy) From 0ae80208abe9e6e424730ac0d486e2567db0c718 Mon Sep 17 00:00:00 2001 From: Arnaud Farbos Date: Thu, 5 Sep 2024 17:01:54 -0700 Subject: [PATCH 3/3] fix test Signed-off-by: Arnaud Farbos --- pkg/health/health_test.go | 2 +- ...hy.yaml => hpa-v1-degraded-partially.yaml} | 0 .../testdata/hpa-v2-degraded-partially.yaml | 30 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) rename pkg/health/testdata/{hpa-v1-healthy.yaml => hpa-v1-degraded-partially.yaml} (100%) diff --git a/pkg/health/health_test.go b/pkg/health/health_test.go index 13118be55..f19469ac6 100644 --- a/pkg/health/health_test.go +++ b/pkg/health/health_test.go @@ -87,7 +87,7 @@ func TestHPA(t *testing.T) { assertAppHealth(t, "./testdata/hpa-v2beta1-healthy-disabled.yaml", HealthStatusHealthy) assertAppHealth(t, "./testdata/hpa-v2beta1-healthy.yaml", HealthStatusHealthy) assertAppHealth(t, "./testdata/hpa-v1-degraded.yaml", HealthStatusDegraded) - assertAppHealth(t, "./testdata/hpa-v1-healthy.yaml", HealthStatusHealthy) + assertAppHealth(t, "./testdata/hpa-v1-degraded-partially.yaml", HealthStatusDegraded) assertAppHealth(t, "./testdata/hpa-v1-healthy-toofew.yaml", HealthStatusHealthy) assertAppHealth(t, "./testdata/hpa-v1-progressing.yaml", HealthStatusProgressing) assertAppHealth(t, "./testdata/hpa-v1-progressing-with-no-annotations.yaml", HealthStatusProgressing) diff --git a/pkg/health/testdata/hpa-v1-healthy.yaml b/pkg/health/testdata/hpa-v1-degraded-partially.yaml similarity index 100% rename from pkg/health/testdata/hpa-v1-healthy.yaml rename to pkg/health/testdata/hpa-v1-degraded-partially.yaml diff --git a/pkg/health/testdata/hpa-v2-degraded-partially.yaml b/pkg/health/testdata/hpa-v2-degraded-partially.yaml index 4bd74b1b2..a9040b5d3 100644 --- a/pkg/health/testdata/hpa-v2-degraded-partially.yaml +++ b/pkg/health/testdata/hpa-v2-degraded-partially.yaml @@ -14,21 +14,21 @@ spec: targetCPUUtilizationPercentage: 80 status: conditions: - - lastTransitionTime: '2024-09-05T20:20:41Z' - message: the HPA controller was able to get the target's current scale - reason: SucceededGetScale - status: 'True' - type: AbleToScale - - lastTransitionTime: '2024-09-05T20:20:56Z' - message: >- - the HPA was unable to compute the replica count: failed to get memory - utilization: unable to get metrics for resource memory: unable to fetch - metrics from resource metrics API: the server could not find the - requested resource (get pods.metrics.k8s.io) - reason: FailedGetResourceMetric - status: 'False' - type: ScalingActive - type: ScalingLimited + - lastTransitionTime: '2024-09-05T20:20:41Z' + message: the HPA controller was able to get the target's current scale + reason: SucceededGetScale + status: 'True' + type: AbleToScale + - lastTransitionTime: '2024-09-05T20:20:56Z' + message: | + the HPA was unable to compute the replica count: failed to get memory + utilization: unable to get metrics for resource memory: unable to fetch + metrics from resource metrics API: the server could not find the + requested resource (get pods.metrics.k8s.io) + reason: FailedGetResourceMetric + status: 'False' + type: ScalingActive + type: ScalingLimited currentMetrics: - resource: current: