sigs.k8s.io/kueue@v0.6.2/site/static/examples/python/sample-queue-control.py (about)

     1  #!/usr/bin/env python3
     2  
     3  import argparse
     4  from kubernetes import config, client
     5  
     6  # sample-queue-control.py
     7  # This will show how to interact with queues
     8  
     9  # Make sure your cluster is running!
    10  config.load_kube_config()
    11  crd_api = client.CustomObjectsApi()
    12  api_client = crd_api.api_client
    13  
    14  
    15  def get_parser():
    16      parser = argparse.ArgumentParser(
    17          description="Interact with Queues e",
    18          formatter_class=argparse.RawTextHelpFormatter,
    19      )
    20      parser.add_argument(
    21          "--namespace",
    22          help="namespace to list for",
    23          default="default",
    24      )
    25      return parser
    26  
    27  
    28  def main():
    29      """
    30      Get a listing of jobs in the queue
    31      """
    32      parser = get_parser()
    33      args, _ = parser.parse_known_args()
    34  
    35      listing = crd_api.list_namespaced_custom_object(
    36          group="kueue.x-k8s.io",
    37          version="v1beta1",
    38          namespace=args.namespace,
    39          plural="localqueues",
    40      )
    41      list_queues(listing)
    42  
    43      listing = crd_api.list_namespaced_custom_object(
    44          group="batch",
    45          version="v1",
    46          namespace=args.namespace,
    47          plural="jobs",
    48      )
    49      list_jobs(listing)
    50  
    51  
    52  def list_jobs(listing):
    53      """
    54      Iterate and show job metadata.
    55      """
    56      if not listing:
    57          print("💼️ There are no jobs.")
    58          return
    59  
    60      print("\n💼️ Jobs")
    61      for job in listing["items"]:
    62          jobname = job["metadata"]["name"]
    63          status = (
    64              "TBA" if "succeeded" not in job["status"] else job["status"]["succeeded"]
    65          )
    66          ready = job["status"]["ready"]
    67          print(f"Found job {jobname}")
    68          print(f"  Succeeded: {status}")
    69          print(f"  Ready: {ready}")
    70  
    71  
    72  def list_queues(listing):
    73      """
    74      Helper function to iterate over and list queues.
    75      """
    76      if not listing:
    77          print("⛑️  There are no queues.")
    78          return
    79  
    80      print("\n⛑️  Local Queues")
    81  
    82      # This is listing queues
    83      for q in listing["items"]:
    84          print(f'Found queue {q["metadata"]["name"]}')
    85          print(f"  Admitted workloads: {q['status']['admittedWorkloads']}")
    86          print(f"  Pending workloads: {q['status']['pendingWorkloads']}")
    87  
    88          # And flavors with resources
    89          for f in q["status"]["flavorUsage"]:
    90              print(f'  Flavor {f["name"]} has resources {f["resources"]}')
    91  
    92  
    93  if __name__ == "__main__":
    94      main()