Skip to content

Commit 103f432

Browse files
authored
feat(bootstrap): TF changes for GCP CAPI migration (#835)
Signed-off-by: David van der Spek <[email protected]>
1 parent 4baf83d commit 103f432

File tree

15 files changed

+136
-60
lines changed

15 files changed

+136
-60
lines changed

bootstrap/plural/recipes/gcp-cluster-api-simple-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sections:
1616
message: must begin with a lowercase letter, and can only contain lowercase letters, numbers or hyphens after
1717
items:
1818
- type: TERRAFORM
19-
name: gcp-bootstrap-cluster-api
19+
name: gcp-bootstrap
2020
- type: HELM
2121
name: bootstrap
2222
- type: HELM

bootstrap/terraform/gcp-bootstrap-cluster-api/deps.yaml

Lines changed: 0 additions & 11 deletions
This file was deleted.

bootstrap/terraform/gcp-bootstrap-cluster-api/main.tf

Lines changed: 0 additions & 3 deletions
This file was deleted.

bootstrap/terraform/gcp-bootstrap-cluster-api/terraform.tfvars

Lines changed: 0 additions & 1 deletion
This file was deleted.

bootstrap/terraform/gcp-bootstrap-cluster-api/variables.tf

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# GCP Kubernetes bootstrapping
22

3-
Provisions a gke cluster with node pool, along with default networking etc.
3+
Provisions a gke cluster with node pool, along with default networking etc.

bootstrap/terraform/gcp-bootstrap/deps.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ apiVersion: plural.sh/v1alpha1
22
kind: Dependencies
33
metadata:
44
description: Creates a GKE cluster and adds initial configuration
5-
version: 0.2.23
5+
version: 0.2.24
66
spec:
77
dependencies: []
88
providers:
99
- gcp
1010
outputs:
1111
cluster: cluster
1212
vpc_network: vpc_network
13+
capi_sa_workload_identity_email: capi_sa_workload_identity_email
1314
provider_wirings:
1415
cluster: module.gcp-bootstrap.cluster

bootstrap/terraform/gcp-bootstrap/locals.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ locals {
55
services_cidr_name = "${var.gcp_region}-${var.cluster_name}-services"
66
vpc_network_name = var.vpc_network_name != "" ? var.vpc_network_name : "${var.vpc_name_prefix}-network"
77
vpc_subnetwork_name = var.vpc_subnetwork_name != "" ? var.vpc_subnetwork_name : "${var.vpc_name_prefix}-subnetwork"
8-
}
8+
}

bootstrap/terraform/gcp-bootstrap/main.tf

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
resource "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

1516
resource "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

4646
module "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

9091
resource "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+
103126
module "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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** main.tf **/
2+
moved {
3+
from = google_compute_network.vpc_network
4+
to = google_compute_network.vpc_network[0]
5+
}
6+
7+
moved {
8+
from = google_compute_subnetwork.vpc_subnetwork
9+
to = google_compute_subnetwork.vpc_subnetwork[0]
10+
}
11+
12+
moved {
13+
from = module.gke
14+
to = module.gke[0]
15+
}
16+
17+
moved {
18+
from = kubernetes_namespace.bootstrap
19+
to = kubernetes_namespace.bootstrap[0]
20+
}
21+
22+
moved {
23+
from = kubernetes_service_account.certmanager
24+
to = kubernetes_service_account.certmanager[0]
25+
}
26+
27+
/** services.tf **/
28+
moved {
29+
from = google_project_service.gcr
30+
to = google_project_service.gcr[0]
31+
}
32+
33+
moved {
34+
from = google_project_service.container
35+
to = google_project_service.container[0]
36+
}
37+
38+
moved {
39+
from = google_project_service.storage
40+
to = google_project_service.storage[0]
41+
}
42+
43+
moved {
44+
from = google_project_service.dns
45+
to = google_project_service.dns[0]
46+
}
47+
48+
moved {
49+
from = google_project_service.compute
50+
to = google_project_service.compute[0]
51+
}

0 commit comments

Comments
 (0)