Skip to content

Commit ac64299

Browse files
authored
Adds an Ignore option for Metadata (#44)
1 parent 191d64b commit ac64299

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ To help in these scenarios there are the following options to be used when calcu
6565
- `IgnoreStatusFields`
6666
- `IgnoreVolumeClaimTemplateTypeMetaAndStatus`
6767
- `IgnorePDBSelector`
68+
- `IgnoreField("field-name-to-ignore")`
6869

6970
Example:
7071
```
@@ -91,6 +92,11 @@ This CalculateOption clears volumeClaimTemplate fields from both objects before
9192
Checks `selector` fields of PDB objects before comparing and removes them if they match. `reflect.DeepEquals` is used for the equality check.
9293
This is required because map fields using `patchStrategy:"replace"` will always diff regardless if they are otherwise equal.
9394

95+
#### IgnoreField("field-name-to-ignore")
96+
97+
This CalculateOption removes the field provided (as a string) in the call before comparing them. A common usage might be to remove the metadata fields by using the `IgnoreField("metadata")` option.
98+
99+
94100
## Contributing
95101

96102
If you find this project useful here's how you can help:

Diff for: patch/deletenull.go

+31
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ func IgnoreStatusFields() CalculateOption {
4141
}
4242
}
4343

44+
func IgnoreField(field string) CalculateOption {
45+
return func(current, modified []byte) ([]byte, []byte, error) {
46+
current, err := deleteDataField(current, field)
47+
if err != nil {
48+
return []byte{}, []byte{}, errors.Wrap(err, "could not delete the field from current byte sequence")
49+
}
50+
51+
modified, err = deleteDataField(modified, field)
52+
if err != nil {
53+
return []byte{}, []byte{}, errors.Wrap(err, "could not delete the field from modified byte sequence")
54+
}
55+
56+
return current, modified, nil
57+
}
58+
}
59+
4460
func IgnoreVolumeClaimTemplateTypeMetaAndStatus() CalculateOption {
4561
return func(current, modified []byte) ([]byte, []byte, error) {
4662
current, err := deleteVolumeClaimTemplateFields(current)
@@ -173,6 +189,21 @@ func deleteNullInSlice(m []interface{}) ([]interface{}, error) {
173189
return filteredSlice, nil
174190
}
175191

192+
func deleteDataField(obj []byte, fieldName string) ([]byte, error) {
193+
var objectMap map[string]interface{}
194+
err := json.Unmarshal(obj, &objectMap)
195+
if err != nil {
196+
return []byte{}, errors.Wrap(err, "could not unmarshal byte sequence")
197+
}
198+
delete(objectMap, fieldName)
199+
obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(objectMap)
200+
if err != nil {
201+
return []byte{}, errors.Wrap(err, "could not marshal byte sequence")
202+
}
203+
204+
return obj, nil
205+
}
206+
176207
func deleteStatusField(obj []byte) ([]byte, error) {
177208
var objectMap map[string]interface{}
178209
err := json.Unmarshal(obj, &objectMap)

Diff for: tests/integration_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,27 @@ func TestIntegration(t *testing.T) {
725725
n := i.(*appsv1.StatefulSet)
726726
n.Spec.Template.ObjectMeta.Labels = map[string]string{"c": "d"}
727727
}),
728+
NewTestMatch("Ignore Metadata field",
729+
&v1.Pod{
730+
731+
ObjectMeta: standardObjectMeta(),
732+
Spec: v1.PodSpec{
733+
Containers: []v1.Container{
734+
{
735+
Name: "test-container",
736+
Image: "test-image",
737+
},
738+
},
739+
},
740+
}).
741+
withRemoteChange(func(i interface{}) {
742+
pod := i.(*v1.Pod)
743+
pod.Labels = map[string]string{"a": "b"}
744+
}).
745+
withLocalChange(func(i interface{}) {
746+
pod := i.(*v1.Pod)
747+
pod.Labels = map[string]string{"c": "d"}
748+
}),
728749
}
729750
runAll(t, tests)
730751
}
@@ -748,7 +769,12 @@ func runAll(t *testing.T, tests []*TestItem) {
748769
if testing.Verbose() {
749770
log.Printf("> %s", test.name)
750771
}
751-
err := testMatchOnObject(test)
772+
var err error
773+
if test.name == "Ignore Metadata field" {
774+
err = testMatchOnObject(test, "metadata")
775+
} else {
776+
err = testMatchOnObject(test, "")
777+
}
752778
if err != nil {
753779
if *failonerror {
754780
t.Fatalf("Test '%s' failed: %s %s", test.name, err, errors.GetDetails(err))

Diff for: tests/main_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,14 @@ func (t *TestItem) withIgnoreVersions(v []string) *TestItem {
186186
return t
187187
}
188188

189-
func testMatchOnObject(testItem *TestItem) error {
189+
func testMatchOnObject(testItem *TestItem, ignoreField string) error {
190190
var existing metav1.Object
191191
var err error
192192
opts := []patch.CalculateOption{
193193
patch.IgnoreStatusFields(),
194194
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
195195
patch.IgnorePDBSelector(),
196+
patch.IgnoreField(ignoreField),
196197
}
197198

198199
newObject := testItem.object

0 commit comments

Comments
 (0)