diff --git a/sky/cli.py b/sky/cli.py index eef29fa7d1b..75b2acf9c42 100644 --- a/sky/cli.py +++ b/sky/cli.py @@ -4771,8 +4771,11 @@ def local_up(gpus: bool): message_str = 'Creating local cluster{}...' message_str = message_str.format((' with GPU support (this may take up ' 'to 15 minutes)') if gpus else '') - path_to_package = os.path.dirname(os.path.dirname(__file__)) + + # Set port forwarding option to ingress for local cluster, and make backup change_config_port() + + path_to_package = os.path.dirname(os.path.dirname(__file__)) up_script_path = os.path.join(path_to_package, 'sky/utils/kubernetes', 'create_cluster.sh') @@ -4877,19 +4880,6 @@ def local_up(gpus: bool): 'runtime settings. See https://kind.sigs.k8s.io/docs/user/quick-start/#settings-for-docker-desktop for more info.' # pylint: disable=line-too-long f'{gpu_hint}') -def change_config_port(): - if not skypilot_config.loaded_config_path(): - default_config = {'kubernetes': {'ports': 'loadbalancer'}} - config_path = os.path.expanduser(CONFIG_PATH) - common_utils.dump_yaml(config_path, default_config) - skypilot_config._try_load_config() - backup_path = os.path.expanduser(BACKUP_PATH) - backup_config = skypilot_config.to_dict() - common_utils.dump_yaml(backup_path, backup_config) - updated_config = skypilot_config.set_nested(('kubernetes', 'ports'), 'ingress') - common_utils.dump_yaml(skypilot_config.loaded_config_path(), updated_config) - skypilot_config._try_load_config() - @local.command('down', cls=_DocumentedCodeCommand) @usage_lib.entrypoint @@ -4897,6 +4887,9 @@ def local_down(): """Deletes a local cluster.""" cluster_removed = False + # Restore port forwarding option to for local cluster from backup + restore_config_port() + path_to_package = os.path.dirname(os.path.dirname(__file__)) down_script_path = os.path.join(path_to_package, 'sky/utils/kubernetes', 'delete_cluster.sh') @@ -4938,6 +4931,43 @@ def local_down(): f'{colorama.Fore.GREEN}Local cluster removed.{style.RESET_ALL}') +def change_config_port(): + """Set the `kubernetes:ports` to be `ingress` and backup the original value.""" + + if not skypilot_config.loaded_config_path(): + logger.debug(f'Making base {CONFIG_PATH} file as it did not previously exist.') + default_config = {'kubernetes': {'ports': 'loadbalancer'}} + config_path = os.path.expanduser(CONFIG_PATH) + common_utils.dump_yaml(config_path, default_config) + skypilot_config._try_load_config() + + logger.debug(f'Creating backup at {BACKUP_PATH} of current kubernetes port forwarding option.') + backup_path = os.path.expanduser(BACKUP_PATH) + backup_port_config = skypilot_config.to_dict()['kubernetes']['ports'] + common_utils.dump_yaml(backup_path, {'kubernetes': {'ports': backup_port_config}}) + + current_path = skypilot_config.loaded_config_path() + logger.debug(f'Set current port forwarding option to ingress in {current_path}.') + updated_config = skypilot_config.set_nested(('kubernetes', 'ports'), 'ingress') + common_utils.dump_yaml(current_path, updated_config) + skypilot_config._try_load_config() + + +def restore_config_port(): + """Restore the original value of `kubernetes:ports`.""" + + current_path = skypilot_config.loaded_config_path() + logger.debug(f'Restore original port forwarding option in {current_path} from backup file {BACKUP_PATH}.') + backup_path = os.path.expanduser(BACKUP_PATH) + backup_port_config = common_utils.read_yaml(backup_path)['kubernetes']['ports'] + restored_config = skypilot_config.set_nested(('kubernetes', 'ports'), backup_port_config) + common_utils.dump_yaml(current_path, restored_config) + + logger.debug(f'Resetting backup file {BACKUP_PATH} to be empty.') + common_utils.dump_yaml(backup_path, {}) + skypilot_config._try_load_config() + + def main(): return cli() diff --git a/sky/provision/kubernetes/network_utils.py b/sky/provision/kubernetes/network_utils.py index b69f995e2e7..587aa56e8fd 100644 --- a/sky/provision/kubernetes/network_utils.py +++ b/sky/provision/kubernetes/network_utils.py @@ -23,15 +23,6 @@ def get_port_mode( mode_str = mode_str or skypilot_config.get_nested( ('kubernetes', 'ports'), kubernetes_enums.KubernetesPortMode.LOADBALANCER.value) - """ - curr_context = kubernetes_utils.get_current_kube_config_context_name() - running_kind = curr_context == kubernetes_utils.KIND_CONTEXT_NAME - default = ( - (running_kind and kubernetes_enums.KubernetesPortMode.INGRESS.value) or - kubernetes_enums.KubernetesPortMode.LOADBALANCER.value - ) - mode_str = mode_str or skypilot_config.get_nested(('kubernetes', 'ports'), default) - """ try: port_mode = kubernetes_enums.KubernetesPortMode(mode_str) except ValueError as e: