generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
285 lines (234 loc) · 9.54 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
locals {
# sleep times definition
sleep_time_catalog_create = "60s"
sleep_time_operator_create = "120s"
sleep_time_sampleapp_create = "30s" # time to wait for the sampleapp route to be defined
# helm chart names
ibm_operator_catalog_chart = "ibm-operator-catalog"
websphere_liberty_operator_chart = "websphere-liberty-operator"
websphere_liberty_operator_group_chart = "websphere-liberty-operator-group"
websphere_liberty_operator_sampleapp_chart = "websphere-liberty-operator-sampleapp"
# validation of ws_liberty_operator_target_namespace - if null the value of ws_liberty_operator_namespace must be equal to "openshift-operators" https://www.ibm.com/docs/en/was-liberty/core?topic=operator-installing-red-hat-openshift-cli#in-t-cli__install-op-cli__title__1
default_liberty_operator_namespace = "openshift-operators"
operator_target_namespace_cnd = var.ws_liberty_operator_target_namespace == null && var.ws_liberty_operator_namespace != local.default_liberty_operator_namespace
operator_target_namespace_msg = "if input var ws_liberty_operator_target_namespace is null the value of ws_liberty_operator_namespace must be equal to ${local.default_liberty_operator_namespace}"
# tflint-ignore: terraform_unused_declarations
operator_target_namespace_chk = regex("^${local.operator_target_namespace_msg}$", (!local.operator_target_namespace_cnd ? local.operator_target_namespace_msg : ""))
}
data "ibm_container_cluster_config" "cluster_config" {
cluster_name_id = var.cluster_id
config_dir = "${path.module}/kubeconfig"
endpoint_type = var.cluster_config_endpoint_type != "default" ? var.cluster_config_endpoint_type : null
}
# creating the namespace to deploy the helm releases to install the WebSphere Liberty Operator
resource "kubernetes_namespace" "helm_release_operator_namespace" {
metadata {
name = var.operator_helm_release_namespace
}
timeouts {
delete = "30m"
}
lifecycle {
ignore_changes = [
metadata[0].annotations,
metadata[0].labels
]
}
}
locals {
ibm_operator_catalog_image_tag_digest = "v4.16@sha256:a00212a207a34ec3773adb548e2e7cd3999c6668abc5207bf06cc2f4bd1f3ca3" # datasource: icr.io/cpopen/ibm-operator-catalog
ibm_operator_catalog_path = "icr.io/cpopen/ibm-operator-catalog"
}
# if add_ibm_operator_catalog is true going on with adding the IBM Operator Catalog source
resource "helm_release" "ibm_operator_catalog" {
depends_on = [kubernetes_namespace.helm_release_operator_namespace]
count = var.add_ibm_operator_catalog == true ? 1 : 0
name = "ibm-operator-catalog-helm-release"
chart = "${path.module}/chart/${local.ibm_operator_catalog_chart}"
namespace = var.operator_helm_release_namespace
create_namespace = false
timeout = 300
force_update = true
cleanup_on_fail = false
wait = true
recreate_pods = true
disable_openapi_validation = false
set {
name = "image.path"
type = "string"
value = local.ibm_operator_catalog_path
}
set {
name = "image.version"
type = "string"
value = local.ibm_operator_catalog_image_tag_digest
}
}
# waiting for the catalog to be configured and correctly pulled
resource "time_sleep" "wait_catalog" {
depends_on = [helm_release.ibm_operator_catalog[0]]
count = var.add_ibm_operator_catalog == true ? 1 : 0
create_duration = local.sleep_time_catalog_create
}
# if ws_liberty_operator_target_namespace != null the operator group must be created
resource "helm_release" "websphere_liberty_operator_group" {
count = var.ws_liberty_operator_target_namespace != null ? 1 : 0
depends_on = [time_sleep.wait_catalog[0], kubernetes_namespace.helm_release_operator_namespace]
name = "websphere-liberty-operator-group-helm-release"
chart = "${path.module}/chart/${local.websphere_liberty_operator_group_chart}"
namespace = var.operator_helm_release_namespace
create_namespace = false
timeout = 300
force_update = true
cleanup_on_fail = false
wait = true
recreate_pods = true
disable_openapi_validation = false
set {
name = "operatornamespace"
type = "string"
value = var.ws_liberty_operator_namespace
}
set {
name = "operatortargetnamespace"
type = "string"
value = var.ws_liberty_operator_target_namespace
}
}
resource "kubernetes_namespace" "websphere_liberty_operator_namespace" {
count = var.create_ws_liberty_operator_namespace == true ? 1 : 0
metadata {
name = var.ws_liberty_operator_namespace
}
timeouts {
delete = "30m"
}
lifecycle {
ignore_changes = [
metadata[0].annotations,
metadata[0].labels
]
}
}
resource "helm_release" "websphere_liberty_operator" {
depends_on = [time_sleep.wait_catalog[0], helm_release.websphere_liberty_operator_group[0], kubernetes_namespace.websphere_liberty_operator_namespace[0]]
name = "websphere-liberty-operator-helm-release"
chart = "${path.module}/chart/${local.websphere_liberty_operator_chart}"
namespace = var.operator_helm_release_namespace
create_namespace = false
timeout = 300
force_update = true
cleanup_on_fail = false
wait = true
recreate_pods = true
disable_openapi_validation = false
set {
name = "operatornamespace"
type = "string"
value = var.ws_liberty_operator_namespace
}
set {
name = "installplanapprovalconfig"
type = "string"
value = var.ws_liberty_operator_install_plan_approval
}
# executed to approve install-plans if var.ws_liberty_operator_install_plan_approval is Manual (if Automatic no change is expected)
provisioner "local-exec" {
command = "${path.module}/scripts/approve-install-plan.sh ${var.ws_liberty_operator_namespace}"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config.config_file_path
}
}
}
resource "time_sleep" "wait_websphere_liberty_operator" {
depends_on = [helm_release.websphere_liberty_operator]
create_duration = local.sleep_time_operator_create
}
##############################################################################
# Confirm websphere operator is operational
##############################################################################
resource "null_resource" "confirm_websphere_liberty_operator_operational" {
depends_on = [time_sleep.wait_websphere_liberty_operator]
provisioner "local-exec" {
command = "${path.module}/scripts/confirm-wsloperator-operational.sh ${var.ws_liberty_operator_namespace}"
interpreter = ["/bin/bash", "-c"]
environment = {
KUBECONFIG = data.ibm_container_cluster_config.cluster_config.config_file_path
}
}
}
resource "kubernetes_namespace" "websphere_liberty_sampleapp_namespace" {
depends_on = [null_resource.confirm_websphere_liberty_operator_operational]
count = var.install_wslo_sampleapp == true ? 1 : 0
metadata {
name = var.wslo_sampleapp_namespace
}
timeouts {
delete = "30m"
}
lifecycle {
ignore_changes = [
metadata[0].annotations,
metadata[0].labels
]
}
}
locals {
websphere_liberty_operator_sampleapp_image_path = "icr.io/appcafe/open-liberty/samples/getting-started"
websphere_liberty_operator_sampleapp_image_tag_digest = "latest@sha256:724994d67734dde70f478e6d92167f383374edb45defed3f6cf62b056d8c5cea" # datasource: icr.io/appcafe/open-liberty/samples/getting-started
}
resource "helm_release" "websphere_liberty_operator_sampleapp" {
depends_on = [kubernetes_namespace.websphere_liberty_sampleapp_namespace[0]]
count = var.install_wslo_sampleapp == true ? 1 : 0
name = "websphere-liberty-operator-sampleapp-helm-release"
chart = "${path.module}/chart/${local.websphere_liberty_operator_sampleapp_chart}"
namespace = var.operator_helm_release_namespace
create_namespace = false
timeout = 300
force_update = true
cleanup_on_fail = false
wait = true
recreate_pods = true
disable_openapi_validation = false
set {
name = "application.image.path"
type = "string"
value = local.websphere_liberty_operator_sampleapp_image_path
}
set {
name = "application.image.version"
type = "string"
value = local.websphere_liberty_operator_sampleapp_image_tag_digest
}
set {
name = "application.name"
type = "string"
value = var.wslo_sampleapp_name
}
set {
name = "application.namespace"
type = "string"
value = var.wslo_sampleapp_namespace
}
}
# waiting for the sample app to be deployed before starting to query for the URL
resource "time_sleep" "wait_sampleapp" {
depends_on = [helm_release.websphere_liberty_operator_sampleapp[0]]
count = var.install_wslo_sampleapp == true ? 1 : 0
create_duration = local.sleep_time_sampleapp_create
}
data "external" "websphere_liberty_operator_sampleapp_url" {
depends_on = [time_sleep.wait_sampleapp[0]]
program = ["python3", "${path.module}/scripts/get-sampleapp-url.py"]
query = {
kubeconfig = data.ibm_container_cluster_config.cluster_config.config_file_path
appnamespace = var.wslo_sampleapp_namespace
appname = var.wslo_sampleapp_name
attempts = 30 # attempts to retrieve the sampleapp url (sleep of 10s between attempts)
}
}
locals {
# getting sampleapp_url in a JSON string in the format {"sampleapp_url": "[THE URL]"}
sampleapp_url_response = jsondecode(data.external.websphere_liberty_operator_sampleapp_url.result.response)
}