Skip to content

Commit

Permalink
add function for listing lqs by flavors
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin <[email protected]>
  • Loading branch information
KPostOffice committed Oct 16, 2024
1 parent 7694218 commit c406d96
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
20 changes: 20 additions & 0 deletions src/codeflare_sdk/common/kubernetes_cluster/kube_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import executing
from kubernetes import client, config
from urllib3.util import parse_url
import os


# private methods
Expand Down Expand Up @@ -49,3 +50,22 @@ def _kube_api_error_handling(
elif e.reason == "Conflict":
raise FileExistsError(exists_msg)
raise e


def get_current_namespace(): # pragma: no cover
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
try:
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
active_context = file.readline().strip("\n")
return active_context
except Exception as e:
print("Unable to find current namespace")
print("trying to gather from current context")
try:
_, active_context = config.list_kube_config_contexts(config_check())
except Exception as e:
return _kube_api_error_handling(e)
try:
return active_context["context"]["namespace"]
except KeyError:
return None
50 changes: 49 additions & 1 deletion src/codeflare_sdk/common/kueue/kueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional
from typing import Optional, List
from codeflare_sdk.common import _kube_api_error_handling
from codeflare_sdk.common.kubernetes_cluster.auth import config_check, get_api_client
from kubernetes import client
from kubernetes.client.exceptions import ApiException

from codeflare_sdk.common.kubernetes_cluster.kube_api_helpers import (
get_current_namespace,
)


def get_default_kueue_name(namespace: str):
# If the local queue is set, use it. Otherwise, try to use the default queue.
Expand Down Expand Up @@ -45,6 +49,50 @@ def get_default_kueue_name(namespace: str):
return lq["metadata"]["name"]


def list_local_queues(
namespace: Optional[str] = None, flavors: Optional[List[str]] = None
) -> List[dict]:
"""
This function lists all local queues in the namespace provided.
If no namespace is provided, it will use the current namespace. If flavors is provided, it will only return local
queues that support all the flavors provided.
Note:
Depending on the version of the local queue API, the available flavors may not be present in the response.
Args:
namespace (str, optional): The namespace to list local queues from. Defaults to None.
flavors (List[str], optional): The flavors to filter local queues by. Defaults to None.
Returns:
List[dict]: A list of dictionaries containing the name of the local queue and the available flavors
"""
if namespace is None:
namespace = get_current_namespace()
try:
config_check()
api_instance = client.CustomObjectsApi(get_api_client())
local_queues = api_instance.list_namespace_custom_object(
group="kueue.x-k8s.io",
version="v1beta1",
namespace=namespace,
plural="localqueues",
)
except ApiException as e:
return _kube_api_error_handling(e)
to_return = []
for lq in local_queues["items"]:
item = {"name": lq["metadata"]["name"]}
if "flavors" in lq["status"]:
item["flavors"] = [f["name"] for f in lq["status"]["flavors"]]
if flavors is not None and not set(flavors).issubset(set(item["flavors"])):
continue
elif flavors is not None:
continue # NOTE: may be indicative old local queue API and might be worth while raising or warning here
to_return.append(item)
return to_return


def local_queue_exists(namespace: str, local_queue_name: str):
# get all local queues in the namespace
try:
Expand Down
5 changes: 3 additions & 2 deletions src/codeflare_sdk/common/widgets/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
config_check,
get_api_client,
)
from codeflare_sdk.common.kubernetes_cluster.kube_api_helpers import (
get_current_namespace,
)


class RayClusterManagerWidgets:
Expand All @@ -43,8 +46,6 @@ class RayClusterManagerWidgets:
"""

def __init__(self, ray_clusters_df: pd.DataFrame, namespace: str = None):
from ...ray.cluster.cluster import get_current_namespace

# Data
self.ray_clusters_df = ray_clusters_df
self.namespace = get_current_namespace() if not namespace else namespace
Expand Down
20 changes: 1 addition & 19 deletions src/codeflare_sdk/ray/cluster/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
config_check,
get_api_client,
)
from ...common.kubernetes_cluster.kube_api_helpers import get_current_namespace
from . import pretty_print
from .generate_yaml import (
generate_appwrapper,
Expand Down Expand Up @@ -573,25 +574,6 @@ def list_all_queued(
return resources


def get_current_namespace(): # pragma: no cover
if os.path.isfile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"):
try:
file = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r")
active_context = file.readline().strip("\n")
return active_context
except Exception as e:
print("Unable to find current namespace")
print("trying to gather from current context")
try:
_, active_context = config.list_kube_config_contexts(config_check())
except Exception as e:
return _kube_api_error_handling(e)
try:
return active_context["context"]["namespace"]
except KeyError:
return None


def get_cluster(
cluster_name: str,
namespace: str = "default",
Expand Down

0 comments on commit c406d96

Please sign in to comment.