Skip to content

Commit

Permalink
Updating Documentation for exchanging credentials with the kubernetes… (
Browse files Browse the repository at this point in the history
#10153)

Co-authored-by: Omar Buhidma <omar@buhidma.net>
  • Loading branch information
venotar and Omar Buhidma authored Apr 2, 2024
1 parent ccc46c0 commit 998f552
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ provider "kubernetes" {
)
}
```
Although the above can result in authentication errors, over time, as the token recorded in the google_client_cofig data resource is short lived (thus it expires) and it's stored in state. Fortunately, the [kubernetes provider can accept valid credentials from an exec-based plugin](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs#exec-plugins) to fetch a new token before each Terraform operation (so long as you have the [gke-cloud-auth-plugin for kubectl installed](https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke)), like so:

```hcl
# Retrieve an access token as the Terraform runner
data "google_client_config" "provider" {}
data "google_container_cluster" "my_cluster" {
name = "my-cluster"
location = "us-central1"
}
provider "kubernetes" {
host = "https://${data.google_container_cluster.my_cluster.endpoint}"
token = data.google_client_config.provider.access_token
cluster_ca_certificate = base64decode(
data.google_container_cluster.my_cluster.master_auth[0].cluster_ca_certificate,
)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "gke-gcloud-auth-plugin"
}
}
```

Alternatively, you can authenticate as another service account on which your
Terraform user has been granted the `roles/iam.serviceAccountTokenCreator`
Expand Down

0 comments on commit 998f552

Please sign in to comment.