Skip to content

Commit e3979ae

Browse files
committed
[patch] postgresdb pvc issue fix
1 parent 0ab0147 commit e3979ae

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

src/mas/devops/tekton.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from jinja2 import Environment, FileSystemLoader
2424

25-
from .ocp import getConsoleURL, waitForCRD, waitForDeployment, crdExists, waitForPVC
25+
from .ocp import getConsoleURL, waitForCRD, waitForDeployment, crdExists, waitForPVC, getStorageClasses
2626

2727
logger = logging.getLogger(__name__)
2828

@@ -125,7 +125,7 @@ def installOpenShiftPipelines(dynClient: DynamicClient, customStorageClassName:
125125
return False
126126

127127

128-
def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str, pvcName: str, storageClassName: str) -> bool:
128+
def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str, pvcName: str, storageClassName: str = None) -> bool:
129129
"""
130130
OpenShift Pipelines has a problem when there is no default storage class defined in a cluster, this function
131131
patches the PVC used to store pipeline results to add a specific storage class into the PVC spec and waits for the
@@ -137,18 +137,49 @@ def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str,
137137
:type namespace: str
138138
:param pvcName: Name of the PVC that we want to fix
139139
:type pvcName: str
140-
:param storageClassName: Name of the storage class that we want to update the PVC to reference
140+
:param storageClassName: Name of the storage class that we want to update the PVC to reference (optional, will auto-select if not provided)
141141
:type storageClassName: str
142-
:return: Description
142+
:return: True if PVC is successfully patched and bound, False otherwise
143143
:rtype: bool
144144
"""
145145
pvcAPI = dynClient.resources.get(api_version="v1", kind="PersistentVolumeClaim")
146+
storageClassAPI = dynClient.resources.get(api_version="storage.k8s.io/v1", kind="StorageClass")
147+
146148
try:
147149
pvc = pvcAPI.get(name=pvcName, namespace=namespace)
150+
151+
# Check if PVC is pending and has no storage class
148152
if pvc.status.phase == "Pending" and pvc.spec.storageClassName is None:
149-
pvc.spec.storageClassName = storageClassName
153+
# Determine which storage class to use
154+
targetStorageClass = None
155+
156+
if storageClassName is not None:
157+
# Verify the provided storage class exists
158+
try:
159+
storageClassAPI.get(name=storageClassName)
160+
targetStorageClass = storageClassName
161+
logger.info(f"Using provided storage class '{storageClassName}' for PVC {pvcName}")
162+
except NotFoundError:
163+
logger.warning(f"Provided storage class '{storageClassName}' not found, will try to detect available storage class")
164+
165+
# If no valid custom storage class, try to detect one
166+
if targetStorageClass is None:
167+
logger.warning("No storage class provided or provided storage class not found, attempting to use first available storage class")
168+
storageClasses = getStorageClasses(dynClient)
169+
if len(storageClasses) > 0:
170+
# Use the first available storage class
171+
targetStorageClass = storageClasses[0].metadata.name
172+
logger.info(f"Using first available storage class '{targetStorageClass}' for PVC {pvcName}")
173+
else:
174+
logger.error(f"Unable to set storageClassName in PVC {pvcName}. No storage classes available in the cluster.")
175+
return False
176+
177+
# Patch the PVC with the storage class
178+
pvc.spec.storageClassName = targetStorageClass
179+
logger.info(f"Patching PVC {pvcName} with storageClassName: {targetStorageClass}")
150180
pvcAPI.patch(body=pvc, namespace=namespace)
151181

182+
# Wait for the PVC to be bound
152183
maxRetries = 60
153184
foundReadyPVC = False
154185
retries = 0
@@ -158,6 +189,7 @@ def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str,
158189
patchedPVC = pvcAPI.get(name=pvcName, namespace=namespace)
159190
if patchedPVC.status.phase == "Bound":
160191
foundReadyPVC = True
192+
logger.info(f"PVC {pvcName} is now bound")
161193
else:
162194
logger.debug(f"Waiting 5s for PVC {pvcName} to be bound before checking again ...")
163195
sleep(5)
@@ -166,6 +198,9 @@ def addMissingStorageClassToTektonPVC(dynClient: DynamicClient, namespace: str,
166198
return False
167199

168200
return foundReadyPVC
201+
else:
202+
logger.warning(f"PVC {pvcName} is not in Pending state or already has a storageClassName")
203+
return pvc.status.phase == "Bound"
169204

170205
except NotFoundError:
171206
logger.error(f"PVC {pvcName} does not exist")

0 commit comments

Comments
 (0)