diff --git a/src/mas/devops/mas.py b/src/mas/devops/mas.py index 549f1be2..1abce036 100644 --- a/src/mas/devops/mas.py +++ b/src/mas/devops/mas.py @@ -151,7 +151,7 @@ def getMasChannel(dynClient: DynamicClient, instanceId: str) -> str: """ try: masSubscription = getSubscription(dynClient, f"mas-{instanceId}-core", "ibm-mas") - return masSubscription.items[0]["spec"]["channel"] + return masSubscription.spec.channel except NotFoundError: return False except UnauthorizedError: diff --git a/src/mas/devops/olm.py b/src/mas/devops/olm.py index 0943406e..8ad8fa0f 100644 --- a/src/mas/devops/olm.py +++ b/src/mas/devops/olm.py @@ -58,21 +58,16 @@ def ensureOperatorGroupExists(dynClient: DynamicClient, env: Environment, namesp def getSubscription(dynClient: DynamicClient, namespace: str, packageName: str): - try: - labelSelector = f"operators.coreos.com/{packageName}.{namespace}" - logger.debug(f"Get Subscription for {packageName} in {namespace}") - subscriptionsAPI = dynClient.resources.get( - api_version="operators.coreos.com/v1alpha1", kind="Subscription" - ) - subscription = subscriptionsAPI.get( - label_selector=labelSelector, namespace=namespace - ) - if subscription.items == 0: - raise NotFoundError - except NotFoundError: + labelSelector = f"operators.coreos.com/{packageName}.{namespace}" + logger.debug(f"Get Subscription for {packageName} in {namespace}") + subscriptionsAPI = dynClient.resources.get(api_version="operators.coreos.com/v1alpha1", kind="Subscription") + subscriptions = subscriptionsAPI.get(label_selector=labelSelector, namespace=namespace) + if len(subscriptions.items) == 0: logger.info(f"No matching Subscription found for {packageName} in {namespace}") - subscription = None - return subscription + return None + elif len(subscriptions.items) > 0: + logger.warning(f"More than one ({len(subscriptions.items)}) Subscriptions found for {packageName} in {namespace}") + return subscriptions.items[0] def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str, packageChannel: str = None, catalogSource: str = None, catalogSourceNamespace: str = "openshift-marketplace", config: dict = None): @@ -184,7 +179,7 @@ def deleteSubscription(dynClient: DynamicClient, namespace: str, packageName: st def _findAndDeleteResources(api, resourceType: str, labelSelector: str, namespace: str): resources = api.get(label_selector=labelSelector, namespace=namespace) - if resources.items == 0: + if len(resources.items) == 0: logger.info(f"No matching {resourceType}s to delete") else: for item in resources.items: diff --git a/test/src/test_olm.py b/test/src/test_olm.py index d198c802..0e148388 100644 --- a/test/src/test_olm.py +++ b/test/src/test_olm.py @@ -41,12 +41,26 @@ def test_crud(): assert subscription.metadata.name == "ibm-sls" assert subscription.metadata.namespace == namespace + subscriptionLookup1 = olm.getSubscription(dynClient, namespace, "ibm-sls") + subscriptionLookup2 = olm.getSubscription(dynClient, namespace, "ibm-truststore-mgr") + + assert subscriptionLookup1.metadata.name == "ibm-sls" + assert subscriptionLookup1.metadata.namespace == namespace + assert subscriptionLookup1.spec.channel == "3.x" + assert subscriptionLookup2.metadata.namespace == namespace + assert subscriptionLookup2.spec.channel == "1.x-stable" + # When we install the ibm-sls subscription OLM will automatically create the ibm-truststore-mgr # subscription, but when we delete the subscription, OLM will not automatically remove the latter olm.deleteSubscription(dynClient, namespace, "ibm-sls") olm.deleteSubscription(dynClient, namespace, "ibm-truststore-mgr") ocp.deleteNamespace(dynClient, namespace) + failedSubscriptionLookup1 = olm.getSubscription(dynClient, namespace, "ibm-sls") + failedSubscriptionLookup2 = olm.getSubscription(dynClient, namespace, "ibm-truststore-mgr") + assert failedSubscriptionLookup1 is None + assert failedSubscriptionLookup2 is None + def test_crud_with_config(): namespace = "cli-fvt-2"