Skip to content

Commit

Permalink
✨ Add predicate for Generation change on update event
Browse files Browse the repository at this point in the history
  • Loading branch information
hasbro17 committed Aug 7, 2019
1 parent ee41a80 commit f67d7fa
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,32 @@ func (ResourceVersionChangedPredicate) Update(e event.UpdateEvent) bool {
}
return true
}

// GenerationChangedPredicate implements a default update predicate function on generation change
type GenerationChangedPredicate struct {
Funcs
}

// Update implements default UpdateEvent filter for validating generation change
func (GenerationChangedPredicate) Update(e event.UpdateEvent) bool {
if e.MetaOld == nil {
log.Error(nil, "Update event has no old metadata", "event", e)
return false
}
if e.ObjectOld == nil {
log.Error(nil, "Update event has no old runtime object to update", "event", e)
return false
}
if e.ObjectNew == nil {
log.Error(nil, "Update event has no new runtime object for update", "event", e)
return false
}
if e.MetaNew == nil {
log.Error(nil, "Update event has no new metadata", "event", e)
return false
}
if e.MetaNew.GetGeneration() == e.MetaOld.GetGeneration() {
return false
}
return true
}
130 changes: 130 additions & 0 deletions pkg/predicate/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,134 @@ var _ = Describe("Predicate", func() {
})

})

Describe("When checking a GenerationChangedPredicate", func() {
instance := predicate.GenerationChangedPredicate{}
Context("Where the old object doesn't have a Generation or metadata", func() {
It("should return false", func() {
new := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

failEvnt := event.UpdateEvent{
MetaNew: new.GetObjectMeta(),
ObjectNew: new,
}
Expect(instance.Create(event.CreateEvent{})).Should(BeTrue())
Expect(instance.Delete(event.DeleteEvent{})).Should(BeTrue())
Expect(instance.Generic(event.GenericEvent{})).Should(BeTrue())
Expect(instance.Update(failEvnt)).Should(BeFalse())
})
})

Context("Where the new object doesn't have a Generation or metadata", func() {
It("should return false", func() {
old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

failEvnt := event.UpdateEvent{
MetaOld: old.GetObjectMeta(),
ObjectOld: old,
}
Expect(instance.Create(event.CreateEvent{})).Should(BeTrue())
Expect(instance.Delete(event.DeleteEvent{})).Should(BeTrue())
Expect(instance.Generic(event.GenericEvent{})).Should(BeTrue())
Expect(instance.Update(failEvnt)).Should(BeFalse())
})
})

Context("Where the Generation hasn't changed", func() {
It("should return false", func() {
new := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

failEvnt := event.UpdateEvent{
MetaOld: old.GetObjectMeta(),
ObjectOld: old,
MetaNew: new.GetObjectMeta(),
ObjectNew: new,
}
Expect(instance.Create(event.CreateEvent{})).Should(BeTrue())
Expect(instance.Delete(event.DeleteEvent{})).Should(BeTrue())
Expect(instance.Generic(event.GenericEvent{})).Should(BeTrue())
Expect(instance.Update(failEvnt)).Should(BeFalse())
})
})

Context("Where the Generation has changed", func() {
It("should return true", func() {
new := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 2,
}}
passEvt := event.UpdateEvent{
MetaOld: old.GetObjectMeta(),
ObjectOld: old,
MetaNew: new.GetObjectMeta(),
ObjectNew: new,
}
Expect(instance.Create(event.CreateEvent{})).Should(BeTrue())
Expect(instance.Delete(event.DeleteEvent{})).Should(BeTrue())
Expect(instance.Generic(event.GenericEvent{})).Should(BeTrue())
Expect(instance.Update(passEvt)).Should(BeTrue())
})
})

Context("Where the objects or metadata are missing", func() {

It("should return false", func() {
new := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
Generation: 1,
}}

failEvt1 := event.UpdateEvent{MetaOld: old.GetObjectMeta(), ObjectOld: old, MetaNew: new.GetObjectMeta()}
failEvt2 := event.UpdateEvent{MetaOld: old.GetObjectMeta(), MetaNew: new.GetObjectMeta(), ObjectNew: new}
failEvt3 := event.UpdateEvent{MetaOld: old.GetObjectMeta(), ObjectOld: old, ObjectNew: new}
Expect(instance.Create(event.CreateEvent{})).Should(BeTrue())
Expect(instance.Delete(event.DeleteEvent{})).Should(BeTrue())
Expect(instance.Generic(event.GenericEvent{})).Should(BeTrue())
Expect(instance.Update(failEvt1)).Should(BeFalse())
Expect(instance.Update(failEvt2)).Should(BeFalse())
Expect(instance.Update(failEvt3)).Should(BeFalse())
})
})

})
})

0 comments on commit f67d7fa

Please sign in to comment.