Skip to content

Commit aa56c69

Browse files
harsh42774Harsh Tamakuwaladurera
authored
[patch] Check for existing OpenShift Pipelines subscription (#23)
Co-authored-by: Harsh Tamakuwala <[email protected]> Co-authored-by: David Parker <[email protected]>
1 parent 0c0fbfd commit aa56c69

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/mas/devops/ocp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,13 @@ def getStorageClasses(dynClient: DynamicClient) -> list:
153153

154154
def isSNO(dynClient: DynamicClient) -> bool:
155155
return len(getNodes(dynClient)) == 1
156+
157+
def crdExists(dynClient: DynamicClient, crdName: str) -> bool:
158+
crdAPI = dynClient.resources.get(api_version="apiextensions.k8s.io/v1", kind="CustomResourceDefinition")
159+
try:
160+
crd = crdAPI.get(name=crdName)
161+
logger.debug(f"CRD does exist: {crdName}")
162+
return True
163+
except NotFoundError:
164+
logger.debug(f"CRD does not exist: {crdName}")
165+
return False

src/mas/devops/tekton.py

Lines changed: 21 additions & 20 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
25+
from .ocp import getConsoleURL, waitForCRD, waitForDeployment, crdExists
2626

2727
logger = logging.getLogger(__name__)
2828

@@ -36,25 +36,26 @@ def installOpenShiftPipelines(dynClient: DynamicClient) -> bool:
3636

3737
# Create the Operator Subscription
3838
try:
39-
manifest = packagemanifestAPI.get(name="openshift-pipelines-operator-rh", namespace="openshift-marketplace")
40-
defaultChannel = manifest.status.defaultChannel
41-
catalogSource = manifest.status.catalogSource
42-
catalogSourceNamespace = manifest.status.catalogSourceNamespace
43-
44-
logger.info(f"OpenShift Pipelines Operator Details: {catalogSourceNamespace}/{catalogSource}@{defaultChannel}")
45-
46-
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
47-
env = Environment(
48-
loader=FileSystemLoader(searchpath=templateDir)
49-
)
50-
template = env.get_template("subscription.yml.j2")
51-
renderedTemplate = template.render(
52-
pipelines_channel=defaultChannel,
53-
pipelines_source=catalogSource,
54-
pipelines_source_namespace=catalogSourceNamespace
55-
)
56-
subscription = yaml.safe_load(renderedTemplate)
57-
subscriptionsAPI.apply(body=subscription, namespace="openshift-operators")
39+
if not crdExists(dynClient, "pipelines.tekton.dev"):
40+
manifest = packagemanifestAPI.get(name="openshift-pipelines-operator-rh", namespace="openshift-marketplace")
41+
defaultChannel = manifest.status.defaultChannel
42+
catalogSource = manifest.status.catalogSource
43+
catalogSourceNamespace = manifest.status.catalogSourceNamespace
44+
45+
logger.info(f"OpenShift Pipelines Operator Details: {catalogSourceNamespace}/{catalogSource}@{defaultChannel}")
46+
47+
templateDir = path.join(path.abspath(path.dirname(__file__)), "templates")
48+
env = Environment(
49+
loader=FileSystemLoader(searchpath=templateDir)
50+
)
51+
template = env.get_template("subscription.yml.j2")
52+
renderedTemplate = template.render(
53+
pipelines_channel=defaultChannel,
54+
pipelines_source=catalogSource,
55+
pipelines_source_namespace=catalogSourceNamespace
56+
)
57+
subscription = yaml.safe_load(renderedTemplate)
58+
subscriptionsAPI.apply(body=subscription, namespace="openshift-operators")
5859

5960
except NotFoundError:
6061
logger.warning("Error: Couldn't find package manifest for Red Hat Openshift Pipelines Operator")

0 commit comments

Comments
 (0)