-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathspace.py
More file actions
172 lines (146 loc) · 6.95 KB
/
space.py
File metadata and controls
172 lines (146 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import click
import json
import yaml
from tabulate import tabulate
from sagemaker.hyperpod.space.hyperpod_space import HPSpace
from sagemaker.hyperpod.cli.space_utils import generate_click_command
from hyperpod_space_template.registry import SCHEMA_REGISTRY
from hyperpod_space_template.v1_0.model import SpaceConfig
from sagemaker.hyperpod.common.telemetry.telemetry_logging import (
_hyperpod_telemetry_emitter,
)
from sagemaker.hyperpod.common.telemetry.constants import Feature
from sagemaker.hyperpod.cli.constants.space_constants import DEFAULT_SPACE_PORT
from sagemaker.hyperpod.common.cli_decorators import handle_cli_exceptions
@click.command("hyp-space")
@click.option("--debug", is_flag=True, help="Enable debug mode")
@generate_click_command(
schema_pkg="hyperpod_space_template",
registry=SCHEMA_REGISTRY,
)
@handle_cli_exceptions()
def space_create(version, debug, config):
"""Create a space resource."""
space_config = SpaceConfig(**config)
space = HPSpace(config=space_config)
space.create(debug=debug)
click.echo(f"Space '{space_config.name}' created successfully in namespace '{space_config.namespace}'")
@click.command("hyp-space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@click.option("--output", "-o", type=click.Choice(["table", "json"]), default="table")
@handle_cli_exceptions()
def space_list(namespace, output):
"""List space resources."""
spaces = HPSpace.list(namespace=namespace)
if output == "json":
spaces_data = []
for space in spaces:
space_dict = space.config.model_dump()
spaces_data.append(space_dict)
click.echo(json.dumps(spaces_data, indent=2))
else:
if spaces:
table_data = []
for space in spaces:
# Extract status conditions from raw resource
available = ""
progressing = ""
degraded = ""
if space.status and 'conditions' in space.status:
conditions = {c['type']: c['status'] for c in space.status['conditions']}
available = conditions.get('Available', '')
progressing = conditions.get('Progressing', '')
degraded = conditions.get('Degraded', '')
table_data.append([
space.config.name,
namespace,
available,
progressing,
degraded
])
click.echo(tabulate(table_data, headers=["NAME", "NAMESPACE", "AVAILABLE", "PROGRESSING", "DEGRADED"]))
else:
click.echo("No spaces found")
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@click.option("--output", "-o", type=click.Choice(["yaml", "json"]), default="yaml")
@handle_cli_exceptions()
def space_describe(name, namespace, output):
"""Describe a space resource."""
current_space = HPSpace.get(name=name, namespace=namespace)
# Combine config and raw resource data
current_space.raw_resource.get('metadata', {}).pop('managedFields', None)
if output == "json":
click.echo(json.dumps(current_space.raw_resource, indent=2))
else:
click.echo(yaml.dump(current_space.raw_resource, default_flow_style=False))
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@handle_cli_exceptions()
def space_delete(name, namespace):
"""Delete a space resource."""
current_space = HPSpace.get(name=name, namespace=namespace)
current_space.delete()
click.echo(f"Requested deletion for Space '{name}' in namespace '{namespace}'")
@click.command("hyp-space")
@generate_click_command(
schema_pkg="hyperpod_space_template",
registry=SCHEMA_REGISTRY,
is_update=True,
)
@handle_cli_exceptions()
def space_update(version, config):
"""Update a space resource."""
current_space = HPSpace.get(name=config['name'], namespace=config['namespace'])
if not config.get("display_name"):
config["display_name"] = current_space.config.display_name
current_space.update(**config)
click.echo(f"Space '{current_space.config.name}' updated successfully in namespace '{config['namespace']}'")
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@handle_cli_exceptions()
def space_start(name, namespace):
"""Start a space resource."""
current_space = HPSpace.get(name=name, namespace=namespace)
current_space.start()
click.echo(f"Space '{name}' start requested")
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@handle_cli_exceptions()
def space_stop(name, namespace):
"""Stop a space resource."""
current_space = HPSpace.get(name=name, namespace=namespace)
current_space.stop()
click.echo(f"Space '{name}' stop requested")
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@click.option("--pod-name", required=False, help="Name of the pod to get logs from")
@click.option("--container", required=False, help="Name of the container to get logs from")
@handle_cli_exceptions()
def space_get_logs(name, namespace, pod_name, container):
"""Get logs for a space resource."""
current_space = HPSpace.get(name=name, namespace=namespace)
logs = current_space.get_logs(pod_name=pod_name, container=container)
click.echo(logs)
@click.command("hyp-space")
@click.option("--name", required=True, help="Name of the space")
@click.option("--namespace", "-n", required=False, default="default", help="Kubernetes namespace")
@click.option("--local-port", required=False, default=DEFAULT_SPACE_PORT, help="Localhost port that is mapped to the space")
def space_portforward(name, namespace, local_port):
"""Port forward to localhost for a space resource."""
# Validate input port
try:
local_port = int(local_port)
except ValueError:
raise ValueError("Port values must be valid integers")
if not (1 <= local_port <= 65535):
raise ValueError(f"Port must be between 1 and 65535, got {local_port}")
current_space = HPSpace.get(name=name, namespace=namespace)
click.echo(f"Forwarding from local port {local_port} to space `{name}` in namespace `{namespace}`.")
click.echo(f"Please access the service via `http://localhost:{local_port}`. Press Ctrl+C to stop.")
current_space.portforward_space(local_port)