def get_running_state_pods(namespace=defaults.ROOK_CLUSTER_NAMESPACE): """ Checks the running state pods in a given namespace. Returns: List: all the pod objects that are in running state only """ list_of_pods = get_all_pods(namespace) ocp_pod_obj = OCP(kind=constants.POD, namespace=namespace) running_pods_object = list() for pod in list_of_pods: status = ocp_pod_obj.get_resource(pod.name, 'STATUS') if "Running" in status: running_pods_object.append(pod) return running_pods_object
def get_pod_restarts_count(namespace=defaults.ROOK_CLUSTER_NAMESPACE): """ Gets the dictionary of pod and its restart count for all the pods in a given namespace Returns: dict: dictionary of pod name and its corresponding restart count """ list_of_pods = get_all_pods(namespace) restart_dict = {} ocp_pod_obj = OCP(kind=constants.POD, namespace=namespace) for p in list_of_pods: # we don't want to compare osd-prepare and canary pods as they get created freshly when an osd need to be added. if "rook-ceph-osd-prepare" not in p.name and "rook-ceph-drain-canary" not in p.name: restart_dict[p.name] = int( ocp_pod_obj.get_resource(p.name, 'RESTARTS')) logging.info(f"get_pod_restarts_count: restarts dict = {restart_dict}") return restart_dict
def check_pods_in_running_state(namespace=defaults.ROOK_CLUSTER_NAMESPACE): """ checks whether all the pods in a given namespace are in Running state or not Returns: Boolean: True, if all pods in Running state. False, otherwise """ ret_val = True list_of_pods = get_all_pods(namespace) ocp_pod_obj = OCP(kind=constants.POD, namespace=namespace) for p in list_of_pods: # we don't want to compare osd-prepare and canary pods as they get created freshly when an osd need to be added. if "rook-ceph-osd-prepare" not in p.name and "rook-ceph-drain-canary" not in p.name: status = ocp_pod_obj.get_resource(p.name, 'STATUS') if status not in "Running": logging.error( f"The pod {p.name} is in {status} state. Expected = Running" ) ret_val = False return ret_val
def setup(self, kv_version, request): """ Setup csi-kms-connection-details configmap """ # Initialize Vault self.vault = kms.Vault() self.vault.gather_init_vault_conf() self.vault.update_vault_env_vars() # Check if cert secrets already exist, if not create cert resources ocp_obj = OCP(kind="secret", namespace=constants.OPENSHIFT_STORAGE_NAMESPACE) try: ocp_obj.get_resource(resource_name="ocs-kms-ca-secret", column="NAME") except CommandFailed as cfe: if "not found" not in str(cfe): raise else: self.vault.create_ocs_vault_cert_resources() # Create vault namespace, backend path and policy in vault self.vault_resource_name = create_unique_resource_name("test", "vault") self.vault.vault_create_namespace(namespace=self.vault_resource_name) self.vault.vault_create_backend_path( backend_path=self.vault_resource_name, kv_version=kv_version ) self.vault.vault_create_policy(policy_name=self.vault_resource_name) ocp_obj = OCP(kind="configmap", namespace=constants.OPENSHIFT_STORAGE_NAMESPACE) # If csi-kms-connection-details exists, edit the configmap to add new vault config try: ocp_obj.get_resource( resource_name="csi-kms-connection-details", column="NAME" ) self.new_kmsid = self.vault_resource_name vdict = defaults.VAULT_CSI_CONNECTION_CONF for key in vdict.keys(): old_key = key vdict[self.new_kmsid] = vdict.pop(old_key) vdict[self.new_kmsid]["VAULT_BACKEND_PATH"] = self.vault_resource_name vdict[self.new_kmsid]["VAULT_NAMESPACE"] = self.vault_resource_name # Workaround for BZ-1997624 if kv_version == "v1": vdict[self.new_kmsid]["VAULT_BACKEND"] = "kv" else: vdict[self.new_kmsid]["VAULT_BACKEND"] = "kv-v2" kms.update_csi_kms_vault_connection_details(vdict) except CommandFailed as cfe: if "not found" not in str(cfe): raise else: self.new_kmsid = "1-vault" self.vault.create_vault_csi_kms_connection_details( kv_version=kv_version ) def finalizer(): # Remove the vault config from csi-kms-connection-details configMap if len(kms.get_encryption_kmsid()) > 1: kms.remove_kmsid(self.new_kmsid) # Delete the resources in vault self.vault.remove_vault_backend_path() self.vault.remove_vault_policy() self.vault.remove_vault_namespace() request.addfinalizer(finalizer)
class OCS(object): """ Base OCSClass """ def __init__(self, **kwargs): """ Initializer function Args: kwargs (dict): 1) For existing resource, use OCP.reload() to get the resource's dictionary and use it to pass as **kwargs 2) For new resource, use yaml files templates under /templates/CSI like: obj_dict = load_yaml( os.path.join( TEMPLATE_DIR, "some_resource.yaml" ) ) """ self.data = kwargs self._api_version = self.data.get('api_version') self._kind = self.data.get('kind') self._namespace = None if 'metadata' in self.data: self._namespace = self.data.get('metadata').get('namespace') self._name = self.data.get('metadata').get('name') self.ocp = OCP(api_version=self._api_version, kind=self.kind, namespace=self._namespace) with tempfile.NamedTemporaryFile(mode='w+', prefix=self._kind, delete=False) as temp_file_info: self.temp_yaml = temp_file_info.name # This _is_delete flag is set to True if the delete method was called # on object of this class and was successfull. self._is_deleted = False @property def api_version(self): return self._api_version @property def kind(self): return self._kind @property def namespace(self): return self._namespace @property def name(self): return self._name @property def is_deleted(self): return self._is_deleted def reload(self): """ Reloading the OCS instance with the new information from its actual data. After creating a resource from a yaml file, the actual yaml file is being changed and more information about the resource is added. """ self.data = self.get() self.__init__(**self.data) def get(self, out_yaml_format=True): return self.ocp.get(resource_name=self.name, out_yaml_format=out_yaml_format) def status(self): return self.ocp.get_resource(self.name, 'STATUS') def describe(self): return self.ocp.describe(resource_name=self.name) def create(self, do_reload=True): log.info(f"Adding {self.kind} with name {self.name}") templating.dump_data_to_temp_yaml(self.data, self.temp_yaml) status = self.ocp.create(yaml_file=self.temp_yaml) if do_reload: self.reload() return status def delete(self, wait=True, force=False): """ Delete the OCS object if its not already deleted (using the internal is_deleted flag) Args: wait (bool): Wait for object to be deleted force (bool): Force delete object Returns: bool: True if deleted, False otherwise """ # Avoid accidental delete of default storageclass and secret if (self.name == constants.DEFAULT_STORAGECLASS_CEPHFS or self.name == constants.DEFAULT_STORAGECLASS_RBD): log.info("Attempt to delete default Secret or StorageClass") return if self._is_deleted: log.info(f"Attempt to remove resource: {self.name} which is" f"already deleted! Skipping delete of this resource!") result = True else: result = self.ocp.delete(resource_name=self.name, wait=wait, force=force) self._is_deleted = True return result def apply(self, **data): with open(self.temp_yaml, 'w') as yaml_file: yaml.dump(data, yaml_file) assert self.ocp.apply( yaml_file=self.temp_yaml), (f"Failed to apply changes {data}") self.reload() def add_label(self, label): """ Addss a new label Args: label (str): New label to be assigned for this pod E.g: "label=app='rook-ceph-mds'" """ status = self.ocp.add_label(resource_name=self.name, label=label) self.reload() return status def delete_temp_yaml_file(self): utils.delete_file(self.temp_yaml) def __getstate__(self): """ unset attributes for serializing the object """ self_dict = self.__dict__ del self.temp_yaml return self_dict def __setstate__(self, d): """ reset attributes for serializing the object """ self.temp_yaml = None self.__dict__.update(d)