Skip to content

Commit 064486c

Browse files
authored
Ignore time in ManagedFields when processing events from dependent resources (#59)
Signed-off-by: Fabian von Feilitzsch <[email protected]>
1 parent 0449c0c commit 064486c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

predicate/dependent.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package predicate
1717
import (
1818
"reflect"
1919

20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2122
"sigs.k8s.io/controller-runtime/pkg/event"
2223
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -91,6 +92,9 @@ func (DependentPredicate) Update(e event.UpdateEvent) bool {
9192
old.SetResourceVersion("")
9293
new.SetResourceVersion("")
9394

95+
old.SetManagedFields(removeTimeFromManagedFields(old.GetManagedFields()))
96+
new.SetManagedFields(removeTimeFromManagedFields(new.GetManagedFields()))
97+
9498
if reflect.DeepEqual(old.Object, new.Object) {
9599
return false
96100
}
@@ -99,3 +103,15 @@ func (DependentPredicate) Update(e event.UpdateEvent) bool {
99103
new.GroupVersionKind().GroupVersion(), "kind", new.GroupVersionKind().Kind)
100104
return true
101105
}
106+
107+
func removeTimeFromManagedFields(fields []metav1.ManagedFieldsEntry) []metav1.ManagedFieldsEntry {
108+
if fields == nil {
109+
return nil
110+
}
111+
newFields := []metav1.ManagedFieldsEntry{}
112+
for _, field := range fields {
113+
field.Time = nil
114+
newFields = append(newFields, field)
115+
}
116+
return newFields
117+
}

predicate/dependent_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package predicate
1616

1717
import (
18+
"time"
19+
1820
. "github.com/onsi/ginkgo"
1921
. "github.com/onsi/gomega"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2023
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2124
)
2225

@@ -66,6 +69,32 @@ var _ = Describe("DependentPredicate", func() {
6669
Expect(pred.Update(e)).To(BeFalse())
6770
})
6871
})
72+
73+
When("except time in managedFields is different", func() {
74+
BeforeEach(func() {
75+
curTime := time.Now()
76+
oldTime := metav1.NewTime(curTime)
77+
oldObj.SetManagedFields([]metav1.ManagedFieldsEntry{{
78+
Manager: "test",
79+
Operation: "Update",
80+
APIVersion: "v1",
81+
Time: &oldTime,
82+
}})
83+
84+
duration, _ := time.ParseDuration("4h")
85+
newTime := metav1.NewTime(curTime.Add(duration))
86+
newObj.SetManagedFields([]metav1.ManagedFieldsEntry{{
87+
Manager: "test",
88+
Operation: "Update",
89+
APIVersion: "v1",
90+
Time: &newTime,
91+
}})
92+
})
93+
It("should return false", func() {
94+
e := makeUpdateEventFor(oldObj, newObj)
95+
Expect(pred.Update(e)).To(BeFalse())
96+
})
97+
})
6998
})
7099

71100
When("objects are different", func() {

0 commit comments

Comments
 (0)