-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
I wanted to query for a Kubernetes custom resource across all namespaces, and while processing each I needed to know the namespace the resource it came from. However I discovered that metadata is not available in the output of the kubernetes_resources data source for (at least) custom resources. I tested with multiple unrelated custom resources and observed this. I only tested with the ConfigMap built in resource and noticed that it was present in the output though (I didn't test any other built in resources).
A minimal example to reproduce is below. This is simplified from my actual use case. Change api_version and kind in data.kubernetes_resources.customresources to match any CR that is present in your cluster for testing.
Terraform Version, Provider Version and Kubernetes Version
Terraform version: 1.5.7 (or Tofu v1.10.6)
Kubernetes provider version: v3.0.1
Kubernetes version: K3s v1.32.5+k3s1
Affected Resource(s)
- kubernetes_resources
Terraform Configuration Files
data "kubernetes_resources" "customresources" {
api_version = "networking.istio.io/v1"
kind = "VirtualService"
}
data "kubernetes_resources" "configmaps" {
api_version = "v1"
kind = "ConfigMap"
}
output "namespaces_with_customresources" {
value = toset([ for customresource in data.kubernetes_resources.customresources.objects : customresource.metadata.namespace ])
}
output "namespaces_with_configmaps" {
value = toset([ for configmap in data.kubernetes_resources.configmaps.objects : configmap.metadata.namespace ])
}Steps to Reproduce
terraform init
terraform apply
Expected Behavior
Both built in Kubernetes resources and custom resources should populate .objects.metadata in their results. I only observed ConfigMaps populating this, but not my custom resource.
Actual Behavior
$ terraform plan
data.kubernetes_resources.customresources: Reading...
data.kubernetes_resources.configmaps: Reading...
data.kubernetes_resources.configmaps: Read complete after 0s
data.kubernetes_resources.customresources: Read complete after 0s
Changes to Outputs:
+ namespaces_with_configmaps = [
+ "default",
]
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
╷
│ Error: Unsupported attribute
│
│ on main.tf line 12, in output "namespaces_with_customresources":
│ 12: value = toset([ for customresource in data.kubernetes_resources.customresources.objects : customresource.metadata.namespace ])
│
│ This object does not have an attribute named "metadata".
╵
Important Factoids
I tested with a couple different CRs. I didn't test with that many built-in k8s resources, maybe the issue is broader
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment