Search code examples
kubernetesterraformgoogle-kubernetes-enginekubectl

Terraform kubectl provider error: failed to create kubernetes rest client for read of resource


I have a Terraform config that (among other resources) creates a Google Kubernetes Engine cluster on Google Cloud. I'm using the kubectl provider to add YAML manifests for a ManagedCertificate and a FrontendConfig, since these are not part of the kubernetes or google providers. This works as expected when applying the Terraform config from my local machine, but when I try to execute it in our CI pipeline, I get the following error for both of the kubectl_manifest resources:

Error: failed to create kubernetes rest client for read of resource: Get "http://localhost/api?timeout=32s": dial tcp 127.0.0.1:80: connect: connection refused

Since I'm only facing this issue during CI, my first guess is that the service account is missing the right scopes, but as far as I can tell, all scopes are present. Any suggestions and ideas are greatly appreciated!


Solution

  • Fixed the issue by adding load_config_file = false to the kubectl provider config. My provider config now looks like this:

    data "google_client_config" "default" {}
    
    provider "kubernetes" {
      host                   = "https://${endpoint from GKE}"
      token                  = data.google_client_config.default.access_token
      cluster_ca_certificate = base64decode(CA certificate from GKE)
    }
    
    provider "kubectl" {
      host                   = "https://${endpoint from GKE}"
      token                  = data.google_client_config.default.access_token
      cluster_ca_certificate = base64decode(CA certificate from GKE)
      load_config_file       = false
    }