Terraform-provider-kubernetes: Working with kubernetes_deployment and kubernetes_horizontal_pod_autoscaler

Created on 7 Aug 2019  Â·  4Comments  Â·  Source: hashicorp/terraform-provider-kubernetes

Hi Guys,

Terraform Version

Terraform v0.11.14

  • provider.ansiblevault v1.1.0
  • provider.google v2.12.0
  • provider.google-beta v2.12.0
  • provider.kubernetes v1.8.1

Affected Resource(s)

  • kubernetes_deployment
  • kubernetes_horizontal_pod_autoscaler

Terraform Configuration Files

resource "kubernetes_deployment" "wa_worker" {
  metadata {
    name      = "wa-worker"
    namespace = "${var.env}"

    labels = {
      app     = "wa-worker"
      env     = "${var.env}"
      managed = "terraform"
    }
  }

  spec {
    # No `replicas` set here fallback to 1
  }
...

}

resource "kubernetes_horizontal_pod_autoscaler" "wa_worker" {
  metadata {
    name      = "wa-worker"
    namespace = "${var.env}"
  }

  spec {
    max_replicas                      = 5
    min_replicas                      = 3
    target_cpu_utilization_percentage = "95"

    scale_target_ref {
      api_version = "extensions/v1beta1"
      kind = "deployment"
      name = "${kubernetes_deployment.wa_worker.metadata.0.name}"
    }
  }
}

Debug Output

```
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

~ kubernetes_deployment.wa_worker
spec.0.replicas: "3" => "1"

Plan: 0 to add, 1 to change, 0 to destroy.```

Panic Output

None

Expected Behavior

Since we have a HPA, terraform should skip this part

Actual Behavior

Terraform try to scaledown to 1 replica

Steps to Reproduce

  1. terraform plan

Most helpful comment

It looks like the provider is explicitly defaulting this value to 1 if you don't set it, so we are always sending a replicas field to the k8s API. https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/kubernetes/resource_kubernetes_deployment.go#L72

We should remove the default for this field so that k8s handles the defaulting behaviour.

All 4 comments

It looks like the provider is explicitly defaulting this value to 1 if you don't set it, so we are always sending a replicas field to the k8s API. https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/kubernetes/resource_kubernetes_deployment.go#L72

We should remove the default for this field so that k8s handles the defaulting behaviour.

I had a look into this a bit more. Rather than modify the provider to do this I think what we actually want to do in this case is use the ignore_changes feature. This way the deployment will be created with a default of 1 if it's not set, but further changes to the replicas field will be ignored.

e.g.

resource "kubernetes_deployment" "test" {

    # ...

    lifecycle {
        ignore_changes = [
            spec.0.replicas
        ]
    }
}

The issue I see with this is just that the explicitness of this might be somewhat surprising to the user because the field gets implicitly ignored when you use kubectl apply/YAML.

Hi,

Thank for your time, I'll take a look.

Le mar. 18 févr. 2020 à 21:26, John Houston notifications@github.com a
écrit :

I had a look into this a bit more. Rather than modify the provider to do
this I think what we actually want to do in this case is use the
ignore_changes
https://www.terraform.io/docs/configuration/resources.html#ignore_changes
feature. This way the deployment will be created with a default of 1 if
it's not set, but further changes to the replicas field will be ignored.

e.g

resource "kubernetes_deployment" "test" {

# ...

lifecycle {
    ignore_changes = [
        spec.0.replicas
    ]
}

}

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/terraform-providers/terraform-provider-kubernetes/issues/584?email_source=notifications&email_token=AA72DLQ47BJOHHAQVCTZETDRDQ77RA5CNFSM4IKAM6J2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMEPTIY#issuecomment-587790755,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA72DLQI5SDMGJFQ4V2AUULRDQ77RANCNFSM4IKAM6JQ
.

Working has expect thank @jrhouston

Was this page helpful?
0 / 5 - 0 ratings