gavinbunney / terraform-provider-kubectl

Terraform provider to handle raw kubernetes manifest yaml files
https://registry.terraform.io/providers/gavinbunney/kubectl
Mozilla Public License 2.0
612 stars 105 forks source link

import state does not show correctly #200

Closed mehrdad-faridi closed 2 years ago

mehrdad-faridi commented 2 years ago

Hi,

based on the document for import state(here) when I import state, the state file does not show correctly.

terraform import kubectl_manifest.prod-manifests "apps/v1//Deployment//nginx-deployment//default"

kubectl_manifest.prod-manifests: Importing from ID "apps/v1//Deployment//nginx-deployment//default"...
kubectl_manifest.prod-manifests: Import prepared!
  Prepared kubectl_manifest for import
kubectl_manifest.prod-manifests: Refreshing state... [id=/apis/apps/v1/namespaces/default/deployments/nginx-deployment]

Import successful!

but Terraform state show is:

terraform state list
data.google_client_config.current
data.google_container_cluster.gke
data.kubectl_path_documents.manifest
kubectl_manifest.prod-manifests             # <--- this line

but if we don't use `terraform import` command and use `terraform apply` command(to see the correct state file) we see another state name/style: `terraform state list` ```terraform data.google_client_config.current data.google_container_cluster.gke data.kubectl_path_documents.manifest kubectl_manifest.prod-manifests["/apis/apps/v1/namespaces/default/deployments/nginx-deployment"] ```

the terraform code block for kubectl provider I used is: ```terraform data "kubectl_path_documents" "manifest" { pattern = "./manifests/*.yaml" } resource "kubectl_manifest" "prod-manifests" { for_each = data.kubectl_path_documents.manifest.manifests yaml_body = each.value } ```
Terraform and kubectl provider version: ```bash Terraform v1.2.6 on darwin_arm64 + provider registry.terraform.io/gavinbunney/kubectl v1.14.0 ```
DaleyKD commented 2 years ago

This is not a provider issue. This is exactly how terraform works.

You have a for_each block in the kubectl_manifest resource. That will create an array of resources, where the index is the key of the map.

Therefore, you need to change your import to:

terraform import kubectl_manifest.prod-manifests["/apis/apps/v1/namespaces/default/deployments/nginx-deployment"] "apps/v1//Deployment//nginx-deployment//default"

(In PowerShell for Windows, it'll be:

terraform import 'kubectl_manifest.prod-manifests[\"/apis/apps/v1/namespaces/default/deployments/nginx-deployment\"]' "apps/v1//Deployment//nginx-deployment//default"
mehrdad-faridi commented 2 years ago

yes, you're right @DaleyKD. thank you for your help. :raised_hands:


and also you have used a single quote to work(if you do not use it you'll get `Error: Index value required`). so the below command worked for me. ```bash terraform import kubectl_manifest.prod-manifests'["/apis/apps/v1/namespaces/default/deployments/nginx-deployment"]' "apps/v1//Deployment//nginx-deployment//default" ```