From 1269e4fe3ac8c8ee3f19f94141675b8c71dbc81b Mon Sep 17 00:00:00 2001 From: SushilG96 Date: Tue, 14 Jan 2020 19:10:33 +0530 Subject: [lib] Add lib to match PV, Heketi and get gluster vols by name prefix This contains libraries - 1. match_pv_and_heketi_volumes - library for matching heketi volumes and PVCs. 2. heketi_volume_list_by_name_prefix - library to getvolume id and cluster id. 3. match_heketi_and_gluster_volumes_by_prefix - library for matching heketi and gluster volumes. Change-Id: I02af31405a9836000d758a2ffac932be13e52a03 --- .../openshiftstoragelibs/gluster_ops.py | 22 +++++++++++++++ .../openshiftstoragelibs/heketi_ops.py | 32 ++++++++++++++++++++++ .../openshiftstoragelibs/openshift_ops.py | 29 ++++++++++++++++++++ 3 files changed, 83 insertions(+) (limited to 'openshift-storage-libs/openshiftstoragelibs') diff --git a/openshift-storage-libs/openshiftstoragelibs/gluster_ops.py b/openshift-storage-libs/openshiftstoragelibs/gluster_ops.py index b80c587d..422c8a01 100644 --- a/openshift-storage-libs/openshiftstoragelibs/gluster_ops.py +++ b/openshift-storage-libs/openshiftstoragelibs/gluster_ops.py @@ -267,3 +267,25 @@ def get_block_hosting_volume_name(heketi_client_node, heketi_server_url, for vol in gluster_vol_list: if block_hosting_vol_match.group(1).strip() in vol: return vol + + +@podcmd.GlustoPod() +def match_heketi_and_gluster_volumes_by_prefix(heketi_volumes, prefix): + """Match volumes from heketi and gluster using given volume name prefix + + Args: + heketi_volumes (list): List of heketi volumes with which gluster + volumes need to be matched + prefix (str): Volume prefix by which the volumes needs to be filtered + """ + g_vol_list = get_volume_list("auto_get_gluster_endpoint") + g_volumes = [ + g_vol.replace(prefix, "") + for g_vol in g_vol_list if g_vol.startswith(prefix)] + + vol_difference = set(heketi_volumes) ^ set(g_volumes) + err_msg = ("Heketi and Gluster volume list match failed" + "Heketi volumes: {}, Gluster Volumes: {}," + "Difference: {}" + .format(heketi_volumes, g_volumes, vol_difference)) + assert not vol_difference, err_msg diff --git a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py index 6b08b5d5..0ce32632 100644 --- a/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py +++ b/openshift-storage-libs/openshiftstoragelibs/heketi_ops.py @@ -27,6 +27,7 @@ MASTER_NODE = list(g.config["ocp_servers"]["master"].keys())[0] HEKETI_BHV = re.compile(r"Id:(\S+)\s+Cluster:(\S+)\s+Name:(\S+)\s\[block\]") HEKETI_OPERATIONS = re.compile(r"Id:(\S+)\s+Type:(\S+)\s+Status:(\S+)") HEKETI_NODES = re.compile(r"Id:(\S+)\s+Cluster:(\S+)") +HEKETI_VOLUME = r"Id:(\S+)\s+Cluster:(\S+)\s+Name:(%s_\S+)" GET_HEKETI_PODNAME_CMD = ( "oc get pods -l deploymentconfig=%s -o=custom-columns=:.metadata.name " @@ -1884,3 +1885,34 @@ def get_bricks_on_heketi_node( for device in node_info['devices']: bricks += device['bricks'] return bricks + + +def heketi_volume_list_by_name_prefix( + heketi_client_node, heketi_server_url, prefix, **kwargs): + """Get heketi volume id, cluster and name by volume name prefix. + + Args: + heketi_client_node (str): Node on which cmd has to be executed. + heketi_server_url (str): Heketi server url. + prefix (str): Volume name prefix. + + Kwargs: + The keys, values in kwargs are: + - secret : (str)|None + - user : (str)|None + + Returns: + list: Tuple of volume id, cluster id and name + + Raises: + exceptions.AssertionError: If command fails to execute on + heketi server. + """ + # Delete json key from kwargs + kwargs.pop("json", None) + + h_volumes = heketi_volume_list( + heketi_client_node, heketi_server_url, **kwargs) + + vol_regex = re.compile(HEKETI_VOLUME % prefix) + return vol_regex.findall(h_volumes.strip()) diff --git a/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py b/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py index 1835d9ca..07a527da 100644 --- a/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py +++ b/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py @@ -1966,3 +1966,32 @@ def oc_patch(ocp_node, rtype, rname, changes, raise_on_error=True): out = command.cmd_run( cmd, hostname=ocp_node, raise_on_error=raise_on_error) return out or None + + +def match_pv_and_heketi_volumes(hostname, heketi_volumes, pvc_prefix): + """Match heketi volumes and PVs + + Args: + hostname (str): Hostname on which we want to check heketi + volumes and PVCs + heketi_volumes (list): List of heketi volume names + pvc_prefix (str): PVC name prefix given by user at the time + of pvc creation + """ + + custom_columns = [ + r':.spec.claimRef.name', + r':.metadata.annotations."pv\.kubernetes\.io\/provisioned\-by"', + r':.metadata.annotations."gluster\.kubernetes\.io\/heketi-volume\-id"' + ] + pv_volumes = set([ + pv[2] + for pv in oc_get_custom_resource(hostname, "pv", custom_columns) + if pv[0].startswith(pvc_prefix) and pv[1] == "kubernetes.io/glusterfs" + ]) + + vol_diff = pv_volumes ^ set(heketi_volumes) + err_msg = ("PV and Heketi volume list match failed" + "PV: {}, Heketi volumes {}, " + "Difference: {}".format(pv_volumes, heketi_volumes, vol_diff)) + assert not vol_diff, err_msg -- cgit