Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions sky/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4394,15 +4394,17 @@ class CloudFilter(enum.Enum):
LOCAL = 'local'


def _get_glob_clusters(
clusters: List[str],
silent: bool = False,
workspaces_filter: Optional[Set[str]] = None) -> List[str]:
def _get_glob_clusters(clusters: List[str],
silent: bool = False,
workspaces_filter: Optional[Set[str]] = None,
exclude_managed_clusters: bool = False) -> List[str]:
"""Returns a list of clusters that match the glob pattern."""
glob_clusters = []
for cluster in clusters:
glob_cluster = global_user_state.get_glob_cluster_names(
cluster, workspaces_filter=workspaces_filter)
cluster,
workspaces_filter=workspaces_filter,
exclude_managed_clusters=exclude_managed_clusters)
if len(glob_cluster) == 0 and not silent:
logger.info(f'Cluster {cluster} not found.')
glob_clusters.extend(glob_cluster)
Expand Down Expand Up @@ -4648,6 +4650,8 @@ def get_clusters(
logger.debug('Suppressing include_credentials for viewer caller')
include_credentials = False

exclude_managed_clusters = not (_include_is_managed or
env_options.Options.SHOW_DEBUG_INFO.get())
if cluster_names is not None:
if isinstance(cluster_names, str):
cluster_names = [cluster_names]
Expand All @@ -4660,14 +4664,14 @@ def get_clusters(
non_glob_cluster_names.append(cluster_name)
cluster_names = non_glob_cluster_names
if glob_cluster_names:
# Otherwise a pattern also matching a managed cluster would
# report it as not found below.
cluster_names += _get_glob_clusters(
glob_cluster_names,
silent=True,
workspaces_filter=accessible_workspaces)
workspaces_filter=accessible_workspaces,
exclude_managed_clusters=exclude_managed_clusters)

exclude_managed_clusters = False
if not (_include_is_managed or env_options.Options.SHOW_DEBUG_INFO.get()):
exclude_managed_clusters = True
user_hashes_filter = None
if not all_users:
user_hashes_filter = {common_utils.get_current_user().id}
Expand Down
8 changes: 5 additions & 3 deletions sky/global_user_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2242,9 +2242,9 @@ def get_status_from_cluster_name(


@metrics_lib.time_me
def get_glob_cluster_names(
cluster_name: str,
workspaces_filter: Optional[Set[str]] = None) -> List[str]:
def get_glob_cluster_names(cluster_name: str,
workspaces_filter: Optional[Set[str]] = None,
exclude_managed_clusters: bool = False) -> List[str]:
engine = _db_manager.get_engine()
assert cluster_name is not None, 'cluster_name cannot be None'
with orm.Session(engine) as session:
Expand All @@ -2261,6 +2261,8 @@ def get_glob_cluster_names(
if workspaces_filter is not None:
query = query.filter(
cluster_table.c.workspace.in_(workspaces_filter))
if exclude_managed_clusters:
query = query.filter(cluster_table.c.is_managed == int(False))
rows = query.all()
return [row.name for row in rows]

Expand Down
46 changes: 46 additions & 0 deletions tests/unit_tests/test_backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sky import check as sky_check
from sky import clouds
from sky import exceptions
from sky import global_user_state
from sky import models
from sky import skypilot_config
from sky.backends import backend_utils
Expand All @@ -19,6 +20,7 @@
from sky.utils import common_utils
from sky.utils import status_lib
from sky.utils import yaml_utils
from sky.utils.db import db_utils


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -923,3 +925,47 @@ def test_make_safe_symlink_command_leaves_target_unquoted():
source='/etc/config', target='~/.sky/file_mounts/etc/config')
assert 'ln -s ~/.sky/file_mounts/etc/config /etc/config' in cmd
assert "'~/.sky/file_mounts/etc/config'" not in cmd


class _PicklableHandle:
launched_resources = None
cluster_name_on_cloud = 'on-cloud'
launched_nodes = 1


@pytest.mark.parametrize('include_managed', [False, True])
def test_glob_does_not_report_hidden_managed_clusters_as_missing(
monkeypatch, tmp_path, include_managed):
monkeypatch.setenv(constants.SKY_RUNTIME_DIR_ENV_VAR_KEY, str(tmp_path))
# Debug mode shows managed clusters too.
monkeypatch.delenv('SKYPILOT_DEBUG', raising=False)
monkeypatch.setattr(
global_user_state, '_db_manager',
db_utils.DatabaseManager('state',
global_user_state.create_table,
post_init_fn=lambda _: global_user_state.
_sqlite_supports_returning()))
for name, is_managed in (('web-1', False), ('web-job-7', True)):
global_user_state.add_or_update_cluster(
cluster_name=name,
cluster_handle=_PicklableHandle(),
requested_resources=set(),
ready=True,
is_managed=is_managed)
monkeypatch.setattr('sky.workspaces.core.get_accessible_workspace_names',
lambda **kwargs: {constants.SKYPILOT_DEFAULT_WORKSPACE})
monkeypatch.setattr(
'sky.utils.resources_utils.get_readable_resources_repr',
lambda handle, simplified_only: ('', None)
if simplified_only else ('', ''))
info = mock.MagicMock()
monkeypatch.setattr(backend_utils.logger, 'info', info)

records = backend_utils.get_clusters(refresh=common.StatusRefreshMode.NONE,
cluster_names=['web-*'],
all_users=True,
_include_is_managed=include_managed)

expected = {'web-1', 'web-job-7'} if include_managed else {'web-1'}
assert {record['name'] for record in records} == expected
assert not any('not found' in str(call) for call in info.call_args_list)
Loading