-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtekton.py
More file actions
413 lines (354 loc) · 17.6 KB
/
tekton.py
File metadata and controls
413 lines (354 loc) · 17.6 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# *****************************************************************************
# Copyright (c) 2024 IBM Corporation and other Contributors.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# *****************************************************************************
import logging
import yaml
from datetime import datetime
from os import path
from time import sleep
from kubeconfig import kubectl
from openshift.dynamic import DynamicClient
from openshift.dynamic.exceptions import NotFoundError, UnprocessibleEntityError
from jinja2 import Environment, FileSystemLoader
from .ocp import getConsoleURL, waitForCRD, waitForDeployment, crdExists
from .mas import waitForPVC, patchPendingPVC
logger = logging.getLogger(__name__)
# customStorageClassName is used when no default Storageclass is available on cluster,
# openshift-pipelines creates PVC which looks for default. customStorageClassName is patched into PVC when default is unavailable.
def installOpenShiftPipelines(dynClient: DynamicClient, customStorageClassName: str = None) -> bool:
"""
Install the OpenShift Pipelines Operator and wait for it to be ready to use
"""
packagemanifestAPI = dynClient.resources.get(api_version="packages.operators.coreos.com/v1", kind="PackageManifest")
subscriptionsAPI = dynClient.resources.get(api_version="operators.coreos.com/v1alpha1", kind="Subscription")
# Create the Operator Subscription
try:
if not crdExists(dynClient, "pipelines.tekton.dev"):
manifest = packagemanifestAPI.get(name="openshift-pipelines-operator-rh", namespace="openshift-marketplace")
defaultChannel = manifest.status.defaultChannel
catalogSource = manifest.status.catalogSource
catalogSourceNamespace = manifest.status.catalogSourceNamespace
logger.info(f"OpenShift Pipelines Operator Details: {catalogSourceNamespace}/{catalogSource}@{defaultChannel}")
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
template = env.get_template("subscription.yml.j2")
renderedTemplate = template.render(
subscription_name="openshift-pipelines-operator",
subscription_namespace="openshift-operators",
package_name="openshift-pipelines-operator-rh",
package_channel=defaultChannel,
catalog_name=catalogSource,
catalog_namespace=catalogSourceNamespace
)
subscription = yaml.safe_load(renderedTemplate)
subscriptionsAPI.apply(body=subscription, namespace="openshift-operators")
except NotFoundError:
logger.warning("Error: Couldn't find package manifest for Red Hat Openshift Pipelines Operator")
except UnprocessibleEntityError:
logger.warning("Error: Couldn't create/update OpenShift Pipelines Operator Subscription")
# Wait for the CRD to be available
logger.debug("Waiting for tasks.tekton.dev CRD to be available")
foundReadyCRD = waitForCRD(dynClient, "tasks.tekton.dev")
if foundReadyCRD:
logger.info("OpenShift Pipelines Operator is installed and ready")
else:
logger.error("OpenShift Pipelines Operator is NOT installed and ready")
return False
# Wait for the webhook to be ready
logger.debug("Waiting for tekton-pipelines-webhook Deployment to be ready")
foundReadyWebhook = waitForDeployment(dynClient, namespace="openshift-pipelines", deploymentName="tekton-pipelines-webhook")
if foundReadyWebhook:
logger.info("OpenShift Pipelines Webhook is installed and ready")
else:
logger.error("OpenShift Pipelines Webhook is NOT installed and ready")
return False
# Wait for the postgredb-tekton-results-postgres-0 PVC to be ready
# this PVC doesn't come up when there's no default storage class is in the cluster,
# this is causing the pvc to be in pending state and causing the tekton-results-postgres statefulSet in pending,
# due to these resources not coming up, the MAS pre-install check in the pipeline times out checking the health of this statefulSet,
# causing failure in pipeline.
# Refer https://github.com/ibm-mas/cli/issues/1511
logger.debug("Waiting for postgredb-tekton-results-postgres-0 PVC to be ready")
foundReadyPVC = waitForPVC(dynClient, namespace="openshift-pipelines", pvcName="postgredb-tekton-results-postgres-0")
if foundReadyPVC:
logger.info("OpenShift Pipelines postgres is installed and ready")
return True
else:
patchedPVC = patchPendingPVC(dynClient, namespace="openshift-pipelines", pvcName="postgredb-tekton-results-postgres-0", storageClassName=customStorageClassName)
if patchedPVC:
logger.info("OpenShift Pipelines postgres is installed and ready")
return True
else:
logger.error("OpenShift Pipelines postgres PVC is NOT ready")
return False
def updateTektonDefinitions(namespace: str, yamlFile: str) -> None:
"""
Install/update the MAS tekton pipeline and task definitions
Unfortunately there's no API equivalent of what the kubectl CLI gives
us with the ability to just apply a file containing a mix of resource types
https://github.com/gtaylor/kubeconfig-python/
Throws:
- kubeconfig.exceptions.KubectlCommandError
"""
result = kubectl.run(subcmd_args=['apply', '-n', namespace, '-f', yamlFile])
for line in result.split("\n"):
logger.debug(line)
def preparePipelinesNamespace(dynClient: DynamicClient, instanceId: str = None, storageClass: str = None, accessMode: str = None, waitForBind: bool = True, configureRBAC: bool = True):
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
if instanceId is None:
namespace = "mas-pipelines"
template = env.get_template("pipelines-rbac-cluster.yml.j2")
else:
namespace = f"mas-{instanceId}-pipelines"
template = env.get_template("pipelines-rbac.yml.j2")
if configureRBAC:
# Create RBAC
renderedTemplate = template.render(mas_instance_id=instanceId)
logger.debug(renderedTemplate)
crb = yaml.safe_load(renderedTemplate)
clusterRoleBindingAPI = dynClient.resources.get(api_version="rbac.authorization.k8s.io/v1", kind="ClusterRoleBinding")
clusterRoleBindingAPI.apply(body=crb, namespace=namespace)
# Create PVC (instanceId namespace only)
if instanceId is not None:
template = env.get_template("pipelines-pvc.yml.j2")
renderedTemplate = template.render(
mas_instance_id=instanceId,
pipeline_storage_class=storageClass,
pipeline_storage_accessmode=accessMode
)
logger.debug(renderedTemplate)
pvc = yaml.safe_load(renderedTemplate)
pvcAPI = dynClient.resources.get(api_version="v1", kind="PersistentVolumeClaim")
pvcAPI.apply(body=pvc, namespace=namespace)
if instanceId is not None and waitForBind:
logger.debug("Waiting for PVC to be bound")
pvcIsBound = False
while not pvcIsBound:
configPVC = pvcAPI.get(name="config-pvc", namespace=namespace)
if configPVC.status.phase == "Bound":
pvcIsBound = True
else:
logger.debug("Waiting 15s before checking status of PVC again")
logger.debug(configPVC)
sleep(15)
def prepareInstallSecrets(dynClient: DynamicClient, instanceId: str, slsLicenseFile: str = None, additionalConfigs: dict = None, certs: str = None, podTemplates: str = None) -> None:
namespace = f"mas-{instanceId}-pipelines"
secretsAPI = dynClient.resources.get(api_version="v1", kind="Secret")
# 1. Secret/pipeline-additional-configs
# -------------------------------------------------------------------------
# Must exist, but can be empty
try:
secretsAPI.delete(name="pipeline-additional-configs", namespace=namespace)
except NotFoundError:
pass
if additionalConfigs is None:
additionalConfigs = {
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {
"name": "pipeline-additional-configs"
}
}
secretsAPI.create(body=additionalConfigs, namespace=namespace)
# 2. Secret/pipeline-sls-entitlement
# -------------------------------------------------------------------------
try:
secretsAPI.delete(name="pipeline-sls-entitlement", namespace=namespace)
except NotFoundError:
pass
if slsLicenseFile is None:
slsLicenseFile = {
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {
"name": "pipeline-sls-entitlement"
}
}
secretsAPI.create(body=slsLicenseFile, namespace=namespace)
# 3. Secret/pipeline-certificates
# -------------------------------------------------------------------------
# Must exist. It could be an empty secret at the first place before customer configure it
try:
secretsAPI.delete(name="pipeline-certificates", namespace=namespace)
except NotFoundError:
pass
if certs is None:
certs = {
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {
"name": "pipeline-certificates"
}
}
secretsAPI.create(body=certs, namespace=namespace)
# 4. Secret/pipeline-pod-templates
# -------------------------------------------------------------------------
# Must exist, but can be empty
try:
secretsAPI.delete(name="pipeline-pod-templates", namespace=namespace)
except NotFoundError:
pass
if podTemplates is None:
podTemplates = {
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {
"name": "pipeline-pod-templates"
}
}
secretsAPI.create(body=podTemplates, namespace=namespace)
def testCLI() -> None:
pass
# echo -n "Testing availability of $CLI_IMAGE in cluster ..."
# EXISTING_DEPLOYMENT_IMAGE=$(oc -n $PIPELINES_NS get deployment mas-cli -o jsonpath='{.spec.template.spec.containers[0].image}' 2>/dev/null)
# if [[ "$EXISTING_DEPLOYMENT_IMAGE" != "CLI_IMAGE" ]]
# then oc -n $PIPELINES_NS apply -f $CONFIG_DIR/deployment-$MAS_INSTANCE_ID.yaml &>> $LOGFILE
# fi
# oc -n $PIPELINES_NS wait --for=condition=Available=true deployment mas-cli --timeout=3m &>> $LOGFILE
# if [[ "$?" == "0" ]]; then
# # All is good
# echo -en "\033[1K" # Clear current line
# echo -en "\033[u" # Restore cursor position
# echo -e "${COLOR_GREEN}$CLI_IMAGE is available from the target OCP cluster${TEXT_RESET}"
# else
# echo -en "\033[1K" # Clear current line
# echo -en "\033[u" # Restore cursor position
# # We can't get the image, so there's no point running the pipeline
# echo_warning "Failed to validate $CLI_IMAGE in the target OCP cluster"
# echo "This image must be accessible from your OpenShift cluster to run the installation:"
# echo "- If you are running an offline (air gap) installation this likely means you have not mirrored this image to your private registry"
# echo "- It could also mean that your cluster's ImageContentSourcePolicy is misconfigured and does not contain an entry for quay.io/ibmmas"
# echo "- Check the deployment status of mas-cli in your pipeline namespace. This will provide you with more information about the issue."
# echo -e "\n\n[WARNING] Failed to validate $CLI_IMAGE in the target OCP cluster" >> $LOGFILE
# echo_hr1 >> $LOGFILE
# oc -n $PIPELINES_NS get pods --selector="app=mas-cli" -o yaml >> $LOGFILE
# exit 1
# fi
def launchUpgradePipeline(dynClient: DynamicClient,
instanceId: str,
skipPreCheck: bool = False,
masChannel: str = "",
params: dict = {}) -> str:
"""
Create a PipelineRun to upgrade the chosen MAS instance
"""
pipelineRunsAPI = dynClient.resources.get(api_version="tekton.dev/v1beta1", kind="PipelineRun")
namespace = f"mas-{instanceId}-pipelines"
timestamp = datetime.now().strftime("%y%m%d-%H%M")
# Create the PipelineRun
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
template = env.get_template("pipelinerun-upgrade.yml.j2")
renderedTemplate = template.render(
timestamp=timestamp,
mas_instance_id=instanceId,
skip_pre_check=skipPreCheck,
mas_channel=masChannel,
**params
)
logger.debug(renderedTemplate)
pipelineRun = yaml.safe_load(renderedTemplate)
pipelineRunsAPI.apply(body=pipelineRun, namespace=namespace)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-{instanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{instanceId}-upgrade-{timestamp}"
return pipelineURL
def launchUninstallPipeline(dynClient: DynamicClient,
instanceId: str,
droNamespace: str,
certManagerProvider: str = "redhat",
uninstallCertManager: bool = False,
uninstallGrafana: bool = False,
uninstallCatalog: bool = False,
uninstallCommonServices: bool = False,
uninstallUDS: bool = False,
uninstallMongoDb: bool = False,
uninstallSLS: bool = False) -> str:
"""
Create a PipelineRun to uninstall the chosen MAS instance (and selected dependencies)
"""
pipelineRunsAPI = dynClient.resources.get(api_version="tekton.dev/v1beta1", kind="PipelineRun")
namespace = f"mas-{instanceId}-pipelines"
timestamp = datetime.now().strftime("%y%m%d-%H%M")
# Create the PipelineRun
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
template = env.get_template("pipelinerun-uninstall.yml.j2")
grafanaAction = "uninstall" if uninstallGrafana else "none"
certManagerAction = "uninstall" if uninstallCertManager else "none"
commonServicesAction = "uninstall" if uninstallCommonServices else "none"
ibmCatalogAction = "uninstall" if uninstallCatalog else "none"
mongoDbAction = "uninstall" if uninstallMongoDb else "none"
slsAction = "uninstall" if uninstallSLS else "none"
udsAction = "uninstall" if uninstallUDS else "none"
# Render the pipelineRun
renderedTemplate = template.render(
timestamp=timestamp,
mas_instance_id=instanceId,
grafana_action=grafanaAction,
cert_manager_provider=certManagerProvider,
cert_manager_action=certManagerAction,
common_services_action=commonServicesAction,
ibm_catalogs_action=ibmCatalogAction,
mongodb_action=mongoDbAction,
sls_action=slsAction,
uds_action=udsAction,
dro_namespace=droNamespace
)
logger.debug(renderedTemplate)
pipelineRun = yaml.safe_load(renderedTemplate)
pipelineRunsAPI.apply(body=pipelineRun, namespace=namespace)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-{instanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{instanceId}-uninstall-{timestamp}"
return pipelineURL
def launchPipelineRun(dynClient: DynamicClient, namespace: str, templateName: str, params: dict) -> str:
pipelineRunsAPI = dynClient.resources.get(api_version="tekton.dev/v1beta1", kind="PipelineRun")
timestamp = datetime.now().strftime("%y%m%d-%H%M")
# Create the PipelineRun
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
template = env.get_template(f"{templateName}.yml.j2")
# Render the pipelineRun
renderedTemplate = template.render(
timestamp=timestamp,
**params
)
logger.debug(renderedTemplate)
pipelineRun = yaml.safe_load(renderedTemplate)
pipelineRunsAPI.apply(body=pipelineRun, namespace=namespace)
return timestamp
def launchInstallPipeline(dynClient: DynamicClient, params: dict) -> str:
"""
Create a PipelineRun to install the chosen MAS instance (and selected dependencies)
"""
instanceId = params["mas_instance_id"]
namespace = f"mas-{instanceId}-pipelines"
timestamp = launchPipelineRun(dynClient, namespace, "pipelinerun-install", params)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-{instanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{instanceId}-install-{timestamp}"
return pipelineURL
def launchUpdatePipeline(dynClient: DynamicClient, params: dict) -> str:
"""
Create a PipelineRun to update the Maximo Operator Catalog
"""
namespace = "mas-pipelines"
timestamp = launchPipelineRun(dynClient, namespace, "pipelinerun-update", params)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-pipelines/tekton.dev~v1beta1~PipelineRun/mas-update-{timestamp}"
return pipelineURL