Skip to content

Commit 0d4e6f3

Browse files
author
sig-bhutwala
authored
feat: add ability to impersonate for kubectl-wrapper module (#91)
* add ability to impersonate for kubectl-wrapper module * use make generate_docs * make it backwards compatible * fix conditional and ci * flip the ternary * terraform fmt * try with a true flag * shift additional 2 in case of service impersonation
1 parent 866b840 commit 0d4e6f3

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

modules/kubectl-wrapper/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module "kubectl" {
3131
| create\_cmd\_triggers | List of any additional triggers for the create command execution. | map | `<map>` | no |
3232
| enabled | Flag to optionally disable usage of this module. | bool | `"true"` | no |
3333
| gcloud\_sdk\_version | The gcloud sdk version to download. | string | `"281.0.0"` | no |
34+
| 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 |
3435
| internal\_ip | Use internal ip for the cluster endpoint. | bool | `"false"` | no |
3536
| kubectl\_create\_command | The kubectl command to create resources. | string | n/a | yes |
3637
| kubectl\_destroy\_command | The kubectl command to destroy resources. | string | n/a | yes |

modules/kubectl-wrapper/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ module "gcloud_kubectl" {
2929
service_account_key_file = var.service_account_key_file
3030

3131
create_cmd_entrypoint = "${path.module}/scripts/kubectl_wrapper.sh"
32-
create_cmd_body = "${local.base_cmd} ${var.kubectl_create_command}"
32+
create_cmd_body = var.impersonate_service_account == "" ? "${local.base_cmd} ${var.kubectl_create_command}" : "${local.base_cmd} true ${var.impersonate_service_account} ${var.kubectl_create_command}"
3333
create_cmd_triggers = var.create_cmd_triggers
3434
destroy_cmd_entrypoint = "${path.module}/scripts/kubectl_wrapper.sh"
35-
destroy_cmd_body = "${local.base_cmd} ${var.kubectl_destroy_command}"
35+
destroy_cmd_body = var.impersonate_service_account == "" ? "${local.base_cmd} ${var.kubectl_destroy_command}" : "${local.base_cmd} true ${var.impersonate_service_account} ${var.kubectl_destroy_command}"
3636
}

modules/kubectl-wrapper/scripts/kubectl_wrapper.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ LOCATION=$2
2626
PROJECT_ID=$3
2727
INTERNAL=$4
2828
USE_EXISTING_CONTEXT=$5
29+
ENABLE_IMPERSONATE_SERVICE_ACCOUNT=$6
30+
IMPERSONATE_SERVICE_ACCOUNT=$7
2931

3032
shift 5
3133

@@ -50,6 +52,10 @@ else
5052
LOCATION_TYPE=$(grep -o "-" <<< "${LOCATION}" | wc -l)
5153

5254
CMD="gcloud container clusters get-credentials ${CLUSTER_NAME} --project ${PROJECT_ID}"
55+
if [[ "${ENABLE_IMPERSONATE_SERVICE_ACCOUNT}" == true ]]; then
56+
CMD+=" --impersonate-service-account ${IMPERSONATE_SERVICE_ACCOUNT}"
57+
shift 2
58+
fi
5359

5460
if [[ $LOCATION_TYPE -eq 2 ]] ;then
5561
CMD+=" --zone ${LOCATION}"

modules/kubectl-wrapper/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ variable "service_account_key_file" {
9898
description = "Path to service account key file to auth as for running `gcloud container clusters get-credentials`."
9999
default = ""
100100
}
101+
102+
variable "impersonate_service_account" {
103+
type = string
104+
description = "An optional service account to impersonate for gcloud commands. If this service account is not specified, the module will use Application Default Credentials."
105+
default = ""
106+
}

0 commit comments

Comments
 (0)