Skip to content

Commit b82cd62

Browse files
committed
Improve smart diffing to remove possibility to generate spurious suppressions
1 parent 13d7125 commit b82cd62

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

commodore/catalog.py

+37-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import yaml
88

99
from . import git
10-
from .helpers import rm_tree_contents, lieutenant_query
10+
from .helpers import rm_tree_contents, lieutenant_query, sliding_window
1111
from .cluster import Cluster
1212
from .config import Config, Migration
1313
from .k8sobject import K8sObject
@@ -116,43 +116,55 @@ def _push_catalog(cfg: Config, repo, commit_message):
116116
click.echo(" > Skipping commit+push to catalog in local mode...")
117117

118118

119-
def _ignore_kapitan_029_030_non_semantic(line):
119+
def _is_semantic_diff_kapitan_029_030(win: Tuple[str, str]) -> bool:
120120
"""
121-
Ignore non-semantic changes in the (already sorted by K8s object) diff between
122-
Kapitan 0.29 and 0.30.
121+
Returns True if a pair of lines of a diff which is already sorted
122+
by K8s object indicates that this diff contains a semantic change
123+
when migrating from Kapitan 0.29 to 0.30.
123124
124-
This includes:
125+
The function expects pairs of diff lines as input.
126+
127+
The function treats the following diffs as non-semantic:
125128
* Change of "app.kubernetes.io/managed-by: Tiller" to
126129
"app.kubernetes.io/managed-by: Helm"
127130
* Change of "heritage: Tiller" to "heritage: Helm"
128131
* `null` objects not emitted in multi-object YAML documents anymore
129132
"""
130-
line = line.strip()
131-
# We don't care about context and metadata lines
132-
if not (line.startswith("-") or line.startswith("+")):
133-
return True
133+
line_a, line_b = map(str.rstrip, win)
134+
135+
# Ignore context and metadata lines:
136+
if (
137+
line_a.startswith(" ")
138+
or line_b.startswith(" ")
139+
or line_a.startswith("@@")
140+
or line_b.startswith("@@")
141+
):
142+
return False
143+
134144
# Ignore changes where we don't emit a null object preceded or followed
135145
# by a stream separator anymore
136-
if line in ("-null", "----"):
137-
return True
146+
if line_a == "-null" and line_b == "----":
147+
return False
148+
if line_b == "-null" and line_a == "----":
149+
return False
150+
138151
# Ignore changes which are only about Tiller -> Helm as object manager
139-
if line.startswith("-") and (
140-
line.endswith("app.kubernetes.io/managed-by: Tiller")
141-
or line.endswith("heritage: Tiller")
142-
):
143-
return True
144-
if line.startswith("+") and (
145-
line.endswith("app.kubernetes.io/managed-by: Helm")
146-
or line.endswith("heritage: Helm")
147-
):
148-
return True
152+
if line_a.startswith("-") and line_b.startswith("+"):
153+
if line_a.endswith("app.kubernetes.io/managed-by: Tiller") and line_b.endswith(
154+
"app.kubernetes.io/managed-by: Helm"
155+
):
156+
return False
157+
if line_a.endswith("heritage: Tiller") and line_b.endswith("heritage: Helm"):
158+
return False
149159

150-
return False
160+
# Don't ignore any other diffs
161+
return True
151162

152163

153164
def _kapitan_029_030_difffunc(
154165
before_text: str, after_text: str, fromfile: str = "", tofile: str = ""
155166
) -> Tuple[Iterable[str], bool]:
167+
156168
before_objs = sorted(yaml.safe_load_all(before_text), key=K8sObject)
157169
before_sorted_lines = yaml.dump_all(before_objs).split("\n")
158170

@@ -167,8 +179,9 @@ def _kapitan_029_030_difffunc(
167179
tofile=tofile,
168180
)
169181
diff_lines = list(diff)
170-
suppress_diff = all(
171-
_ignore_kapitan_029_030_non_semantic(line) for line in diff_lines[2:]
182+
suppress_diff = not any(
183+
_is_semantic_diff_kapitan_029_030(win)
184+
for win in sliding_window(diff_lines[2:], 2)
172185
)
173186

174187
return diff_lines, suppress_diff

0 commit comments

Comments
 (0)