Skip to content

Commit

Permalink
Use mashumaro for yaml parsing and drop pydantic (#631)
Browse files Browse the repository at this point in the history
Allows removing a lot of code, given mashumaro is simpler. pydantic also
had a very odd upgrade strategy, and is growing too complex for use in
this project.
  • Loading branch information
allenporter authored Apr 21, 2024
1 parent 4d83af7 commit 5507e74
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 171 deletions.
187 changes: 55 additions & 132 deletions flux_local/manifest.py

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions flux_local/tool/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def run( # type: ignore[no-untyped-def]
cols.insert(0, "cluster")
for cluster in manifest.clusters:
for ks in cluster.kustomizations:
value = ks.dict(include=set(cols))
value = { k: v for k, v in ks.compact_dict().items() if k in cols }
if output == "wide":
value["helmrepos"] = len(ks.helm_repos)
value["releases"] = len(ks.helm_releases)
Expand Down Expand Up @@ -126,7 +126,7 @@ async def run( # type: ignore[no-untyped-def]
results: list[dict[str, Any]] = []
for cluster in manifest.clusters:
for helmrelease in cluster.helm_releases:
value = helmrelease.dict(include=set(cols))
value = { k: v for k, v in helmrelease.compact_dict().items() if k in cols }
value["revision"] = str(helmrelease.chart.version)
value["chart"] = f"{helmrelease.namespace}-{helmrelease.chart.name}"
value["source"] = helmrelease.chart.repo_name
Expand Down Expand Up @@ -230,7 +230,7 @@ async def run( # type: ignore[no-untyped-def]
cols = ["path", "kustomizations"]
results: list[dict[str, Any]] = []
for cluster in manifest.clusters:
value: dict[str, Any] = cluster.dict(include=set(cols))
value = { k: v for k, v in cluster.compact_dict().items() if k in cols }
value["kustomizations"] = len(cluster.kustomizations)
results.append(value)

Expand Down
3 changes: 2 additions & 1 deletion flux_local/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def expand_value_references(
f"Error building HelmRelease '{helm_release.namespaced_name}': {err}"
)

return helm_release.model_copy(update={"values": values})
helm_release.values = values
return helm_release


def expand_postbuild_substitute_reference(
Expand Down
7 changes: 0 additions & 7 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[mypy]
plugins = pydantic.mypy
ignore_missing_imports = True
exclude = (venv|build)
check_untyped_defs = True
Expand All @@ -16,9 +15,3 @@ warn_unreachable = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_no_return = True

[pydantic-mypy]
init_forbid_extra = False
init_typed = True
warn_required_dynamic_aliases = True
warn_untyped_fields = True
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ aiofiles==23.2.1
black==24.4.0
coverage==7.4.4
GitPython==3.1.43
mashumaro==3.12
mypy==1.9.0
nest_asyncio==1.6.0
pdoc==14.4.0
pip==24.0
pre-commit==3.7.0
pydantic==2.7.0
pytest==8.1.1
pytest-asyncio==0.23.6
pytest-cov==5.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ python_requires = >=3.10
install_requires =
aiofiles>=22.1.0
nest_asyncio>=1.5.6
pydantic>=2.5.2
python-slugify>=8.0.0
GitPython>=3.1.30
PyYAML>=6.0
mashumaro>=3.12
# Note: flux-local provides repo testing using pytest
pytest>=7.2.1
pytest-asyncio>=0.20.3
Expand Down
2 changes: 1 addition & 1 deletion tests/test_kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def test_target_namespace() -> None:
async def test_flux_build_path_is_not_dir() -> None:
"""Test case where the flux build path does not exist."""
cmd = kustomize.flux_build(
manifest.Kustomization(name="example", path="./"),
manifest.Kustomization(name="example", path="./", namespace="flux-system"),
Path(TESTDATA_DIR) / "does-not-exist",
)
with pytest.raises(exceptions.FluxException, match="not a directory"):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ async def test_serializing_manifest(tmp_path: Path) -> None:
)
await write_manifest(tmp_path / "file.yaml", manifest)
new_manifest = await read_manifest(tmp_path / "file.yaml")
assert new_manifest.model_dump() == {
assert new_manifest.compact_dict() == {
"clusters": [
{
"path": "./example",
"kustomizations": [],
},
]
}
assert new_manifest.compact_dict() == new_manifest.model_dump()
assert new_manifest.compact_dict() == manifest.model_dump()
33 changes: 11 additions & 22 deletions tests/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ def test_values_references_with_values_key() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
valuesKey="some-key",
targetPath="target.path",
values_key="some-key",
target_path="target.path",
),
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-binary-data-configmap",
valuesKey="some-key",
values_key="some-key",
),
],
)
Expand Down Expand Up @@ -115,10 +113,9 @@ def test_values_references_with_missing_values_key() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
valuesKey="some-key-does-not-exist",
targetPath="target.path",
values_key="some-key-does-not-exist",
target_path="target.path",
)
],
)
Expand Down Expand Up @@ -156,7 +153,6 @@ def test_values_references_invalid_yaml() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
)
],
Expand Down Expand Up @@ -193,7 +189,6 @@ def test_values_references_invalid_binary_data() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
)
],
Expand Down Expand Up @@ -233,11 +228,10 @@ def test_values_reference_invalid_target_path() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
valuesKey="some-key",
values_key="some-key",
# Target above is a list
targetPath="target.path",
target_path="target.path",
)
],
)
Expand Down Expand Up @@ -273,19 +267,16 @@ def test_values_reference_invalid_configmap_and_secret() -> None:
values_from=[
ValuesReference(
kind="ConfigMap",
namespace="test",
name="test-values-configmap",
optional=False, # We just log
),
ValuesReference(
kind="Secret",
namespace="test",
name="test-values-secret",
optional=False, # We just log
),
ValuesReference(
kind="UnknownKind",
namespace="test",
name="test-values-secret",
),
],
Expand Down Expand Up @@ -317,17 +308,15 @@ def test_values_references_secret() -> None:
values_from=[
ValuesReference(
kind="Secret",
namespace="test",
name="test-values-secret",
valuesKey="some-key1",
targetPath="target.path1",
values_key="some-key1",
target_path="target.path1",
),
ValuesReference(
kind="Secret",
namespace="test",
name="test-string-values-secret",
valuesKey="some-key2",
targetPath="target.path2",
values_key="some-key2",
target_path="target.path2",
),
],
)
Expand Down

0 comments on commit 5507e74

Please sign in to comment.