Skip to content

Commit a2f4a38

Browse files
author
Michael Sauter
authored
Merge pull request #146 from opendevstack/fix/omit-legacy-fields
Remove legacy fields /userNames and /groupNames
2 parents ed9a173 + d917465 commit a2f4a38

File tree

6 files changed

+121
-3
lines changed

6 files changed

+121
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: authorization.openshift.io/v1
2+
groupNames:
3+
- dedicated-admins
4+
- system:serviceaccounts:dedicated-admin
5+
kind: RoleBinding
6+
metadata:
7+
creationTimestamp: null
8+
name: admin-0
9+
roleRef:
10+
name: admin
11+
subjects:
12+
- kind: Group
13+
name: dedicated-admins
14+
- kind: SystemGroup
15+
name: system:serviceaccounts:dedicated-admin
16+
userNames: null
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: authorization.openshift.io/v1
2+
groupNames: null
3+
kind: RoleBinding
4+
metadata:
5+
name: admin-0
6+
roleRef:
7+
name: admin
8+
subjects:
9+
- kind: ServiceAccount
10+
name: jenkins
11+
namespace: foo-cd
12+
userNames:
13+
- system:serviceaccount:foo-cd:jenkins

internal/test/golden/export/rolebinding-generate-name.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apiVersion: template.openshift.io/v1
22
kind: Template
33
objects:
44
- apiVersion: authorization.openshift.io/v1
5-
groupNames: null
65
kind: RoleBinding
76
metadata:
87
generateName: system:image-pusher-
@@ -12,5 +11,3 @@ objects:
1211
- kind: ServiceAccount
1312
name: default
1413
namespace: foo-dev
15-
userNames:
16-
- system:serviceaccount:foo-dev:default
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- Current State (OpenShift cluster)
2+
+++ Desired State (Processed template)
3+
@@ -5,8 +5,7 @@
4+
roleRef:
5+
name: admin
6+
subjects:
7+
-- kind: Group
8+
- name: dedicated-admins
9+
-- kind: SystemGroup
10+
- name: system:serviceaccounts:dedicated-admin
11+
+- kind: ServiceAccount
12+
+ name: jenkins
13+
+ namespace: foo-cd

pkg/openshift/changeset_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,79 @@ func TestCalculateChangesAppliedConfiguration(t *testing.T) {
333333
}
334334
}
335335

336+
func TestCalculateChangesOmittedFields(t *testing.T) {
337+
338+
tests := map[string]struct {
339+
platformFixture string
340+
templateFixture string
341+
expectedAction string
342+
expectedPatches jsonPatches
343+
expectedDiffGoldenFile string
344+
}{
345+
"Rolebinding with legacy fields": {
346+
platformFixture: "rolebinding-platform",
347+
templateFixture: "rolebinding-template",
348+
expectedAction: "Update",
349+
expectedPatches: jsonPatches{
350+
&jsonPatch{
351+
Op: "replace",
352+
Path: "/subjects/0/kind",
353+
Value: "ServiceAccount",
354+
},
355+
&jsonPatch{
356+
Op: "replace",
357+
Path: "/subjects/0/name",
358+
Value: "jenkins",
359+
},
360+
&jsonPatch{
361+
Op: "add",
362+
Path: "/subjects/0/namespace",
363+
Value: "foo-cd",
364+
},
365+
&jsonPatch{
366+
Op: "remove",
367+
Path: "/subjects/1",
368+
},
369+
},
370+
expectedDiffGoldenFile: "rolebinding-changed",
371+
},
372+
}
373+
374+
for name, tc := range tests {
375+
t.Run(name, func(t *testing.T) {
376+
platformItem := getPlatformItem(t, "item-omitted-fields/"+tc.platformFixture+".yml")
377+
templateItem := getTemplateItem(t, "item-omitted-fields/"+tc.templateFixture+".yml")
378+
changes, err := calculateChanges(templateItem, platformItem, []string{}, true)
379+
if err != nil {
380+
t.Fatal(err)
381+
}
382+
if len(changes) != 1 {
383+
t.Fatalf("Expected 1 change, got: %d", len(changes))
384+
}
385+
actualChange := changes[0]
386+
if actualChange.Action != tc.expectedAction {
387+
t.Fatalf("Expected change action to be: %s, got: %s. Patches: \n%s", tc.expectedAction, actualChange.Action, actualChange.PrettyJSONPatches())
388+
}
389+
if len(actualChange.Patches) != len(tc.expectedPatches) {
390+
t.Fatalf("Expected patches:\n%s\n--- got: ---\n%s", pretty(tc.expectedPatches), actualChange.PrettyJSONPatches())
391+
}
392+
for i, got := range actualChange.Patches {
393+
want := tc.expectedPatches[i]
394+
if diff := cmp.Diff(want, got); diff != "" {
395+
t.Errorf("Change diff mismatch (-want +got):\n%s", diff)
396+
}
397+
}
398+
if len(tc.expectedDiffGoldenFile) > 0 {
399+
want := strings.TrimSpace(getGoldenDiff(t, "item-omitted-fields", tc.expectedDiffGoldenFile+".txt"))
400+
got := strings.TrimSpace(actualChange.Diff(true))
401+
if diff := cmp.Diff(want, got); diff != "" {
402+
t.Errorf("Change diff mismatch (-want +got):\n%s", diff)
403+
}
404+
}
405+
})
406+
}
407+
}
408+
336409
func TestEmptyValuesDoNotCauseDrift(t *testing.T) {
337410
platformItem := getPlatformItem(t, "empty-values/bc-platform.yml")
338411
templateItem := getTemplateItem(t, "empty-values/bc-template.yml")

pkg/openshift/item.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var (
3030
"/status",
3131
"/spec/volumeName",
3232
"/spec/template/metadata/creationTimestamp",
33+
"/groupNames",
34+
"/userNames",
3335
}
3436
platformManagedRegexFields = []string{
3537
"^/spec/triggers/[0-9]*/imageChangeParams/lastTriggeredImage",
@@ -246,9 +248,13 @@ func (i *ResourceItem) parseConfig(m map[string]interface{}) error {
246248
}
247249

248250
// Remove platform-managed simple fields
251+
legacyFields := []string{"/userNames", "/groupNames"}
249252
for _, p := range platformManagedSimpleFields {
250253
deletePointer, _ := gojsonpointer.NewJsonPointer(p)
251254
_, _ = deletePointer.Delete(m)
255+
if utils.Includes(legacyFields, p) {
256+
cli.VerboseMsg("Removed", p, "which is used for legacy clients, but not supported by Tailor")
257+
}
252258
}
253259

254260
i.Config = m

0 commit comments

Comments
 (0)