Skip to content

Commit 0f66b26

Browse files
committed
feat: support for kustomize
1 parent dbeecaf commit 0f66b26

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

transpire/internal/kustomize.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import shutil
2+
import urllib.parse
3+
from subprocess import PIPE, run
4+
from typing import Any
5+
6+
import yaml
7+
8+
from transpire.internal.config import CLIConfig
9+
from transpire.internal.context import get_app_context
10+
11+
__all__ = ["build_kustomization_from_versions", "build_kustomization"]
12+
13+
14+
def assert_kubectl() -> None:
15+
"""ensure kubectl binary is available"""
16+
17+
if shutil.which("kubectl") is None:
18+
raise RuntimeError("`kubectl` must be installed and in your $PATH")
19+
20+
21+
def exec_kustomize(args: list[str], check: bool = True) -> tuple[bytes, bytes]:
22+
"""executes a kustomize command and returns (stdout, stderr)"""
23+
24+
config = CLIConfig.from_env()
25+
process = run(
26+
[
27+
"kubectl",
28+
"kustomize",
29+
*args,
30+
],
31+
check=False,
32+
stdout=PIPE,
33+
stderr=PIPE,
34+
)
35+
36+
if check and process.returncode != 0:
37+
raise ValueError(process.stderr)
38+
39+
return (process.stdout, process.stderr)
40+
41+
42+
def build_kustomization(
43+
repo_url: str,
44+
path: str,
45+
version: str,
46+
) -> list[dict]:
47+
"""build a kustomization and return a list of manifests"""
48+
49+
kustomize_url = urllib.parse.urljoin(repo_url, path)
50+
full_url = urllib.parse.urlparse(f"{kustomize_url}")._replace(query=f"ref={version}")
51+
52+
# TODO: Capture `stderr` output and make available to tracing.
53+
stdout, _ = exec_kustomize(
54+
[full_url.geturl()],
55+
check=True,
56+
)
57+
58+
# save our souls
59+
# <https://github.com/prometheus-community/helm-charts/pull/2238>
60+
# <https://github.com/prometheus-operator/prometheus-operator/pull/4897>
61+
# <https://github.com/yaml/pyyaml/issues/89>
62+
# <https://github.com/allenporter/k8s-gitops/commit/304c64c57926d2747328c0803c246be7dd827fdd>
63+
yaml.constructor.SafeConstructor.add_constructor(
64+
"tag:yaml.org,2002:value", yaml.constructor.SafeConstructor.construct_yaml_str # type: ignore
65+
)
66+
67+
return list(yaml.safe_load_all(stdout))
68+
69+
70+
def build_kustomization_from_versions(
71+
name: str,
72+
versions: dict[str, Any],
73+
values: dict = {},
74+
) -> list[dict]:
75+
"""thin wrapper around build_chart that builds based off a versions dict"""
76+
77+
return build_kustomization(
78+
repo_url=versions[name]["repo_url"],
79+
path=versions[name]["path"],
80+
version=versions[name]["version"],
81+
)

transpire/kustomize.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from transpire.internal.kustomize import build_kustomization_from_versions, build_kustomization
2+
3+
__all__ = ["build_kustomization_from_versions", "build_kustomization"]

0 commit comments

Comments
 (0)