Skip to content

Commit

Permalink
finished new model for updating config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheth committed Mar 16, 2024
1 parent 5a67dcb commit 3f5054f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
58 changes: 44 additions & 14 deletions sky/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -4877,26 +4880,16 @@ 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
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')
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 0 additions & 9 deletions sky/provision/kubernetes/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3f5054f

Please sign in to comment.