<p>I'm bring a GKE cluster up with Terraform, which works nicely. I then want Terraform to perform some Kubernetes-level operations on the cluster (using the k8s provider) - nothing massive, just installing a couple of Deployments etc.</p>

<p>The problem I'm having is permissions. I'd like a <strong>neat, tidy, declarative</strong> way to make a cluster and have a set of credentials in hand that I can use short-term to do "admin" operations on it, including bootstrapping other users. I know how to make the google user that's running TF an admin of the cluster (that question comes up a lot), but that doesn't seem very <em>nice</em>. Not least, the k8s TF provider doesn't support clusterolebinding (<a href="https://github.com/terraform-providers/terraform-provider-kubernetes/issues/30" rel="noreferrer">Issue</a>, <a href="https://github.com/terraform-providers/terraform-provider-kubernetes/pull/73" rel="noreferrer">partial PR</a>) so you have to "shell out" with a <code>local-exec</code> provisioner to first run <code>gcloud container clusters get-credentials</code> and then <code>kubectl create clusterrolebinding ...</code>.</p>

<p>Similarly, I don't want to set a master password, because I don't want to run with HTTP basic auth on. The nicest option looks to be the key/cert pair that's returned by the TF GKE resource, but that has a CN of "client", and that user has no power. So again, the only way to use it is to shell out to kubectl, pass it the gcloud service account credentials, and get it to add a clusterrolebinding for "client", at which point I may as well just do everything as the service account like above.</p>

<p>For contrast, on EKS the (AWS IAM) user that creates the cluster has cluster-admin out of the box (I assume the AWS authn provider claim's the user is in "system:masters").</p>

<p>My actual question here is: is there a neat, fully declarative way in Terraform to bring up a cluster and have available (ideally as output) a potent set of credentials to use and then drop? (yes I know they'll stay in the tfstate)</p>

<p>My options seem to be:</p>

<ul>
<li>"shell out" to give TF's google ID (ideally a service account) cluster-admin (which is privilege escalation, but which works due to the gcloud authz plugin)</li>
<li>Enable HTTP basic auth and give the admin account a password, then have an aliased k8s provisioner use that to do minimal bootstrapping of another service account.</li>
<li>Enable ABAC so that "client" (the CN of the output key/cert) has infinite power - this is what I'm currently running with, don't judge me!</li>
</ul>

<p>And I don't like any of them!</p>

<p>I've been running into a similar problem, which has gotten particularly nasty since a <a href="https://github.com/terraform-providers/terraform-provider-google/issues/3217" rel="noreferrer">recent Kubernetes issue unexpectedly disabled basic auth by default</a>, which broke my previously-functioning Terraform configuration as soon as I tried to build a new cluster from the same config.</p>

<p>Finally found an answer in <a href="https://stackoverflow.com/a/55007787/3198554">this SO answer</a>, which recommends a method of using Terraform's Google IAM creds to connect to the cluster without needing the "shell out". Note that this method allows cluster permissions to be bootstrapped in Terraform with no external tooling/hacks/etc and without needing to have basic auth enabled.</p>

<p>The relevant part of that answer is:</p>

<pre><code>data "google_client_config" "default" {}

provider "kubernetes" {
  host     = "${google_container_cluster.default.endpoint}"

  token = "${data.google_client_config.default.access_token}"
  cluster_ca_certificate = "${base64decode(google_container_cluster.default.master_auth.0.cluster_ca_certificate)}"

  load_config_file = false
}
</code></pre>
