Skip to content

Commit 794d77e

Browse files
committed
[minor] Allow installplanapproval and startingcsv to be set on subscription
1 parent c85db82 commit 794d77e

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

src/mas/devops/olm.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
from time import sleep
1313
from os import path
14+
from typing import Optional
1415

1516
from kubernetes.dynamic.exceptions import NotFoundError
1617
from openshift.dynamic import DynamicClient
@@ -117,7 +118,7 @@ def getSubscription(dynClient: DynamicClient, namespace: str, packageName: str):
117118
return subscriptions.items[0]
118119

119120

120-
def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str, packageChannel: str = None, catalogSource: str = None, catalogSourceNamespace: str = "openshift-marketplace", config: dict = None, installMode: str = "OwnNamespace"):
121+
def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str, packageChannel: Optional[str] = None, catalogSource: Optional[str] = None, catalogSourceNamespace: str = "openshift-marketplace", config: Optional[dict] = None, installMode: str = "OwnNamespace", installPlanApproval: Optional[str] = None, startingCSV: Optional[str] = None):
121122
"""
122123
Create or update an operator subscription in a namespace.
123124
@@ -133,6 +134,8 @@ def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str
133134
catalogSourceNamespace (str, optional): Namespace of the catalog source. Defaults to "openshift-marketplace".
134135
config (dict, optional): Additional subscription configuration. Defaults to None.
135136
installMode (str, optional): Install mode for the OperatorGroup. Defaults to "OwnNamespace".
137+
installPlanApproval (str, optional): Install plan approval mode ("Automatic" or "Manual"). Defaults to None.
138+
startingCSV (str, optional): The specific CSV version to install. Defaults to None.
136139
137140
Returns:
138141
Subscription: The created or updated subscription resource
@@ -190,7 +193,9 @@ def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str
190193
package_name=packageName,
191194
package_channel=packageChannel,
192195
catalog_name=catalogSource,
193-
catalog_namespace=catalogSourceNamespace
196+
catalog_namespace=catalogSourceNamespace,
197+
install_plan_approval=installPlanApproval,
198+
starting_csv=startingCSV
194199
)
195200
subscription = yaml.safe_load(renderedTemplate)
196201
subscriptionsAPI.apply(body=subscription, namespace=namespace)
@@ -208,8 +213,8 @@ def applySubscription(dynClient: DynamicClient, namespace: str, packageName: str
208213
raise OLMException(f"Found 0 InstallPlans for {packageName}")
209214
elif len(installPlanResources.items) > 1:
210215
logger.warning(f"More than 1 InstallPlan found for {packageName}")
211-
else:
212-
installPlanName = installPlanResources.items[0].metadata.name
216+
217+
installPlanName = installPlanResources.items[0].metadata.name
213218

214219
# Wait for InstallPlan to complete
215220
logger.debug(f"Waiting for InstallPlan {installPlanName}")

src/mas/devops/templates/subscription.yml.j2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ spec:
99
channel: {{ package_channel }}
1010
source: {{ catalog_name }}
1111
sourceNamespace: {{ catalog_namespace }}
12+
{%- if install_plan_approval is not none %}
13+
installPlanApproval: {{ install_plan_approval }}
14+
{%- endif %}
15+
{%- if starting_csv is not none %}
16+
startingCSV: {{ starting_csv }}
17+
{%- endif %}
1218
{%- if subscription_config is not none %}
1319
config: {{ subscription_config }}
1420
{%- endif %}

test/src/test_olm.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,67 @@ def test_crud_with_config():
7979
olm.deleteSubscription(dynClient, namespace, "ibm-sls")
8080
olm.deleteSubscription(dynClient, namespace, "ibm-truststore-mgr")
8181
ocp.deleteNamespace(dynClient, namespace)
82+
83+
84+
def test_crud_with_manual_approval():
85+
namespace = "cli-fvt-3"
86+
subscription = olm.applySubscription(
87+
dynClient,
88+
namespace,
89+
"ibm-sls",
90+
packageChannel="3.x",
91+
installPlanApproval="Manual"
92+
)
93+
assert subscription.metadata.name == "ibm-sls"
94+
assert subscription.metadata.namespace == namespace
95+
assert subscription.spec.installPlanApproval == "Manual"
96+
97+
# When we install the ibm-sls subscription OLM will automatically create the ibm-truststore-mgr
98+
# subscription, but when we delete the subscription, OLM will not automatically remove the latter
99+
olm.deleteSubscription(dynClient, namespace, "ibm-sls")
100+
olm.deleteSubscription(dynClient, namespace, "ibm-truststore-mgr")
101+
ocp.deleteNamespace(dynClient, namespace)
102+
103+
104+
def test_crud_with_starting_csv():
105+
namespace = "cli-fvt-4"
106+
# Note: This test assumes a specific CSV version exists in the catalog
107+
# You may need to adjust the version based on what's available
108+
subscription = olm.applySubscription(
109+
dynClient,
110+
namespace,
111+
"ibm-sls",
112+
packageChannel="3.x",
113+
startingCSV="ibm-sls.v3.8.0"
114+
)
115+
assert subscription.metadata.name == "ibm-sls"
116+
assert subscription.metadata.namespace == namespace
117+
assert subscription.spec.startingCSV == "ibm-sls.v3.8.0"
118+
119+
# When we install the ibm-sls subscription OLM will automatically create the ibm-truststore-mgr
120+
# subscription, but when we delete the subscription, OLM will not automatically remove the latter
121+
olm.deleteSubscription(dynClient, namespace, "ibm-sls")
122+
olm.deleteSubscription(dynClient, namespace, "ibm-truststore-mgr")
123+
ocp.deleteNamespace(dynClient, namespace)
124+
125+
126+
def test_crud_with_manual_approval_and_starting_csv():
127+
namespace = "cli-fvt-5"
128+
subscription = olm.applySubscription(
129+
dynClient,
130+
namespace,
131+
"ibm-sls",
132+
packageChannel="3.x",
133+
installPlanApproval="Manual",
134+
startingCSV="ibm-sls.v3.8.0"
135+
)
136+
assert subscription.metadata.name == "ibm-sls"
137+
assert subscription.metadata.namespace == namespace
138+
assert subscription.spec.installPlanApproval == "Manual"
139+
assert subscription.spec.startingCSV == "ibm-sls.v3.8.0"
140+
141+
# When we install the ibm-sls subscription OLM will automatically create the ibm-truststore-mgr
142+
# subscription, but when we delete the subscription, OLM will not automatically remove the latter
143+
olm.deleteSubscription(dynClient, namespace, "ibm-sls")
144+
olm.deleteSubscription(dynClient, namespace, "ibm-truststore-mgr")
145+
ocp.deleteNamespace(dynClient, namespace)

0 commit comments

Comments
 (0)