|
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
|
@@ -646,29 +647,79 @@ def _create_oauth_sidecar_object(
|
646 | 647 | )
|
647 | 648 |
|
648 | 649 |
|
649 |
| -def write_components(user_yaml: dict, output_file_name: str): |
| 650 | +def get_default_kueue_name(namespace: str): |
| 651 | + # If the local queue is set, use it. Otherwise, try to use the default queue. |
| 652 | + try: |
| 653 | + config_check() |
| 654 | + api_instance = client.CustomObjectsApi(api_config_handler()) |
| 655 | + local_queues = api_instance.list_namespaced_custom_object( |
| 656 | + group="kueue.x-k8s.io", |
| 657 | + version="v1beta1", |
| 658 | + namespace=namespace, |
| 659 | + plural="localqueues", |
| 660 | + ) |
| 661 | + except Exception as e: # pragma: no cover |
| 662 | + return _kube_api_error_handling(e) |
| 663 | + for lq in local_queues["items"]: |
| 664 | + if ( |
| 665 | + "annotations" in lq["metadata"] |
| 666 | + and "kueue.x-k8s.io/default-queue" in lq["metadata"]["annotations"] |
| 667 | + and lq["metadata"]["annotations"]["kueue.x-k8s.io/default-queue"].lower() |
| 668 | + == "true" |
| 669 | + ): |
| 670 | + return lq["metadata"]["name"] |
| 671 | + raise ValueError( |
| 672 | + "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" |
| 673 | + ) |
| 674 | + |
| 675 | + |
| 676 | +def write_components( |
| 677 | + user_yaml: dict, output_file_name: str, namespace: str, local_queue: Optional[str] |
| 678 | +): |
650 | 679 | # Create the directory if it doesn't exist
|
651 | 680 | directory_path = os.path.dirname(output_file_name)
|
652 | 681 | if not os.path.exists(directory_path):
|
653 | 682 | os.makedirs(directory_path)
|
654 | 683 |
|
655 | 684 | components = user_yaml.get("spec", "resources")["resources"].get("GenericItems")
|
656 | 685 | open(output_file_name, "w").close()
|
| 686 | + lq_name = local_queue or get_default_kueue_name(namespace) |
657 | 687 | with open(output_file_name, "a") as outfile:
|
658 | 688 | for component in components:
|
659 | 689 | if "generictemplate" in component:
|
| 690 | + if ( |
| 691 | + "workload.codeflare.dev/appwrapper" |
| 692 | + in component["generictemplate"]["metadata"]["labels"] |
| 693 | + ): |
| 694 | + del component["generictemplate"]["metadata"]["labels"][ |
| 695 | + "workload.codeflare.dev/appwrapper" |
| 696 | + ] |
| 697 | + labels = component["generictemplate"]["metadata"]["labels"] |
| 698 | + labels.update({"kueue.x-k8s.io/queue-name": lq_name}) |
660 | 699 | outfile.write("---\n")
|
661 | 700 | yaml.dump(
|
662 | 701 | component["generictemplate"], outfile, default_flow_style=False
|
663 | 702 | )
|
664 | 703 | print(f"Written to: {output_file_name}")
|
665 | 704 |
|
666 | 705 |
|
667 |
| -def load_components(user_yaml: dict, name: str): |
| 706 | +def load_components( |
| 707 | + user_yaml: dict, name: str, namespace: str, local_queue: Optional[str] |
| 708 | +): |
668 | 709 | component_list = []
|
669 | 710 | components = user_yaml.get("spec", "resources")["resources"].get("GenericItems")
|
| 711 | + lq_name = local_queue or get_default_kueue_name(namespace) |
670 | 712 | for component in components:
|
671 | 713 | if "generictemplate" in component:
|
| 714 | + if ( |
| 715 | + "workload.codeflare.dev/appwrapper" |
| 716 | + in component["generictemplate"]["metadata"]["labels"] |
| 717 | + ): |
| 718 | + del component["generictemplate"]["metadata"]["labels"][ |
| 719 | + "workload.codeflare.dev/appwrapper" |
| 720 | + ] |
| 721 | + labels = component["generictemplate"]["metadata"]["labels"] |
| 722 | + labels.update({"kueue.x-k8s.io/queue-name": lq_name}) |
672 | 723 | component_list.append(component["generictemplate"])
|
673 | 724 |
|
674 | 725 | resources = "---\n" + "---\n".join(
|
@@ -711,6 +762,7 @@ def generate_appwrapper(
|
711 | 762 | ingress_options: dict,
|
712 | 763 | write_to_file: bool,
|
713 | 764 | verify_tls: bool,
|
| 765 | + local_queue: Optional[str], |
714 | 766 | ):
|
715 | 767 | user_yaml = read_template(template)
|
716 | 768 | appwrapper_name, cluster_name = gen_names(name)
|
@@ -771,18 +823,18 @@ def generate_appwrapper(
|
771 | 823 | if is_openshift_cluster():
|
772 | 824 | enable_openshift_oauth(user_yaml, cluster_name, namespace)
|
773 | 825 |
|
774 |
| - directory_path = os.path.expanduser("~/.codeflare/appwrapper/") |
| 826 | + directory_path = os.path.expanduser("~/.codeflare/resources/") |
775 | 827 | outfile = os.path.join(directory_path, appwrapper_name + ".yaml")
|
776 | 828 |
|
777 | 829 | if write_to_file:
|
778 | 830 | if mcad:
|
779 | 831 | write_user_appwrapper(user_yaml, outfile)
|
780 | 832 | else:
|
781 |
| - write_components(user_yaml, outfile) |
| 833 | + write_components(user_yaml, outfile, namespace, local_queue) |
782 | 834 | return outfile
|
783 | 835 | else:
|
784 | 836 | if mcad:
|
785 | 837 | user_yaml = load_appwrapper(user_yaml, name)
|
786 | 838 | else:
|
787 |
| - user_yaml = load_components(user_yaml, name) |
| 839 | + user_yaml = load_components(user_yaml, name, namespace, local_queue) |
788 | 840 | return user_yaml
|
0 commit comments