-
Notifications
You must be signed in to change notification settings - Fork 0
/
sa.tf
78 lines (68 loc) · 3.29 KB
/
sa.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
/**
* Copyright 2018 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.
*/
// This file was automatically generated from a template in ./autogen/main
locals {
service_account_list = compact(
concat(
google_service_account.cluster_service_account.*.email,
["dummy"],
),
)
// if user set var.service_accont it will be used even if var.create_service_account==true, so service account will be created but not used
service_account = (var.service_account == "" || var.service_account == "create") && var.create_service_account ? local.service_account_list[0] : var.service_account
}
resource "random_string" "cluster_service_account_suffix" {
upper = false
lower = true
special = false
length = 4
}
resource "google_service_account" "cluster_service_account" {
count = var.create_service_account ? 1 : 0
project = var.project_id
account_id = "tf-gke-${substr(var.name, 0, min(15, length(var.name)))}-${random_string.cluster_service_account_suffix.result}"
display_name = "Terraform-managed service account for cluster ${var.name}"
}
resource "google_project_iam_member" "cluster_service_account-log_writer" {
count = var.create_service_account ? 1 : 0
project = google_service_account.cluster_service_account[0].project
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
}
resource "google_project_iam_member" "cluster_service_account-metric_writer" {
count = var.create_service_account ? 1 : 0
project = google_project_iam_member.cluster_service_account-log_writer[0].project
role = "roles/monitoring.metricWriter"
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
}
resource "google_project_iam_member" "cluster_service_account-monitoring_viewer" {
count = var.create_service_account ? 1 : 0
project = google_project_iam_member.cluster_service_account-metric_writer[0].project
role = "roles/monitoring.viewer"
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
}
resource "google_project_iam_member" "cluster_service_account-resourceMetadata-writer" {
count = var.create_service_account ? 1 : 0
project = google_project_iam_member.cluster_service_account-monitoring_viewer[0].project
role = "roles/stackdriver.resourceMetadata.writer"
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
}
resource "google_project_iam_member" "cluster_service_account-gcr" {
count = var.create_service_account && var.grant_registry_access ? 1 : 0
project = var.registry_project_id == "" ? var.project_id : var.registry_project_id
role = "roles/storage.objectViewer"
member = "serviceAccount:${google_service_account.cluster_service_account[0].email}"
}