Skip to content

Implement predicate #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pkg/predicate/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

package predicate

import "sigs.k8s.io/controller-runtime/pkg/event"
import (
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var log = logf.KBLog.WithName("predicate").WithName("eventFilters")

// Predicate filters events before enqueuing the keys.
type Predicate interface {
Expand All @@ -34,6 +39,7 @@ type Predicate interface {
}

var _ Predicate = Funcs{}
var _ Predicate = ResourceVersionChangedPredicate{}

// Funcs is a function that implements Predicate.
type Funcs struct {
Expand Down Expand Up @@ -81,3 +87,32 @@ func (p Funcs) Generic(e event.GenericEvent) bool {
}
return true
}

// ResourceVersionChangedPredicate implements a default update predicate function on resource version change
type ResourceVersionChangedPredicate struct {
Funcs
}

// Update implements default UpdateEvent filter for validating resource version change
func (ResourceVersionChangedPredicate) Update(e event.UpdateEvent) bool {
if e.MetaOld == nil {
log.Error(nil, "UpdateEvent has no old metadata", "UpdateEvent", e)
return false
}
if e.ObjectOld == nil {
log.Error(nil, "GenericEvent has no old runtime object to update", "GenericEvent", e)
return false
}
if e.ObjectNew == nil {
log.Error(nil, "GenericEvent has no new runtime object for update", "GenericEvent", e)
return false
}
if e.MetaNew == nil {
log.Error(nil, "UpdateEvent has no new metadata", "UpdateEvent", e)
return false
}
if e.MetaNew.GetResourceVersion() == e.MetaOld.GetResourceVersion() {
return false
}
return true
}
2 changes: 1 addition & 1 deletion pkg/predicate/predicate_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package predicate
package predicate_test

import (
"testing"
Expand Down
133 changes: 133 additions & 0 deletions pkg/predicate/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,137 @@ var _ = Describe("Predicate", func() {
close(done)
})
})

Describe("When checking a ResourceVersionChangedPredicate", func() {
instance := predicate.ResourceVersionChangedPredicate{}

Context("Where the old object doesn't have a ResourceVersion or metadata", func() {
It("should return false", func() {
new := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
ResourceVersion: "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 ResourceVersion or metadata", func() {
It("should return false", func() {
old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
ResourceVersion: "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())
Expect(instance.Update(failEvnt)).Should(BeFalse())
})
})

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

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

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())
Expect(instance.Update(failEvnt)).Should(BeFalse())
})
})

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

old := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: "biz",
ResourceVersion: "v2",
}}
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",
ResourceVersion: "v1",
}}

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

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())
})
})

})
})