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

kubernetes_deployment resources #1880

Closed
jan4984 opened this issue Oct 26, 2022 · 4 comments · Fixed by #1882 or #1889
Closed

kubernetes_deployment resources #1880

jan4984 opened this issue Oct 26, 2022 · 4 comments · Fixed by #1882 or #1889
Assignees

Comments

@jan4984
Copy link

jan4984 commented Oct 26, 2022

Terraform Version, Provider Version and Kubernetes Version

Terraform version: v1.2.9
Kubernetes provider version:  v2.14.0
Kubernetes version: 1.21.0

Affected Resource(s)

kubernetes_deployment

Terraform Configuration Files

resource "kubernetes_deployment" "local-example" {
  metadata {
    name = "local-example"
    namespace = "default"
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        test = "local-example"
      }
    }

    template {
      metadata {
        labels = {
          test = "local-example"
        }
      }

      spec {
        container {
          # image = "${var.addresses[0]}:5000/rancher/mirrored-library-nginx:1.21.1-alpine"
          image = "rancher/mirrored-library-nginx:1.21.1-alpine"
          name  = "example"

          resources {
            limits = {
              cpu    = "0.5"
              memory = "512Mi"
            }
            requests = {
              cpu    = "250m"
              memory = "50Mi"
            }
          }
          liveness_probe {
            http_get {
              path = "/"
              port = 80

              http_header {
                name  = "X-Custom-Header"
                value = "Awesome"
              }
            }

            initial_delay_seconds = 3
            period_seconds        = 3
          }
        }
      }
    }
  }
}

terraform {
  required_providers {
    kubernetes = {
      source = "hashicorp/kubernetes"
      version = "2.14.0"
    }
  }
}

provider "kubernetes" {
  config_path = "kube_config_cluster.yml"
}

Debug Output

when I comment out the resources {} block, the deployment didn't updated

Panic Output

Steps to Reproduce

  1. terraform apply
  2. comment out the resources {} block
  3. terraform apply

Expected Behavior

the deployment should remove his resources block

Actual Behavior

terraform apply output
No changes. Your infrastructure matches the configuration.

If I set limite and request to {},even not change the configuration, everytime terraform apply update the development

kubernetes_deployment.xxxx will be updated in-place

                      ~ resources {
                          + limits   = (known after apply)
                          + requests = (known after apply)
                        }

References

#1876
#754

Please don't close bug arbitrarily

@jan4984 jan4984 added the bug label Oct 26, 2022
@github-actions github-actions bot removed the bug label Oct 26, 2022
@arybolovlev arybolovlev self-assigned this Oct 26, 2022
@arybolovlev
Copy link
Contributor

Hi @jan4984,

Thank you for opening this issue.

As you can see from the source code here and here spec.container.resources, spec.container.resources.limits and spec.container.resources.requests are computed resources. It means that they will be computed with the value received from the remote system if they are not specified in the terraform code. If you have them set up in your code, Terraform will update the Kubernetes resource with the value you have specified and update the state file accordingly. If it is not in the code, then Terraform will read the value from the Kubernetes resource and update the state file accordingly. That is why you can see this behavior once remove spec.container.resources from your code.

If you want to delete spec.container.resources from the Kubernetes resource, you have to add a "blank" resource, like this:

resources {
    limits = {}
    requests = {}
}

I will update our documentation.

Thank you.

@jan4984
Copy link
Author

jan4984 commented Nov 1, 2022

Thank you @arybolovlev. If I keep

resources {
    limits = {}
    requests = {}
}

then everytime terraform plan will update the deployment resource.

# kubernetes_deployment.database-zoo-3 will be updated in-place
  ~ resource "kubernetes_deployment" "database-zoo-3" {
        id               = "default/database-zoo-3"
        # (1 unchanged attribute hidden)

      ~ spec {
            # (5 unchanged attributes hidden)

          ~ template {

              ~ spec {
                    # (12 unchanged attributes hidden)

                  ~ container {
                        name                       = "zoo-3"
                        # (9 unchanged attributes hidden)

                      ~ resources {
                          + limits   = (known after apply)
                          + requests = (known after apply)
                        }

                        # (8 unchanged blocks hidden)
                    }

                    # (1 unchanged block hidden)
                }

                # (1 unchanged block hidden)
            }

            # (2 unchanged blocks hidden)
        }

        # (1 unchanged block hidden)
    }

It's the right way of computed resource? I don't want the update triggered if I not change my code.

@arybolovlev
Copy link
Contributor

Hi @jan4984,

Thank you for spotting this one! I am working on a solution and let you know once it is done.

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.