12

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? :)

Update with terraform 1.4.0

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

provider "kubernetes" {
  host                   = module.k8s.cluster_endpoint
  cluster_ca_certificate = base64decode(module.k8s.cluster_certificate_authority_data)

  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    command     = "aws"
    args        = ["eks", "get-token", "--cluster-name", module.k8s.cluster_name]
  }
}

resource "kubernetes_namespace" "velero" {
  metadata {
    name = var.velero_ns
  }
}
Padi
  • 711
  • 9
  • 18
  • 1
    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 Oct 20 '20 at 08:30
  • 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 – Padi Oct 20 '20 at 08:42
  • Can you edit your question to include the `terraform` block definition with the `required_providers` as well please? – ydaetskcoR Oct 20 '20 at 08:53
  • terraform successfully init the `gavinbunney/kubectl` but when I add `kubectl_manifest` resource in k8s.module terraform is trying to load `hashicorp/kubectl` provider – Padi Oct 20 '20 at 09:11
  • What version of terraform are you using? Whats the output from `terraform -v`? – Liam Oct 20 '20 at 09:16
  • @Liam updated 'some outputs:', thanks – Padi Oct 20 '20 at 09:40
  • Hello, I ran the tutorials you mentioned but I couldn't replicate the issue you are having (same terraform version and plugins). Could you provide the exact steps (and files) you followed to get to that point for reproduction purposes? – Dawid Kruk Oct 21 '20 at 07:21
  • @DawidKruk thanks :) If you already have the setup can you try to add a module containing one resource from `hashicorp/kubernetes` for example `resource "kubernetes_namespace" "example" { metadata { name = "my-first-namespace" } }` and one module with a resource from `gavinbunney/kubectl` – Padi Oct 21 '20 at 09:26
  • because in my env, when I add the `kubectl_manifest` resource in main.tf works good and `gavinbunney/kubectl` is used as a provider but when I create a module (modules/k8s_templates) terraform is trying to find `hashicorp/kubectl` – Padi Oct 21 '20 at 09:28

5 Answers5

4

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"
    }
  }
}

Jethro
  • 3,029
  • 3
  • 27
  • 56
2

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 :)

Padi
  • 711
  • 9
  • 18
2

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

In my project, I ran the following command:

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.

allexiusw
  • 1,533
  • 2
  • 16
  • 23
ARMP125
  • 59
  • 4
1

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

RVndra Singh
  • 146
  • 1
  • 5
0

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.