Skip to content

Commit a9ebe65

Browse files
committed
feat(acm): remove direct kubectl commands
kubectl-wrapper currently breaks if one has to access the api using a proxy (IAP).
1 parent dbb57a2 commit a9ebe65

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

modules/acm/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ data "google_client_config" "default" {}
9595
| policy\_bundles | A list of Policy Controller policy bundles git urls (example: https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/policy-essentials-v2022) to install on the cluster. | `list(string)` | `[]` | no |
9696
| policy\_dir | Subfolder containing configs in ACM Git repo. If un-set, uses Config Management default. | `string` | `""` | no |
9797
| project\_id | GCP project\_id used to reach cluster. | `string` | n/a | yes |
98+
| restart\_gatekeeper\_controller\_manager | Restart the gatekeeper controller manager after setting up workload id (needs to be done manually if a proxy to gke api is required) | `bool` | default |
9899
| secret\_type | git authentication secret type, is passed through to ConfigManagement spec.git.secretType. Overriden to value 'ssh' if `create_ssh_key` is true | `string` | `"ssh"` | no |
99100
| source\_format | Configures a non-hierarchical repo if set to 'unstructured'. Uses [ACM defaults](https://cloud.google.com/anthos-config-management/docs/how-to/installing#configuring-config-management-operator) when unset. | `string` | `""` | no |
100101
| ssh\_auth\_key | Key for Git authentication. Overrides 'create\_ssh\_key' variable. Can be set using 'file(path/to/file)'-function. | `string` | `null` | no |

modules/acm/creds.tf

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,57 @@ resource "time_sleep" "wait_acm" {
3939
}
4040

4141
resource "google_service_account_iam_binding" "ksa_iam" {
42-
count = length(local.iam_ksa_binding_members) > 0 ? 1 : 0
42+
count = length(local.iam_ksa_binding_members) > 0 ? 1 : 0
43+
depends_on = [google_gke_hub_feature_membership.main]
44+
4345
service_account_id = google_service_account.acm_metrics_writer_sa[0].name
4446
role = "roles/iam.workloadIdentityUser"
4547

4648
members = [
4749
for ksa in local.iam_ksa_binding_members : "serviceAccount:${var.project_id}.svc.id.goog[${ksa}]"
4850
]
49-
50-
depends_on = [google_gke_hub_feature_membership.main]
5151
}
5252

53-
module "annotate-sa-config-management-monitoring" {
54-
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
55-
version = "~> 3.1"
53+
resource "kubernetes_annotations" "annotate-sa-config-management-monitoring" {
54+
count = var.enable_config_sync && var.create_metrics_gcp_sa ? 1 : 0
5655

57-
count = var.enable_config_sync && var.create_metrics_gcp_sa ? 1 : 0
58-
skip_download = true
59-
cluster_name = var.cluster_name
60-
cluster_location = var.location
61-
project_id = var.project_id
56+
api_version = "v1"
57+
kind = "ServiceAccount"
6258

63-
kubectl_create_command = "kubectl annotate --overwrite sa -n config-management-monitoring default iam.gke.io/gcp-service-account=${google_service_account.acm_metrics_writer_sa[0].email}"
64-
kubectl_destroy_command = "kubectl annotate sa -n config-management-monitoring default iam.gke.io/gcp-service-account-"
59+
metadata {
60+
name = "default"
61+
namespace = "config-management-monitoring"
62+
}
6563

66-
module_depends_on = time_sleep.wait_acm
64+
annotations = {
65+
"iam.gke.io/gcp-service-account" : google_service_account.acm_metrics_writer_sa[0].email
66+
}
67+
68+
depends_on = [time_sleep.wait_acm]
6769
}
6870

69-
module "annotate-sa-gatekeeper-system" {
70-
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
71-
version = "~> 3.1"
71+
resource "kubernetes_annotations" "annotate-sa-gatekeeper-system" {
72+
count = var.enable_policy_controller && var.create_metrics_gcp_sa ? 1 : 0
73+
depends_on = [time_sleep.wait_acm]
7274

73-
count = var.enable_policy_controller && var.create_metrics_gcp_sa ? 1 : 0
74-
skip_download = true
75-
cluster_name = var.cluster_name
76-
cluster_location = var.location
77-
project_id = var.project_id
75+
api_version = "v1"
76+
kind = "ServiceAccount"
7877

79-
kubectl_create_command = "kubectl annotate --overwrite sa -n gatekeeper-system gatekeeper-admin iam.gke.io/gcp-service-account=${google_service_account.acm_metrics_writer_sa[0].email}"
80-
kubectl_destroy_command = "kubectl annotate sa -n gatekeeper-system gatekeeper-admin iam.gke.io/gcp-service-account-"
78+
metadata {
79+
name = "gatekeeper-admin"
80+
namespace = "gatekeeper-system"
81+
}
8182

82-
module_depends_on = time_sleep.wait_acm
83+
annotations = {
84+
"iam.gke.io/gcp-service-account" : google_service_account.acm_metrics_writer_sa[0].email
85+
}
8386
}
8487

8588
module "annotate-sa-gatekeeper-system-restart" {
8689
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
8790
version = "~> 3.1"
8891

89-
count = var.enable_policy_controller && var.create_metrics_gcp_sa ? 1 : 0
92+
count = var.enable_policy_controller && var.create_metrics_gcp_sa && var.restart_gatekeeper_controller_manager ? 1 : 0
9093
skip_download = true
9194
cluster_name = var.cluster_name
9295
cluster_location = var.location
@@ -95,7 +98,7 @@ module "annotate-sa-gatekeeper-system-restart" {
9598
kubectl_create_command = "kubectl rollout restart deployment gatekeeper-controller-manager -n gatekeeper-system"
9699
kubectl_destroy_command = ""
97100

98-
module_depends_on = module.annotate-sa-gatekeeper-system
101+
module_depends_on = resource.kubernetes_annotations.annotate-sa-gatekeeper-system
99102
}
100103

101104
resource "google_service_account" "acm_metrics_writer_sa" {

modules/acm/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,9 @@ variable "metrics_gcp_sa_name" {
176176
type = string
177177
default = "acm-metrics-writer"
178178
}
179+
180+
variable "restart_gatekeeper_controller_manager" {
181+
description = "Restart the gatekeeper controller manager after setting up workload id (needs to be done manually if a proxy to gke api is required)"
182+
type = bool
183+
default = true
184+
}

0 commit comments

Comments
 (0)