kubexec 0.6.2__tar.gz → 0.7.0__tar.gz
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.
- kubexec-0.7.0/LICENSE +21 -0
- kubexec-0.7.0/MANIFEST.in +2 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/PKG-INFO +29 -3
- {kubexec-0.6.2 → kubexec-0.7.0}/README.md +16 -2
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/__init__.py +1 -1
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/cli.py +8 -7
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/config.py +22 -2
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/executor.py +27 -15
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/k8s_client.py +41 -11
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/kuberlist.py +1 -1
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/utils.py +17 -13
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec.egg-info/PKG-INFO +29 -3
- kubexec-0.7.0/kubexec.egg-info/SOURCES.txt +36 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec.egg-info/requires.txt +3 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec.egg-info/top_level.txt +0 -1
- kubexec-0.7.0/pytest.ini +6 -0
- kubexec-0.7.0/requirements.txt +3 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/setup.py +21 -3
- kubexec-0.7.0/tests/climb/__init__.py +0 -0
- kubexec-0.7.0/tests/climb/conftest.py +26 -0
- kubexec-0.7.0/tests/climb/test_cluster.py +80 -0
- kubexec-0.7.0/tests/climb/test_e2e_jobs.py +65 -0
- kubexec-0.7.0/tests/conftest.py +70 -0
- kubexec-0.7.0/tests/unit/__init__.py +0 -0
- kubexec-0.7.0/tests/unit/conftest.py +16 -0
- kubexec-0.7.0/tests/unit/test_cli.py +85 -0
- kubexec-0.7.0/tests/unit/test_config.py +73 -0
- kubexec-0.7.0/tests/unit/test_executor.py +227 -0
- kubexec-0.7.0/tests/unit/test_k8s_client.py +119 -0
- kubexec-0.7.0/tests/unit/test_templates.py +52 -0
- kubexec-0.7.0/tests/unit/test_utils.py +66 -0
- kubexec-0.7.0/tests/unit/test_version.py +27 -0
- kubexec-0.6.2/kubexec.egg-info/SOURCES.txt +0 -18
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/exceptions.py +0 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec/templates.py +0 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec.egg-info/dependency_links.txt +0 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/kubexec.egg-info/entry_points.txt +0 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/setup.cfg +0 -0
- {kubexec-0.6.2 → kubexec-0.7.0}/tests/__init__.py +0 -0
kubexec-0.7.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Andrea Telatin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kubexec
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Execute commands and scripts on Kubernetes pods with Docker access
|
|
5
|
+
Home-page: https://github.com/telatin/kubexec
|
|
5
6
|
Author: kubexec team
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Source, https://github.com/telatin/kubexec
|
|
9
|
+
Project-URL: Issues, https://github.com/telatin/kubexec/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/telatin/kubexec#readme
|
|
6
11
|
Classifier: Development Status :: 3 - Alpha
|
|
7
12
|
Classifier: Intended Audience :: Developers
|
|
8
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
9
13
|
Classifier: Operating System :: OS Independent
|
|
10
14
|
Classifier: Programming Language :: Python :: 3
|
|
11
15
|
Classifier: Programming Language :: Python :: 3.10
|
|
@@ -14,13 +18,21 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
14
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
19
|
Requires-Python: >=3.10
|
|
16
20
|
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
17
22
|
Requires-Dist: kubernetes>=28.1.0
|
|
18
23
|
Requires-Dist: PyYAML>=6.0
|
|
19
24
|
Requires-Dist: rich>=13.0.0
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
20
27
|
Dynamic: author
|
|
21
28
|
Dynamic: classifier
|
|
22
29
|
Dynamic: description
|
|
23
30
|
Dynamic: description-content-type
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: project-url
|
|
35
|
+
Dynamic: provides-extra
|
|
24
36
|
Dynamic: requires-dist
|
|
25
37
|
Dynamic: requires-python
|
|
26
38
|
Dynamic: summary
|
|
@@ -28,7 +40,7 @@ Dynamic: summary
|
|
|
28
40
|
# kubexec
|
|
29
41
|
|
|
30
42
|
|
|
31
|
-

|
|
32
44
|
|
|
33
45
|
Execute commands and scripts on Kubernetes pods with Docker access.
|
|
34
46
|
|
|
@@ -78,3 +90,17 @@ kubexec "ls -la > output.txt"
|
|
|
78
90
|
kubexec -d community.wave.seqera.io/library/spades:4.1.0--77799c52e1d1054a \
|
|
79
91
|
"spades.py -1 tests/reads/s_6_1.fastq.gz -2 tests/reads/s_6_2.fastq.gz -o spades-out"
|
|
80
92
|
```
|
|
93
|
+
|
|
94
|
+
## Testing
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip install -e '.[test]'
|
|
98
|
+
|
|
99
|
+
pytest # unit tests; CLIMB tests run too when CLIMB is detected
|
|
100
|
+
pytest tests/unit # unit tests only (no cluster needed, runs anywhere)
|
|
101
|
+
pytest -m "not e2e" # on CLIMB, skip tests that create real jobs
|
|
102
|
+
KUBEXEC_TEST_CLIMB=0 pytest # force-skip the CLIMB suite (=1 forces it on)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
CLIMB is detected by the in-cluster service account plus `/shared/team` and `/shared/public`
|
|
106
|
+
being mounted. Tests marked `xfail` document known bugs and will fail loudly (strict) once fixed.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# kubexec
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-

|
|
5
5
|
|
|
6
6
|
Execute commands and scripts on Kubernetes pods with Docker access.
|
|
7
7
|
|
|
@@ -50,4 +50,18 @@ kubexec "ls -la > output.txt"
|
|
|
50
50
|
# This will create spades-out/ directory in your current working directory
|
|
51
51
|
kubexec -d community.wave.seqera.io/library/spades:4.1.0--77799c52e1d1054a \
|
|
52
52
|
"spades.py -1 tests/reads/s_6_1.fastq.gz -2 tests/reads/s_6_2.fastq.gz -o spades-out"
|
|
53
|
-
```
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Testing
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
pip install -e '.[test]'
|
|
59
|
+
|
|
60
|
+
pytest # unit tests; CLIMB tests run too when CLIMB is detected
|
|
61
|
+
pytest tests/unit # unit tests only (no cluster needed, runs anywhere)
|
|
62
|
+
pytest -m "not e2e" # on CLIMB, skip tests that create real jobs
|
|
63
|
+
KUBEXEC_TEST_CLIMB=0 pytest # force-skip the CLIMB suite (=1 forces it on)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
CLIMB is detected by the in-cluster service account plus `/shared/team` and `/shared/public`
|
|
67
|
+
being mounted. Tests marked `xfail` document known bugs and will fail loudly (strict) once fixed.
|
|
@@ -39,6 +39,7 @@ Examples:
|
|
|
39
39
|
|
|
40
40
|
parser.add_argument(
|
|
41
41
|
'target',
|
|
42
|
+
nargs='?',
|
|
42
43
|
metavar='TARGET',
|
|
43
44
|
help='Command string or path to script file to execute'
|
|
44
45
|
)
|
|
@@ -156,14 +157,14 @@ def main() -> int:
|
|
|
156
157
|
config = Config(args.config)
|
|
157
158
|
|
|
158
159
|
# Initialize Kubernetes client
|
|
159
|
-
k8s_client = KubernetesClient(args.context)
|
|
160
|
+
k8s_client = KubernetesClient(args.context, verify_ssl=config.get('verify_ssl', True))
|
|
160
161
|
|
|
161
162
|
# Handle special operations
|
|
162
163
|
if args.list_jobs:
|
|
163
|
-
return _handle_list_jobs(k8s_client, args.namespace or config.get('namespace'))
|
|
164
|
+
return _handle_list_jobs(k8s_client, config, args.namespace or config.get('namespace'))
|
|
164
165
|
|
|
165
166
|
if args.cleanup_old:
|
|
166
|
-
return _handle_cleanup_old(k8s_client, args.namespace or config.get('namespace'), args.cleanup_old)
|
|
167
|
+
return _handle_cleanup_old(k8s_client, config, args.namespace or config.get('namespace'), args.cleanup_old)
|
|
167
168
|
|
|
168
169
|
# Validate target
|
|
169
170
|
if not args.target:
|
|
@@ -213,11 +214,11 @@ def main() -> int:
|
|
|
213
214
|
return 1
|
|
214
215
|
|
|
215
216
|
|
|
216
|
-
def _handle_list_jobs(k8s_client: KubernetesClient, namespace: str) -> int:
|
|
217
|
+
def _handle_list_jobs(k8s_client: KubernetesClient, config: Config, namespace: str) -> int:
|
|
217
218
|
"""Handle --list-jobs operation"""
|
|
218
219
|
try:
|
|
219
220
|
from .executor import KubeExecutor
|
|
220
|
-
executor = KubeExecutor(
|
|
221
|
+
executor = KubeExecutor(config, k8s_client)
|
|
221
222
|
jobs = executor.list_jobs(namespace)
|
|
222
223
|
|
|
223
224
|
if not jobs:
|
|
@@ -237,11 +238,11 @@ def _handle_list_jobs(k8s_client: KubernetesClient, namespace: str) -> int:
|
|
|
237
238
|
return 1
|
|
238
239
|
|
|
239
240
|
|
|
240
|
-
def _handle_cleanup_old(k8s_client: KubernetesClient, namespace: str, max_age_hours: int) -> int:
|
|
241
|
+
def _handle_cleanup_old(k8s_client: KubernetesClient, config: Config, namespace: str, max_age_hours: int) -> int:
|
|
241
242
|
"""Handle --cleanup-old operation"""
|
|
242
243
|
try:
|
|
243
244
|
from .executor import KubeExecutor
|
|
244
|
-
executor = KubeExecutor(
|
|
245
|
+
executor = KubeExecutor(config, k8s_client)
|
|
245
246
|
cleaned_count = executor.cleanup_old_jobs(namespace, max_age_hours)
|
|
246
247
|
|
|
247
248
|
print(f"Cleaned up {cleaned_count} jobs older than {max_age_hours} hours")
|
|
@@ -26,6 +26,7 @@ class Config:
|
|
|
26
26
|
'workdir': '/tmp',
|
|
27
27
|
'cleanup': True,
|
|
28
28
|
'verbose': False,
|
|
29
|
+
'verify_ssl': True, # Set to false to skip Kubernetes API TLS verification
|
|
29
30
|
'timeout': 3600, # 1 hour default timeout
|
|
30
31
|
'ttl_seconds_after_finished': 60, # Based on Nextflow config
|
|
31
32
|
'security_context': {
|
|
@@ -80,6 +81,11 @@ class Config:
|
|
|
80
81
|
# Override with environment variables
|
|
81
82
|
self._apply_env_overrides(merged_config)
|
|
82
83
|
|
|
84
|
+
if os.getenv('KUBEXEC_TIMEOUT') is None:
|
|
85
|
+
merged_config['timeout'] = self._parse_timeout(
|
|
86
|
+
merged_config['timeout'], f"'timeout' in {self.config_file}"
|
|
87
|
+
)
|
|
88
|
+
|
|
83
89
|
return merged_config
|
|
84
90
|
|
|
85
91
|
def _create_default_config(self) -> None:
|
|
@@ -104,19 +110,33 @@ class Config:
|
|
|
104
110
|
'KUBEXEC_WORKDIR': 'workdir',
|
|
105
111
|
'KUBEXEC_CLEANUP': 'cleanup',
|
|
106
112
|
'KUBEXEC_VERBOSE': 'verbose',
|
|
113
|
+
'KUBEXEC_VERIFY_SSL': 'verify_ssl',
|
|
107
114
|
'KUBEXEC_TIMEOUT': 'timeout',
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
for env_var, config_key in env_mappings.items():
|
|
111
118
|
env_value = os.getenv(env_var)
|
|
112
119
|
if env_value is not None:
|
|
113
|
-
if config_key in ['cleanup', 'verbose']:
|
|
120
|
+
if config_key in ['cleanup', 'verbose', 'verify_ssl']:
|
|
114
121
|
config[config_key] = env_value.lower() in ('true', '1', 'yes', 'on')
|
|
115
122
|
elif config_key == 'timeout':
|
|
116
|
-
config[config_key] =
|
|
123
|
+
config[config_key] = self._parse_timeout(env_value, env_var)
|
|
117
124
|
else:
|
|
118
125
|
config[config_key] = env_value
|
|
119
126
|
|
|
127
|
+
@staticmethod
|
|
128
|
+
def _parse_timeout(value: Any, source: str) -> int:
|
|
129
|
+
"""Validate a timeout value (seconds): must be a positive integer"""
|
|
130
|
+
try:
|
|
131
|
+
if isinstance(value, bool):
|
|
132
|
+
raise ValueError
|
|
133
|
+
timeout = int(str(value).strip())
|
|
134
|
+
except ValueError:
|
|
135
|
+
raise ConfigurationError(f"Invalid {source} {value!r}: expected a positive integer (seconds)")
|
|
136
|
+
if timeout <= 0:
|
|
137
|
+
raise ConfigurationError(f"Invalid {source} {value!r}: expected a positive integer (seconds)")
|
|
138
|
+
return timeout
|
|
139
|
+
|
|
120
140
|
def get(self, key: str, default: Any = None) -> Any:
|
|
121
141
|
"""Get configuration value"""
|
|
122
142
|
return self.config.get(key, default)
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"""Command and script execution logic for kubexec"""
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
+
import shlex
|
|
5
|
+
import uuid
|
|
4
6
|
import logging
|
|
5
7
|
from typing import List, Optional, Dict, Any, Tuple
|
|
8
|
+
from kubernetes.client.rest import ApiException
|
|
6
9
|
from .k8s_client import KubernetesClient
|
|
7
10
|
from .config import Config
|
|
8
11
|
from .utils import make_unique_name, is_script_file, parse_volume_mount, validate_resource_spec
|
|
9
|
-
from .exceptions import KubeExecError, JobExecutionError
|
|
12
|
+
from .exceptions import KubeExecError, JobExecutionError, KubernetesClientError
|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
logger = logging.getLogger(__name__)
|
|
@@ -65,9 +68,7 @@ class KubeExecutor:
|
|
|
65
68
|
# Note: For existing pods, we can't mount volumes, so we work in the current directory
|
|
66
69
|
if is_script_file(target):
|
|
67
70
|
# For script files, we need to copy the script to the pod first
|
|
68
|
-
|
|
69
|
-
script_name = os.path.basename(target)
|
|
70
|
-
command = ["/bin/bash", "-c", f"cat << 'EOF' > /tmp/{script_name}\n{script_content}\nEOF\nchmod +x /tmp/{script_name} && /tmp/{script_name}"]
|
|
71
|
+
command = ["/bin/bash", "-c", self._script_shell_command(target)]
|
|
71
72
|
else:
|
|
72
73
|
# Direct command execution in existing pod
|
|
73
74
|
command = ["/bin/bash", "-c", target]
|
|
@@ -159,7 +160,6 @@ class KubeExecutor:
|
|
|
159
160
|
volume_mount_specs.extend(create_shared_volume_mounts())
|
|
160
161
|
|
|
161
162
|
# Determine kubexec entry path within shared volumes
|
|
162
|
-
import os
|
|
163
163
|
current_dir = os.getcwd()
|
|
164
164
|
|
|
165
165
|
# Map current directory to container path using shared volumes
|
|
@@ -192,22 +192,34 @@ class KubeExecutor:
|
|
|
192
192
|
})
|
|
193
193
|
|
|
194
194
|
# Prepare command
|
|
195
|
+
cd_prefix = f"cd {shlex.quote(kubexec_entry_path)} && "
|
|
195
196
|
if is_script_file(target):
|
|
196
|
-
script_content = self._read_script_file(target)
|
|
197
|
-
script_name = os.path.basename(target)
|
|
198
|
-
|
|
199
197
|
# Embed script content directly in command instead of using ConfigMap
|
|
200
198
|
# This avoids permission issues with ConfigMap creation
|
|
201
|
-
command = [
|
|
202
|
-
"/bin/bash", "-c",
|
|
203
|
-
f"cd {kubexec_entry_path} && cat << 'KUBEXEC_SCRIPT_EOF' > /tmp/{script_name}\n{script_content}\nKUBEXEC_SCRIPT_EOF\nchmod +x /tmp/{script_name} && /tmp/{script_name}"
|
|
204
|
-
]
|
|
199
|
+
command = ["/bin/bash", "-c", self._script_shell_command(target, prefix=cd_prefix)]
|
|
205
200
|
else:
|
|
206
201
|
# Direct command execution - change to entry directory first
|
|
207
|
-
command = ["/bin/bash", "-c", f"
|
|
202
|
+
command = ["/bin/bash", "-c", f"{cd_prefix}{target}"]
|
|
208
203
|
|
|
209
204
|
return command, volumes, volume_mount_specs
|
|
210
205
|
|
|
206
|
+
def _script_shell_command(self, script_path: str, prefix: str = "") -> str:
|
|
207
|
+
"""Shell snippet that writes a local script into /tmp of the pod and runs it"""
|
|
208
|
+
content = self._read_script_file(script_path)
|
|
209
|
+
remote_path = shlex.quote(f"/tmp/{os.path.basename(script_path)}")
|
|
210
|
+
|
|
211
|
+
# Pick a heredoc delimiter that cannot occur in the script
|
|
212
|
+
delimiter = f"KUBEXEC_EOF_{uuid.uuid4().hex[:8]}"
|
|
213
|
+
while delimiter in content:
|
|
214
|
+
delimiter = f"KUBEXEC_EOF_{uuid.uuid4().hex[:8]}"
|
|
215
|
+
|
|
216
|
+
if not content.endswith("\n"):
|
|
217
|
+
content += "\n"
|
|
218
|
+
return (
|
|
219
|
+
f"{prefix}cat << '{delimiter}' > {remote_path}\n{content}{delimiter}\n"
|
|
220
|
+
f"chmod +x {remote_path} && {remote_path}"
|
|
221
|
+
)
|
|
222
|
+
|
|
211
223
|
def _read_script_file(self, file_path: str) -> str:
|
|
212
224
|
"""Read script file content"""
|
|
213
225
|
try:
|
|
@@ -219,7 +231,7 @@ class KubeExecutor:
|
|
|
219
231
|
def list_jobs(self, namespace: str = "default") -> List[Dict[str, Any]]:
|
|
220
232
|
"""List kubexec jobs in namespace"""
|
|
221
233
|
try:
|
|
222
|
-
jobs = self.batch_v1.list_namespaced_job(
|
|
234
|
+
jobs = self.k8s_client.batch_v1.list_namespaced_job(
|
|
223
235
|
namespace=namespace,
|
|
224
236
|
label_selector="app=kubexec"
|
|
225
237
|
)
|
|
@@ -245,7 +257,7 @@ class KubeExecutor:
|
|
|
245
257
|
import datetime
|
|
246
258
|
cutoff_time = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=max_age_hours)
|
|
247
259
|
|
|
248
|
-
jobs = self.batch_v1.list_namespaced_job(
|
|
260
|
+
jobs = self.k8s_client.batch_v1.list_namespaced_job(
|
|
249
261
|
namespace=namespace,
|
|
250
262
|
label_selector="app=kubexec"
|
|
251
263
|
)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"""Kubernetes client operations for kubexec"""
|
|
2
2
|
|
|
3
|
+
import ssl
|
|
3
4
|
import time
|
|
4
5
|
import logging
|
|
6
|
+
import urllib3
|
|
5
7
|
from typing import List, Tuple, Optional, Dict, Any
|
|
6
8
|
from kubernetes import client, config
|
|
7
9
|
from kubernetes.client.rest import ApiException
|
|
@@ -15,13 +17,13 @@ logger = logging.getLogger(__name__)
|
|
|
15
17
|
class KubernetesClient:
|
|
16
18
|
"""Kubernetes client wrapper for kubexec operations"""
|
|
17
19
|
|
|
18
|
-
def __init__(self, context: Optional[str] = None):
|
|
19
|
-
"""Initialize Kubernetes client
|
|
20
|
+
def __init__(self, context: Optional[str] = None, verify_ssl: bool = True):
|
|
21
|
+
"""Initialize Kubernetes client
|
|
22
|
+
|
|
23
|
+
TLS certificates are verified by default. Set verify_ssl=False (config
|
|
24
|
+
key `verify_ssl` / env KUBEXEC_VERIFY_SSL) to explicitly opt out.
|
|
25
|
+
"""
|
|
20
26
|
try:
|
|
21
|
-
# Disable SSL verification warnings for self-signed certificates
|
|
22
|
-
import urllib3
|
|
23
|
-
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
24
|
-
|
|
25
27
|
if context:
|
|
26
28
|
config.load_kube_config(context=context)
|
|
27
29
|
else:
|
|
@@ -36,18 +38,46 @@ class KubernetesClient:
|
|
|
36
38
|
logger.warning("No kubeconfig found, using service account authentication")
|
|
37
39
|
config.load_incluster_config()
|
|
38
40
|
|
|
39
|
-
# Configure client for potential SSL issues
|
|
40
41
|
configuration = client.Configuration.get_default_copy()
|
|
41
|
-
if
|
|
42
|
+
if verify_ssl:
|
|
43
|
+
configuration.verify_ssl = True
|
|
44
|
+
else:
|
|
45
|
+
logger.warning("TLS certificate verification is disabled")
|
|
42
46
|
configuration.verify_ssl = False
|
|
47
|
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
48
|
+
|
|
49
|
+
api_client = client.ApiClient(configuration)
|
|
50
|
+
if verify_ssl:
|
|
51
|
+
self._relax_strict_ca_check(api_client, configuration)
|
|
43
52
|
|
|
44
|
-
self.batch_v1 = client.BatchV1Api(
|
|
45
|
-
self.core_v1 = client.CoreV1Api(
|
|
46
|
-
self.apps_v1 = client.AppsV1Api(
|
|
53
|
+
self.batch_v1 = client.BatchV1Api(api_client)
|
|
54
|
+
self.core_v1 = client.CoreV1Api(api_client)
|
|
55
|
+
self.apps_v1 = client.AppsV1Api(api_client)
|
|
47
56
|
|
|
48
57
|
except Exception as e:
|
|
49
58
|
raise KubernetesClientError(f"Failed to initialize Kubernetes client: {e}")
|
|
50
59
|
|
|
60
|
+
@staticmethod
|
|
61
|
+
def _relax_strict_ca_check(api_client: client.ApiClient, configuration: client.Configuration) -> None:
|
|
62
|
+
"""Keep certificate verification on, but drop Python's VERIFY_X509_STRICT.
|
|
63
|
+
|
|
64
|
+
Python 3.13+ enables VERIFY_X509_STRICT by default, which rejects CA
|
|
65
|
+
certificates lacking the keyUsage extension, as is the case for the
|
|
66
|
+
CLIMB cluster CA. The chain and hostname are still fully verified.
|
|
67
|
+
"""
|
|
68
|
+
pool_kw = getattr(api_client.rest_client.pool_manager, 'connection_pool_kw', None)
|
|
69
|
+
if pool_kw is None or not hasattr(ssl, 'VERIFY_X509_STRICT'):
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
if configuration.ssl_ca_cert or configuration.ca_cert_data:
|
|
73
|
+
# urllib3 loads the configured CA into this context; trust only that CA
|
|
74
|
+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
75
|
+
else:
|
|
76
|
+
ctx = ssl.create_default_context()
|
|
77
|
+
ctx.verify_flags &= ~ssl.VERIFY_X509_STRICT
|
|
78
|
+
pool_kw['ssl_context'] = ctx
|
|
79
|
+
api_client.rest_client.pool_manager.clear()
|
|
80
|
+
|
|
51
81
|
def get_pods(self, namespace: str = "default") -> List[str]:
|
|
52
82
|
"""Get list of pod names in namespace"""
|
|
53
83
|
try:
|
|
@@ -266,7 +266,7 @@ def main() -> int:
|
|
|
266
266
|
namespace = args.namespace or config.get('namespace')
|
|
267
267
|
|
|
268
268
|
# Initialize Kubernetes client
|
|
269
|
-
k8s_client = KubernetesClient(args.context)
|
|
269
|
+
k8s_client = KubernetesClient(args.context, verify_ssl=config.get('verify_ssl', True))
|
|
270
270
|
|
|
271
271
|
if args.watch:
|
|
272
272
|
watch_pods(k8s_client, namespace, args.kubexec)
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
import random
|
|
5
|
+
import re
|
|
5
6
|
import string
|
|
6
7
|
import time
|
|
7
8
|
from typing import Tuple
|
|
@@ -47,21 +48,24 @@ def parse_volume_mount(volume_spec: str) -> Tuple[str, str, bool]:
|
|
|
47
48
|
return host_path, pod_path, readonly
|
|
48
49
|
|
|
49
50
|
|
|
51
|
+
_MEMORY_RE = re.compile(r'\d+(\.\d+)?(Ki|Mi|Gi|Ti|Pi|Ei|k|M|G|T|P|E)')
|
|
52
|
+
_CPU_RE = re.compile(r'(\d+(\.\d+)?|\d+m)')
|
|
53
|
+
|
|
54
|
+
|
|
50
55
|
def validate_resource_spec(resource: str, resource_type: str) -> str:
|
|
51
|
-
"""Validate resource specification (memory/CPU)
|
|
56
|
+
"""Validate resource specification (memory/CPU)
|
|
57
|
+
|
|
58
|
+
Memory uses Kubernetes quantity suffixes (Mi, Gi, M, G, ...); a bare number
|
|
59
|
+
is taken as MiB. Note that 'm' (milli) is only valid for CPU.
|
|
60
|
+
"""
|
|
61
|
+
resource = str(resource).strip()
|
|
52
62
|
if resource_type == "memory":
|
|
53
|
-
if
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
raise ValueError(f"Invalid memory spec: {resource}")
|
|
63
|
+
if resource.isdigit():
|
|
64
|
+
return f"{resource}Mi"
|
|
65
|
+
if not _MEMORY_RE.fullmatch(resource):
|
|
66
|
+
raise ValueError(f"Invalid memory spec: {resource!r}. Use a number with a unit such as 512Mi or 2Gi")
|
|
58
67
|
elif resource_type == "cpu":
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return resource
|
|
62
|
-
except ValueError:
|
|
63
|
-
if resource.endswith('m'):
|
|
64
|
-
return resource
|
|
65
|
-
raise ValueError(f"Invalid CPU spec: {resource}")
|
|
68
|
+
if not _CPU_RE.fullmatch(resource):
|
|
69
|
+
raise ValueError(f"Invalid CPU spec: {resource!r}. Use cores (e.g. 2, 0.5) or millicores (e.g. 500m)")
|
|
66
70
|
|
|
67
71
|
return resource
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kubexec
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Execute commands and scripts on Kubernetes pods with Docker access
|
|
5
|
+
Home-page: https://github.com/telatin/kubexec
|
|
5
6
|
Author: kubexec team
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Source, https://github.com/telatin/kubexec
|
|
9
|
+
Project-URL: Issues, https://github.com/telatin/kubexec/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/telatin/kubexec#readme
|
|
6
11
|
Classifier: Development Status :: 3 - Alpha
|
|
7
12
|
Classifier: Intended Audience :: Developers
|
|
8
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
9
13
|
Classifier: Operating System :: OS Independent
|
|
10
14
|
Classifier: Programming Language :: Python :: 3
|
|
11
15
|
Classifier: Programming Language :: Python :: 3.10
|
|
@@ -14,13 +18,21 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
14
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
19
|
Requires-Python: >=3.10
|
|
16
20
|
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
17
22
|
Requires-Dist: kubernetes>=28.1.0
|
|
18
23
|
Requires-Dist: PyYAML>=6.0
|
|
19
24
|
Requires-Dist: rich>=13.0.0
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
20
27
|
Dynamic: author
|
|
21
28
|
Dynamic: classifier
|
|
22
29
|
Dynamic: description
|
|
23
30
|
Dynamic: description-content-type
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: project-url
|
|
35
|
+
Dynamic: provides-extra
|
|
24
36
|
Dynamic: requires-dist
|
|
25
37
|
Dynamic: requires-python
|
|
26
38
|
Dynamic: summary
|
|
@@ -28,7 +40,7 @@ Dynamic: summary
|
|
|
28
40
|
# kubexec
|
|
29
41
|
|
|
30
42
|
|
|
31
|
-

|
|
32
44
|
|
|
33
45
|
Execute commands and scripts on Kubernetes pods with Docker access.
|
|
34
46
|
|
|
@@ -78,3 +90,17 @@ kubexec "ls -la > output.txt"
|
|
|
78
90
|
kubexec -d community.wave.seqera.io/library/spades:4.1.0--77799c52e1d1054a \
|
|
79
91
|
"spades.py -1 tests/reads/s_6_1.fastq.gz -2 tests/reads/s_6_2.fastq.gz -o spades-out"
|
|
80
92
|
```
|
|
93
|
+
|
|
94
|
+
## Testing
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip install -e '.[test]'
|
|
98
|
+
|
|
99
|
+
pytest # unit tests; CLIMB tests run too when CLIMB is detected
|
|
100
|
+
pytest tests/unit # unit tests only (no cluster needed, runs anywhere)
|
|
101
|
+
pytest -m "not e2e" # on CLIMB, skip tests that create real jobs
|
|
102
|
+
KUBEXEC_TEST_CLIMB=0 pytest # force-skip the CLIMB suite (=1 forces it on)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
CLIMB is detected by the in-cluster service account plus `/shared/team` and `/shared/public`
|
|
106
|
+
being mounted. Tests marked `xfail` document known bugs and will fail loudly (strict) once fixed.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pytest.ini
|
|
5
|
+
requirements.txt
|
|
6
|
+
setup.py
|
|
7
|
+
kubexec/__init__.py
|
|
8
|
+
kubexec/cli.py
|
|
9
|
+
kubexec/config.py
|
|
10
|
+
kubexec/exceptions.py
|
|
11
|
+
kubexec/executor.py
|
|
12
|
+
kubexec/k8s_client.py
|
|
13
|
+
kubexec/kuberlist.py
|
|
14
|
+
kubexec/templates.py
|
|
15
|
+
kubexec/utils.py
|
|
16
|
+
kubexec.egg-info/PKG-INFO
|
|
17
|
+
kubexec.egg-info/SOURCES.txt
|
|
18
|
+
kubexec.egg-info/dependency_links.txt
|
|
19
|
+
kubexec.egg-info/entry_points.txt
|
|
20
|
+
kubexec.egg-info/requires.txt
|
|
21
|
+
kubexec.egg-info/top_level.txt
|
|
22
|
+
tests/__init__.py
|
|
23
|
+
tests/conftest.py
|
|
24
|
+
tests/climb/__init__.py
|
|
25
|
+
tests/climb/conftest.py
|
|
26
|
+
tests/climb/test_cluster.py
|
|
27
|
+
tests/climb/test_e2e_jobs.py
|
|
28
|
+
tests/unit/__init__.py
|
|
29
|
+
tests/unit/conftest.py
|
|
30
|
+
tests/unit/test_cli.py
|
|
31
|
+
tests/unit/test_config.py
|
|
32
|
+
tests/unit/test_executor.py
|
|
33
|
+
tests/unit/test_k8s_client.py
|
|
34
|
+
tests/unit/test_templates.py
|
|
35
|
+
tests/unit/test_utils.py
|
|
36
|
+
tests/unit/test_version.py
|
kubexec-0.7.0/pytest.ini
ADDED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
"""Setup script for kubexec package"""
|
|
2
2
|
|
|
3
|
+
import re
|
|
4
|
+
from pathlib import Path
|
|
3
5
|
from setuptools import setup, find_packages
|
|
4
6
|
|
|
7
|
+
# Single source of truth: kubexec/__init__.py (read, not imported, since the
|
|
8
|
+
# package dependencies may not be installed yet when this runs)
|
|
9
|
+
version = re.search(
|
|
10
|
+
r'^__version__\s*=\s*["\']([^"\']+)["\']',
|
|
11
|
+
(Path(__file__).parent / "kubexec" / "__init__.py").read_text(encoding="utf-8"),
|
|
12
|
+
re.M,
|
|
13
|
+
).group(1)
|
|
14
|
+
|
|
5
15
|
try:
|
|
6
16
|
with open("README.md", "r", encoding="utf-8") as fh:
|
|
7
17
|
long_description = fh.read()
|
|
@@ -20,16 +30,23 @@ except FileNotFoundError:
|
|
|
20
30
|
|
|
21
31
|
setup(
|
|
22
32
|
name="kubexec",
|
|
23
|
-
version=
|
|
33
|
+
version=version,
|
|
24
34
|
author="kubexec team",
|
|
35
|
+
license="MIT",
|
|
36
|
+
license_files=["LICENSE"],
|
|
25
37
|
description="Execute commands and scripts on Kubernetes pods with Docker access",
|
|
26
38
|
long_description=long_description,
|
|
27
39
|
long_description_content_type="text/markdown",
|
|
28
|
-
|
|
40
|
+
url="https://github.com/telatin/kubexec",
|
|
41
|
+
project_urls={
|
|
42
|
+
"Source": "https://github.com/telatin/kubexec",
|
|
43
|
+
"Issues": "https://github.com/telatin/kubexec/issues",
|
|
44
|
+
"Documentation": "https://github.com/telatin/kubexec#readme",
|
|
45
|
+
},
|
|
46
|
+
packages=find_packages(include=["kubexec", "kubexec.*"]),
|
|
29
47
|
classifiers=[
|
|
30
48
|
"Development Status :: 3 - Alpha",
|
|
31
49
|
"Intended Audience :: Developers",
|
|
32
|
-
"License :: OSI Approved :: MIT License",
|
|
33
50
|
"Operating System :: OS Independent",
|
|
34
51
|
"Programming Language :: Python :: 3",
|
|
35
52
|
"Programming Language :: Python :: 3.10",
|
|
@@ -39,6 +56,7 @@ setup(
|
|
|
39
56
|
],
|
|
40
57
|
python_requires=">=3.10",
|
|
41
58
|
install_requires=requirements,
|
|
59
|
+
extras_require={"test": ["pytest>=7"]},
|
|
42
60
|
entry_points={
|
|
43
61
|
"console_scripts": [
|
|
44
62
|
"kubexec=kubexec.cli:main",
|
|
File without changes
|