def get_nodes_memory(self): """Get list of all node memory capacities.""" try: nodes = current_k8s_corev1_api_client.list_node() return [ kubernetes_memory_to_bytes(node.status.capacity["memory"]) for node in nodes.items ] except ValueError as e: # FIXME: after new Kubernetes release this should be not needed msg = "Error while retreiving k8s list of nodes." logging.error(msg) logging.error(e) return []
def check_predefined_conditions(): """Check k8s predefined conditions for the nodes.""" try: node_info = current_k8s_corev1_api_client.list_node() for node in node_info.items: # check based on the predefined conditions about the # node status: MemoryPressure, OutOfDisk, KubeletReady # DiskPressure, PIDPressure, for condition in node.status.conditions: if not condition.status: return False except ApiException as e: log.error('Something went wrong while getting node information.') log.error(e) return False return True
def check_predefined_conditions(): """Check Kubernetes predefined conditions for the nodes.""" try: node_info = json.loads( current_k8s_corev1_api_client.list_node( _preload_content=False).data.decode()) for node in node_info["items"]: # check based on the predefined conditions about the # node status: MemoryPressure, OutOfDisk, KubeletReady # DiskPressure, PIDPressure, for condition in node.get("status", {}).get("conditions", {}): if not condition.get("status"): return False except ApiException as e: logging.error("Something went wrong while getting node information.") logging.error(e) return False return True
def get_memory_usage(self): """Get nodes memory usage.""" result = dict() try: nodes = current_k8s_corev1_api_client.list_node() for node in nodes.items: result[node.metadata.name] = { "capacity": node.status.capacity["memory"] } node_metrics = current_k8s_custom_objects_api_client.list_cluster_custom_object( "metrics.k8s.io", "v1beta1", "nodes" ) for node_metric in node_metrics.get("items", []): node_name = node_metric["metadata"]["name"] result[node_name]["usage"] = node_metric["usage"]["memory"] node_capacity = result[node_name]["capacity"] node_usage = result[node_name]["usage"] node_usage_percentage = ClusterHealth.get_percentage( kubernetes_memory_to_bytes(node_usage), kubernetes_memory_to_bytes(node_capacity), ) result[node_name]["percentage"] = f"{node_usage_percentage}%" result[node_name]["available"] = node_capacity - node_usage except ApiException as e: msg = "Error while calling `metrics.k8s.io` API." logging.error(msg) logging.error(e) return {"error": msg} except ValueError as e: # FIXME: after new Kubernetes release this should be not needed msg = "Error while retreiving k8s list of nodes." logging.error(msg) logging.error(e) return {"error": msg} return result
def get_unschedulable_nodes(self): """Get list of node names that are not schedulable.""" nodes = current_k8s_corev1_api_client.list_node( field_selector="spec.unschedulable=true" ) return [node.metadata.name for node in nodes.items]
def get_nodes(self): """Get list of all node names.""" nodes = current_k8s_corev1_api_client.list_node() return [node.metadata.name for node in nodes.items]