Skip to content

Commit fe5f3eb

Browse files
author
Michael Sauter
authored
Merge pull request #180 from opendevstack/fix/empty-drift
Do not calculate drift when adding empty string to missing fields
2 parents f6006c6 + 342ac2d commit fe5f3eb

File tree

6 files changed

+99
-13
lines changed

6 files changed

+99
-13
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
kind: BuildConfig
2+
apiVersion: v1
3+
metadata:
4+
name: foo
5+
labels:
6+
app: foo
7+
spec:
8+
nodeSelector: null
9+
postCommit: {}
10+
resources: {}
11+
runPolicy: Serial
12+
triggers: []
13+
source:
14+
binary: {}
15+
type: Binary
16+
strategy:
17+
type: Docker
18+
dockerStrategy:
19+
env:
20+
- name: FOO_BAR
21+
- name: BAZ
22+
value: qux
23+
output:
24+
to:
25+
kind: ImageStreamTag
26+
name: foo:latest
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
kind: BuildConfig
2+
apiVersion: v1
3+
metadata:
4+
name: foo
5+
labels:
6+
app: foo
7+
spec:
8+
nodeSelector: null
9+
postCommit: {}
10+
resources: {}
11+
runPolicy: Serial
12+
triggers: []
13+
source:
14+
binary: {}
15+
type: Binary
16+
strategy:
17+
type: Docker
18+
dockerStrategy:
19+
env:
20+
- name: FOO_BAR
21+
value: ""
22+
- name: BAZ
23+
value: qux
24+
output:
25+
to:
26+
kind: ImageStreamTag
27+
name: foo:latest

pkg/openshift/changeset.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,19 @@ func calculateChanges(templateItem *ResourceItem, platformItem *ResourceItem, pr
160160

161161
}
162162
comparedPaths[path] = true
163-
addedPaths = append(addedPaths, path)
163+
164+
// OpenShift sometimes removes the whole field when the value is an
165+
// empty string. Therefore, we do not want to add the path in that
166+
// case, otherwise we would cause endless drift. See
167+
// https://github.com/opendevstack/tailor/issues/157.
168+
if v, ok := templateItemVal.(string); ok && len(v) == 0 {
169+
_, err := pathPointer.Delete(templateItem.Config)
170+
if err != nil {
171+
return nil, err
172+
}
173+
} else {
174+
addedPaths = append(addedPaths, path)
175+
}
164176
} else {
165177
// Pointer exists in both items
166178
switch templateItemVal.(type) {

pkg/openshift/changeset_test.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,40 @@ func TestCalculateChangesOmittedFields(t *testing.T) {
256256
}
257257

258258
func TestEmptyValuesDoNotCauseDrift(t *testing.T) {
259-
platformItem := getPlatformItem(t, "empty-values/bc-platform.yml")
260-
templateItem := getTemplateItem(t, "empty-values/bc-template.yml")
261-
changes, err := calculateChanges(templateItem, platformItem, []string{}, true)
262-
if err != nil {
263-
t.Fatal(err)
264-
}
265-
if len(changes) != 1 {
266-
t.Fatalf("Expected 1 change, got: %d", len(changes))
259+
260+
tests := map[string]struct {
261+
platformFixture string
262+
templateFixture string
263+
expectedAction string
264+
}{
265+
"Field not defined in template": {
266+
platformFixture: "bc-platform-defaulted.yml",
267+
templateFixture: "bc-template-defaulted.yml",
268+
expectedAction: "Noop",
269+
},
270+
"Field not set in platform, and empty in template": {
271+
platformFixture: "bc-platform-missing-env.yml",
272+
templateFixture: "bc-template-empty-env.yml",
273+
expectedAction: "Noop",
274+
},
267275
}
268-
actualChange := changes[0]
269-
expectedAction := "Noop"
270-
if actualChange.Action != expectedAction {
271-
t.Fatalf("Expected change action to be: %s, got: %s. Diff was: %s", expectedAction, actualChange.Action, actualChange.Diff(false))
276+
277+
for name, tc := range tests {
278+
t.Run(name, func(t *testing.T) {
279+
platformItem := getPlatformItem(t, "empty-values/"+tc.platformFixture)
280+
templateItem := getTemplateItem(t, "empty-values/"+tc.templateFixture)
281+
changes, err := calculateChanges(templateItem, platformItem, []string{}, true)
282+
if err != nil {
283+
t.Fatal(err)
284+
}
285+
if len(changes) != 1 {
286+
t.Fatalf("Expected 1 change, got: %d", len(changes))
287+
}
288+
actualChange := changes[0]
289+
if actualChange.Action != tc.expectedAction {
290+
t.Fatalf("Expected change action to be: %s, got: %s. Diff was: %s", tc.expectedAction, actualChange.Action, actualChange.Diff(false))
291+
}
292+
})
272293
}
273294
}
274295

0 commit comments

Comments
 (0)