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: prevent inconsistent planning #46

Merged
merged 1 commit into from
Sep 19, 2023
Merged

fix: prevent inconsistent planning #46

merged 1 commit into from
Sep 19, 2023

Conversation

Adphi
Copy link
Contributor

@Adphi Adphi commented Sep 19, 2023

source: zenobios:fix/162-inconsistent_planning
Signed-off-by: Adphi <[email protected]>
@alekc
Copy link
Owner

alekc commented Sep 19, 2023

@Adphi thanks for raising a pr, however I cannot reproduce the original issue with the latest tip of alekc/kubectl

terraform {
  required_version = ">= 0.13"

  required_providers {
    kubectl = {
      source  = "alekc/kubectl"
    }
  }
}

provider "kubectl" {
  load_config_file = true
}

locals {
  date    = tonumber(formatdate("YYYYM", timestamp()))
  is_even = local.date % 2 == 0
}

resource "kubectl_manifest" "test" {
  yaml_body = <<YAML
terraform {
  required_version = ">= 0.13"

  required_providers {
    kubectl = {
      source  = "alekc/kubectl"
      version = "2.0.2"
    }
  }
}

provider "kubectl" {
  load_config_file = true
}

locals {
  date    = tonumber(formatdate("YYYYM", timestamp()))
  is_even = local.date % 2 == 0
}

resource "kubectl_manifest" "test" {
  yaml_body = <<YAML
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-cm
  namespace: default
data:
  test: "static"
  month_parity: "${local.is_even ? "even" : "odd"}"
YAML
}

Do you have an example of reproduction of the error on alekc/kubectl?

@Adphi
Copy link
Contributor Author

Adphi commented Sep 19, 2023

@alekc here is what I used to test:

main.tf:

provider "kubectl" {}

provider "kubernetes" {
  config_path = "~/.kube/config"
}

locals {
  var = formatdate("YYYYMMDDhhmmss", timestamp())
}

resource "kubernetes_namespace" "test" {
  metadata {
    name = "terraform-test"
    labels = {
      "test" = local.var
    }
  }
}

resource "kubectl_manifest" "secret" {
  yaml_body = data.template_file.secret.rendered
  override_namespace = kubernetes_namespace.test.metadata.0.name
}

data template_file "secret" {
  template = file("${path.module}/secret.yaml")
  vars = {
    var = kubernetes_namespace.test.metadata.0.resource_version
  }
}

secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: test-secret
  namespace: test
stringData:
  var: "${var}"

If you run it, it will succeed the first time, but fail every time after that because "var" is updated.

@alekc
Copy link
Owner

alekc commented Sep 19, 2023

Brilliant, thank you. I've been able to reproduce it with stripped down version

terraform {
  required_version = ">= 0.13"

  required_providers {
    kubectl = {
      source = "alekc/kubectl"
    }
  }
}

provider "kubectl" {
  load_config_file = true
}

resource "kubectl_manifest" "secret" {
  yaml_body = templatefile("${path.module}/secret.yaml", {
    var = formatdate("YYYYMMDDhhmmss", timestamp())
  })
}

@Adphi
Copy link
Contributor Author

Adphi commented Sep 19, 2023

For some reason, the fix is not working with this version:

terraform {
  required_version = ">= 0.13"

  required_providers {
    kubectl = {
      source  = "alekc/kubectl"
      version = ">= 2.0.0"
    }
  }
}

provider "kubectl" {}

provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_namespace" "test" {
  metadata {
    name = "terraform-test"
    labels = {
      "test" = formatdate("YYYYMMDDhhmmss", timestamp())
    }
  }
}

resource "kubectl_manifest" "secret" {
  yaml_body = <<-EOT
    apiVersion: v1
    kind: Secret
    metadata:
      name: test-secret
    stringData:
      var: "${kubernetes_namespace.test.metadata.0.resource_version}"
  EOT
  override_namespace = kubernetes_namespace.test.metadata.0.name
}

@alekc alekc merged commit a8eb8d0 into alekc:master Sep 19, 2023
37 checks passed
alekc added a commit that referenced this pull request Sep 19, 2023
@alekc
Copy link
Owner

alekc commented Sep 19, 2023

@Adphi you are right. Raised a new issue to track and fix it.

alekc added a commit that referenced this pull request Sep 19, 2023
dfroberg pushed a commit to froberg-co/terraform-provider-kubectl that referenced this pull request Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants