-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enables an existing GSA to be used when setting up Workload Ide…
…ntity (#955)
- Loading branch information
1 parent
e53a949
commit 712fc54
Showing
5 changed files
with
97 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ | |
|
||
This module creates: | ||
|
||
* GCP Service Account | ||
* IAM Service Account binding to `roles/iam.workloadIdentityUser` | ||
* Optionally, a Google Service Account | ||
* Optionally, a Kubernetes Service Account | ||
|
||
## Usage | ||
|
||
The `terraform-google-workload-identity` can create a kubernetes service account for you, or use an existing kubernetes service account. | ||
The `terraform-google-workload-identity` can create service accounts for you, | ||
or you can use existing accounts; this applies for both the Google and | ||
Kubernetes accounts. | ||
|
||
### Creating a Workload Identity | ||
|
||
|
@@ -20,17 +22,17 @@ module "my-app-workload-identity" { | |
name = "my-application-name" | ||
namespace = "default" | ||
project_id = "my-gcp-project-name" | ||
roles = ["roles/storage.Admin", "roles/compute.Admin"] | ||
roles = ["roles/storage.Admin", "roles/compute.Admin"] | ||
} | ||
``` | ||
|
||
This will create: | ||
|
||
* GCP Service Account named: `[email protected]` | ||
* Google Service Account named: `[email protected]` | ||
* Kubernetes Service Account named: `my-application-name` in the `default` namespace | ||
* IAM Binding (`roles/iam.workloadIdentityUser`) between the service accounts | ||
|
||
Usage from a kubernetes deployment: | ||
Usage from a Kubernetes deployment: | ||
|
||
```yaml | ||
metadata: | ||
|
@@ -43,27 +45,47 @@ spec: | |
serviceAccountName: my-application-name | ||
``` | ||
### Using an existing Google Service Account | ||
An existing Google service account can optionally be used. | ||
```hcl | ||
resource "google_service_account" "preexisting" { | ||
account_id = "preexisting-sa" | ||
} | ||
|
||
module "my-app-workload-identity" { | ||
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity" | ||
use_existing_gcp_sa = true | ||
name = google_service_account.preexisting.account_id | ||
project_id = var.project_id | ||
} | ||
``` | ||
|
||
### Using an existing Kubernetes Service Account | ||
|
||
An existing kubernetes service account can optionally be used. When using an existing k8s servicea account the annotation `"iam.gke.io/gcp-service-account"` must be set. | ||
An existing Kubernetes service account can optionally be used. | ||
|
||
```hcl | ||
resource "kubernetes_service_account" "preexisting" { | ||
metadata { | ||
name = "preexisting-sa" | ||
name = "preexisting-sa" | ||
namespace = "prod" | ||
} | ||
} | ||
module "my-app-workload-identity" { | ||
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity" | ||
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity" | ||
use_existing_k8s_sa = true | ||
name = "preexisting-sa" | ||
namespace = "prod" | ||
name = kubernetes_service_account.preexisting.metadata[0].name | ||
namespace = kubernetes_service_account.preexisting.metadata[0].namespace | ||
project_id = var.project_id | ||
} | ||
``` | ||
|
||
If annotation is disabled (via `annotate_k8s_sa = false`), the existing Kubernetes service account must | ||
already bear the `"iam.gke.io/gcp-service-account"` annotation. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
|
@@ -72,13 +94,15 @@ module "my-app-workload-identity" { | |
| annotate\_k8s\_sa | Annotate the kubernetes service account with 'iam.gke.io/gcp-service-account' annotation. Valid in cases when an existing SA is used. | `bool` | `true` | no | | ||
| automount\_service\_account\_token | Enable automatic mounting of the service account token | `bool` | `false` | no | | ||
| cluster\_name | Cluster name. Required if using existing KSA. | `string` | `""` | no | | ||
| gcp\_sa\_name | Name for the Google service account; overrides `var.name`. | `string` | `null` | no | | ||
| impersonate\_service\_account | An optional service account to impersonate for gcloud commands. If this service account is not specified, the module will use Application Default Credentials. | `string` | `""` | no | | ||
| k8s\_sa\_name | Name for the existing Kubernetes service account | `string` | `null` | no | | ||
| k8s\_sa\_name | Name for the Kubernetes service account; overrides `var.name`. | `string` | `null` | no | | ||
| location | Cluster location (region if regional cluster, zone if zonal cluster). Required if using existing KSA. | `string` | `""` | no | | ||
| name | Name for both service accounts. The GCP SA will be truncated to the first 30 chars if necessary. | `string` | n/a | yes | | ||
| namespace | Namespace for k8s service account | `string` | `"default"` | no | | ||
| namespace | Namespace for the Kubernetes service account | `string` | `"default"` | no | | ||
| project\_id | GCP project ID | `string` | n/a | yes | | ||
| roles | (optional) A list of roles to be added to the created Service account | `list(string)` | `[]` | no | | ||
| roles | A list of roles to be added to the created service account | `list(string)` | `[]` | no | | ||
| use\_existing\_gcp\_sa | Use an existing Google service account instead of creating one | `bool` | `false` | no | | ||
| use\_existing\_k8s\_sa | Use an existing kubernetes service account instead of creating one | `bool` | `false` | no | | ||
|
||
## Outputs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters