Skip to content

Commit

Permalink
Adds an Ignore option for Metadata (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-a-joyce authored Oct 18, 2021
1 parent 191d64b commit ac64299
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ To help in these scenarios there are the following options to be used when calcu
- `IgnoreStatusFields`
- `IgnoreVolumeClaimTemplateTypeMetaAndStatus`
- `IgnorePDBSelector`
- `IgnoreField("field-name-to-ignore")`

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

#### IgnoreField("field-name-to-ignore")

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.


## Contributing

If you find this project useful here's how you can help:
Expand Down
31 changes: 31 additions & 0 deletions patch/deletenull.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ func IgnoreStatusFields() CalculateOption {
}
}

func IgnoreField(field string) CalculateOption {
return func(current, modified []byte) ([]byte, []byte, error) {
current, err := deleteDataField(current, field)
if err != nil {
return []byte{}, []byte{}, errors.Wrap(err, "could not delete the field from current byte sequence")
}

modified, err = deleteDataField(modified, field)
if err != nil {
return []byte{}, []byte{}, errors.Wrap(err, "could not delete the field from modified byte sequence")
}

return current, modified, nil
}
}

func IgnoreVolumeClaimTemplateTypeMetaAndStatus() CalculateOption {
return func(current, modified []byte) ([]byte, []byte, error) {
current, err := deleteVolumeClaimTemplateFields(current)
Expand Down Expand Up @@ -173,6 +189,21 @@ func deleteNullInSlice(m []interface{}) ([]interface{}, error) {
return filteredSlice, nil
}

func deleteDataField(obj []byte, fieldName string) ([]byte, error) {
var objectMap map[string]interface{}
err := json.Unmarshal(obj, &objectMap)
if err != nil {
return []byte{}, errors.Wrap(err, "could not unmarshal byte sequence")
}
delete(objectMap, fieldName)
obj, err = json.ConfigCompatibleWithStandardLibrary.Marshal(objectMap)
if err != nil {
return []byte{}, errors.Wrap(err, "could not marshal byte sequence")
}

return obj, nil
}

func deleteStatusField(obj []byte) ([]byte, error) {
var objectMap map[string]interface{}
err := json.Unmarshal(obj, &objectMap)
Expand Down
28 changes: 27 additions & 1 deletion tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,27 @@ func TestIntegration(t *testing.T) {
n := i.(*appsv1.StatefulSet)
n.Spec.Template.ObjectMeta.Labels = map[string]string{"c": "d"}
}),
NewTestMatch("Ignore Metadata field",
&v1.Pod{

ObjectMeta: standardObjectMeta(),
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test-container",
Image: "test-image",
},
},
},
}).
withRemoteChange(func(i interface{}) {
pod := i.(*v1.Pod)
pod.Labels = map[string]string{"a": "b"}
}).
withLocalChange(func(i interface{}) {
pod := i.(*v1.Pod)
pod.Labels = map[string]string{"c": "d"}
}),
}
runAll(t, tests)
}
Expand All @@ -748,7 +769,12 @@ func runAll(t *testing.T, tests []*TestItem) {
if testing.Verbose() {
log.Printf("> %s", test.name)
}
err := testMatchOnObject(test)
var err error
if test.name == "Ignore Metadata field" {
err = testMatchOnObject(test, "metadata")
} else {
err = testMatchOnObject(test, "")
}
if err != nil {
if *failonerror {
t.Fatalf("Test '%s' failed: %s %s", test.name, err, errors.GetDetails(err))
Expand Down
3 changes: 2 additions & 1 deletion tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ func (t *TestItem) withIgnoreVersions(v []string) *TestItem {
return t
}

func testMatchOnObject(testItem *TestItem) error {
func testMatchOnObject(testItem *TestItem, ignoreField string) error {
var existing metav1.Object
var err error
opts := []patch.CalculateOption{
patch.IgnoreStatusFields(),
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
patch.IgnorePDBSelector(),
patch.IgnoreField(ignoreField),
}

newObject := testItem.object
Expand Down

0 comments on commit ac64299

Please sign in to comment.