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 kubectl/oc context with the provided server URL and authentication token.
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 kubectl is not found on the path |
Raises:
| Type | Description |
|---|---|
KubectlNotFoundError
|
If kubectl/oc is not available in the system PATH |
Source code in src/mas/devops/ocp.py
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the ClusterVersion resource cannot be retrieved |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the namespace does not exist |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the namespace resource cannot be accessed |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the namespace does not exist (caught and logged) |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the CRD is not found (caught and retried) |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the deployment is not found (caught and retried) |
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") |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the console route is not found |
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 |
str
|
List of node resources as dictionaries |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If nodes cannot be retrieved |
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 |
str
|
The StorageClass resource, or None if not found |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If the StorageClass does not exist (caught and returns None) |
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 |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If StorageClasses cannot be retrieved |
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
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 |