-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtekton.py
More file actions
908 lines (759 loc) · 38.7 KB
/
tekton.py
File metadata and controls
908 lines (759 loc) · 38.7 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# *****************************************************************************
# 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, waitForPVC, getStorageClasses, getStorageClassVolumeBindingMode
logger = logging.getLogger(__name__)
def installOpenShiftPipelines(dynClient: DynamicClient, customStorageClassName: str = None) -> bool:
"""
Install the OpenShift Pipelines Operator and wait for it to be ready to use.
Creates the operator subscription, waits for the CRD and webhook to be ready,
and handles PVC storage class configuration if needed.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
customStorageClassName (str, optional): Custom storage class name for Tekton PVC. Defaults to None.
Returns:
bool: True if installation is successful, False otherwise
Raises:
NotFoundError: If the package manifest is not found
UnprocessibleEntityError: If the subscription cannot be created
"""
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
# Workaround for bug in OpenShift Pipelines/Tekton
# -------------------------------------------------------------------------
# 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("Checking postgredb-tekton-results-postgres-0 PVC status")
pvcAPI = dynClient.resources.get(api_version="v1", kind="PersistentVolumeClaim")
pvcName = "postgredb-tekton-results-postgres-0"
pvcNamespace = "openshift-pipelines"
# Wait briefly for PVC to be created (max 5 minutes)
maxInitialRetries = 60
pvc = None
for retry in range(maxInitialRetries):
try:
pvc = pvcAPI.get(name=pvcName, namespace=pvcNamespace)
break
except NotFoundError:
if retry < maxInitialRetries - 1:
logger.debug(f"Waiting 5s for PVC {pvcName} to be created (attempt {retry + 1}/{maxInitialRetries})...")
sleep(5)
if pvc is None:
logger.error(f"PVC {pvcName} was not created after {maxInitialRetries * 5} seconds (5 minutes)")
return False
# Check if PVC is already bound
if pvc.status.phase == "Bound":
logger.info("OpenShift Pipelines postgres PVC is already bound and ready")
return True
# Check if PVC is pending without a storage class - needs immediate patching
if pvc.status.phase == "Pending" and pvc.spec.storageClassName is None:
logger.info("PVC is pending without storage class, attempting to patch immediately...")
tektonPVCisReady = addMissingStorageClassToTektonPVC(
dynClient=dynClient,
namespace=pvcNamespace,
pvcName=pvcName,
storageClassName=customStorageClassName
)
if tektonPVCisReady:
logger.info("OpenShift Pipelines postgres is installed and ready")
return True
else:
logger.error("OpenShift Pipelines postgres PVC is NOT ready after patching")
return False
# PVC exists with storage class but not bound yet - wait for it to bind
logger.debug(f"PVC has storage class '{pvc.spec.storageClassName}', waiting for it to be bound...")
foundReadyPVC = waitForPVC(dynClient, namespace=pvcNamespace, pvcName=pvcName)
if foundReadyPVC:
logger.info("OpenShift Pipelines postgres is installed and ready")
return True
else:
logger.error("OpenShift Pipelines postgres PVC is NOT ready")
return False
def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str, pvcName: str, storageClassName: str = None) -> bool:
"""
OpenShift Pipelines has a problem when there is no default storage class defined in a cluster, this function
patches the PVC used to store pipeline results to add a specific storage class into the PVC spec and waits for the
PVC to be bound.
:param dynClient: Kubernetes client, required to work with PVC
:type dynClient: DynamicClient
:param namespace: Namespace where OpenShift Pipelines is installed
:type namespace: str
:param pvcName: Name of the PVC that we want to fix
:type pvcName: str
:param storageClassName: Name of the storage class that we want to update the PVC to reference (optional, will auto-select if not provided)
:type storageClassName: str
:return: True if PVC is successfully patched and bound, False otherwise
:rtype: bool
"""
pvcAPI = dynClient.resources.get(api_version="v1", kind="PersistentVolumeClaim")
storageClassAPI = dynClient.resources.get(api_version="storage.k8s.io/v1", kind="StorageClass")
try:
pvc = pvcAPI.get(name=pvcName, namespace=namespace)
# Check if PVC is pending and has no storage class
if pvc.status.phase == "Pending" and pvc.spec.storageClassName is None:
# Determine which storage class to use
targetStorageClass = None
if storageClassName is not None:
# Verify the provided storage class exists
try:
storageClassAPI.get(name=storageClassName)
targetStorageClass = storageClassName
logger.info(f"Using provided storage class '{storageClassName}' for PVC {pvcName}")
except NotFoundError:
logger.warning(f"Provided storage class '{storageClassName}' not found, will try to detect available storage class")
# If no valid custom storage class, try to detect one
if targetStorageClass is None:
logger.warning("No storage class provided or provided storage class not found, attempting to use first available storage class")
storageClasses = getStorageClasses(dynClient)
if len(storageClasses) > 0:
# Use the first available storage class
targetStorageClass = storageClasses[0].metadata.name
logger.info(f"Using first available storage class '{targetStorageClass}' for PVC {pvcName}")
else:
logger.error(f"Unable to set storageClassName in PVC {pvcName}. No storage classes available in the cluster.")
return False
# Patch the PVC with the storage class
pvc.spec.storageClassName = targetStorageClass
logger.info(f"Patching PVC {pvcName} with storageClassName: {targetStorageClass}")
pvcAPI.patch(body=pvc, namespace=namespace)
# Wait for the PVC to be bound
maxRetries = 60
foundReadyPVC = False
retries = 0
while not foundReadyPVC and retries < maxRetries:
retries += 1
try:
patchedPVC = pvcAPI.get(name=pvcName, namespace=namespace)
if patchedPVC.status.phase == "Bound":
foundReadyPVC = True
logger.info(f"PVC {pvcName} is now bound")
else:
logger.debug(f"Waiting 5s for PVC {pvcName} to be bound before checking again ...")
sleep(5)
except NotFoundError:
logger.error(f"The patched PVC {pvcName} does not exist.")
return False
return foundReadyPVC
else:
logger.warning(f"PVC {pvcName} is not in Pending state or already has a storageClassName")
return pvc.status.phase == "Bound"
except NotFoundError:
logger.error(f"PVC {pvcName} does not exist")
return False
def updateTektonDefinitions(namespace: str, yamlFile: str) -> None:
"""
Install or update MAS Tekton pipeline and task definitions from a YAML file.
Uses kubectl to apply a YAML file containing multiple resource types.
Parameters:
namespace (str): The namespace to apply the definitions to
yamlFile (str): Path to the YAML file containing Tekton definitions
Returns:
None
Raises:
kubeconfig.exceptions.KubectlCommandError: If kubectl command fails
"""
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, createConfigPVC: bool = True, createBackupPVC: bool = False, backupStorageSize: str = "20Gi"):
"""
Prepare a namespace for MAS pipelines by creating RBAC and PVC resources.
Creates cluster-wide or instance-specific pipeline namespace with necessary
role bindings and persistent volume claims.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
instanceId (str, optional): MAS instance ID. If None, creates cluster-wide namespace. Defaults to None.
storageClass (str, optional): Storage class for the PVC. Defaults to None.
accessMode (str, optional): Access mode for the PVC. Defaults to None.
waitForBind (bool, optional): Whether to wait for PVC to bind. Defaults to True.
configureRBAC (bool, optional): Whether to configure RBAC. Defaults to True.
createConfigPVC (bool, optional): Whether to create config PVC. Defaults to True.
createBackupPVC (bool, optional): Whether to create backup PVC. Defaults to False.
backupStorageSize (str, optional): Size of the backup PVC storage. Defaults to "20Gi".
Returns:
None
Raises:
NotFoundError: If resources cannot be created
"""
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:
pvcAPI = dynClient.resources.get(api_version="v1", kind="PersistentVolumeClaim")
# Automatically determine if we should wait for PVC binding based on storage class
volumeBindingMode = getStorageClassVolumeBindingMode(dynClient, storageClass)
waitForBind = (volumeBindingMode == "Immediate")
# Create config PVC if requested
if createConfigPVC:
logger.info("Creating config PVC")
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.apply(body=pvc, namespace=namespace)
if waitForBind:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, waiting for config PVC to bind")
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 config PVC again")
logger.debug(configPVC)
sleep(15)
else:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, skipping config PVC bind wait")
# Create backup PVC if requested
if createBackupPVC:
logger.info("Creating backup PVC")
backupTemplate = env.get_template("pipelines-backup-pvc.yml.j2")
renderedBackupTemplate = backupTemplate.render(
mas_instance_id=instanceId,
pipeline_storage_class=storageClass,
pipeline_storage_accessmode=accessMode,
backup_storage_size=backupStorageSize
)
logger.debug(renderedBackupTemplate)
backupPvc = yaml.safe_load(renderedBackupTemplate)
pvcAPI.apply(body=backupPvc, namespace=namespace)
if waitForBind:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, waiting for backup PVC to bind")
backupPvcIsBound = False
while not backupPvcIsBound:
backupPVC = pvcAPI.get(name="backup-pvc", namespace=namespace)
if backupPVC.status.phase == "Bound":
backupPvcIsBound = True
else:
logger.debug("Waiting 15s before checking status of backup PVC again")
logger.debug(backupPVC)
sleep(15)
else:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, skipping backup PVC bind wait")
def prepareAiServicePipelinesNamespace(dynClient: DynamicClient, instanceId: str = None, storageClass: str = None, accessMode: str = None, waitForBind: bool = True, configureRBAC: bool = True):
"""
Prepare a namespace for AI Service pipelines by creating RBAC and PVC resources.
Creates AI Service-specific pipeline namespace with necessary role bindings
and persistent volume claims.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
instanceId (str, optional): AI Service instance ID. Defaults to None.
storageClass (str, optional): Storage class for the PVC. Defaults to None.
accessMode (str, optional): Access mode for the PVC. Defaults to None.
waitForBind (bool, optional): Whether to wait for PVC to bind. Defaults to True.
configureRBAC (bool, optional): Whether to configure RBAC. Defaults to True.
Returns:
None
Raises:
NotFoundError: If resources cannot be created
"""
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
env = Environment(
loader=FileSystemLoader(searchpath=templateDir)
)
namespace = f"aiservice-{instanceId}-pipelines"
template = env.get_template("aiservice-pipelines-rbac.yml.j2")
if configureRBAC:
# Create RBAC
renderedTemplate = template.render(aiservice_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)
template = env.get_template("aiservice-pipelines-pvc.yml.j2")
renderedTemplate = template.render(
aiservice_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)
# Automatically determine if we should wait for PVC binding based on storage class
volumeBindingMode = getStorageClassVolumeBindingMode(dynClient, storageClass)
waitForBind = (volumeBindingMode == "Immediate")
if waitForBind:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, waiting for PVC to bind")
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)
else:
logger.info(f"Storage class {storageClass} uses volumeBindingMode={volumeBindingMode}, skipping PVC bind wait")
def prepareRestoreSecrets(dynClient: DynamicClient, namespace: str, restoreConfigs: dict = None):
"""
Create or update secret required for MAS Restore pipeline.
Creates secret in the specified namespace:
- pipeline-restore-configs
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
namespace (str): The namespace to create secrets in
restoreConfigs (dict, optional): configuration data for restore. Defaults to None (empty secret).
Returns:
None
Raises:
NotFoundError: If secrets cannot be created
"""
secretsAPI = dynClient.resources.get(api_version="v1", kind="Secret")
# 1. Secret/pipeline-restore-configs
# -------------------------------------------------------------------------
# Must exist, but can be empty
try:
secretsAPI.delete(name="pipeline-restore-configs", namespace=namespace)
except NotFoundError:
pass
if restoreConfigs is None:
restoreConfigs = {
"apiVersion": "v1",
"kind": "Secret",
"type": "Opaque",
"metadata": {
"name": "pipeline-restore-configs"
}
}
secretsAPI.create(body=restoreConfigs, namespace=namespace)
def prepareInstallSecrets(dynClient: DynamicClient, namespace: str, slsLicenseFile: dict | None = None, additionalConfigs: dict | None = None, certs: dict | None = None, podTemplates: dict | None = None) -> None:
"""
Create or update secrets required for MAS installation pipelines.
Creates four secrets in the specified namespace: pipeline-additional-configs,
pipeline-sls-entitlement, pipeline-certificates, and pipeline-pod-templates.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
namespace (str): The namespace to create secrets in
slsLicenseFile (dict, optional): SLS license file content. Defaults to None (empty secret).
additionalConfigs (dict, optional): Additional configuration data. Defaults to None (empty secret).
certs (dict, optional): Certificate data. Defaults to None (empty secret).
podTemplates (dict, optional): Pod template data. Defaults to None (empty secret).
Returns:
None
Raises:
NotFoundError: If secrets cannot be created
"""
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,
uninstallCertManager: bool = False,
uninstallGrafana: bool = False,
uninstallCatalog: bool = False,
uninstallDRO: 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"
ibmCatalogAction = "uninstall" if uninstallCatalog else "none"
mongoDbAction = "uninstall" if uninstallMongoDb else "none"
slsAction = "uninstall" if uninstallSLS else "none"
droAction = "uninstall" if uninstallDRO else "none"
# Render the pipelineRun
renderedTemplate = template.render(
timestamp=timestamp,
mas_instance_id=instanceId,
grafana_action=grafanaAction,
cert_manager_action=certManagerAction,
ibm_catalogs_action=ibmCatalogAction,
mongodb_action=mongoDbAction,
sls_action=slsAction,
dro_action=droAction,
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:
"""
Launch a Tekton PipelineRun from a template.
Creates a PipelineRun resource by rendering a Jinja2 template with the provided parameters.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
namespace (str): The namespace to create the PipelineRun in
templateName (str): Name of the template file (without .yml.j2 extension)
params (dict): Parameters to pass to the template
Returns:
str: Timestamp string used in the PipelineRun name (format: YYMMDD-HHMM)
Raises:
NotFoundError: If the template or namespace is not found
"""
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 a MAS or AI Service instance with selected dependencies.
Automatically detects whether to install MAS or AI Service based on the presence
of mas_instance_id in params.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
params (dict): Installation parameters including instance ID and configuration
Returns:
str: URL to the PipelineRun in the OpenShift console
Raises:
NotFoundError: If resources cannot be created
"""
applicationType = "aiservice" if not params.get("mas_instance_id") else "mas"
params["applicationType"] = applicationType
instanceId = params[f"{applicationType}_instance_id"]
namespace = f"{applicationType}-{instanceId}-pipelines"
timestamp = launchPipelineRun(dynClient, namespace, "pipelinerun-install", params)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/{applicationType}-{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.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
params (dict): Update parameters
Returns:
str: URL to the PipelineRun in the OpenShift console
Raises:
NotFoundError: If resources cannot be created
"""
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
def launchBackupPipeline(dynClient: DynamicClient, params: dict) -> str:
"""
Create a PipelineRun to backup a MAS instance.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
params (dict): Backup parameters including instance ID and configuration
Returns:
str: URL to the PipelineRun in the OpenShift console
Raises:
NotFoundError: If resources cannot be created
"""
instanceId = params["mas_instance_id"]
backupVersion = params["backup_version"]
namespace = f"mas-{instanceId}-pipelines"
timestamp = launchPipelineRun(dynClient, namespace, "pipelinerun-backup", params)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-{instanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{instanceId}-backup-{backupVersion}-{timestamp}"
return pipelineURL
def launchRestorePipeline(dynClient: DynamicClient, params: dict) -> str:
"""
Create a PipelineRun to restore a MAS instance.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
params (dict): Backup/Restore parameters including instance ID and configuration
Returns:
str: URL to the PipelineRun in the OpenShift console
Raises:
NotFoundError: If resources cannot be created
"""
instanceId = params["mas_instance_id"]
restoreVersion = params["restore_version"]
namespace = f"mas-{instanceId}-pipelines"
timestamp = launchPipelineRun(dynClient, namespace, "pipelinerun-restore", params)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/mas-{instanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{instanceId}-restore-{restoreVersion}-{timestamp}"
return pipelineURL
def launchAiServiceUpgradePipeline(dynClient: DynamicClient,
aiserviceInstanceId: str,
skipPreCheck: bool = False,
aiserviceChannel: str = "",
params: dict = {}) -> str:
"""
Create a PipelineRun to upgrade the chosen AI Service instance
"""
pipelineRunsAPI = dynClient.resources.get(api_version="tekton.dev/v1beta1", kind="PipelineRun")
namespace = f"aiservice-{aiserviceInstanceId}-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-aiservice-upgrade.yml.j2")
renderedTemplate = template.render(
timestamp=timestamp,
aiservice_instance_id=aiserviceInstanceId,
skip_pre_check=skipPreCheck,
aiservice_channel=aiserviceChannel,
**params
)
logger.debug(renderedTemplate)
pipelineRun = yaml.safe_load(renderedTemplate)
pipelineRunsAPI.apply(body=pipelineRun, namespace=namespace)
pipelineURL = f"{getConsoleURL(dynClient)}/k8s/ns/aiservice-{aiserviceInstanceId}-pipelines/tekton.dev~v1beta1~PipelineRun/{aiserviceInstanceId}-upgrade-{timestamp}"
return pipelineURL
def prepareInstallRBAC(dynClient: DynamicClient, namespace: str, instanceId: str, installRBACDir: str) -> None:
"""
Apply the minimal install RBAC bundle for a MAS instance.
The bundle is defined by the kustomization under cli/rbac/install and creates the install-user and install-pipeline service accounts
and their associated role bindings.
Parameters:
dynClient (DynamicClient): OpenShift Dynamic Client
instanceId (str): MAS instance ID used to render the RBAC templates
installRBACDir (str): Path to the directory containing the RBAC kustomization and templates
Returns:
None
Raises:
FileNotFoundError: If the RBAC bundle directory or kustomization file does not exists
"""
kustomizationFile = path.join(installRBACDir, "kustomization.yaml")
if not path.isfile(kustomizationFile):
logger.error(f"Cannot find kustomization file for install RBAC at {kustomizationFile}")
raise FileNotFoundError(f"Cannot find kustomization file for install RBAC at {kustomizationFile}")
with open(kustomizationFile, "r") as file:
kustomization = yaml.safe_load(file)
env = Environment()
for resourcePath in kustomization.get("resources", []):
manifestFile = path.join(installRBACDir, resourcePath)
if not path.isfile(manifestFile):
logger.error(f"Cannot find RBAC manifest file at {manifestFile}")
raise FileNotFoundError(f"Cannot find RBAC manifest file at {manifestFile}")
with open(manifestFile, "r") as file:
template = env.from_string(file.read())
renderedManifest = template.render(mas_instance_id=instanceId)
logger.debug(f"Applying RBAC manifest {manifestFile} for instance {instanceId}:\n{renderedManifest}")
for resourceBody in yaml.safe_load_all(renderedManifest):
if resourceBody is None:
continue
apiVersion = resourceBody["apiVersion"]
kind = resourceBody["kind"]
metadata = resourceBody.get("metadata", {})
name = metadata.get("name", "<unnamed>")
namespace = metadata.get("namespace")
logger.debug(f"Applying RBAC resource {kind}/{name} in namespace {namespace} for instance {instanceId}")
resourceAPI = dynClient.resources.get(api_version=apiVersion, kind=kind)
if namespace:
resourceAPI.apply(body=resourceBody, namespace=namespace)
else:
resourceAPI.apply(body=resourceBody)