From 5d17159997a0c4afa57fb7bc77e8db6e3aef8596 Mon Sep 17 00:00:00 2001 From: Sanjay Prabhakar Date: Tue, 9 Dec 2025 14:38:58 +0000 Subject: [PATCH 1/3] [patch] added ocp functions --- src/mas/devops/ocp.py | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/mas/devops/ocp.py b/src/mas/devops/ocp.py index 4219c5cc..3ebf0da6 100644 --- a/src/mas/devops/ocp.py +++ b/src/mas/devops/ocp.py @@ -242,6 +242,59 @@ def crdExists(dynClient: DynamicClient, crdName: str) -> bool: logger.debug(f"CRD does not exist: {crdName}") return False +def getCR(dynClient: DynamicClient, cr_api_version: str, cr_kind: str, cr_name: str, namespace: str = None) -> dict: + """ + Get a Custom Resource + """ + + try: + crAPI = dynClient.resources.get(api_version=cr_api_version, kind=cr_kind) + if namespace: + cr = crAPI.get(name=cr_name, namespace=namespace) + else: + cr = crAPI.get(name=cr_name) + return cr + except NotFoundError: + logger.debug(f"CR {cr_name} of kind {cr_kind} does not exist in namespace {namespace}") + + return {} + +def getSecret(dynClient: DynamicClient, namespace: str, secret_name: str) -> dict: + """ + Get a Secret + """ + try: + secretAPI = dynClient.resources.get(api_version="v1", kind="Secret") + secret = secretAPI.get(name=secret_name, namespace=namespace) + logger.debug(f"Secret {secret_name} exists in namespace {namespace}") + return secret.to_dict() + except NotFoundError: + logger.debug(f"Secret {secret_name} does not exist in namespace {namespace}") + return {} + +def apply_resource(dynClient: DynamicClient, resource_yaml: str, namespace: str): + """ + Apply a Kubernetes resource from its YAML definition. + If the resource already exists, it will be updated. + If it does not exist, it will be created. + """ + resource_dict = yaml.safe_load(resource_yaml) + kind = resource_dict['kind'] + api_version = resource_dict['apiVersion'] + metadata = resource_dict['metadata'] + name = metadata['name'] + + try: + resource = dynClient.resources.get(api_version=api_version, kind=kind) + # Try to get the existing resource + existing_resource = resource.get(name=name, namespace=namespace) + # If found, update it + logger.debug(f"Updating existing {kind} '{name}' in namespace '{namespace}'") + resource.patch(body=resource_dict, namespace=namespace, name=name) + except NotFoundError: + # If not found, create it + logger.debug(f"Creating new {kind} '{name}' in namespace '{namespace}'") + resource.create(body=resource_dict, namespace=namespace) def listInstances(dynClient: DynamicClient, apiVersion: str, kind: str) -> list: """ From 50f9ccc9b9d60834c5c12946c741f36023c9a442 Mon Sep 17 00:00:00 2001 From: Sanjay Prabhakar Date: Tue, 9 Dec 2025 14:53:14 +0000 Subject: [PATCH 2/3] [patch] fix unused var --- src/mas/devops/ocp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mas/devops/ocp.py b/src/mas/devops/ocp.py index 3ebf0da6..4fb2b55d 100644 --- a/src/mas/devops/ocp.py +++ b/src/mas/devops/ocp.py @@ -287,7 +287,7 @@ def apply_resource(dynClient: DynamicClient, resource_yaml: str, namespace: str) try: resource = dynClient.resources.get(api_version=api_version, kind=kind) # Try to get the existing resource - existing_resource = resource.get(name=name, namespace=namespace) + resource.get(name=name, namespace=namespace) # If found, update it logger.debug(f"Updating existing {kind} '{name}' in namespace '{namespace}'") resource.patch(body=resource_dict, namespace=namespace, name=name) From 5d4ec46cd140e92ca0ba170b8ade9717306fb0ff Mon Sep 17 00:00:00 2001 From: Sanjay Prabhakar Date: Tue, 9 Dec 2025 15:00:07 +0000 Subject: [PATCH 3/3] [patch] fix autopep8 complaints --- src/mas/devops/ocp.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mas/devops/ocp.py b/src/mas/devops/ocp.py index 4fb2b55d..50d24530 100644 --- a/src/mas/devops/ocp.py +++ b/src/mas/devops/ocp.py @@ -242,6 +242,7 @@ def crdExists(dynClient: DynamicClient, crdName: str) -> bool: logger.debug(f"CRD does not exist: {crdName}") return False + def getCR(dynClient: DynamicClient, cr_api_version: str, cr_kind: str, cr_name: str, namespace: str = None) -> dict: """ Get a Custom Resource @@ -259,6 +260,7 @@ def getCR(dynClient: DynamicClient, cr_api_version: str, cr_kind: str, cr_name: return {} + def getSecret(dynClient: DynamicClient, namespace: str, secret_name: str) -> dict: """ Get a Secret @@ -272,6 +274,7 @@ def getSecret(dynClient: DynamicClient, namespace: str, secret_name: str) -> dic logger.debug(f"Secret {secret_name} does not exist in namespace {namespace}") return {} + def apply_resource(dynClient: DynamicClient, resource_yaml: str, namespace: str): """ Apply a Kubernetes resource from its YAML definition. @@ -296,6 +299,7 @@ def apply_resource(dynClient: DynamicClient, resource_yaml: str, namespace: str) logger.debug(f"Creating new {kind} '{name}' in namespace '{namespace}'") resource.create(body=resource_dict, namespace=namespace) + def listInstances(dynClient: DynamicClient, apiVersion: str, kind: str) -> list: """ Get a list of instances of a particular CR on the cluster