terraform-provider-kubectl icon indicating copy to clipboard operation
terraform-provider-kubectl copied to clipboard

Cannot import CRD definition

Open headyj opened this issue 2 years ago • 3 comments

I cannot import an existing resource in my state using the provided documentation:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: rabbitmqclusters.rabbitmq.com
terraform import kubectl_manifest.rabbitmq_clusters
apiextensions.k8s.io/v1//CustomResourceDefinition//rabbitmqclusters.rabbitmq.com
kubectl_manifest.rabbitmq_clusters[0]: Importing from ID "apiextensions.k8s.io/v1//CustomResourceDefinition//rabbitmqclusters.rabbitmq.com"...
╷
│ Error: failed to get resource apiextensions.k8s.io/v1 CustomResourceDefinition rabbitmqclusters.rabbitmq.com from kubernetes: customresourcedefinitions.apiextensions.k8s.io "rabbitmqclusters.rabbitmq.com" not found

As you can see, it says that the rabbitmqclusters.rabbitmq.com crd does not exist, but it does:

kubectl get crd rabbitmqclusters.rabbitmq.com -o jsonpath='{.apiVersion}:{.metadata.name}'
apiextensions.k8s.io/v1:rabbitmqclusters.rabbitmq.com

headyj avatar Mar 07 '22 12:03 headyj

I have a similar problem, except I get an Unauthorized (but I am able to otherwise manage the cluster resources).

Trying to do

terraform import -var-file=env/${ENVIRONMENT}.tfvars 'kubectl_manifest.grafana_operator_crds["grafanadashboards"]' apiextensions.k8s.io/v1//CustomResourceDefinition//grafanadashboards.integreatly.org

The error:

╷
│ Error: failed to get resource apiextensions.k8s.io/v1 CustomResourceDefinition grafanadashboards.integreatly.org from kubernetes: Unauthorized
│ 
│ 
╵

And I can see the resource exists:

kubectl get crd grafanadashboards.integreatly.org -o jsonpath='{.apiVersion}:{.metadata.name}'
apiextensions.k8s.io/v1:grafanadashboards.integreatly.org

NickLarsenNZ avatar Oct 14 '22 19:10 NickLarsenNZ

I managed to resolve my issue with this: https://github.com/hashicorp/terraform-provider-kubernetes/issues/918#issuecomment-823675834

NickLarsenNZ avatar Oct 14 '22 19:10 NickLarsenNZ

Had the same problem where I received an error that the resource that I was trying to import does not exist. After trying it with a slightly different command I actually got an helpful error back: Error: expected ID in format apiVersion//kind//name//namespace, received: v1//name-of-resource

So after adjusting the ID accordingly the import was finally successful 🙌

terraform import kubectl_manifest.my_configmap v1//ConfigMap//name-of-configmap-//name-of-namespace

I'm assuming by default it looks for the resource in the default namespace if it isn't specified in the import command.

kolja-lucht avatar Feb 09 '23 11:02 kolja-lucht