Open redzioch opened 1 year ago
I have come across the same thing
When trying to check for an already existent or not deployment -
of course when it exists it works
when it doesn't Fail with the same error output : Error: Provider produced null object
this is really stupid in my opinion - should be either in a try block or someother way to indicate that it returned a null instead of failing
now one has to find workaround to achieve this...
Has anyone found a workaround for this issue? I'm trying to check if a CRD exists in the cluster so I know if another resource can be installed.
This is possibile using data kubernetes_resources using field_selector
or label_selector
, so to check if CRD is created use:
data "kubernetes_resources" "example" {
api_version = "apiextensions.k8s.io/v1"
kind = "CustomResourceDefinition"
field_selector = "metadata.name==awsnodetemplates.karpenter.k8s.aws"
}
then use conditional resource like:
resource "kubernetes_manifest" "awsnodetemplate_provider" {
count = length(data.kubernetes_resources.example.objects)>0 ? 1 : 0
...
}
be careful as there are kubernetes_resource
and kubernetes_resources
. Selectors are available only in plural version ending with "s".
This is possibile using data kubernetes_resources using
field_selector
orlabel_selector
, so to check if CRD is created use:data "kubernetes_resources" "example" { api_version = "apiextensions.k8s.io/v1" kind = "CustomResourceDefinition" field_selector = "metadata.name==awsnodetemplates.karpenter.k8s.aws" }
then use conditional resource like:
resource "kubernetes_manifest" "awsnodetemplate_provider" { count = length(data.kubernetes_resources.example.objects)>0 ? 1 : 0 ... }
be careful as there are
kubernetes_resource
andkubernetes_resources
. Selectors are available only in plural version ending with "s".
This doesn't work because count
needs to be known in the plan
phase, and the result of data
is after apply:
The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on.
Marking this issue as stale due to inactivity. If this issue receives no comments in the next 30 days it will automatically be closed. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. This helps our maintainers find and focus on the active issues. Maintainers may also remove the stale label at their discretion. Thank you!
When k8s resource described in
data.kubernetes_resource
does not existterraform plan/apply
ends with error. When resource exists, data is correctly returned. I am trying to use this data source to verify if required Helm chart (MetalLB) is installed and CRDs exist.Terraform Version, Provider Version and Kubernetes Version
Affected Resource(s)
kubernetes_resource
Terraform Configuration Files
Debug Output
https://gist.github.com/redzioch/967808d1b7b3eb436ba3a2f4177f41a6
Steps to Reproduce
git clone https://github.com/redzioch/terraform-kubernetes-resource-error.git
terraform plan
Expected Behavior
When resource does not exist,
data.kubernetes_resource.address_pool_crd.object
should benull
.Actual Behavior
Community Note