|
| 1 | +""" |
| 2 | +Copyright 2024 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from ..utils import xpk_exit, xpk_print |
| 18 | +from ..core.kueue import verify_kueuectl |
| 19 | +from .cluster import set_cluster_command |
| 20 | +from ..core.commands import ( |
| 21 | + run_command_for_value, |
| 22 | +) |
| 23 | +from ..core.core import ( |
| 24 | + add_zone_and_project, |
| 25 | +) |
| 26 | +import json |
| 27 | +from tabulate import tabulate |
| 28 | +from argparse import Namespace |
| 29 | + |
| 30 | +table_fmt = 'plain' |
| 31 | + |
| 32 | + |
| 33 | +def info(args: Namespace) -> None: |
| 34 | + """Provide info about localqueues, clusterqueues and their resources. |
| 35 | +
|
| 36 | + Args: |
| 37 | + args: user provided arguments for running the command. |
| 38 | + Returns: |
| 39 | + None |
| 40 | + """ |
| 41 | + add_zone_and_project(args) |
| 42 | + set_cluster_command_code = set_cluster_command(args) |
| 43 | + if set_cluster_command_code != 0: |
| 44 | + xpk_exit(set_cluster_command_code) |
| 45 | + |
| 46 | + verify_kueuectl(args) |
| 47 | + |
| 48 | + lqs = run_kueuectl_list_localqueue(args) |
| 49 | + cqs = run_kueuectl_list_clusterqueue(args) |
| 50 | + aggregate_results(cqs, lqs) |
| 51 | + |
| 52 | + |
| 53 | +def aggregate_results(cqs: list[dict], lqs: list[dict]) -> None: |
| 54 | + """Aggregate listed clusterqueues and localqueues with resource usage and print them as table. |
| 55 | +
|
| 56 | + Args: |
| 57 | + lqs: list of localqueues. |
| 58 | + cqs: list of clusterqueues. |
| 59 | + Returns: |
| 60 | + None |
| 61 | + """ |
| 62 | + try: |
| 63 | + cq_list = json.loads(cqs)['items'] |
| 64 | + except ValueError: |
| 65 | + xpk_print('Incorrect respone from list clusterqueue') |
| 66 | + xpk_print(cqs) |
| 67 | + xpk_exit(1) |
| 68 | + |
| 69 | + try: |
| 70 | + lq_list = json.loads(lqs)['items'] |
| 71 | + except ValueError: |
| 72 | + xpk_print('Incorrect respone from list localqueue') |
| 73 | + xpk_print(lqs) |
| 74 | + xpk_exit(1) |
| 75 | + nominalQuotas = get_nominal_quotas(cq_list) |
| 76 | + cq_usages = parse_queue_lists(cq_list, nominalQuotas) |
| 77 | + lq_usages = parse_queue_lists(lq_list, nominalQuotas) |
| 78 | + |
| 79 | + xpk_print( |
| 80 | + 'Cluster Queues usage \n', |
| 81 | + tabulate(cq_usages, headers='keys', tablefmt=table_fmt), |
| 82 | + ) |
| 83 | + xpk_print( |
| 84 | + 'Local Queues usage \n', |
| 85 | + tabulate(lq_usages, headers='keys', tablefmt=table_fmt), |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def get_nominal_quotas(cqs: list[dict]) -> dict[str, dict[str, str]]: |
| 90 | + """Get quotas from clusterqueues. |
| 91 | + This function retrieves how much of resource in each flavor is assigned to cluster queue. |
| 92 | + It parses flavors of passed cluster queues. |
| 93 | + Args: |
| 94 | + - cqs - list of cluster queues. |
| 95 | + Returns: |
| 96 | + - dictionary of cluster queues resources quotas in format: |
| 97 | + {cq_name:{{flavorName:resourceName}:quota} |
| 98 | + """ |
| 99 | + quotas = {} |
| 100 | + for cq in cqs: |
| 101 | + spec = cq['spec'] |
| 102 | + cq_name = cq['metadata']['name'] |
| 103 | + quotas[cq_name] = {} |
| 104 | + for rg in spec['resourceGroups']: |
| 105 | + for flavor in rg['flavors']: |
| 106 | + name = flavor['name'] |
| 107 | + for resource in flavor['resources']: |
| 108 | + key = f'{name}:{resource["name"]}' |
| 109 | + quotas[cq_name][key] = resource['nominalQuota'] |
| 110 | + return quotas |
| 111 | + |
| 112 | + |
| 113 | +def parse_queue_lists( |
| 114 | + qs: list[dict], |
| 115 | + flavor_resource_quotas: dict, |
| 116 | + reservation_key: str = 'flavorsReservation', |
| 117 | +) -> list[dict]: |
| 118 | + qs_usage_list = [] |
| 119 | + for q in qs: |
| 120 | + queue_name = q['metadata']['name'] |
| 121 | + q_pending_workloads = q['status']['pendingWorkloads'] |
| 122 | + q_admitted_workloads = q['status']['admittedWorkloads'] |
| 123 | + q_status = { |
| 124 | + 'QUEUE': queue_name, |
| 125 | + 'ADMITTED_WORKLOADS': q_admitted_workloads, |
| 126 | + 'PENDING_WORKLOADS': q_pending_workloads, |
| 127 | + } |
| 128 | + q_status.update( |
| 129 | + get_flavors_usage(q, reservation_key, flavor_resource_quotas) |
| 130 | + ) |
| 131 | + qs_usage_list.append(q_status) |
| 132 | + return qs_usage_list |
| 133 | + |
| 134 | + |
| 135 | +def get_flavors_resources_reservations( |
| 136 | + cq_name: str, flavors_res: list[dict] |
| 137 | +) -> dict[str, dict[str, str]]: |
| 138 | + """Get usage of flavors resources. |
| 139 | + This function parser flavorsReservation section of clusterQueue of LocalQueue. |
| 140 | + Args: |
| 141 | + - cq_name - name of ClusterQueue to which flavors belong. |
| 142 | + - flavors_res - list of reservations made by flavors |
| 143 | + Returns: |
| 144 | + Dict containing usage of each resource in flavor for each flavor in cluster or local queue. |
| 145 | + Dict format: {cq_name: {{flavor:resource}:reservation}} |
| 146 | + """ |
| 147 | + reservations = {} |
| 148 | + reservations[cq_name] = {} |
| 149 | + for flavor_name, flavor_resources_reservation_list in flavors_res.items(): |
| 150 | + for resource in flavor_resources_reservation_list: |
| 151 | + reservations[cq_name][f'{flavor_name}:{resource["name"]}'] = resource[ |
| 152 | + 'total' |
| 153 | + ] |
| 154 | + |
| 155 | + return reservations |
| 156 | + |
| 157 | + |
| 158 | +def get_flavors_usage( |
| 159 | + q_entry: dict, res_field: str, flavor_resource_quotas: dict |
| 160 | +) -> list[dict]: |
| 161 | + """Parse q_entry to retrieve list of each resource usage in flavour. |
| 162 | + Args: |
| 163 | + q_entry - single entry into either LocalQueue or ClusterQueue structured as json |
| 164 | + flavor_resource_quotas - nominalQuota of flavors resource usage for each clusterqueue |
| 165 | + Returns: |
| 166 | + list of dicts where each list entry is in format (key, entry) where: |
| 167 | + - key is flavorName:resourceName |
| 168 | + - entry is flavorResourceReservation/flavorResourceQuota |
| 169 | + """ |
| 170 | + status = q_entry['status'] |
| 171 | + flavors_res = status[res_field] |
| 172 | + queue_type = q_entry['kind'] |
| 173 | + |
| 174 | + flavors_res = {flavor['name']: flavor['resources'] for flavor in flavors_res} |
| 175 | + usage_fraction = {} |
| 176 | + cq_name = ( |
| 177 | + q_entry['metadata']['name'] |
| 178 | + if queue_type == 'ClusterQueue' |
| 179 | + else q_entry['spec']['clusterQueue'] |
| 180 | + ) |
| 181 | + |
| 182 | + reservations = get_flavors_resources_reservations(cq_name, flavors_res) |
| 183 | + |
| 184 | + for cq_name, cq_reservations in reservations.items(): |
| 185 | + cq_nominal_quotas = flavor_resource_quotas[cq_name] |
| 186 | + |
| 187 | + for flavor_resource, flavor_resource_quota in cq_nominal_quotas.items(): |
| 188 | + flavor_resource_reservation = cq_reservations[flavor_resource] |
| 189 | + usage_fraction[flavor_resource] = ( |
| 190 | + f'{flavor_resource_reservation}/{flavor_resource_quota}' |
| 191 | + ) |
| 192 | + return usage_fraction |
| 193 | + |
| 194 | + |
| 195 | +def run_kueuectl_list_localqueue(args: Namespace) -> str: |
| 196 | + """Run the kueuectl list localqueue command. |
| 197 | +
|
| 198 | + Args: |
| 199 | + args: user provided arguments for running the command. |
| 200 | +
|
| 201 | + Returns: |
| 202 | + kueuectl list localqueue formatted as json string. |
| 203 | + """ |
| 204 | + command = 'kubectl kueue list localqueue -o json' |
| 205 | + if args.namespace != '': |
| 206 | + command += f' --namespace {args.namespace}' |
| 207 | + return_code, val = run_command_for_value(command, 'list localqueue', args) |
| 208 | + |
| 209 | + if return_code != 0: |
| 210 | + xpk_print(f'Cluster info request returned ERROR {return_code}') |
| 211 | + xpk_exit(return_code) |
| 212 | + return val |
| 213 | + |
| 214 | + |
| 215 | +def run_kueuectl_list_clusterqueue(args: Namespace) -> str: |
| 216 | + """Run the kueuectl list clusterqueue command. |
| 217 | +
|
| 218 | + Args: |
| 219 | + args: user provided arguments for running the command. |
| 220 | +
|
| 221 | + Returns: |
| 222 | + kueuectl list clusterqueue formatted as json string |
| 223 | + """ |
| 224 | + command = 'kubectl kueue list clusterqueue -o json' |
| 225 | + |
| 226 | + return_code, val = run_command_for_value(command, 'list clusterqueue', args) |
| 227 | + |
| 228 | + if return_code != 0: |
| 229 | + xpk_print(f'Cluster info request returned ERROR {return_code}') |
| 230 | + xpk_exit(return_code) |
| 231 | + return val |
0 commit comments