This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
293 lines (268 loc) · 8.55 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
locals {
chart_name = "spinnaker"
chart_version = var.chart_version
release_name = "spinnaker"
namespace = var.namespace
repository = "https://kubernetes-charts.storage.googleapis.com"
bucket_prefix = "spinnaker"
bucket_name = module.s3_bucket.this_s3_bucket_id
role_name = local.bucket_name
provider_url = replace(var.oidc_provider_issuer_url, "https://", "")
values = {
halyard = {
image = {
tag = "stable"
}
spinnakerVersion = var.spinnaker_version
additionalScripts = {
create = true
data = {
"enable_prometheus.sh" = <<EOF
$HAL_COMMAND config metric-stores prometheus enable
$HAL_COMMAND config security ui edit --override-base-url https://${var.ingress_deck_hostname}
$HAL_COMMAND config security api edit --override-base-url https://${var.ingress_gate_hostname}
$HAL_COMMAND config security authn oauth2 edit --provider github \
--client-id ${var.oauth_github_client_id} \
--client-secret ${var.oauth_github_client_secret} \
--pre-established-redirect-uri https://${var.ingress_gate_hostname}/login
$HAL_COMMAND config security authn oauth2 enable
%{for account in var.accounts.kubernetes}
if $HAL_COMMAND config provider kubernetes account get ${account.context}; then
PROVIDER_COMMAND='edit'
else
PROVIDER_COMMAND='add'
fi
$HAL_COMMAND config provider kubernetes account $PROVIDER_COMMAND ${account.context} \
--context ${account.context} \
--kubeconfig-file /opt/halyard/additionalSecrets/accounts.kubeconfig \
--only-spinnaker-managed true \
--namespaces="${join(",", account.namespaces)}" \
--all-kinds \
--provider-version v2
$HAL_COMMAND config provider kubernetes account edit ${account.context} --remove-omit-namespace kube-system
$HAL_COMMAND config provider kubernetes account edit ${account.context} --remove-omit-namespace kube-public
%{endfor}
$HAL_COMMAND config artifact helm enable
%{for account in var.accounts.helm}
if $HAL_COMMAND config artifact helm account get ${account.name}; then
PROVIDER_COMMAND='edit'
else
PROVIDER_COMMAND='add'
fi
$HAL_COMMAND config artifact helm account $PROVIDER_COMMAND ${account.name} \
--repository ${account.url} \
--username-password-file /opt/halyard/additionalSecrets/helm-${account.name}.txt
%{endfor}
$HAL_COMMAND config provider docker-registry account delete dockerhub
$HAL_COMMAND config ci wercker disable
$HAL_COMMAND config repository artifactory disable
EOF
}
}
additionalServiceSettings = {
"front50.yml" = {
kubernetes = {
serviceAccountName = kubernetes_service_account.spinnaker.metadata[0].name
securityContext = {
fsGroup = 100
}
}
}
"deck.yml" = {
kubernetes = {
serviceType = "ClusterIP"
}
}
"gate.yml" = {
kubernetes = {
serviceType = "ClusterIP"
}
}
}
additionalSecrets = {
create = true
data = zipmap(
concat([for i, v in var.accounts.helm : "helm-${v.name}.txt"], ["accounts.kubeconfig"]),
concat([for i, v in var.accounts.helm : base64encode("${v.username}:${trimspace(v.password)}")], [base64encode(module.kubeconfig.stdout)])
)
}
}
spinnakerFeatureFlags = [
"managed-pipeline-templates-v2-ui",
"chaos"
]
serviceAccount = {
serviceAccountAnnotations = {
"eks.amazonaws.com/role-arn" = module.iam.this_iam_role_arn
}
}
kubeConfig = {
enabled = false
/* secretName = kubernetes_secret.kubeconfig.metadata[0].name
secretKey = "kubeconfig"
contexts = [
var.deployment_context
]
deploymentContext = var.deployment_context
*/
onlySpinnakerManaged = {
enabled = true
}
}
ingress = {
enabled = var.ingress_enabled
host = var.ingress_deck_hostname
annotations = {
"kubernetes.io/ingress.class" : var.ingress_class
"cert-manager.io/cluster-issuer" : var.ingress_cluster_issuer
}
tls = [
{
secretName = "spinnaker-deck-tls"
hosts = [var.ingress_deck_hostname]
}
]
}
ingressGate = {
enabled = var.ingress_enabled
host = var.ingress_gate_hostname
annotations = {
"kubernetes.io/ingress.class" : var.ingress_class
"cert-manager.io/cluster-issuer" : var.ingress_cluster_issuer
}
tls = [
{
secretName = "spinnaker-gate-tls"
hosts = [var.ingress_gate_hostname]
}
]
}
minio = {
enabled = false
}
s3 = {
enabled = true
bucket = local.bucket_name
region = data.aws_region.spinnaker.name
}
}
}
data aws_region "spinnaker" {}
data aws_caller_identity "spinnaker" {}
module "iam" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
create_role = true
role_name = "${local.release_name}-irsa-${local.bucket_name}-halyard"
provider_url = local.provider_url
oidc_fully_qualified_subjects = [
"system:serviceaccount:${local.namespace}:${local.release_name}-spinnaker-halyard",
"system:serviceaccount:${local.namespace}:spinnaker"
]
tags = var.tags
}
data "aws_iam_policy_document" "spinnaker" {
statement {
actions = [
"s3:*"
]
resources = [module.s3_bucket.this_s3_bucket_arn, "${module.s3_bucket.this_s3_bucket_arn}/*"]
}
}
resource "aws_iam_role_policy" "halyard" {
name = local.bucket_name
role = module.iam.this_iam_role_name
policy = data.aws_iam_policy_document.spinnaker.json
}
resource "kubernetes_service_account" "spinnaker" {
metadata {
name = "spinnaker"
namespace = kubernetes_namespace.spinnaker.metadata[0].name
annotations = {
"eks.amazonaws.com/role-arn" = module.iam.this_iam_role_arn
}
}
}
resource "local_file" "kubeconfig" {
count = length(var.accounts.kubernetes)
filename = "kubeconfig-${count.index}"
sensitive_content = var.accounts.kubernetes[count.index].kubeconfig
}
resource "kubernetes_namespace" "spinnaker" {
metadata {
name = var.namespace
}
}
module "kubeconfig" {
source = "matti/resource/shell"
environment = {
KUBECONFIG = join(":", [for i, file in local_file.kubeconfig : file.filename])
}
command = "kubectl config view --flatten #${timestamp()}"
}
resource "kubernetes_secret" "kubeconfig" {
depends_on = [module.kubeconfig]
metadata {
name = "kubernetes-accounts"
namespace = var.namespace
}
data = {
kubeconfig = module.kubeconfig.stdout
timestamp = timestamp()
}
}
output "kubeconfig" {
value = {
stdout = module.kubeconfig.stdout
kubeconfig = join(";", [for i, file in local_file.kubeconfig : file.filename])
}
}
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket_prefix = local.bucket_prefix
acl = "private"
force_destroy = true
versioning = {
enabled = true
}
tags = var.tags
}
resource "helm_release" "spinnaker" {
name = local.release_name
chart = local.chart_name
version = local.chart_version
repository = local.repository
namespace = local.namespace
create_namespace = false
wait = false
values = [yamlencode(local.values)]
}
resource "kubernetes_cluster_role" "spinnaker" {
metadata {
name = "spinnaker"
}
rule {
api_groups = [""]
verbs = ["get", "list"]
resources = ["namespaces"]
}
}
resource "kubernetes_cluster_role_binding" "spinnaker" {
metadata {
name = "spinnaker"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "spinnaker"
}
subject {
namespace = var.deployment_serviceaccount_namespace
kind = "ServiceAccount"
name = var.deployment_serviceaccount_name
}
}
module "spinnaker_namespace" {
source = "./modules/namespace"
namespace = kubernetes_namespace.spinnaker.metadata[0].name
create_namespace = false
}