|
17 | 17 | (in the cluster sub-module) for AppWrapper generation. |
18 | 18 | """ |
19 | 19 |
|
| 20 | +from typing import Optional |
20 | 21 | import typing |
21 | 22 | import yaml |
22 | 23 | import sys |
@@ -460,29 +461,79 @@ def _create_oauth_sidecar_object( |
460 | 461 | ) |
461 | 462 |
|
462 | 463 |
|
463 | | -def write_components(user_yaml: dict, output_file_name: str): |
| 464 | +def get_default_kueue_name(namespace: str): |
| 465 | + # If the local queue is set, use it. Otherwise, try to use the default queue. |
| 466 | + try: |
| 467 | + config_check() |
| 468 | + api_instance = client.CustomObjectsApi(api_config_handler()) |
| 469 | + local_queues = api_instance.list_namespaced_custom_object( |
| 470 | + group="kueue.x-k8s.io", |
| 471 | + version="v1beta1", |
| 472 | + namespace=namespace, |
| 473 | + plural="localqueues", |
| 474 | + ) |
| 475 | + except Exception as e: # pragma: no cover |
| 476 | + return _kube_api_error_handling(e) |
| 477 | + for lq in local_queues["items"]: |
| 478 | + if ( |
| 479 | + "annotations" in lq["metadata"] |
| 480 | + and "kueue.x-k8s.io/default-queue" in lq["metadata"]["annotations"] |
| 481 | + and lq["metadata"]["annotations"]["kueue.x-k8s.io/default-queue"].lower() |
| 482 | + == "true" |
| 483 | + ): |
| 484 | + return lq["metadata"]["name"] |
| 485 | + raise ValueError( |
| 486 | + "Default Local Queue with kueue.x-k8s.io/default-queue: true annotation not found please create a default Local Queue or provide the local_queue name in Cluster Configuration" |
| 487 | + ) |
| 488 | + |
| 489 | + |
| 490 | +def write_components( |
| 491 | + user_yaml: dict, output_file_name: str, namespace: str, local_queue: Optional[str] |
| 492 | +): |
464 | 493 | # Create the directory if it doesn't exist |
465 | 494 | directory_path = os.path.dirname(output_file_name) |
466 | 495 | if not os.path.exists(directory_path): |
467 | 496 | os.makedirs(directory_path) |
468 | 497 |
|
469 | 498 | components = user_yaml.get("spec", "resources")["resources"].get("GenericItems") |
470 | 499 | open(output_file_name, "w").close() |
| 500 | + lq_name = local_queue or get_default_kueue_name(namespace) |
471 | 501 | with open(output_file_name, "a") as outfile: |
472 | 502 | for component in components: |
473 | 503 | if "generictemplate" in component: |
| 504 | + if ( |
| 505 | + "workload.codeflare.dev/appwrapper" |
| 506 | + in component["generictemplate"]["metadata"]["labels"] |
| 507 | + ): |
| 508 | + del component["generictemplate"]["metadata"]["labels"][ |
| 509 | + "workload.codeflare.dev/appwrapper" |
| 510 | + ] |
| 511 | + labels = component["generictemplate"]["metadata"]["labels"] |
| 512 | + labels.update({"kueue.x-k8s.io/queue-name": lq_name}) |
474 | 513 | outfile.write("---\n") |
475 | 514 | yaml.dump( |
476 | 515 | component["generictemplate"], outfile, default_flow_style=False |
477 | 516 | ) |
478 | 517 | print(f"Written to: {output_file_name}") |
479 | 518 |
|
480 | 519 |
|
481 | | -def load_components(user_yaml: dict, name: str): |
| 520 | +def load_components( |
| 521 | + user_yaml: dict, name: str, namespace: str, local_queue: Optional[str] |
| 522 | +): |
482 | 523 | component_list = [] |
483 | 524 | components = user_yaml.get("spec", "resources")["resources"].get("GenericItems") |
| 525 | + lq_name = local_queue or get_default_kueue_name(namespace) |
484 | 526 | for component in components: |
485 | 527 | if "generictemplate" in component: |
| 528 | + if ( |
| 529 | + "workload.codeflare.dev/appwrapper" |
| 530 | + in component["generictemplate"]["metadata"]["labels"] |
| 531 | + ): |
| 532 | + del component["generictemplate"]["metadata"]["labels"][ |
| 533 | + "workload.codeflare.dev/appwrapper" |
| 534 | + ] |
| 535 | + labels = component["generictemplate"]["metadata"]["labels"] |
| 536 | + labels.update({"kueue.x-k8s.io/queue-name": lq_name}) |
486 | 537 | component_list.append(component["generictemplate"]) |
487 | 538 |
|
488 | 539 | resources = "---\n" + "---\n".join( |
@@ -523,6 +574,7 @@ def generate_appwrapper( |
523 | 574 | priority_val: int, |
524 | 575 | write_to_file: bool, |
525 | 576 | verify_tls: bool, |
| 577 | + local_queue: Optional[str], |
526 | 578 | ): |
527 | 579 | user_yaml = read_template(template) |
528 | 580 | appwrapper_name, cluster_name = gen_names(name) |
@@ -575,18 +627,18 @@ def generate_appwrapper( |
575 | 627 | if is_openshift_cluster(): |
576 | 628 | enable_openshift_oauth(user_yaml, cluster_name, namespace) |
577 | 629 |
|
578 | | - directory_path = os.path.expanduser("~/.codeflare/appwrapper/") |
| 630 | + directory_path = os.path.expanduser("~/.codeflare/resources/") |
579 | 631 | outfile = os.path.join(directory_path, appwrapper_name + ".yaml") |
580 | 632 |
|
581 | 633 | if write_to_file: |
582 | 634 | if mcad: |
583 | 635 | write_user_appwrapper(user_yaml, outfile) |
584 | 636 | else: |
585 | | - write_components(user_yaml, outfile) |
| 637 | + write_components(user_yaml, outfile, namespace, local_queue) |
586 | 638 | return outfile |
587 | 639 | else: |
588 | 640 | if mcad: |
589 | 641 | user_yaml = load_appwrapper(user_yaml, name) |
590 | 642 | else: |
591 | | - user_yaml = load_components(user_yaml, name) |
| 643 | + user_yaml = load_components(user_yaml, name, namespace, local_queue) |
592 | 644 | return user_yaml |
0 commit comments