Skip to content
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

Fix HPA metric type Pods target average value handling #2658

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions kubernetes/resource_kubernetes_horizontal_pod_autoscaler_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,63 @@ func TestAccKubernetesHorizontalPodAutoscalerV2_containerResource(t *testing.T)
})
}

func TestAccKubernetesHorizontalPodAutoscalerV2_MetricsPods(t *testing.T) {
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
resourceName := "kubernetes_horizontal_pod_autoscaler_v2.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
skipIfClusterVersionLessThan(t, "1.23.0")
},
IDRefreshName: resourceName,
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesHorizontalPodAutoscalerV2Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesHorizontalPodAutoscalerV2Config_MetricsPods(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesHorizontalPodAutoscalerV2Exists(resourceName),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.behavior.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.max_replicas", "2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.min_replicas", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.scale_target_ref.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.scale_target_ref.0.kind", "Deployment"),
resource.TestCheckResourceAttr(resourceName, "spec.0.scale_target_ref.0.name", "TerraformAccTest"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.#", "5"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.0.type", "Pods"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.0.pods.0.metric.0.name", "Value_value_average_value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.0.pods.0.target.0.type", "Value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.0.pods.0.target.0.value", "10"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.0.pods.0.target.0.average_value", "5"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.1.type", "Pods"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.1.pods.0.metric.0.name", "Value_average_value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.1.pods.0.target.0.type", "Value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.1.pods.0.target.0.average_value", "5"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.2.type", "Pods"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.2.pods.0.metric.0.name", "AverageValue_average_value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.2.pods.0.target.0.type", "AverageValue"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.2.pods.0.target.0.average_value", "5"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.3.type", "Pods"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.3.pods.0.metric.0.name", "Utilization_average_utilization_average_value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.3.pods.0.target.0.type", "Utilization"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.3.pods.0.target.0.average_utilization", "15"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.3.pods.0.target.0.average_value", "5"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.4.type", "Pods"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.4.pods.0.metric.0.name", "Utilization_average_value"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.4.pods.0.target.0.type", "Utilization"),
resource.TestCheckResourceAttr(resourceName, "spec.0.metric.4.pods.0.target.0.average_value", "5"),
),
},
},
})
}

func testAccCheckKubernetesHorizontalPodAutoscalerV2Destroy(s *terraform.State) error {
conn, err := testAccProvider.Meta().(KubeClientsets).MainClientset()

Expand Down Expand Up @@ -537,3 +594,82 @@ func testAccKubernetesHorizontalPodAutoscalerV2Config_containerResource(name str
}
`, name)
}

func testAccKubernetesHorizontalPodAutoscalerV2Config_MetricsPods(name string) string {
return fmt.Sprintf(`resource "kubernetes_horizontal_pod_autoscaler_v2" "test" {
metadata {
name = %q
}

spec {
max_replicas = 2
scale_target_ref {
kind = "Deployment"
name = "TerraformAccTest"
}
metric {
type = "Pods"
pods {
metric {
name = "Value_value_average_value"
}
target {
type = "Value"
value = "10"
average_value = "5"
}
}
}
metric {
type = "Pods"
pods {
metric {
name = "Value_average_value"
}
target {
type = "Value"
average_value = "5"
}
}
}
metric {
type = "Pods"
pods {
metric {
name = "AverageValue_average_value"
}
target {
type = "AverageValue"
average_value = "5"
}
}
}
metric {
type = "Pods"
pods {
metric {
name = "Utilization_average_utilization_average_value"
}
target {
type = "Utilization"
average_utilization = "15"
average_value = "5"
}
}
}
metric {
type = "Pods"
pods {
metric {
name = "Utilization_average_value"
}
target {
type = "Utilization"
average_value = "5"
}
}
}
}
}
`, name)
}
22 changes: 20 additions & 2 deletions kubernetes/structure_horizontal_pod_autoscaler_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ func expandV2MetricTarget(m map[string]interface{}) autoscalingv2.MetricTarget {
if v, ok := m["average_utilization"].(int); ok && v > 0 {
target.AverageUtilization = ptr.To(int32(v))
}
if v, ok := m["average_value"].(string); ok && v != "0" && v != "" {
q := resource.MustParse(v)
target.AverageValue = &q
}
case autoscalingv2.ValueMetricType:
if v, ok := m["value"].(string); ok && v != "0" && v != "" {
q := resource.MustParse(v)
target.Value = &q
}
if v, ok := m["average_value"].(string); ok && v != "0" && v != "" {
q := resource.MustParse(v)
target.AverageValue = &q
}
}

return target
Expand Down Expand Up @@ -302,9 +310,19 @@ func flattenV2MetricTarget(target autoscalingv2.MetricTarget) []interface{} {
case autoscalingv2.AverageValueMetricType:
m["average_value"] = target.AverageValue.String()
case autoscalingv2.UtilizationMetricType:
m["average_utilization"] = *target.AverageUtilization
if target.AverageUtilization != nil {
m["average_utilization"] = *target.AverageUtilization
}
if target.AverageValue != nil {
m["average_value"] = target.AverageValue.String()
}
case autoscalingv2.ValueMetricType:
m["value"] = target.Value.String()
if target.Value != nil {
m["value"] = target.Value.String()
}
if target.AverageValue != nil {
m["average_value"] = target.AverageValue.String()
}
}

return []interface{}{m}
Expand Down
Loading