hashicorp / terraform-provider-kubernetes-alpha

A Terraform provider for Kubernetes that uses dynamic resource types and server-side apply. Supports all Kubernetes resources.
https://registry.terraform.io/providers/hashicorp/kubernetes-alpha/latest
Mozilla Public License 2.0
490 stars 63 forks source link

Conditionally omitted arguments not working if type has to be object. #196

Open landorg opened 3 years ago

landorg commented 3 years ago

Terraform, Provider, Kubernetes versions

Terraform version: 0.13.5
Provider version: 0.2.1
Kubernetes version: 1.19

Affected Resource(s)

    apiVersion = "acid.zalan.do/v1"
    kind       = "postgresql"

https://postgres-operator.readthedocs.io/en/latest/reference/cluster_manifest/#custom-tls-certificates

Terraform Configuration Files

I'm trying to conditionally omit the spec.tls argument.

      tls = var.tls == false ? null : {
        secretName = kubernetes_secret.postgres_cert.0.metadata.0.name
      }
      tls = var.tls == false ? {} : {
        secretName = kubernetes_secret.postgres_cert.0.metadata.0.name
      }

Debug Output

Error: rpc error: code = Unknown desc = update dry-run for 'nominatim-databases/nominatim-postgres-cluster' failed: postgresql.acid.zalan.do "nominatim-postgres-cluster" is invalid: spec.tls: Invalid value: "null": spec.tls in body must be of type object: "null"
Error: rpc error: code = Unknown desc = update dry-run for 'keycloak/keycloak-postgres-cluster' failed: postgresql.acid.zalan.do "keycloak-postgres-cluster" is invalid: spec.tls.secretName: Required value

Steps to Reproduce

resource "kubernetes_manifest" "postgres" {
  provider = kubernetes-alpha

  manifest = {
    apiVersion = "acid.zalan.do/v1"
    kind       = "postgresql"
    metadata = {
      name      = "${var.name}-postgres-cluster"
      namespace = var.namespace
      labels = {

      }
    }
    spec = {
      dockerImage = "registry.opensource.zalan.do/acid/spilo-13:2.0-p2"
      databases = {
        postgres = "postgres"
      }
      numberOfInstances = var.replicas
      postgresql = {
        version = var.postgres_major_version
      }
      podAnnotations = {
        "ad.datadoghq.com/stolon.check_names"  = jsonencode(["postgres"])
        "ad.datadoghq.com/stolon.init_configs" = jsonencode([{}])
        "ad.datadoghq.com/stolon.instances" = jsonencode([
          {
            host     = "%%host%%"
            port     = "5432"
            username = "datadog"
            password = "%%env_POSTGRES_DD_PASSWORD%%"
            tags = [
              "postgres-cluster:${var.name}"
            ]
          }
        ])
      }
      preparedDatabases = var.prepared_databases
      spiloFSGroup      = 103
      teamId            = var.name

      tls = var.tls == false ? {secretName = "xxx"} : {
        secretName = kubernetes_secret.postgres_cert.0.metadata.0.name
      }

      users = {
        postgres = [
          "superuser",
          "createdb",
        ]
      }
      resources = {
        requests = {
          cpu    = "100m"
          memory = "1500Mi"
        }
        limits = {
          cpu    = "1000m"
          memory = "2000Mi"
        }
      }
      volume = {
        size = "10Gi"
      }
    }
  }
}
  1. terraform apply
phlegx commented 3 years ago

Hi there!

I think I have a similar issue here (Kubernetes Alpha v0.5.0). When I try to remove this section and hit apply

      patroni = {
        initdb = {
          encoding = "UTF8"
        }
      }

I do get the following error:


│ Error: spec.patroni.initdb
│ 
│   with module.core.module.postgres.kubernetes_manifest.postgres,
│   on ../../../terraform-modules/databases/postgres/main.tf line 29, in resource "kubernetes_manifest" "postgres":
│   29: resource "kubernetes_manifest" "postgres" {
│ 
│ Invalid value: "null": spec.patroni.initdb in body must be of type object: "null"

Is this the same issue perhaps? Can and will this be eventually solved in the near future?

thanks a lot Martin