-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
144 lines (129 loc) · 6.33 KB
/
main.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
/*
* Database, user and bucket list for Drupal projects. If not specified, the
* name of the database, user and bucket will be generated using the project
* name.
* Specifying a database name, user name or bucket name is suggested only when
* you want to import in Terraform or move in the module existing resources.
*/
locals {
drupal_database_and_user_list = [
for p in var.drupal_projects_list : {
namespace = p.kubernetes_namespace == null ? "${p.project_name}-${p.gitlab_project_id}-${p.release_branch_name}" : p.kubernetes_namespace
release_branch_name = p.release_branch_name
database = p.database_name != null ? p.database_name : "${replace(p.project_name, "-", "_")}_${p.gitlab_project_id}_${p.release_branch_name}_dp"
user = p.database_user_name != null ? p.database_user_name : "${replace(p.project_name, "-", "_")}_${p.gitlab_project_id}_${p.release_branch_name}_dp_u"
host = p.database_host != null ? p.database_host : null
project_id = p.gitlab_project_id
helm_release_name = p.helm_release_name
port = p.database_port
}
]
drupal_buckets_list = [
for p in var.drupal_projects_list : {
name = p.bucket_name != null ? p.bucket_name : "${replace(p.project_name, "_", "-")}-${p.gitlab_project_id}-${p.release_branch_name}-drupal"
force_destroy = p.bucket_force_destroy
append_random_suffix = p.bucket_append_random_suffix
location = p.bucket_location != null ? p.bucket_location : var.region
storage_class = p.bucket_storage_class
enable_versioning = p.bucket_enable_versioning
enable_disaster_recovery = p.bucket_enable_disaster_recovery
set_all_users_as_viewer = p.bucket_set_all_users_as_viewer
labels = p.bucket_labels
tag_list = p.bucket_tag_list
bucket_obj_adm = p.bucket_obj_adm
bucket_obj_vwr = p.bucket_obj_vwr
namespace = p.kubernetes_namespace == null ? "${p.project_name}-${p.gitlab_project_id}-${p.release_branch_name}" : p.kubernetes_namespace
release_branch_name = p.release_branch_name
host = p.bucket_host
project_id = p.gitlab_project_id
helm_release_name = p.helm_release_name
legacy_public_files_path = p.bucket_legacy_public_files_path
soft_delete_retention_seconds = p.bucket_soft_delete_retention_seconds
}
]
namespace_list = [
for p in var.drupal_projects_list : {
namespace = p.kubernetes_namespace == null ? "${p.project_name}-${p.gitlab_project_id}-${p.release_branch_name}" : p.kubernetes_namespace
labels = merge(
p.kubernetes_namespace_labels,
var.default_k8s_labels
)
project_name = p.project_name
network_policy = p.network_policy
}
]
distinct_namespaces = distinct([for n in local.namespace_list : n.namespace])
# NetworkPolicy per namespace. The variable validation guarantees that there is only one NetworkPolicy type per namespace.
network_policy_per_namespace = {
for i in local.distinct_namespaces : i => [
for p in var.drupal_projects_list : p.network_policy if(p.kubernetes_namespace == null ? "${p.project_name}-${p.gitlab_project_id}-${p.release_branch_name}" : p.kubernetes_namespace) == i
][0]
}
}
# Add new databases and users to the CloudSQL master instance.
module "drupal_databases_and_users" {
count = trimspace(var.cloudsql_instance_name) != "" && trimspace(var.cloudsql_privileged_user_name) != "" && trimspace(var.cloudsql_privileged_user_password) != "" && var.create_databases_and_users == true ? 1 : 0
source = "github.com/sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper?ref=0.3.2"
project_id = var.project_id
region = var.region
cloudsql_instance_name = var.cloudsql_instance_name
cloudsql_privileged_user_name = var.cloudsql_privileged_user_name
cloudsql_privileged_user_password = var.cloudsql_privileged_user_password
database_and_user_list = local.drupal_database_and_user_list
}
# ----------------------
# Drupal buckets
# ----------------------
# Add Drupal buckets and Hmac-key credentials with versioning and disaster
# recovery enabled by default.
module "drupal_buckets" {
count = var.create_buckets == true ? 1 : 0
source = "github.com/sparkfabrik/terraform-google-gcp-application-bucket-creation-helper?ref=0.10.0"
project_id = var.project_id
buckets_list = local.drupal_buckets_list
logging_bucket_name = var.logging_bucket_name
disaster_recovery_bucket_location = var.bucket_disaster_recovery_location
global_tags = var.global_tags
}
resource "kubernetes_namespace" "namespace" {
for_each = var.use_existing_kubernetes_namespaces ? {} : {
for p in tolist(toset(local.namespace_list)) : p.namespace => p
}
metadata {
name = each.value.namespace
labels = each.value.labels
}
}
data "kubernetes_namespace" "namespace" {
for_each = var.use_existing_kubernetes_namespaces ? {
for p in tolist(toset(local.namespace_list)) : p.namespace => p
} : {}
metadata {
name = each.value.namespace
}
}
resource "kubernetes_network_policy_v1" "this" {
for_each = {
for namespace, network_policy in local.network_policy_per_namespace : namespace => network_policy if network_policy != ""
}
metadata {
name = "network-policy-${each.value}"
namespace = var.use_existing_kubernetes_namespaces ? data.kubernetes_namespace.namespace[each.key].metadata[0].name : resource.kubernetes_namespace.namespace[each.key].metadata[0].name
}
spec {
pod_selector {}
policy_types = ["Ingress"]
ingress {
dynamic "from" {
for_each = each.value == "isolated" ? { "isolated" : true } : {}
content {
pod_selector {}
}
}
dynamic "from" {
for_each = each.value == "restricted" ? { "restricted" : true } : {}
content {}
}
}
}
}