1

After the cluster is created, I want to continue the deployment, but I cannot read the kubeconfig file properly. How can i fix that ? By the way, I can connect my cluster with .kubeconfig file. But terraform cannot.

'config_path' refers to an invalid path: ".kubeconfig": stat .kubeconfig: no such file or directory

Error: Failed to create deployment: Post "http://localhost/apis/apps/v1/namespaces/default/deployments": dial tcp 127.0.0.1:80: connect: connection refused

deployment.tf

resource "kubernetes_deployment" "nginx_deployment" {
  provider = kubernetes.kb
  ...
  ...
}

provider.tf

terraform {
  required_providers {
    ovh = {
      source = "ovh/ovh"
      version = "0.16.0"
    }
    kubernetes = {
      source = "hashicorp/kubernetes"
      version = "2.8.0"
    }

    local = {
      source = "hashicorp/local"
      version = "2.1.0"
    }
  }
}

# Configure the OVHcloud Provider
provider "ovh" {
  endpoint           = "ovh-ca"
  application_key    = ""
  application_secret = ""
  consumer_key       = ""
}

provider "kubernetes" {
  alias = "kb"
#  load_config_file = "false"
  config_path = ".kubeconfig"
}

provider "local" {
  #comment
}

resource "local_file" "kubeconfig" {
  content  = ovh_cloud_project_kube.my_kube_cluster.kubeconfig
  filename = ".kubeconfig"
}
MrTux01
  • 343
  • 3
  • 9
  • Are you sure that the path of the kubeconfig is correct? Try to put the kubeconfig file in the same dirctory of the main.tf and change the path to `./.kubeconfig` – Danilo Cacace Feb 16 '22 at 17:59
  • It didn't work with absolute path either. Example: /home/user/.kubeconfig – MrTux01 Feb 16 '22 at 18:04
  • 1
    I do the same thing in my project and `kubeconfig_path = "~/.kube/config"` works for me. Try to use another path or another filename for kubeconfig – Danilo Cacace Feb 17 '22 at 08:49
  • Even I am facing this issue. The surprising part is when I do Terraform apply subsequently then it is able to find the file and continues creating the remaining set of resources. I have tried different options like ${path.root}/kubeconfig, ${path.cwd}/kubeconfig, ${path.module}/kubeconfig – Nitin G Jun 16 '22 at 10:45

1 Answers1

0

You have to replace

provider "kubernetes" {
  alias = "kb"
#  load_config_file = "false"
  config_path = ".kubeconfig"
}

by

provider "kubernetes" {
  alias = "kb"
  config_path = local_file.kubeconfig.filename
}

And may I suggest you to use a local_sentitive_file instead of a local_file?