kube-envx 0.2.2a0__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.
- kube_envx-0.2.2a0/LICENSE +21 -0
- kube_envx-0.2.2a0/PKG-INFO +94 -0
- kube_envx-0.2.2a0/README.md +75 -0
- kube_envx-0.2.2a0/pyproject.toml +37 -0
- kube_envx-0.2.2a0/src/kenvx/__init__.py +0 -0
- kube_envx-0.2.2a0/src/kenvx/__main__.py +18 -0
- kube_envx-0.2.2a0/src/kenvx/cli/__init__.py +0 -0
- kube_envx-0.2.2a0/src/kenvx/cli/main.py +160 -0
- kube_envx-0.2.2a0/src/kenvx/console.py +40 -0
- kube_envx-0.2.2a0/src/kenvx/kube.py +168 -0
- kube_envx-0.2.2a0/src/kenvx/style.py +27 -0
- kube_envx-0.2.2a0/src/kenvx/utils.py +51 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ohmycoffe
|
|
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.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kube-envx
|
|
3
|
+
Version: 0.2.2a0
|
|
4
|
+
Summary: Extract environment variables from Kubernetes resources in dotenv format
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Author: ohmycoffe
|
|
7
|
+
Author-email: ohmycoffe1@gmail.com
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Requires-Dist: questionary (>=2.1.1,<3.0.0)
|
|
16
|
+
Requires-Dist: typer (>=0.16.0,<0.17.0)
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# kube-kenvx
|
|
20
|
+
|
|
21
|
+
[](https://www.python.org/)
|
|
22
|
+
[](LICENSE)
|
|
23
|
+
|
|
24
|
+
Extract and export environment variables from Kubernetes Deployments and Argo WorkflowTemplates — interactively or scripted.
|
|
25
|
+
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
**pipx**:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pipx install git+https://github.com/ohmycoffe/kube-envx.git
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Poetry** (development):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
poetry install
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
Run without arguments for fully interactive mode:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
kenvx
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or pass options directly to skip individual prompts:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
kenvx --kind deployment --namespace my-namespace --name my-service
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Options
|
|
57
|
+
|
|
58
|
+
| Option | Default | Description |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `--kind` | — | `deployment` or `workflowtemplate`. Prompted if omitted. |
|
|
61
|
+
| `--namespace` | — | Kubernetes namespace. Prompted if omitted. |
|
|
62
|
+
| `--name` | — | Resource name. Prompted if omitted. |
|
|
63
|
+
| `--output` | `env` | Output format: `env` or `json`. |
|
|
64
|
+
| `-v` / `-vv` | — | Verbosity: info / debug. |
|
|
65
|
+
|
|
66
|
+
### Environment variables
|
|
67
|
+
|
|
68
|
+
| Variable | Description |
|
|
69
|
+
|---|---|
|
|
70
|
+
| `ENVX_NAMESPACE` | Default namespace, overridden by `--namespace`. |
|
|
71
|
+
|
|
72
|
+
## Examples
|
|
73
|
+
|
|
74
|
+
Export env vars for a deployment to a dotenv file:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
kenvx --kind deployment --name my-service --namespace prod > .env
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Extract WorkflowTemplate env vars as JSON:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
kenvx --kind workflowtemplate --name my-workflow --namespace argo --output json
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Requirements
|
|
87
|
+
|
|
88
|
+
- Python 3.10+
|
|
89
|
+
- [`kubectl`](https://kubernetes.io/docs/tasks/tools/) configured with cluster access
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT — see [LICENSE](LICENSE).
|
|
94
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# kube-kenvx
|
|
2
|
+
|
|
3
|
+
[](https://www.python.org/)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
Extract and export environment variables from Kubernetes Deployments and Argo WorkflowTemplates — interactively or scripted.
|
|
7
|
+
|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
**pipx**:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pipx install git+https://github.com/ohmycoffe/kube-envx.git
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Poetry** (development):
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
poetry install
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
Run without arguments for fully interactive mode:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
kenvx
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or pass options directly to skip individual prompts:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
kenvx --kind deployment --namespace my-namespace --name my-service
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Options
|
|
39
|
+
|
|
40
|
+
| Option | Default | Description |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| `--kind` | — | `deployment` or `workflowtemplate`. Prompted if omitted. |
|
|
43
|
+
| `--namespace` | — | Kubernetes namespace. Prompted if omitted. |
|
|
44
|
+
| `--name` | — | Resource name. Prompted if omitted. |
|
|
45
|
+
| `--output` | `env` | Output format: `env` or `json`. |
|
|
46
|
+
| `-v` / `-vv` | — | Verbosity: info / debug. |
|
|
47
|
+
|
|
48
|
+
### Environment variables
|
|
49
|
+
|
|
50
|
+
| Variable | Description |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `ENVX_NAMESPACE` | Default namespace, overridden by `--namespace`. |
|
|
53
|
+
|
|
54
|
+
## Examples
|
|
55
|
+
|
|
56
|
+
Export env vars for a deployment to a dotenv file:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
kenvx --kind deployment --name my-service --namespace prod > .env
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Extract WorkflowTemplate env vars as JSON:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
kenvx --kind workflowtemplate --name my-workflow --namespace argo --output json
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Requirements
|
|
69
|
+
|
|
70
|
+
- Python 3.10+
|
|
71
|
+
- [`kubectl`](https://kubernetes.io/docs/tasks/tools/) configured with cluster access
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "kube-envx"
|
|
3
|
+
version = "0.2.2a0"
|
|
4
|
+
description = "Extract environment variables from Kubernetes resources in dotenv format"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "ohmycoffe",email = "ohmycoffe1@gmail.com"}
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"typer (>=0.16.0,<0.17.0)",
|
|
12
|
+
"questionary (>=2.1.1,<3.0.0)",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
kenvx = "kenvx.__main__:app"
|
|
17
|
+
envx = "kenvx.__main__:deprecated_entry"
|
|
18
|
+
|
|
19
|
+
[tool.poetry]
|
|
20
|
+
packages = [
|
|
21
|
+
{ from = "src", include = "kenvx" },
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[tool.poetry.group.dev.dependencies]
|
|
25
|
+
ruff = "^0.12.8"
|
|
26
|
+
pytest = "^9.0.3"
|
|
27
|
+
|
|
28
|
+
[tool.ruff]
|
|
29
|
+
line-length = 88
|
|
30
|
+
|
|
31
|
+
[tool.ruff.lint]
|
|
32
|
+
select = ["E", "F", "I", "B", "UP"]
|
|
33
|
+
ignore = ["E501"]
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
37
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from kenvx.cli.main import app
|
|
4
|
+
from kenvx.console import console
|
|
5
|
+
from kenvx.style import COLOR_WARNING
|
|
6
|
+
|
|
7
|
+
logging.basicConfig()
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def deprecated_entry() -> None:
|
|
11
|
+
console.print(
|
|
12
|
+
f"[bold {COLOR_WARNING}]Warning:[/] 'envx' has been deprecated, use 'kenvx' instead."
|
|
13
|
+
)
|
|
14
|
+
app()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
app()
|
|
File without changes
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import enum
|
|
4
|
+
import json
|
|
5
|
+
import subprocess
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
import questionary
|
|
9
|
+
import typer
|
|
10
|
+
|
|
11
|
+
from kenvx.console import console, print_error
|
|
12
|
+
from kenvx.kube import (
|
|
13
|
+
get_available_deployments,
|
|
14
|
+
get_available_namespaces,
|
|
15
|
+
get_available_workflowtemplates,
|
|
16
|
+
get_deployment_envs,
|
|
17
|
+
get_workflowtemplate_envs,
|
|
18
|
+
)
|
|
19
|
+
from kenvx.style import COLOR_MUTED, STYLE
|
|
20
|
+
from kenvx.utils import export_as_dotenv, resolve_namespace, setup_logging
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ResourceKind(str, enum.Enum):
|
|
24
|
+
DEPLOYMENT = "deployment"
|
|
25
|
+
WORKFLOWTEMPLATE = "workflowtemplate"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ExportFormat(str, enum.Enum):
|
|
29
|
+
ENV = "env"
|
|
30
|
+
JSON = "json"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
app = typer.Typer()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def select_resource_kind() -> ResourceKind:
|
|
37
|
+
selected = questionary.select(
|
|
38
|
+
"Select a kind:",
|
|
39
|
+
choices=[
|
|
40
|
+
questionary.Choice(
|
|
41
|
+
title="Deployment",
|
|
42
|
+
value=ResourceKind.DEPLOYMENT,
|
|
43
|
+
description="(Kubernetes Deployment)",
|
|
44
|
+
),
|
|
45
|
+
questionary.Choice(
|
|
46
|
+
title="WorkflowTemplate",
|
|
47
|
+
value=ResourceKind.WORKFLOWTEMPLATE,
|
|
48
|
+
description="(Argo WorkflowTemplate)",
|
|
49
|
+
),
|
|
50
|
+
],
|
|
51
|
+
use_jk_keys=False,
|
|
52
|
+
style=STYLE,
|
|
53
|
+
).ask()
|
|
54
|
+
if not selected:
|
|
55
|
+
raise typer.Exit(code=0)
|
|
56
|
+
return ResourceKind(selected)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@app.callback(invoke_without_command=True)
|
|
60
|
+
def get(
|
|
61
|
+
kind: Annotated[
|
|
62
|
+
ResourceKind | None,
|
|
63
|
+
typer.Option(
|
|
64
|
+
help="Kind of resource to get parameters for. If not provided, you will be prompted to select one.",
|
|
65
|
+
),
|
|
66
|
+
] = None,
|
|
67
|
+
namespace: Annotated[
|
|
68
|
+
str | None,
|
|
69
|
+
typer.Option(
|
|
70
|
+
envvar="KENVX_NAMESPACE",
|
|
71
|
+
help="Kubernetes namespace. If not provided, you will be prompted to select one.",
|
|
72
|
+
),
|
|
73
|
+
] = None,
|
|
74
|
+
name: Annotated[
|
|
75
|
+
str | None,
|
|
76
|
+
typer.Option(
|
|
77
|
+
help="Name of the resource. If not provided, you will be prompted to select one.",
|
|
78
|
+
),
|
|
79
|
+
] = None,
|
|
80
|
+
output: ExportFormat = ExportFormat.ENV,
|
|
81
|
+
verbose: Annotated[
|
|
82
|
+
int,
|
|
83
|
+
typer.Option(
|
|
84
|
+
"--verbose", "-v", count=True, help="Verbose output. Use -vv for debug."
|
|
85
|
+
),
|
|
86
|
+
] = 0,
|
|
87
|
+
):
|
|
88
|
+
"""
|
|
89
|
+
Get environment variables for a Kubernetes deployment or Argo WorkflowTemplate.
|
|
90
|
+
"""
|
|
91
|
+
setup_logging(verbose)
|
|
92
|
+
|
|
93
|
+
if kind is None:
|
|
94
|
+
kind = select_resource_kind()
|
|
95
|
+
|
|
96
|
+
try:
|
|
97
|
+
with console.status(f"[italic {COLOR_MUTED}]Fetching available namespaces…[/]"):
|
|
98
|
+
namespaces = get_available_namespaces()
|
|
99
|
+
except subprocess.CalledProcessError as e:
|
|
100
|
+
print_error(e, "Failed to fetch available namespaces")
|
|
101
|
+
raise typer.Exit(code=1) from None
|
|
102
|
+
|
|
103
|
+
namespace = resolve_namespace(namespace, available_namespaces=namespaces)
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
with console.status(
|
|
107
|
+
f"[italic {COLOR_MUTED}]Fetching available {kind.value}s in {namespace}…[/]"
|
|
108
|
+
):
|
|
109
|
+
if kind == ResourceKind.DEPLOYMENT:
|
|
110
|
+
resources = get_available_deployments(namespace=namespace)
|
|
111
|
+
else:
|
|
112
|
+
resources = get_available_workflowtemplates(namespace=namespace)
|
|
113
|
+
except subprocess.CalledProcessError as e:
|
|
114
|
+
print_error(
|
|
115
|
+
e, f"Failed to fetch available {kind.value}s in namespace '{namespace}'"
|
|
116
|
+
)
|
|
117
|
+
raise typer.Exit(code=1) from None
|
|
118
|
+
if not resources:
|
|
119
|
+
console.print(f"[red]Error: no {kind.value}s found in namespace '{namespace}'")
|
|
120
|
+
raise typer.Exit(code=1)
|
|
121
|
+
|
|
122
|
+
if not name:
|
|
123
|
+
name = questionary.select(
|
|
124
|
+
f"Select a {kind.value}:",
|
|
125
|
+
choices=resources,
|
|
126
|
+
use_search_filter=True,
|
|
127
|
+
use_jk_keys=False,
|
|
128
|
+
style=STYLE,
|
|
129
|
+
).ask()
|
|
130
|
+
if not name:
|
|
131
|
+
raise typer.Exit(code=0)
|
|
132
|
+
|
|
133
|
+
if name not in resources:
|
|
134
|
+
console.print(
|
|
135
|
+
f"[red]Error:[/red] {kind.value} '{name}' not found in namespace '{namespace}'."
|
|
136
|
+
)
|
|
137
|
+
raise typer.Exit(code=1)
|
|
138
|
+
|
|
139
|
+
try:
|
|
140
|
+
with console.status(
|
|
141
|
+
f"[italic {COLOR_MUTED}]Fetching environment variables…[/]"
|
|
142
|
+
):
|
|
143
|
+
if kind == ResourceKind.DEPLOYMENT:
|
|
144
|
+
vals = get_deployment_envs(namespace=namespace, name=name)
|
|
145
|
+
else:
|
|
146
|
+
vals = get_workflowtemplate_envs(namespace=namespace, name=name)
|
|
147
|
+
except subprocess.CalledProcessError as e:
|
|
148
|
+
print_error(e, f"Failed to fetch environment variables for '{name}'")
|
|
149
|
+
raise typer.Exit(code=1) from None
|
|
150
|
+
|
|
151
|
+
if output == ExportFormat.JSON:
|
|
152
|
+
formatted = json.dumps(vals, sort_keys=True)
|
|
153
|
+
elif output == ExportFormat.ENV:
|
|
154
|
+
formatted = export_as_dotenv(vals=vals, name=name)
|
|
155
|
+
print(formatted)
|
|
156
|
+
raise typer.Exit(code=0)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
if __name__ == "__main__":
|
|
160
|
+
app()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import subprocess
|
|
5
|
+
|
|
6
|
+
from rich.console import Console
|
|
7
|
+
from rich.panel import Panel
|
|
8
|
+
|
|
9
|
+
from kenvx.style import COLOR_ERROR
|
|
10
|
+
|
|
11
|
+
console = Console(stderr=True)
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def print_error(e: subprocess.CalledProcessError, msg: str) -> None:
|
|
17
|
+
logger.debug("kubectl error", exc_info=e, stack_info=True)
|
|
18
|
+
stderr = (e.stderr or "no output").strip()
|
|
19
|
+
stdout = (e.stdout or "no output").strip()
|
|
20
|
+
cmd = " ".join(e.cmd)
|
|
21
|
+
content = "\n".join(
|
|
22
|
+
[
|
|
23
|
+
"[dim]stderr:[/dim]",
|
|
24
|
+
f"[{COLOR_ERROR}]{stderr}[/]",
|
|
25
|
+
"",
|
|
26
|
+
"[dim]stdout:[/dim]",
|
|
27
|
+
stdout,
|
|
28
|
+
"",
|
|
29
|
+
f"[dim]command:[/dim] {cmd}",
|
|
30
|
+
f"[dim]exit code:[/dim] {e.returncode}",
|
|
31
|
+
]
|
|
32
|
+
)
|
|
33
|
+
console.print(
|
|
34
|
+
Panel(
|
|
35
|
+
content,
|
|
36
|
+
title=f"[bold {COLOR_ERROR}]{msg}[/]",
|
|
37
|
+
border_style=COLOR_ERROR,
|
|
38
|
+
expand=False,
|
|
39
|
+
)
|
|
40
|
+
)
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
import re
|
|
6
|
+
import subprocess
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from kenvx.utils import decode
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def call_subprocess(cmd: list[str]) -> str:
|
|
17
|
+
logger.debug("%s", " ".join(cmd))
|
|
18
|
+
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
|
19
|
+
return result.stdout
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_available_namespaces() -> list[str]:
|
|
23
|
+
cmd = ["kubectl", "get", "namespaces", "-o", "json"]
|
|
24
|
+
result = call_subprocess(cmd)
|
|
25
|
+
data = json.loads(result)
|
|
26
|
+
return [el["metadata"]["name"] for el in data["items"]]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@lru_cache
|
|
30
|
+
def get_secret(namespace: str, name: str) -> dict[str, Any]:
|
|
31
|
+
cmd = ["kubectl", "get", "secret", name, "-n", namespace, "-o", "json"]
|
|
32
|
+
result = call_subprocess(cmd)
|
|
33
|
+
secret = json.loads(result)
|
|
34
|
+
return secret
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@lru_cache
|
|
38
|
+
def get_configmap(namespace: str, name: str) -> dict[str, Any]:
|
|
39
|
+
cmd = ["kubectl", "get", "configmap", name, "-n", namespace, "-o", "json"]
|
|
40
|
+
result = call_subprocess(cmd)
|
|
41
|
+
configmap = json.loads(result)
|
|
42
|
+
return configmap
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _clean_key(key: str) -> str:
|
|
46
|
+
def strip_argo_inputs_param(key: str) -> str:
|
|
47
|
+
match = re.match(r"^\{\{inputs\.parameters\.(?P<param_name>\w+)\}\}$", key)
|
|
48
|
+
return match.group("param_name") if match else key
|
|
49
|
+
|
|
50
|
+
cleanups: list[Callable[[str], str]] = [
|
|
51
|
+
strip_argo_inputs_param,
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
for cleanup in cleanups:
|
|
55
|
+
key = cleanup(key)
|
|
56
|
+
return key
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_available_deployments(namespace: str) -> list[str]:
|
|
60
|
+
cmd = ["kubectl", "get", "deployment", "-n", namespace, "-o", "json"]
|
|
61
|
+
result = call_subprocess(cmd)
|
|
62
|
+
data = json.loads(result)
|
|
63
|
+
return [el["metadata"]["name"] for el in data["items"]]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def get_deployment_envs(namespace: str, name: str) -> dict[str, str]:
|
|
67
|
+
cmd = ["kubectl", "get", "deployment", name, "-n", namespace, "-o", "json"]
|
|
68
|
+
result = call_subprocess(cmd)
|
|
69
|
+
deployment = json.loads(result)
|
|
70
|
+
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
71
|
+
if len(containers) != 1:
|
|
72
|
+
raise ValueError(f"Expected 1 container, got {len(containers)}")
|
|
73
|
+
return extract_envs_from_container(namespace=namespace, container=containers[0])
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_available_workflowtemplates(namespace: str) -> list[str]:
|
|
77
|
+
cmd = ["kubectl", "get", "workflowtemplate", "-n", namespace, "-o", "json"]
|
|
78
|
+
result = call_subprocess(cmd)
|
|
79
|
+
data = json.loads(result)
|
|
80
|
+
return [el["metadata"]["name"] for el in data["items"]]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def get_workflowtemplate_envs(namespace: str, name: str) -> dict[str, str]:
|
|
84
|
+
cmd = ["kubectl", "get", "workflowtemplate", name, "-n", namespace, "-o", "json"]
|
|
85
|
+
result = call_subprocess(cmd)
|
|
86
|
+
workflow = json.loads(result)
|
|
87
|
+
envs = {}
|
|
88
|
+
for template in workflow["spec"]["templates"]:
|
|
89
|
+
if "container" not in template:
|
|
90
|
+
continue
|
|
91
|
+
fallback_keys = {
|
|
92
|
+
p["name"]: p["default"]
|
|
93
|
+
for p in template.get("inputs", {}).get("parameters", [])
|
|
94
|
+
if "default" in p
|
|
95
|
+
}
|
|
96
|
+
envs.update(
|
|
97
|
+
extract_envs_from_container(
|
|
98
|
+
namespace, template["container"], fallback_keys=fallback_keys
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
return envs
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def extract_envs_from_container(
|
|
105
|
+
namespace: str,
|
|
106
|
+
container: dict[str, Any],
|
|
107
|
+
fallback_keys: dict[str, str] | None = None,
|
|
108
|
+
) -> dict[str, str]:
|
|
109
|
+
if fallback_keys is None:
|
|
110
|
+
fallback_keys = {}
|
|
111
|
+
result = {}
|
|
112
|
+
if "envFrom" in container:
|
|
113
|
+
for env_from in container["envFrom"]:
|
|
114
|
+
if "configMapRef" in env_from:
|
|
115
|
+
configmap = get_configmap(namespace, env_from["configMapRef"]["name"])
|
|
116
|
+
result.update(configmap["data"])
|
|
117
|
+
elif "secretRef" in env_from:
|
|
118
|
+
secret_name = env_from["secretRef"]["name"]
|
|
119
|
+
secret = get_secret(namespace, secret_name)
|
|
120
|
+
encoded = {k: decode(v) for k, v in secret["data"].items()}
|
|
121
|
+
result.update(encoded)
|
|
122
|
+
else:
|
|
123
|
+
raise ValueError(f"Unknown envFrom format: {env_from}")
|
|
124
|
+
|
|
125
|
+
if "env" in container:
|
|
126
|
+
for env in container["env"]:
|
|
127
|
+
name = env["name"]
|
|
128
|
+
if "value" in env:
|
|
129
|
+
value = env["value"]
|
|
130
|
+
result[name] = value
|
|
131
|
+
elif "valueFrom" in env:
|
|
132
|
+
value_from = env["valueFrom"]
|
|
133
|
+
if "configMapKeyRef" in value_from:
|
|
134
|
+
configmap = get_configmap(
|
|
135
|
+
namespace, value_from["configMapKeyRef"]["name"]
|
|
136
|
+
)
|
|
137
|
+
key = value_from["configMapKeyRef"]["key"]
|
|
138
|
+
if key not in configmap["data"]:
|
|
139
|
+
key = fallback_keys.get(_clean_key(key), key)
|
|
140
|
+
if key not in configmap["data"]:
|
|
141
|
+
logger.warning(
|
|
142
|
+
f"{name} won't be set: key {key} not found in ConfigMap {value_from['configMapKeyRef']['name']}"
|
|
143
|
+
)
|
|
144
|
+
value = ""
|
|
145
|
+
else:
|
|
146
|
+
value = configmap["data"][key]
|
|
147
|
+
result[name] = value
|
|
148
|
+
elif "secretKeyRef" in value_from:
|
|
149
|
+
secret_name = value_from["secretKeyRef"]["name"]
|
|
150
|
+
encoded = get_secret(namespace, secret_name)
|
|
151
|
+
key = value_from["secretKeyRef"]["key"]
|
|
152
|
+
if key not in encoded["data"]:
|
|
153
|
+
key = fallback_keys.get(_clean_key(key), key)
|
|
154
|
+
if key not in encoded["data"]:
|
|
155
|
+
logger.warning(
|
|
156
|
+
f"{name} won't be set: key {key} not found in Secret {secret_name}"
|
|
157
|
+
)
|
|
158
|
+
value = ""
|
|
159
|
+
else:
|
|
160
|
+
value = decode(encoded["data"][key])
|
|
161
|
+
result[name] = value
|
|
162
|
+
else:
|
|
163
|
+
logger.warning(
|
|
164
|
+
f"Unknown valueFrom format: {value_from} for {name} ({env})"
|
|
165
|
+
)
|
|
166
|
+
else:
|
|
167
|
+
logger.warning(f"Unknown env format: {env}")
|
|
168
|
+
return result
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from questionary import Style
|
|
2
|
+
|
|
3
|
+
# fmt: off
|
|
4
|
+
COLOR_ACCENT = "#e5c07b" # yellow — draws attention (qmark)
|
|
5
|
+
COLOR_SUCCESS = "#98c379" # green — confirmed / selected
|
|
6
|
+
COLOR_ACTIVE = "#61afef" # blue — navigation / pointer
|
|
7
|
+
COLOR_WARNING = "#d19a66" # orange — warnings / cautions
|
|
8
|
+
COLOR_ERROR = "#e06c75" # red — errors / failures
|
|
9
|
+
COLOR_MUTED = "#5c6370" # gray — secondary elements
|
|
10
|
+
COLOR_SUBTLE = "#4b5263" # dark gray — near-invisible hints
|
|
11
|
+
|
|
12
|
+
# fmt: off
|
|
13
|
+
STYLE = Style(
|
|
14
|
+
[
|
|
15
|
+
("qmark", f"fg:{COLOR_ACCENT} bold"),
|
|
16
|
+
("question", "bold"),
|
|
17
|
+
("answer", f"fg:{COLOR_SUCCESS} bold"),
|
|
18
|
+
("pointer", f"fg:{COLOR_ACTIVE} bold"),
|
|
19
|
+
("highlighted", f"fg:{COLOR_ACTIVE} bold"),
|
|
20
|
+
("selected", f"fg:{COLOR_SUCCESS}"),
|
|
21
|
+
("separator", f"fg:{COLOR_MUTED}"),
|
|
22
|
+
("instruction", f"fg:{COLOR_SUBTLE} italic"),
|
|
23
|
+
("text", ""),
|
|
24
|
+
("disabled", f"fg:{COLOR_SUBTLE} italic"),
|
|
25
|
+
]
|
|
26
|
+
)
|
|
27
|
+
# fmt: on
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import datetime
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
import questionary
|
|
8
|
+
import typer
|
|
9
|
+
|
|
10
|
+
from kenvx.console import console
|
|
11
|
+
from kenvx.style import STYLE
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def setup_logging(verbose: int) -> None:
|
|
15
|
+
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
|
|
16
|
+
level = levels[min(verbose, len(levels) - 1)]
|
|
17
|
+
logging.getLogger("kenvx").setLevel(level)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def decode(val: str) -> str:
|
|
21
|
+
decoded_bytes = base64.b64decode(val)
|
|
22
|
+
return decoded_bytes.decode("utf-8")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def export_as_dotenv(vals: dict[str, str], name: str | None = None) -> str:
|
|
26
|
+
sorted_list = sorted(vals.items(), key=lambda x: x[0])
|
|
27
|
+
res = []
|
|
28
|
+
if name:
|
|
29
|
+
now = datetime.datetime.now().isoformat(timespec="seconds")
|
|
30
|
+
res.append(f"# {name} @ {now}")
|
|
31
|
+
for key, value in sorted_list:
|
|
32
|
+
res.append(f"{key}={value}")
|
|
33
|
+
return "\n".join(res)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def resolve_namespace(value: str | None, available_namespaces: list[str]) -> str:
|
|
37
|
+
if value:
|
|
38
|
+
if value not in available_namespaces:
|
|
39
|
+
console.print(f"[red]Error:[/red] namespace '{value}' not found.")
|
|
40
|
+
raise typer.Exit(code=1)
|
|
41
|
+
return value
|
|
42
|
+
selected = questionary.select(
|
|
43
|
+
"Select a namespace:",
|
|
44
|
+
choices=available_namespaces,
|
|
45
|
+
use_search_filter=True,
|
|
46
|
+
use_jk_keys=False,
|
|
47
|
+
style=STYLE,
|
|
48
|
+
).ask()
|
|
49
|
+
if not selected:
|
|
50
|
+
raise typer.Exit(code=0)
|
|
51
|
+
return selected
|