11resource "google_compute_network" "vpc_network" {
2+ count = var. cluster_api ? 0 : 1
23 name = local. vpc_network_name
34 auto_create_subnetworks = " false"
45
@@ -13,11 +14,10 @@ resource "google_compute_network" "vpc_network" {
1314}
1415
1516resource "google_compute_subnetwork" "vpc_subnetwork" {
16- name = local . vpc_subnetwork_name
17-
17+ count = var . cluster_api ? 0 : 1
18+ name = local . vpc_subnetwork_name
1819 ip_cidr_range = var. vpc_subnetwork_cidr_range
19-
20- network = google_compute_network. vpc_network . name
20+ network = one (google_compute_network. vpc_network [* ]. name )
2121
2222 secondary_ip_range {
2323 range_name = local. pods_cidr_name
@@ -44,14 +44,15 @@ resource "google_compute_subnetwork" "vpc_subnetwork" {
4444}
4545
4646module "gke" {
47+ count = var. cluster_api ? 0 : 1
4748 source = " github.com/pluralsh/terraform-google-kubernetes-engine?ref=filestore-csi-driver"
4849 project_id = var. gcp_project_id
4950 name = var. cluster_name
5051 region = local. gcp_region
51- network = google_compute_network. vpc_network . name
52- subnetwork = google_compute_subnetwork. vpc_subnetwork . name
53- ip_range_pods = google_compute_subnetwork. vpc_subnetwork . secondary_ip_range [0 ]. range_name
54- ip_range_services = google_compute_subnetwork. vpc_subnetwork . secondary_ip_range [1 ]. range_name
52+ network = one ( google_compute_network. vpc_network [ * ] . name )
53+ subnetwork = one ( google_compute_subnetwork. vpc_subnetwork [ * ] . name )
54+ ip_range_pods = one ( google_compute_subnetwork. vpc_subnetwork [ * ] . secondary_ip_range [0 ]. range_name )
55+ ip_range_services = one ( google_compute_subnetwork. vpc_subnetwork [ * ] . secondary_ip_range [1 ]. range_name )
5556 horizontal_pod_autoscaling = true
5657 http_load_balancing = true
5758 remove_default_node_pool = true
@@ -69,7 +70,7 @@ module "gke" {
6970 },
7071 var. cluster_labels ,
7172 )
72- grant_registry_access = var. grant_registry_access
73+ grant_registry_access = var. grant_registry_access
7374
7475 node_pools = var. node_pools
7576
@@ -88,18 +89,40 @@ module "gke" {
8889}
8990
9091resource "kubernetes_namespace" "bootstrap" {
92+ count = var. cluster_api ? 0 : 1
93+
9194 metadata {
9295 name = var. namespace
9396
9497 labels = {
9598 " app.kubernetes.io/managed-by" = " plural"
96- " app.plural.sh/name" = " bootstrap"
99+ " app.plural.sh/name" = " bootstrap"
97100 }
98101 }
99102
100103 depends_on = [module . gke . endpoint ]
101104}
102105
106+ resource "kubernetes_service_account" "certmanager" {
107+ count = var. cluster_api ? 0 : 1
108+ metadata {
109+ name = " certmanager"
110+ namespace = var. namespace
111+
112+ annotations = {
113+ " iam.gke.io/gcp-service-account" = module.certmanager- workload- identity.gcp_service_account_email
114+ }
115+ }
116+
117+ depends_on = [kubernetes_namespace . bootstrap ]
118+ }
119+
120+ data "google_container_cluster" "cluster" {
121+ count = var. cluster_api ? 1 : 0
122+ name = var. cluster_name
123+ location = var. gcp_region
124+ }
125+
103126module "externaldns-workload-identity" {
104127 source = " terraform-google-modules/kubernetes-engine/google//modules/workload-identity"
105128 name = " ${ var . cluster_name } -externaldns"
@@ -139,15 +162,20 @@ module "certmanager-workload-identity" {
139162 depends_on = [google_project_service . iam ]
140163}
141164
142- resource "kubernetes_service_account" "certmanager" {
143- metadata {
144- name = " certmanager"
145- namespace = var. namespace
146-
147- annotations = {
148- " iam.gke.io/gcp-service-account" = module.certmanager- workload- identity.gcp_service_account_email
149- }
150- }
165+ module "capi-workload-identity" {
166+ source = " terraform-google-modules/kubernetes-engine/google//modules/workload-identity"
167+ name = " ${ var . cluster_name } -cluster-api-provider-gcp"
168+ namespace = var. namespace
169+ project_id = var. gcp_project_id
170+ use_existing_k8s_sa = true
171+ annotate_k8s_sa = false
172+ k8s_sa_name = " bootstrap-cluster-api-provider-gcp"
173+ roles = [
174+ " roles/iam.serviceAccountUser" ,
175+ " roles/iam.workloadIdentityUser" ,
176+ " roles/compute.admin" ,
177+ " roles/container.admin" ,
178+ ]
151179
152- depends_on = [kubernetes_namespace . bootstrap ]
153- }
180+ module_depends_on = [google_project_service . iam ]
181+ }
0 commit comments