Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform kubectl provider not found

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

like image 708
Padi Avatar asked Oct 20 '20 08:10

Padi


People also ask

What is Kubernetes provider in Terraform?

The Kubernetes (K8S) provider is used to interact with the resources supported by Kubernetes. The provider needs to be configured with the proper credentials before it can be used. Use the navigation to the left to read about the available resources.

How do you run a Kubectl command from Terraform?

You can use the Terraform resources template_file and null_resource. Notice that I'm using the trigger to run the kubectl command always you modify the template (you may want to replace create with apply). But maybe the best way is to use the Kubernetes provider.

What is a Terraform provider?

A provider is a Terraform plugin that allows users to manage an external API. Provider plugins like the AWS provider or the cloud-init provider act as a translation layer that allows Terraform to communicate with many different cloud providers, databases, and services.

Is it possible to call kubectl from terraform?

Calling kubectl from Terraform does mean that the Kubernetes resources it creates aren’t under the control of Terraform. For crafting your own deployments, persistent volumes, volume claims etc, I recommend using the native Kubernetes provider in Terraform as far as you can, which will give you finer control over the resources it creates.

What is terraform-provider-kubectl?

A set of helpful data resources to process directories of yaml files and inline templating is available. This terraform-provider-kubectl provider has been used by many large Kubernetes installations to completely manage the lifecycle of Kubernetes resources. The provider can be installed and managed automatically by Terraform.

Do you need kubectl with cluster-admin rights for Kubernetes terraform?

The Kubernetes Terraform provider has received some much needed attention recently, but there are still certain resources — in particular the custom Azure resources for deploying things like managed pod identities — which require the use of kubectl with cluster-admin rights to deploy them.

Where can I find versioned documentation for my terraform project?

For providers distributed on the Terraform Registry, versioned documentation is available on each provider's page, via the "Documentation" link in the provider's header.


4 Answers

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

like image 88
Jethro Avatar answered Oct 16 '22 18:10

Jethro


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

like image 2
Padi Avatar answered Oct 16 '22 18:10

Padi


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.

like image 1
ARMP125 Avatar answered Oct 16 '22 19:10

ARMP125


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

like image 1
RVndra Singh Avatar answered Oct 16 '22 19:10

RVndra Singh