-
Notifications
You must be signed in to change notification settings - Fork 0
/
argo-helm.tf
170 lines (141 loc) · 5.31 KB
/
argo-helm.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
locals {
helm_argo_application_enabled = var.enabled && var.argo_enabled && var.argo_helm_enabled
helm_argo_application_wait_enabled = local.helm_argo_application_enabled && length(keys(var.argo_kubernetes_manifest_wait_fields)) > 0
helm_argo_application_values = [
one(data.utils_deep_merge_yaml.argo_helm_values[*].output),
var.argo_helm_values
]
}
data "utils_deep_merge_yaml" "argo_helm_values" {
count = local.helm_argo_application_enabled ? 1 : 0
input = compact([
yamlencode({
"apiVersion" : var.argo_apiversion
}),
yamlencode({
"spec" : local.argo_application_values
}),
yamlencode({
"spec" : var.argo_spec
}),
yamlencode(
local.argo_application_metadata
),
yamlencode({
"spec" : { "source" : { "helm" : { "releaseName" : local.helm_release_name } } }
})
])
}
resource "helm_release" "argo_application" {
count = local.helm_argo_application_enabled ? 1 : 0
chart = "${path.module}/helm/argocd-application"
name = local.helm_release_name
namespace = var.argo_namespace
values = local.helm_argo_application_values
lifecycle {
create_before_destroy = true
}
}
resource "kubernetes_role" "helm_argo_application_wait" {
count = local.helm_argo_application_wait_enabled ? 1 : 0
metadata {
name = "${var.helm_release_name}-argo-application-wait"
namespace = var.argo_namespace
labels = local.argo_application_metadata.labels
annotations = local.argo_application_metadata.annotations
}
rule {
api_groups = ["argoproj.io"]
resources = ["applications"]
verbs = ["get", "list", "watch"]
}
}
resource "kubernetes_role_binding" "helm_argo_application_wait" {
count = local.helm_argo_application_wait_enabled ? 1 : 0
metadata {
name = "${var.helm_release_name}-argo-application-wait"
namespace = var.argo_namespace
labels = local.argo_application_metadata.labels
annotations = local.argo_application_metadata.annotations
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "Role"
name = one(kubernetes_role.helm_argo_application_wait[*].metadata[0].name)
}
subject {
kind = "ServiceAccount"
name = one(kubernetes_service_account.helm_argo_application_wait[*].metadata[0].name)
namespace = one(kubernetes_service_account.helm_argo_application_wait[*].metadata[0].namespace)
}
}
resource "kubernetes_service_account" "helm_argo_application_wait" {
count = local.helm_argo_application_wait_enabled ? 1 : 0
metadata {
name = "${var.helm_release_name}-argo-application-wait"
namespace = var.argo_namespace
labels = local.argo_application_metadata.labels
annotations = local.argo_application_metadata.annotations
}
}
resource "kubernetes_job" "helm_argo_application_wait" {
count = local.helm_argo_application_wait_enabled ? 1 : 0
metadata {
name = "${var.helm_release_name}-argo-application-wait"
namespace = var.argo_namespace
labels = local.argo_application_metadata.labels
annotations = local.argo_application_metadata.annotations
}
spec {
template {
metadata {
name = "${var.helm_release_name}-argo-application-wait"
labels = local.argo_application_metadata.labels
annotations = local.argo_application_metadata.annotations
}
spec {
service_account_name = one(kubernetes_service_account.helm_argo_application_wait[*].metadata[0].name)
dynamic "container" {
for_each = var.argo_kubernetes_manifest_wait_fields
content {
name = "${lower(replace(container.key, ".", "-"))}-${md5(jsonencode(local.helm_argo_application_values))}" # md5 suffix is a workaround for https://github.com/hashicorp/terraform-provider-kubernetes/issues/1325
image = "bitnami/kubectl:latest"
command = ["/bin/bash", "-ecx"]
# Waits for ArgoCD Application to be "Healthy", see https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#wait
# i.e. kubectl wait --for=jsonpath='{.status.sync.status}'=Healthy application.argoproj.io <$addon-name>
args = [
<<-EOT
kubectl wait \
--namespace ${var.argo_namespace} \
--for=jsonpath='{.${container.key}}'=${container.value} \
--timeout=${var.argo_helm_wait_timeout} \
application.argoproj.io ${local.helm_release_name}
EOT
]
}
}
node_selector = var.argo_helm_wait_node_selector
dynamic "toleration" {
for_each = var.argo_helm_wait_tolerations
content {
key = try(toleration.value.key, null)
operator = try(toleration.value.operator, null)
value = try(toleration.value.value, null)
effect = try(toleration.value.effect, null)
}
}
# ArgoCD Application status fields might not be available immediately after creation
restart_policy = "OnFailure"
}
}
backoff_limit = var.argo_helm_wait_backoff_limit
}
wait_for_completion = true
timeouts {
create = var.argo_helm_wait_timeout
update = var.argo_helm_wait_timeout
}
depends_on = [
helm_release.argo_application
]
}