xpk 0.5.0__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. xpk/__init__.py +15 -0
  2. xpk/commands/__init__.py +15 -0
  3. xpk/commands/batch.py +109 -0
  4. xpk/commands/cluster.py +784 -0
  5. xpk/commands/cluster_gcluster.py +185 -0
  6. xpk/commands/info.py +245 -0
  7. xpk/commands/inspector.py +363 -0
  8. xpk/commands/job.py +197 -0
  9. xpk/commands/kind.py +253 -0
  10. xpk/commands/shell.py +120 -0
  11. xpk/commands/version.py +39 -0
  12. xpk/commands/workload.py +692 -0
  13. xpk/core/__init__.py +15 -0
  14. xpk/core/blueprint/__init__.py +15 -0
  15. xpk/core/blueprint/blueprint_definitions.py +61 -0
  16. xpk/core/blueprint/blueprint_generator.py +652 -0
  17. xpk/core/cluster_private.py +197 -0
  18. xpk/core/commands.py +352 -0
  19. xpk/core/core.py +2824 -0
  20. xpk/core/docker_manager.py +308 -0
  21. xpk/core/gcluster_manager.py +158 -0
  22. xpk/core/kjob.py +205 -0
  23. xpk/core/kueue.py +352 -0
  24. xpk/core/nap.py +349 -0
  25. xpk/core/pathways.py +298 -0
  26. xpk/core/ray.py +222 -0
  27. xpk/core/system_characteristics.py +1395 -0
  28. xpk/core/workload.py +133 -0
  29. xpk/core/workload_decorators/__init__.py +15 -0
  30. xpk/core/workload_decorators/rdma_decorator.py +109 -0
  31. xpk/core/workload_decorators/tcpxo_decorator.py +157 -0
  32. xpk/main.py +73 -0
  33. xpk/parser/__init__.py +15 -0
  34. xpk/parser/batch.py +184 -0
  35. xpk/parser/cluster.py +621 -0
  36. xpk/parser/common.py +71 -0
  37. xpk/parser/core.py +109 -0
  38. xpk/parser/info.py +63 -0
  39. xpk/parser/inspector.py +65 -0
  40. xpk/parser/job.py +126 -0
  41. xpk/parser/kind.py +94 -0
  42. xpk/parser/shell.py +50 -0
  43. xpk/parser/validators.py +39 -0
  44. xpk/parser/version.py +23 -0
  45. xpk/parser/workload.py +684 -0
  46. xpk/utils/__init__.py +15 -0
  47. xpk/utils/console.py +55 -0
  48. xpk/utils/file.py +82 -0
  49. xpk/utils/network.py +168 -0
  50. xpk/utils/objects.py +85 -0
  51. xpk/utils/yaml.py +30 -0
  52. {xpk-0.5.0.dist-info → xpk-0.6.0.dist-info}/METADATA +301 -28
  53. xpk-0.6.0.dist-info/RECORD +57 -0
  54. {xpk-0.5.0.dist-info → xpk-0.6.0.dist-info}/WHEEL +1 -1
  55. xpk-0.6.0.dist-info/entry_points.txt +2 -0
  56. xpk-0.5.0.dist-info/RECORD +0 -7
  57. xpk-0.5.0.dist-info/entry_points.txt +0 -2
  58. xpk.py +0 -7282
  59. {xpk-0.5.0.dist-info → xpk-0.6.0.dist-info}/LICENSE +0 -0
  60. {xpk-0.5.0.dist-info → xpk-0.6.0.dist-info}/top_level.txt +0 -0
xpk/commands/kind.py ADDED
@@ -0,0 +1,253 @@
1
+ """
2
+ Copyright 2024 Google LLC
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ from ..core.commands import (
18
+ run_command_for_value,
19
+ run_command_with_updates,
20
+ )
21
+ from ..core.core import (
22
+ set_jobset_on_cluster,
23
+ )
24
+ from ..core.kjob import (
25
+ verify_kjob_installed,
26
+ prepare_kjob,
27
+ apply_kjob_crds,
28
+ )
29
+ from ..core.kueue import (
30
+ install_kueue_on_cluster,
31
+ )
32
+ from ..utils.console import (xpk_exit, xpk_print)
33
+
34
+
35
+ def cluster_create(args) -> None:
36
+ """Function around cluster creation.
37
+
38
+ Args:
39
+ args: user provided arguments for running the command.
40
+
41
+ Returns:
42
+ 0 if successful and 1 otherwise.
43
+ """
44
+ xpk_print(f'Starting cluster create for cluster {args.cluster}:', flush=True)
45
+
46
+ create_cluster_command_code = create_cluster_if_necessary(args)
47
+ if create_cluster_command_code != 0:
48
+ xpk_exit(create_cluster_command_code)
49
+
50
+ set_cluster_command_code = set_local_cluster_command(args)
51
+ if set_cluster_command_code != 0:
52
+ xpk_exit(set_cluster_command_code)
53
+
54
+ xpk_print(
55
+ 'Enabling the jobset API on our cluster, to be deprecated when Jobset is'
56
+ ' globally available'
57
+ )
58
+ set_jobset_on_cluster_code = set_jobset_on_cluster(args)
59
+ if set_jobset_on_cluster_code != 0:
60
+ xpk_exit(set_jobset_on_cluster_code)
61
+
62
+ xpk_print('Enabling Kueue on the cluster')
63
+ install_kueue_on_cluster_code = install_kueue_on_cluster(args)
64
+ if install_kueue_on_cluster_code != 0:
65
+ xpk_exit(install_kueue_on_cluster_code)
66
+
67
+ xpk_print('Verifying kjob installation')
68
+ err_code = verify_kjob_installed(args)
69
+ if err_code > 0:
70
+ xpk_exit(err_code)
71
+
72
+ xpk_print('Applying kjob CDRs')
73
+ err_code = apply_kjob_crds(args)
74
+ if err_code > 0:
75
+ xpk_exit(err_code)
76
+
77
+ xpk_print('Preparing kjob')
78
+ err_code = prepare_kjob(args)
79
+ if err_code > 0:
80
+ xpk_exit(err_code)
81
+
82
+ xpk_print('Kind commands done! Resources are created.')
83
+ xpk_exit(0)
84
+
85
+
86
+ def cluster_delete(args) -> None:
87
+ """Function around cluster delete.
88
+
89
+ Args:
90
+ args: user provided arguments for running the command.
91
+
92
+ Returns:
93
+ 0 if successful and 1 otherwise.
94
+ """
95
+ xpk_print(f'Starting cluster delete for cluster: {args.cluster}', flush=True)
96
+
97
+ run_kind_cluster_delete_command_code = run_kind_cluster_delete_command(args)
98
+ if run_kind_cluster_delete_command_code != 0:
99
+ xpk_exit(run_kind_cluster_delete_command_code)
100
+ xpk_print(f'Kind commands done! Cluster {args.cluster} deleted.')
101
+ xpk_exit(0)
102
+
103
+
104
+ def cluster_list(args) -> None:
105
+ """Function around cluster list.
106
+
107
+ Args:
108
+ args: user provided arguments for running the command.
109
+
110
+ Returns:
111
+ 0 if successful and 1 otherwise.
112
+ """
113
+ if run_kind_clusters_list_command(args):
114
+ xpk_exit(1)
115
+ xpk_exit(0)
116
+
117
+
118
+ def create_cluster_if_necessary(args) -> int:
119
+ """Creates cluster if not present in the project.
120
+
121
+ Args:
122
+ args: user provided arguments for running the command.
123
+
124
+ Returns:
125
+ 0 if successful and 1 otherwise.
126
+ """
127
+ all_clusters, return_code = get_all_local_clusters_programmatic(args)
128
+ if return_code > 0:
129
+ xpk_print('Listing all clusters failed!')
130
+ return 1
131
+ if args.cluster in all_clusters:
132
+ xpk_print('Skipping cluster creation since it already exists.')
133
+ return 0
134
+ else:
135
+ return run_kind_cluster_create_command(args)
136
+
137
+
138
+ def run_kind_cluster_delete_command(args) -> int:
139
+ """Run the Delete Kind Cluster request.
140
+
141
+ Args:
142
+ args: user provided arguments for running the command.
143
+
144
+ Returns:
145
+ 0 if successful and 1 otherwise.
146
+ """
147
+ command = 'kind delete cluster'
148
+
149
+ if args.cluster:
150
+ command += f' --name={args.cluster}'
151
+
152
+ return_code = run_command_with_updates(command, 'Cluster Delete', args)
153
+ if return_code != 0:
154
+ xpk_print(f'Cluster delete request returned ERROR {return_code}')
155
+ return 1
156
+
157
+ return 0
158
+
159
+
160
+ def run_kind_clusters_list_command(args) -> int:
161
+ """List Kind Clusters within the project and location.
162
+
163
+ Args:
164
+ args: user provided arguments for running the command.
165
+
166
+ Returns:
167
+ 0 if successful and 1 otherwise.
168
+ """
169
+ command = 'kind get clusters'
170
+ return_code = run_command_with_updates(command, 'Cluster List', args)
171
+ if return_code != 0:
172
+ xpk_print(f'Cluster list request returned ERROR {return_code}')
173
+ return 1
174
+
175
+ return 0
176
+
177
+
178
+ def run_kind_cluster_create_command(args) -> int:
179
+ """Run the Create Kind Cluster request.
180
+
181
+ Args:
182
+ args: user provided arguments for running the command.
183
+
184
+ Returns:
185
+ 0 if successful and 1 otherwise.
186
+ """
187
+ command = 'kind create cluster'
188
+
189
+ if args.cluster:
190
+ command += f' --name={args.cluster}'
191
+
192
+ if args.k8s_version:
193
+ command += f' --image=kindest/node:v{args.k8s_version}'
194
+
195
+ return_code = run_command_with_updates(command, 'Kind Cluster Create', args)
196
+ if return_code != 0:
197
+ xpk_print(f'GKE Cluster Create request returned ERROR {return_code}')
198
+ return 1
199
+ return 0
200
+
201
+
202
+ def get_all_local_clusters_programmatic(args) -> tuple[list[str], int]:
203
+ """Gets all the local clusters.
204
+
205
+ Args:
206
+ args: user provided arguments for running the command.
207
+
208
+ Returns:
209
+ List of cluster names and 0 if successful and 1 otherwise.
210
+ """
211
+ command = 'kind get clusters'
212
+ return_code, raw_cluster_output = run_command_for_value(
213
+ command, 'Find if Cluster Exists', args
214
+ )
215
+ if return_code != 0:
216
+ xpk_print(f'Find if Cluster Exists returned ERROR {return_code}')
217
+ return [], return_code
218
+
219
+ return raw_cluster_output.splitlines(), 0
220
+
221
+
222
+ def set_local_cluster_command(args) -> int:
223
+ """Run local cluster configuration command to set the kubectl config.
224
+
225
+ Args:
226
+ args: user provided arguments for running the command.
227
+
228
+ Returns:
229
+ 0 if successful and 1 otherwise.
230
+ """
231
+ if not args.cluster:
232
+ command = 'kubectl config current-context'
233
+ return_code, current_context = run_command_for_value(
234
+ command, 'get current-context', args
235
+ )
236
+ xpk_print(
237
+ 'No local cluster name specified. Using current-context'
238
+ f' `{current_context.strip()}`'
239
+ )
240
+ return return_code
241
+
242
+ command = (
243
+ f'kubectl config use-context kind-{args.cluster} --namespace=default'
244
+ )
245
+ task = f'switch to cluster {args.cluster}'
246
+ return_code = run_command_with_updates(
247
+ command,
248
+ task,
249
+ args,
250
+ )
251
+ if return_code != 0:
252
+ xpk_print(f'{task} returned ERROR {return_code}')
253
+ return return_code
xpk/commands/shell.py ADDED
@@ -0,0 +1,120 @@
1
+ """
2
+ Copyright 2024 Google LLC
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ https://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ """
13
+
14
+ from ..core.commands import run_command_with_full_controls, run_command_for_value, run_command_with_updates
15
+ from ..utils.console import xpk_exit, xpk_print
16
+ from argparse import Namespace
17
+
18
+ from ..core.kjob import AppProfileDefaults, PodTemplateDefaults
19
+
20
+
21
+ exit_instructions = 'To exit the shell input "exit".'
22
+
23
+
24
+ def shell(args: Namespace):
25
+ """Enter interactive shell.
26
+ Args:
27
+ args: user provided arguments for running the command.
28
+ Returns:
29
+ 0 if successful and 1 otherwise.
30
+ """
31
+ exisitng_shell_pod_name = get_existing_shell_pod_name(args)
32
+
33
+ if exisitng_shell_pod_name is None:
34
+ return_code = connect_to_new_interactive_shell(args)
35
+ else:
36
+ return_code = connect_to_existing_interactive_shell(
37
+ exisitng_shell_pod_name, args
38
+ )
39
+
40
+ if return_code != 0:
41
+ xpk_print(f'The command failed with code {return_code}.')
42
+ xpk_exit(return_code)
43
+
44
+ xpk_exit(0)
45
+
46
+
47
+ def get_existing_shell_pod_name(args: Namespace) -> str | None:
48
+ return_code, shell_name = run_command_for_value(
49
+ command=(
50
+ 'kubectl get pods --no-headers --field-selector status.phase=Running'
51
+ ' -o custom-columns=":metadata.name"'
52
+ ),
53
+ task='Get existing interactive shell pod name.',
54
+ global_args=args,
55
+ )
56
+ if return_code != 0:
57
+ xpk_print(
58
+ f'Encounter an error with a code {return_code} when checking for'
59
+ ' existing running shell.'
60
+ )
61
+ xpk_exit(return_code)
62
+
63
+ pod_names = shell_name.strip().split('\n')
64
+ kjob_pod_names = [
65
+ name for name in pod_names if AppProfileDefaults.NAME.value in name
66
+ ]
67
+ shell_pod_names = [name for name in kjob_pod_names if 'interactive' in name]
68
+
69
+ return shell_pod_names[0] if shell_pod_names else None
70
+
71
+
72
+ def connect_to_new_interactive_shell(args: Namespace) -> int:
73
+ return run_command_with_full_controls(
74
+ command=(
75
+ 'kubectl-kjob create interactive --profile'
76
+ f' {AppProfileDefaults.NAME.value} --pod-running-timeout 30s'
77
+ ),
78
+ task='Creating new interactive shell and entering it',
79
+ global_args=args,
80
+ instructions=exit_instructions,
81
+ )
82
+
83
+
84
+ def connect_to_existing_interactive_shell(
85
+ pod_name: str, args: Namespace
86
+ ) -> int:
87
+ return run_command_with_full_controls(
88
+ command=(
89
+ f'kubectl exec --stdin --tty {pod_name} --'
90
+ f' {PodTemplateDefaults.INTERACTIVE_COMMAND.value}'
91
+ ),
92
+ task='Entering existing interactive shell',
93
+ global_args=args,
94
+ instructions=exit_instructions,
95
+ )
96
+
97
+
98
+ def shell_stop(args: Namespace):
99
+ """Stop the running interactive shell by deleting the pod.
100
+ Args:
101
+ args: user provided arguments for running the command.
102
+ Returns:
103
+ 0 if successful and 1 otherwise.
104
+ """
105
+ exisitng_shell_pod_name = get_existing_shell_pod_name(args)
106
+
107
+ if exisitng_shell_pod_name is None:
108
+ xpk_print('There is no shell running to stop')
109
+ xpk_exit(0)
110
+
111
+ return_code = run_command_with_updates(
112
+ command=f'kubectl delete pod {exisitng_shell_pod_name}',
113
+ task='Deleting the existing shell.',
114
+ global_args=args,
115
+ )
116
+ if return_code != 0:
117
+ xpk_exit(return_code)
118
+
119
+ xpk_print('The shell was deleted successfully.')
120
+ xpk_exit(0)
@@ -0,0 +1,39 @@
1
+ """
2
+ Copyright 2025 Google LLC
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ from argparse import Namespace
18
+ import os
19
+
20
+ from ..core.commands import run_command_for_value
21
+
22
+ XPK_VERSION = 'v0.6.0'
23
+
24
+ from ..utils.console import xpk_exit, xpk_print
25
+
26
+
27
+ def version(args: Namespace) -> None:
28
+ """Get version of xpk."""
29
+ xpk_print('xpk_version:', XPK_VERSION)
30
+ if os.path.exists(os.path.join(os.getcwd(), '.git')):
31
+ code, xpk_version = run_command_for_value(
32
+ 'git rev-parse HEAD',
33
+ task='Get latest hash',
34
+ global_args=args,
35
+ quiet=True,
36
+ )
37
+ if code != 0:
38
+ xpk_exit(code)
39
+ xpk_print('git commit:', xpk_version.strip('\n'))