Skip to content

Commit

Permalink
refactor(helm-diff)!: use lists for ignore keypaths
Browse files Browse the repository at this point in the history
replaces dot-notation
  • Loading branch information
disrupted committed Feb 26, 2025
1 parent 2f261fb commit dce4b4e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 28 deletions.
5 changes: 3 additions & 2 deletions docs/docs/resources/pipeline-config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ helm_config:
helm_diff_config:
# Set of keys that should not be checked.
ignore:
- name
- imageTag
- ["name"]
- ["imageTag"]
- ["metadata", "labels", "helm.sh/chart"]
# Whether to retain clean up jobs in the cluster or uninstall the, after
# completion.
retain_clean_jobs: false
2 changes: 1 addition & 1 deletion docs/docs/resources/variables/config_env_vars.env
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ KPOPS_HELM_CONFIG__DEBUG=False
# Kubernetes API version used for `Capabilities.APIVersions`
KPOPS_HELM_CONFIG__API_VERSION # No default value, not required
# helm_diff_config.ignore
# Set of keys that should not be checked.
# List of keypaths that should be excluded from the diff.
KPOPS_HELM_DIFF_CONFIG__IGNORE # No default value, required
# retain_clean_jobs
# Whether to retain clean up jobs in the cluster or uninstall the,
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/resources/variables/config_env_vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ These variables take precedence over the settings in `config.yaml`. Variables ma
|KPOPS_HELM_CONFIG__CONTEXT | |False |Name of kubeconfig context (`--kube-context`) |helm_config.context |
|KPOPS_HELM_CONFIG__DEBUG |False |False |Run Helm in Debug mode |helm_config.debug |
|KPOPS_HELM_CONFIG__API_VERSION | |False |Kubernetes API version used for `Capabilities.APIVersions` |helm_config.api_version |
|KPOPS_HELM_DIFF_CONFIG__IGNORE | |True |Set of keys that should not be checked. |helm_diff_config.ignore |
|KPOPS_HELM_DIFF_CONFIG__IGNORE | |True |List of keypaths that should be excluded from the diff. |helm_diff_config.ignore |
|KPOPS_RETAIN_CLEAN_JOBS |False |False |Whether to retain clean up jobs in the cluster or uninstall the, after completion.|retain_clean_jobs |
|KPOPS_STRIMZI_TOPIC | |False |Configuration for Strimzi Kafka Topics. |strimzi_topic |
|KPOPS_OPERATION_MODE |managed |False |The operation mode of KPOps (managed, manifest, argo). |operation_mode |
22 changes: 17 additions & 5 deletions docs/docs/schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,28 @@
"HelmDiffConfig": {
"properties": {
"ignore": {
"description": "Set of keys that should not be checked.",
"description": "List of keypaths that should be excluded from the diff.",
"examples": [
"- name\n- imageTag"
[
"name"
],
[
"imageTag"
],
[
"metadata",
"labels",
"helm.sh/chart"
]
],
"items": {
"type": "string"
"items": {
"type": "string"
},
"type": "array"
},
"title": "Ignore",
"type": "array",
"uniqueItems": true
"type": "array"
}
},
"title": "HelmDiffConfig",
Expand Down
10 changes: 6 additions & 4 deletions kpops/component_handlers/helm_wrapper/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
from kpops.utils.docstring import describe_attr
from kpops.utils.pydantic import DescConfigModel

IgnoreKeyPath = tuple[str, ...]


class HelmDiffConfig(BaseModel):
ignore: set[str] = Field(
default_factory=set,
description="Set of keys that should not be checked.",
examples=["- name\n- imageTag"],
ignore: list[IgnoreKeyPath] = Field(
default_factory=list,
description="List of keypaths that should be excluded from the diff.",
examples=[("name",), ("imageTag",), ("metadata", "labels", "helm.sh/chart")],
)


Expand Down
7 changes: 4 additions & 3 deletions kpops/utils/dict_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import yaml
from dictdiffer import diff, patch

from kpops.component_handlers.helm_wrapper.model import IgnoreKeyPath

if TYPE_CHECKING:
from collections.abc import Iterable, Iterator, Sequence

Expand Down Expand Up @@ -85,13 +87,12 @@ def __find_changed_key(key_1: list[str] | str, key_2: str = "") -> str:
def render_diff(
d1: MutableMapping[str, Any],
d2: MutableMapping[str, Any],
ignore: set[str] | None = None,
ignore: list[IgnoreKeyPath] | None = None,
) -> str | None:
def del_ignored_keys(d: MutableMapping[str, Any]) -> None:
"""Delete key to be ignored, dictionary is modified in-place."""
if ignore:
for i in ignore:
key_path = i.split(".")
for key_path in ignore:
nested = d
try:
for key in key_path[:-1]:
Expand Down
25 changes: 13 additions & 12 deletions tests/utils/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import pytest

from kpops.component_handlers.helm_wrapper.model import IgnoreKeyPath
from kpops.utils.dict_differ import Change, Diff, DiffType, render_diff


@pytest.mark.parametrize(
("d1", "d2", "ignore", "output"),
[
({}, {}, None, None),
({}, {}, "a.b", None),
(
pytest.param({}, {}, None, None),
pytest.param({}, {}, [("a", "b")], None),
pytest.param(
{"a": 1, "b": 2, "c": 3},
{"a": 2, "d": 1},
None,
Expand All @@ -23,19 +24,16 @@
"\x1b[0m\x1b[31m- c: 3\n"
"\x1b[0m",
),
(
pytest.param(
{"a": 1, "b": 2, "c": 3},
{"a": 2, "d": 1},
{"a"},
"\x1b[32m+ d: 1\n"
"\x1b[0m\x1b[31m- b: 2\n"
"\x1b[0m\x1b[31m- c: 3\n"
"\x1b[0m",
[("a")],
"\x1b[32m+ d: 1\n\x1b[0m\x1b[31m- b: 2\n\x1b[0m\x1b[31m- c: 3\n\x1b[0m",
),
(
pytest.param(
{"a": {"a": 1, "b": 2, "c": 3}, "b": 2, "c": 3},
{"a": {"a": 9, "b": 8}, "d": 1},
{"a.a"},
[("a", "a")],
" a:\n"
"\x1b[31m- b: 2\n"
"\x1b[0m\x1b[33m? ^\n"
Expand All @@ -50,7 +48,10 @@
],
)
def test_render_diff(
d1: dict[str, Any], d2: dict[str, Any], ignore: set[str] | None, output: str | None
d1: dict[str, Any],
d2: dict[str, Any],
ignore: list[IgnoreKeyPath] | None,
output: str | None,
):
assert render_diff(d1, d2, ignore) == output

Expand Down

0 comments on commit dce4b4e

Please sign in to comment.