<p>In my terraform config files I create a Kubernetes cluster on GKE and when created, set up a Kubernetes provider to access said cluster and perform various actions such as setting up namespaces.</p>

<p>The problem is that some new namespaces were created in the cluster without terraform and now my attempts to import these namespaces into my state seem fail due to inability to connect to the cluster, which I believe is due to the following (taken from Terraform's official documentation of the import command):</p>

<blockquote>
  <p>The only limitation Terraform has when reading the configuration files is that the import provider configurations must not depend on non-variable inputs. For example, a provider configuration cannot depend on a data source.</p>
</blockquote>

<p>The command I used to import the namespaces is pretty straightforward:</p>

<p><code>terraform import kubernetes_namespace.my_new_namespace my_new_namespace</code></p>

<p>I also tried using the <code>-provdier=""</code> and <code>-config=""</code> but to no avail.</p>

<p>My Kubernetes provider configuration is this:</p>

<pre><code>provider "kubernetes" {
  version = "~&gt; 1.8"

  host  = module.gke.endpoint
  token = data.google_client_config.current.access_token

  cluster_ca_certificate = base64decode(module.gke.cluster_ca_certificate)
}
</code></pre>

<p>An example for a namespace resource I am trying to import is this:</p>

<pre><code>resource "kubernetes_namespace" "my_new_namespace" {
  metadata {
    name = "my_new_namespace"
  }
}
</code></pre>

<p>The import command results in the following:</p>

<blockquote>
  <p>Error: Get <a href="http://localhost/api/v1/namespaces/my_new_namespace" rel="noreferrer">http://localhost/api/v1/namespaces/my_new_namespace</a>: dial tcp [::1]:80: connect: connection refused</p>
</blockquote>

<p>It's obvious it's doomed to fail since it's trying to reach <code>localhost</code> instead of the actual cluster IP and configurations.</p>

<p>Is there any workaround for this use case?</p>

<p>Thanks in advance.</p>

<p>the issue lies with the dynamic data provider. The <code>import</code> statement doesn't have access to it.</p>
<p>For the process of importing, you have to hardcode the provider values.</p>
<p>Change this:</p>
<pre><code>provider &quot;kubernetes&quot; {
  version                = &quot;~&gt; 1.8&quot;
  host                   = module.gke.endpoint
  token                  = data.google_client_config.current.access_token
  cluster_ca_certificate = base64decode(module.gke.cluster_ca_certificate)
}
</code></pre>
<p>to:</p>
<pre><code>provider &quot;kubernetes&quot; {
  version                = &quot;~&gt; 1.8&quot;
  host                   = &quot;https://&lt;ip-of-cluster&gt;&quot;
  token                  = &quot;&lt;token&gt;&quot;
  cluster_ca_certificate = base64decode(&lt;cert&gt;)
  load_config_file       = false
}
</code></pre>
<ul>
<li>The token can be retrieved from <code>gcloud auth print-access-token</code>.</li>
<li>The IP and cert can be retrieved by inspecting the created container resource using <code>terraform state show module.gke.google_container_cluster.your_cluster_resource_name_here</code></li>
</ul>
<p>For provider version 2+ you have to drop <code>load_config_file</code>.</p>
<p>Once in place, import and revert the changes on the provider.</p>
