OCP Module
The ocp module provides functions for interacting with OpenShift/Kubernetes clusters.
ocp
Functions:
connect(server, token, skipVerify=False)
Connect to a target OpenShift Container Platform (OCP) cluster.
Configures Kubernetes client with the provided server URL and authentication token. Updates the default kubeconfig file with the new cluster context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
str
|
The OpenShift cluster API server URL (e.g., "https://api.cluster.example.com:6443") |
required |
token
|
str
|
The authentication token for cluster access |
required |
skipVerify
|
bool
|
Whether to skip TLS certificate verification. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connection was successful, False if configuration fails |
Raises:
| Type | Description |
|---|---|
ConfigException
|
If the Kubernetes configuration cannot be loaded |
Source code in src/mas/devops/ocp.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
getClusterVersion(dynClient)
Get the current OpenShift cluster version.
Retrieves the completed cluster version from the ClusterVersion custom resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The cluster version string (e.g., "4.12.0"), or None if not found |
Source code in src/mas/devops/ocp.py
isClusterVersionInRange(version, releases)
Check if a cluster version matches any of the specified release versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
The cluster version to check (e.g., "4.12.0") |
required |
releases
|
list[str]
|
List of release version prefixes to match against (e.g., ["4.12", "4.13"]) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the version starts with any of the release prefixes, False otherwise |
Source code in src/mas/devops/ocp.py
getNamespace(dynClient, namespace)
Get a Kubernetes namespace by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
namespace
|
str
|
The name of the namespace to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The namespace resource as a dictionary, or an empty dict if not found |
Source code in src/mas/devops/ocp.py
createNamespace(dynClient, namespace, kyvernoLabel=None)
Create a Kubernetes namespace if it does not already exist.
If the namespace exists and a Kyverno label is provided, the namespace will be patched to include the label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
namespace
|
str
|
The name of the namespace to create |
required |
kyvernoLabel
|
str
|
Value for the 'ibm.com/kyverno' label. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always returns True |
Source code in src/mas/devops/ocp.py
deleteNamespace(dynClient, namespace)
Delete a Kubernetes namespace if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
namespace
|
str
|
The name of the namespace to delete |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always returns True |
Source code in src/mas/devops/ocp.py
waitForCRD(dynClient, crdName)
Wait for a Custom Resource Definition (CRD) to be established and ready.
Polls the CRD status up to 100 times with 5-second intervals (max ~8 minutes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
crdName
|
str
|
The name of the CRD to wait for (e.g., "suites.core.mas.ibm.com") |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the CRD becomes established, False if timeout is reached |
Source code in src/mas/devops/ocp.py
waitForDeployment(dynClient, namespace, deploymentName)
Wait for a Kubernetes Deployment to have at least one ready replica.
Polls the deployment status up to 100 times with 5-second intervals (max ~8 minutes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
namespace
|
str
|
The namespace containing the deployment |
required |
deploymentName
|
str
|
The name of the deployment to wait for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the deployment becomes ready, False if timeout is reached |
Source code in src/mas/devops/ocp.py
getConsoleURL(dynClient)
Get the OpenShift web console URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The HTTPS URL of the OpenShift console (e.g., "https://console-openshift-console.apps.cluster.example.com") |
Source code in src/mas/devops/ocp.py
getNodes(dynClient)
Get all nodes in the cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
dict
|
List of node resources as dictionaries |
Source code in src/mas/devops/ocp.py
getStorageClass(dynClient, name)
Get a specific StorageClass by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
name
|
str
|
The name of the StorageClass to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
StorageClass |
dict | None
|
The StorageClass resource, or None if not found |
Source code in src/mas/devops/ocp.py
getStorageClasses(dynClient)
Get all StorageClasses in the cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of StorageClass resources |
Source code in src/mas/devops/ocp.py
getClusterIssuers(dynClient)
Get all ClusterIssuers in the cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of ClusterIssuers resources or an empty list if no cluster issuers |
Source code in src/mas/devops/ocp.py
getClusterIssuer(dynClient, name)
Get a specific ClusterIssuer by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
name
|
str
|
The name of the ClusterIssuer to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ClusterIssuer |
ResourceInstance | None
|
The ClusterIssuer resource, or None if not found |
Source code in src/mas/devops/ocp.py
getStorageClassVolumeBindingMode(dynClient, storageClassName)
Get the volumeBindingMode for a storage class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift dynamic client |
required |
storageClassName
|
str
|
Name of the storage class |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
"Immediate" or "WaitForFirstConsumer" (defaults to "Immediate" if not found) |
Source code in src/mas/devops/ocp.py
isSNO(dynClient)
Check if the cluster is a Single Node OpenShift (SNO) deployment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the cluster has exactly one node, False otherwise |
Source code in src/mas/devops/ocp.py
crdExists(dynClient, crdName)
Check if a Custom Resource Definition (CRD) exists in the cluster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
crdName
|
str
|
The name of the CRD to check (e.g., "suites.core.mas.ibm.com") |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the CRD exists, False otherwise |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the CRD does not exist (caught and returns False) |
Source code in src/mas/devops/ocp.py
getCR(dynClient, cr_api_version, cr_kind, cr_name, namespace=None)
Get a Custom Resource
Source code in src/mas/devops/ocp.py
getSecret(dynClient, namespace, secret_name)
Get a Secret
Source code in src/mas/devops/ocp.py
applyResource(dynClient, apiVersion, kind, body, namespace=None)
Create or patch a Kubernetes resource.
Mimic the OpenShift dynamic client's apply behavior by creating the resource when it does not exist and patching it when it already exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
Kubernetes dynamic client |
required |
apiVersion
|
str
|
API version for the resource |
required |
kind
|
str
|
Resource kind |
required |
body
|
dict
|
Resource manifest to create or patch |
required |
namespace
|
str
|
Namespace for namespaced resources. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ResourceInstance |
The created or patched resource |
Raises:
| Type | Description |
|---|---|
KeyError
|
If metadata.name is missing from the resource body |
Source code in src/mas/devops/ocp.py
apply_resource(dynClient, resource_yaml, namespace)
Apply a Kubernetes resource from its YAML definition.
Create the resource when it does not exist and patch it when it already exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
Kubernetes dynamic client |
required |
resource_yaml
|
str
|
YAML manifest for the resource |
required |
namespace
|
str
|
Namespace for the resource |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ResourceInstance |
The created or patched resource |
Source code in src/mas/devops/ocp.py
listInstances(dynClient, apiVersion, kind)
Get a list of instances of a particular custom resource on the cluster.
Logs information about each instance found, including name and reconciled version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
apiVersion
|
str
|
The API version of the custom resource (e.g., "core.mas.ibm.com/v1") |
required |
kind
|
str
|
The kind of custom resource (e.g., "Suite") |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of custom resource instances as dictionaries |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the custom resource type is not found |
Source code in src/mas/devops/ocp.py
waitForPVC(dynClient, namespace, pvcName)
Wait for a PersistentVolumeClaim (PVC) to be bound.
Allows up to 10 minutes for a PVC to report successful binding, with increasing retry delays (30s, then 1m, 2m, and 5m intervals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
namespace
|
str
|
The namespace containing the PVC |
required |
pvcName
|
str
|
The name of the PVC to wait for |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the PVC becomes bound, False if timeout is reached |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the PVC is not found (caught and retried) |
Source code in src/mas/devops/ocp.py
execInPod(core_v1_api, pod_name, namespace, command, timeout=60)
Executes a command in a Kubernetes pod and returns the standard output. If running this function from inside a pod (i.e. config.load_incluster_config()), the ServiceAccount assigned to the pod must have the following access in one of the Roles bound to it: rules: - apiGroups: - "" resources: - pods/exec verbs: - create - get - list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
core_v1_api
|
CoreV1Api
|
The Kubernetes API client. |
required |
pod_name
|
str
|
The name of the pod to execute the command in. |
required |
namespace
|
str
|
The namespace of the pod. |
required |
command
|
list
|
The command to execute in the pod. |
required |
timeout
|
int
|
The timeout in seconds for the command execution. Defaults to 60. |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The standard output of the command. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the command execution fails or times out. |
Source code in src/mas/devops/ocp.py
updateGlobalPullSecret(dynClient, registryUrl, username, password)
Update the global pull secret in openshift-config namespace with new registry credentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
registryUrl
|
str
|
Registry URL (e.g., "myregistry.com:5000") |
required |
username
|
str
|
Registry username |
required |
password
|
str
|
Registry password |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Updated secret information |
Source code in src/mas/devops/ocp.py
configureIngressForPathBasedRouting(dynClient, ingressControllerName='default')
Configure OpenShift IngressController for path-based routing.
Sets the namespaceOwnership to InterNamespaceAllowed on the specified IngressController, which is required for path-based routing mode in MAS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dynClient
|
DynamicClient
|
OpenShift Dynamic Client |
required |
ingressControllerName
|
optional
|
Name of the IngressController to configure. Defaults to "default". |
'default'
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if configuration was successful or already configured, False otherwise |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the IngressController resource cannot be found |
Source code in src/mas/devops/ocp.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |