7
7
import yaml
8
8
9
9
from . import git
10
- from .helpers import rm_tree_contents , lieutenant_query
10
+ from .helpers import rm_tree_contents , lieutenant_query , sliding_window
11
11
from .cluster import Cluster
12
12
from .config import Config , Migration
13
13
from .k8sobject import K8sObject
@@ -116,43 +116,55 @@ def _push_catalog(cfg: Config, repo, commit_message):
116
116
click .echo (" > Skipping commit+push to catalog in local mode..." )
117
117
118
118
119
- def _ignore_kapitan_029_030_non_semantic ( line ) :
119
+ def _is_semantic_diff_kapitan_029_030 ( win : Tuple [ str , str ]) -> bool :
120
120
"""
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.
123
124
124
- This includes:
125
+ The function expects pairs of diff lines as input.
126
+
127
+ The function treats the following diffs as non-semantic:
125
128
* Change of "app.kubernetes.io/managed-by: Tiller" to
126
129
"app.kubernetes.io/managed-by: Helm"
127
130
* Change of "heritage: Tiller" to "heritage: Helm"
128
131
* `null` objects not emitted in multi-object YAML documents anymore
129
132
"""
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
+
134
144
# Ignore changes where we don't emit a null object preceded or followed
135
145
# 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
+
138
151
# 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
149
159
150
- return False
160
+ # Don't ignore any other diffs
161
+ return True
151
162
152
163
153
164
def _kapitan_029_030_difffunc (
154
165
before_text : str , after_text : str , fromfile : str = "" , tofile : str = ""
155
166
) -> Tuple [Iterable [str ], bool ]:
167
+
156
168
before_objs = sorted (yaml .safe_load_all (before_text ), key = K8sObject )
157
169
before_sorted_lines = yaml .dump_all (before_objs ).split ("\n " )
158
170
@@ -167,8 +179,9 @@ def _kapitan_029_030_difffunc(
167
179
tofile = tofile ,
168
180
)
169
181
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 )
172
185
)
173
186
174
187
return diff_lines , suppress_diff
0 commit comments