Terraform cloud workspace - Error while installing hashicorp/kubectl: provider registry

I am trying to add the gavinbunney/kubectl terraform provider to work with it from a couple of workspaces from terraform cloud. I am using Terraform v0.13.6

So I have it as a required provider in this way by defining gavinbunney/kubectl as a source:

terraform {
  required_version = ">= 0.13.6"
  required_providers {
    kubectl = {
      source  = "gavinbunney/kubectl"
      version = "~> 1.10.0"
    }
  }
}

provider "kubectl" {
  load_config_file       = "false"
  host                   = data.terraform_remote_state.tfc.outputs.kube_config.0.host
  client_certificate     = base64decode(data.terraform_remote_state.tfc.outputs.kube_config.0.client_certificate)
  client_key             = base64decode(data.terraform_remote_state.tfc.outputs.kube_config.0.client_key)
  cluster_ca_certificate = base64decode(data.terraform_remote_state.tfc.outputs.kube_config.0.cluster_ca_certificate)
}

When I run terraform init from my local machine I can see the kubectl provider is installed from gavinbunney/kubectl source, but in addition I got the following error (Please pay attention to the comments on the output below):

> terraform_0.13.6 init
Initializing modules...
....

Initializing the backend...

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform

############# 
# HERE NOT SURE WHY TERRAFORM INSISTS TO LOOK FOR kubectl PROVIDER 
# AT hashicorp/kubectl SOURCE BY DEFAULT
- Finding latest version of hashicorp/kubectl...
############# 

# HERE I CAN SEE gavinbunney/kubectl PROVIDER WAS INSTALLED
- Finding gavinbunney/kubectl versions matching "~> 1.10.0"...
- Installing gavinbunney/kubectl v1.10.0...
- Installed gavinbunney/kubectl v1.10.0 (self-signed, key ID AD64217B5ADD572F)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

# BUT I GOT THIS ERROR 

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

Why is terraform trying to pull kubectl provider from hashicorp/kubectl source when I defined as a source gavinbunney/kubectl?

Looking at my local directory .terraform/plugins/registry.terraform.io I can see indeed I have the following source providers installed:

~/w/my-project/terraform/kubernetes/.terraform/plugins/registry.terraform.io  on 
> ls -all
total 0
drwxr-xr-x  3 bgarcial  staff   96 Feb 26 10:13 carlpett
drwxr-xr-x  3 bgarcial  staff   96 Feb 26 10:13 gavinbunney
drwxr-xr-x  9 bgarcial  staff  288 Feb 26 10:13 hashicorp

If I go to hashicorp directory indeed I can see there is no kubectl provider :

~/w/my-project/terraform/kubernetes/.terraform/plugins/registry.terraform.io/hashicorp  
> ls
azuread    azurerm    helm      kubernetes null       random     template

Of course that is the reason why for my error, even being strange I declared the source as gavinbunney/kubectl, which one indeed exist in my .terraform/plugins/registry.terraform.io/gavinbunney directory

my-project/terraform/kubernetes/.terraform/plugins/registry.terraform.io/gavinbunney  
> ls
kubectl

Seeing this, that I did was to copy from my existing gavinbunney/kubectl to the source terraform 0.13 looks try to look by default the provider, it means registry.terraform.io/hashicorp

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

When I run terraform init again, it is successful

> terraform init
Initializing modules...

Initializing the backend...

Initializing provider plugins...

- Using previously-installed hashicorp/kubernetes v2.0.2

# I DON'T UNDERSTAND WHY IT SAYS THIS SINCE THE PLUGIN WAS NOT FOUND
# BEFORE IN THAT SOURCE
- Using previously-installed hashicorp/kubectl v1.10.0
#####

# HERE I AM STILL USING gavinbunney/kubectl SOURCE
# IS THAT ONE I WANT TO USE
- Using previously-installed gavinbunney/kubectl v1.10.0

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

# IT LOOKS LIKE ITS SET IT UP OFFICIALLY HASHICORP AS A SOURCE
* hashicorp/kubectl: version = "~> 1.10.0"

Terraform has been successfully initialized!
...

When terraform init is executed from my terraform cloud workspace, something similar happens, I can see kubectl plugin provider is installed from gavinbunney/kubectl (that one I defined to use in my terraform configuration):

Terraform v0.13.6
Configuring remote state backend...
Initializing Terraform configuration...

Setup failed: Failed terraform init (exit 1): <nil>

Output:
e[0me[1mInitializing modules...e[0m
.....
e[0me[1mInitializing the backend...e[0m
e[0me[32m
Successfully configured the backend "remote"! Terraform will automatically
use this backend unless the backend configuration changes.e[0m

e[0me[1mInitializing provider plugins...e[0m
- terraform.io/builtin/terraform is built in to Terraform

############# 
# TERRAFORM IS LOOKING FOR hashicorp/kubectl SOURCE
- Finding latest version of hashicorp/kubectl...
#############

# TERRAFORM IS INSTALLING IT FROM gavinbunney/kubectl SOURCE
- Installing gavinbunney/kubectl v1.10.0...
- Installed gavinbunney/kubectl v1.10.0 (self-signed, key ID e[0me[1mAD64217B5ADD572Fe[0me[0m)
########



Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

# BUT THEN TERRAFORM IS ALSO TRYING TO INSTALL IT 
# FROM ehashicorp/kubectl SOURCE

e[1me[31mError: e[0me[0me[1mFailed to install providere[0m

e[0mError while installing hashicorp/kubectl: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/kubectl
e[0me[0m

Reading on, I found this answer:

Terraform v0.13 introduced the idea of third-party providers that belong to other namespaces that are not controlled directly by HashiCorp, but to maximize backward compatibility with modules that were written for Terraform v0.12 and earlier there is a fallback behavior where Terraform will assume that any provider requirement not explicitly declared is aiming to use one of the official providers which now live in the “hashicorp” namespace in the registry, because for Terraform v0.12 and earlier third-party providers were not automatically installable at all.

When writing modules for Terraform v0.13 or later you should include explicit provider requirements to specify the full source addresses for each of the providers your module uses

Why if I am explicitly using provider requirements I have had to copy locally in my local terraform initialization to make it works and in addition terraform cloud is not able to identify the source I am stating for gavinbunney/kubectl v1.10.0.?

NOTE:

Not sure if I will have to check the Implied Local mirrors alternative described there

2 Likes

Why is terraform trying to pull kubectl provider from hashicorp/kubectl source when I defined as a source gavinbunney/kubectl ?

I can’t be sure from the logs, but the most common reason for this sort of error is having references to kubectl resources in child modules in your configuration, without a matching required_providers block. To determine if that’s the case, you can try running the terraform providers command locally: this will tell you where the dependency on hashicorp/kubectl is coming from.

To fix this, in every module using the kubectl provider, you need to tell Terraform what is meant by the short kubectl name, by defining the provider source. Something like this:

terraform {
  required_providers {
    kubectl = {
      source  = "gavinbunney/kubectl"
    }
  }
}

Hope that helps!

2 Likes

Thanks @alisdair indeed that was the solution :slight_smile:

A post was split to a new topic: Error on init: “No available releases match constraints”