def update_system_nodes() -> None: k8s_manager = K8SManager(in_cluster=True) nodes = k8s_manager.list_nodes() cluster = Cluster.load() nodes_to_update = {} nodes_to_create = {node.metadata.name: node for node in nodes} deprecated_nodes = [] for node in cluster.nodes.all(): if node.name in nodes_to_create: nodes_to_update[node.name] = (node, nodes_to_create.pop(node.name)) elif node.is_current: deprecated_nodes.append(node) cluster_updated = False for node in deprecated_nodes: node.is_current = False node.save() cluster_updated = True auditor.record(event_type=CLUSTER_NODE_DELETED, instance=node) for node in nodes_to_create.values(): node_dict = ClusterNode.from_node_item(node) node_dict['cluster'] = cluster instance = ClusterNode.objects.create(**node_dict) cluster_updated = True auditor.record(event_type=CLUSTER_NODE_CREATED, instance=instance) for current_node, new_node in nodes_to_update.values(): node_dict = ClusterNode.from_node_item(new_node) node_updated = False for k, v in node_dict.items(): if v != getattr(current_node, k): setattr(current_node, k, v) node_updated = True if not current_node.is_current: current_node.is_current = True node_updated = True if node_updated: current_node.save() cluster_updated = True auditor.record(event_type=CLUSTER_NODE_UPDATED, instance=current_node) if cluster_updated: cluster = get_cluster_resources() auditor.record(event_type=CLUSTER_RESOURCES_UPDATED, instance=cluster, n_nodes=cluster.n_nodes, memory=round(cluster.memory / (1000**3), 2), n_cpus=cluster.n_cpus, n_gpus=cluster.n_gpus)
def update_system_nodes(): k8s_manager = K8SManager(in_cluster=True) nodes = k8s_manager.list_nodes() cluster = Cluster.load() nodes_to_update = {} nodes_to_create = {node.metadata.name: node for node in nodes} deprecated_nodes = [] for node in cluster.nodes.all(): if node.name in nodes_to_create: nodes_to_update[node.name] = (node, nodes_to_create.pop(node.name)) else: deprecated_nodes.append(node) cluster_updated = False for node in deprecated_nodes: node.is_current = False node.save() cluster_updated = True auditor.record(event_type=CLUSTER_NODE_DELETED, instance=node) for node in nodes_to_create.values(): node_dict = ClusterNode.from_node_item(node) node_dict['cluster'] = cluster instance = ClusterNode.objects.create(**node_dict) cluster_updated = True auditor.record(event_type=CLUSTER_NODE_CREATED, instance=instance) for current_node, new_node in nodes_to_update.values(): node_dict = ClusterNode.from_node_item(new_node) node_updated = False for k, v in node_dict.items(): if v != getattr(current_node, k): setattr(current_node, k, v) node_updated = True if node_updated: current_node.save() cluster_updated = True auditor.record(event_type=CLUSTER_NODE_UPDATED, instance=current_node) if cluster_updated: cluster = get_cluster_resources() auditor.record(event_type=CLUSTER_RESOURCES_UPDATED, instance=cluster, n_nodes=cluster.n_nodes, memory=round(cluster.memory / (1000 ** 3), 2), n_cpus=cluster.n_cpus, n_gpus=cluster.n_gpus)