-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
181 lines (170 loc) · 7.52 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
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
locals {
cluster_identifier = var.generator_segment_index_overwrite == null ? ".path.basenameNormalized" : "(index .path.segments ${var.generator_segment_index_overwrite})"
resource_name = "{{ ${local.cluster_identifier} }}${var.application_name_suffix != null ? "-${var.application_name_suffix}" : ""}"
json_encoded_env_based_annotations = jsonencode(var.env_context_annotations)
}
resource "argocd_application_set" "this" {
metadata {
name = "${var.project_name}-${var.name}"
namespace = var.namespace
}
spec {
go_template = true
generator {
// if the git block is not null, then we want to add it to the generator
dynamic "git" {
for_each = var.generator.git != null ? [var.generator.git] : []
content {
repo_url = var.generator.git.repo_url
revision = var.generator.git.revision
dynamic "file" {
for_each = var.generator.git.files != null ? var.generator.git.files : []
content {
path = file.value
}
}
dynamic "directory" {
for_each = var.generator.git.directories != null ? var.generator.git.directories : []
content {
path = directory.value.path
exclude = directory.value.exclude
}
}
}
}
// if the github block is not null, then we want to add it to the generator
dynamic "pull_request" {
for_each = var.generator.pull_request != null ? var.generator.pull_request : []
content {
requeue_after_seconds = pull_request.value.requeue_after_seconds
github {
owner = "urbanmedia"
repo = pull_request.value.repo
labels = pull_request.value.labels
}
}
}
}
template {
metadata {
// application names are in the format: <name>-<cluster>
// e.g. prometheus-staging
name = "${var.name}-${local.resource_name}"
annotations = merge(
var.annotations,
{
"managed-by" = "argo-cd",
"application-set" = var.name
},
{
for k, v in var.env_context_annotations : k => "{{ $applicationName := \"${var.name}\" }}{{ $resourceName := \"${local.resource_name}\" }}{{ $cluster := \"\" }}{{ if eq ${local.cluster_identifier} \"general-purpose\" }}{{ $cluster = \"in-cluster\" }}{{ else }}{{ $cluster = ${local.cluster_identifier} }}{{ end }}${v}"
}
)
labels = merge(
var.labels,
{
"managed-by" = "argo-cd",
"application-set" = var.name
},
)
}
spec {
project = var.project_name
// TODO(@tagesspiegel/platform-engineers): We want to add the sync block here so we can support progressive syncs
// See: https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Progressive-Syncs/
dynamic "source" {
for_each = var.manifest_source.helm != null ? ["abc"] : []
content {
chart = "{{ default \"${var.manifest_source.helm.chart}\" .chart.name }}"
repo_url = "{{ default \"${var.manifest_source.helm.repo_url}\" .chart.repo }}"
target_revision = "{{ default \"${var.manifest_source.helm.target_revision}\" .chart.version }}"
helm {
release_name = "{{ default \"${var.manifest_source.helm.release_name}\" .chart.release_name }}"
values = "{{ .values }}"
// optional properties only if the git generator is not null
value_files = var.generator.git != null ? ["$values/{{ .path.path }}/values/{{ default \"${var.manifest_source.helm.release_name}\" .chart.release_name | replace \"-\" \"_\"}}.yaml"] : []
ignore_missing_value_files = var.generator.git != null ? true : false
}
}
}
dynamic "source" {
for_each = var.manifest_source.directory != null ? ["abc"] : []
content {
repo_url = var.manifest_source.directory.repo.url
target_revision = var.manifest_source.directory.repo.revision
path = var.manifest_source.directory.repo.path
directory {
include = var.manifest_source.directory.glob_path
recurse = true
}
}
}
dynamic "source" {
for_each = var.generator.git != null ? [var.generator.git] : []
content {
repo_url = var.generator.git.repo_url
target_revision = var.generator.git.revision
ref = "values"
}
}
dynamic "ignore_difference" {
// to protect against null values, we need to check if the ignore_difference block is not null
for_each = var.ignore_difference != null ? var.ignore_difference : []
content {
group = ignore_difference.value.group
jq_path_expressions = ignore_difference.value.jq_path_expressions
json_pointers = ignore_difference.value.json_pointers
kind = ignore_difference.value.kind
name = ignore_difference.value.name
namespace = ignore_difference.value.namespace
}
}
destination {
name = "{{ if (eq ${local.cluster_identifier} \"general-purpose\") }}in-cluster{{ else }}{{ ${local.cluster_identifier} }}{{ end }}"
// if the target_namespace_overwrite is not empty, then we want to use it as the namespace
// (e.g. ingress-controllers (assuming "ingress-controllers" is the target_namespace_overwrite))
// otherwise we check if the configuration provided by the developer has a namespace_overwrite
// if the namespace_overwrite is not set, then we use the namespace based on the project name and git folder name
// (e.g. background-staging (assuming "background" is the project and "staging" the folder name))
// otherwise we use the namespace_overwrite provided by the developer
// (e.g. background-staging-v2 (assuming "background" is the project and "staging-v2" the namespace_overwrite))
namespace = var.target_namespace_overwrite != "" ? var.target_namespace_overwrite : "{{ if not .namespace_overwrite }}${var.project_name}-${local.resource_name}{{ else }}{{ .namespace_overwrite }}{{ end }}"
}
sync_policy {
dynamic "automated" {
for_each = var.sync_policy != null ? [var.sync_policy] : []
content {
prune = var.sync_policy.prune
self_heal = var.sync_policy.self_heal
allow_empty = var.sync_policy.allow_empty
}
}
managed_namespace_metadata {
annotations = merge(
var.namespace_annotations,
{
"managed-by" = "argo-cd"
},
)
labels = merge(
var.namespace_labels,
{
"managed-by" = "argo-cd"
},
)
}
# Only available from ArgoCD 1.5.0 onwards
sync_options = var.sync_options
retry {
limit = var.sync_retries.limit
backoff {
duration = var.sync_retries.duration
max_duration = var.sync_retries.max_duration
factor = var.sync_retries.factor
}
}
}
}
}
}
}