8
votes

I'm trying to add kubectl provider for terraform module and I follow the docs from Terraform kubectl. I run terraform init and provider is installed with success but when I try to add a sample config, for ex: ( or thers from here )

resource "kubectl_server_version" "current" {}

and run terraform plan I got the following msg:

Error: Could not load plugin
Failed to instantiate provider "registry.terraform.io/hashicorp/kubectl" to
obtain schema: unknown provider "registry.terraform.io/hashicorp/kubectl"

and when I tun terraform init ( with the resource in place in module k8s )

Error: Failed to install provider

Error while installing hashicorp/kubectl: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/kubectl

some outputs:

$terraform plugins

├── provider[registry.terraform.io/hashicorp/kubernetes] 1.13.2
├── provider[registry.terraform.io/gavinbunney/kubectl] 1.9.1
├── module.k8s
│   ├── provider[registry.terraform.io/hashicorp/kubectl]
│   └── provider[registry.terraform.io/hashicorp/kubernetes]



$terraform init

Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/kubernetes v1.13.2
- Using previously-installed gavinbunney/kubectl v1.9.1

$terraform -v

Terraform v0.13.4
  + provider registry.terraform.io/gavinbunney/kubectl v1.9.1
  + provider registry.terraform.io/hashicorp/kubernetes v1.13.2
  ....

some config files:

terraform.tf

terraform {

  required_version  = "0.13.4"

  backend "gcs" {
    ...
  }

  required_providers {
    kubernetes = {
        source        = "hashicorp/kubernetes"
        version       = "1.13.2"
      }

    kubectl = {
      source          = "gavinbunney/kubectl"
      version         = "1.9.1"
    }
....

terraform successfully init the gavinbunney/kubectl provider but when I add resource "kubectl_manifest" ... in k8s.module terraform is trying to load hashicorp/kubectl provider

what i'm missing? :)

5
You say it's installed with success but only show an error for trying to install it. Can you show the output when you think it has been installed successfully?ydaetskcoR
terraform init Initializing modules... Initializing the backend... Initializing provider plugins... - Using previously-installed hashicorp/kubernetes v1.13.2 - Using previously-installed gavinbunney/kubectl v1.9.1Padi
Can you edit your question to include the terraform block definition with the required_providers as well please?ydaetskcoR
@ydaetskcoR done, thanksPadi
terraform successfully init the gavinbunney/kubectl but when I add kubectl_manifest resource in k8s.module terraform is trying to load hashicorp/kubectl providerPadi

5 Answers

3
votes

In my case it was due to referencing kubectl resources in a module, but the module needed the provider adding to required_providers within the module:

terraform {
  required_providers {
    kubectl = {
      source  = "gavinbunney/kubectl"
      version = "x.x.x"
    }
  }
}

1
votes

Seems like the problem was that I had the resource "kubectl_server_version" "current" {} among with other resources from hashicorp/kubernetes resources in same module and terraform was trying to load kubectl from hashicorp/kubectl.

When I added gavinbunney/kubectl's resources in main.tf all works good :)

1
votes

When I read the file cat .terraform/plugins/selections.json, I hunderstand that the package is not realy well installed.

In my project, I did :

cp -R .terraform/plugins/registry.terraform.io/gavinbunney/kubectl .terraform/plugins/registry.terraform.io/hashicorp

and after:

terraform init

This look to resolve the problem.

1
votes

To resolve this issue you can look into terraform state replace-provider registry.terraform.io/hashicorp/kubectl gavinbunney/kubectl

0
votes

It seems that if you leave only the kubernetes provider, the kubectl provider works just fine. Means, no need to declare kubernetes and kubectl providers.