/
project_get-gke-credentials.tf
201 lines (163 loc) · 6.16 KB
/
project_get-gke-credentials.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module "get-gke-credentials" {
source = "./modules/project"
github_organization_name = data.github_organization.organization.orgname
github_organization_id = data.github_organization.organization.id
repo_name = "get-gke-credentials"
repo_description = "A GitHub Action that configure authentication to a GKE cluster."
repo_homepage_url = "https://cloud.google.com/gke"
repo_topics = concat([
"gke",
"google-kubernetes-engine",
"kubernetes",
], local.common_topics)
repo_variables = {
"PUBLIC_CLUSTER_NAME" = google_container_cluster.get-gke-credentials-public.name
"PUBLIC_CLUSTER_LOCATION" = google_container_cluster.get-gke-credentials-public.location
"PRIVATE_CLUSTER_NAME" = google_container_cluster.get-gke-credentials-private.name
"PRIVATE_CLUSTER_LOCATION" = google_container_cluster.get-gke-credentials-private.location
}
depends_on = [
google_project_service.services,
]
}
# Grant the custom service account permissions
resource "google_project_iam_member" "get-gke-credentials" {
for_each = toset([
# For listing clusters
"roles/container.viewer",
# For private clusters: https://cloud.google.com/anthos/multicluster-management/gateway/setup#grant_iam_roles_to_users
"roles/gkehub.gatewayReader",
"roles/gkehub.viewer",
])
project = data.google_project.project.project_id
role = each.value
member = "serviceAccount:${module.get-gke-credentials.service_account_email}"
}
# Public GKE cluster
resource "google_compute_subnetwork" "get-gke-credentials-public" {
name = "get-gke-credentials-public"
region = "us-central1"
network = google_compute_network.network.id
ip_cidr_range = "10.0.0.0/24"
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
private_ip_google_access = true
secondary_ip_range {
range_name = "get-gke-credentials-public-pods"
ip_cidr_range = "192.168.0.0/24"
}
secondary_ip_range {
range_name = "get-gke-credentials-public-services"
ip_cidr_range = "192.168.1.0/24"
}
}
resource "google_container_cluster" "get-gke-credentials-public" {
name = "get-gke-credentials-public"
location = google_compute_subnetwork.get-gke-credentials-public.region
enable_autopilot = true
enable_l4_ilb_subsetting = true
deletion_protection = false
initial_node_count = 1
network = google_compute_network.network.id
subnetwork = google_compute_subnetwork.get-gke-credentials-public.id
ip_allocation_policy {
stack_type = "IPV4_IPV6"
cluster_secondary_range_name = google_compute_subnetwork.get-gke-credentials-public.secondary_ip_range[0].range_name
services_secondary_range_name = google_compute_subnetwork.get-gke-credentials-public.secondary_ip_range[1].range_name
}
release_channel {
channel = "REGULAR"
}
timeouts {
create = "15m"
update = "15m"
}
depends_on = [
google_project_service.services["container.googleapis.com"],
]
}
# Private GKE cluster
resource "google_compute_subnetwork" "get-gke-credentials-private" {
name = "get-gke-credentials-private"
region = "us-central1"
network = google_compute_network.network.id
ip_cidr_range = "10.0.1.0/24"
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
private_ip_google_access = true
secondary_ip_range {
range_name = "get-gke-credentials-private-pods"
ip_cidr_range = "192.168.10.0/24"
}
secondary_ip_range {
range_name = "get-gke-credentials-private-services"
ip_cidr_range = "192.168.11.0/24"
}
}
resource "google_container_cluster" "get-gke-credentials-private" {
name = "get-gke-credentials-private"
location = google_compute_subnetwork.get-gke-credentials-private.region
enable_autopilot = true
enable_l4_ilb_subsetting = true
deletion_protection = false
initial_node_count = 1
network = google_compute_network.network.id
subnetwork = google_compute_subnetwork.get-gke-credentials-private.id
private_cluster_config {
enable_private_endpoint = true
enable_private_nodes = true
master_ipv4_cidr_block = "172.16.0.16/28"
}
master_authorized_networks_config {
cidr_blocks {
display_name = "Access within subnet."
cidr_block = google_compute_subnetwork.get-gke-credentials-private.ip_cidr_range
}
}
ip_allocation_policy {
stack_type = "IPV4_IPV6"
cluster_secondary_range_name = google_compute_subnetwork.get-gke-credentials-private.secondary_ip_range[0].range_name
services_secondary_range_name = google_compute_subnetwork.get-gke-credentials-private.secondary_ip_range[1].range_name
}
release_channel {
channel = "REGULAR"
}
timeouts {
create = "15m"
update = "15m"
}
depends_on = [
google_project_service.services["container.googleapis.com"],
]
}
resource "google_gke_hub_membership" "get-get-credentials-private" {
membership_id = "get-gke-credentials-private"
endpoint {
gke_cluster {
resource_link = google_container_cluster.get-gke-credentials-private.id
}
}
authority {
issuer = "https://container.googleapis.com/v1/${google_container_cluster.get-gke-credentials-private.id}"
}
}
resource "google_gke_hub_membership_iam_member" "get-gke-credentials-private" {
project = data.google_project.project.project_id
location = google_gke_hub_membership.get-get-credentials-private.location
membership_id = google_gke_hub_membership.get-get-credentials-private.membership_id
role = "roles/viewer"
member = "serviceAccount:${module.get-gke-credentials.service_account_email}"
}