Skip to content

Commit

Permalink
Fixed calculation of reconcile event handling for PipePods (#1322)
Browse files Browse the repository at this point in the history
* Fixed calculation of event handling for PipePods
* Added basic unit test for eventFilter delete methods

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
  • Loading branch information
ANeumann82 committed Feb 3, 2020
1 parent ace6186 commit 1c0f33c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
37 changes: 21 additions & 16 deletions pkg/controller/instance/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,6 @@ func (r *Reconciler) SetupWithManager(
return requests
})

// resPredicate ignores DeleteEvents for pipe-pods only (marked with task.PipePodAnnotation). This is due to an
// inherent race that was described in detail in #1116 (https://github.com/kudobuilder/kudo/issues/1116)
// tl;dr: pipe-task will delete the pipe pod at the end of the execution. this would normally trigger another
// Instance reconciliation which might end up copying pipe files twice. we avoid this by explicitly ignoring
// DeleteEvents for pipe-pods.
resPredicate := predicate.Funcs{
CreateFunc: func(event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool {
return e.Meta.GetAnnotations() != nil &&
funk.Contains(e.Meta.GetAnnotations(), task.PipePodAnnotation)
},
UpdateFunc: func(event.UpdateEvent) bool { return true },
GenericFunc: func(event.GenericEvent) bool { return true },
}

return ctrl.NewControllerManagedBy(mgr).
For(&kudov1beta1.Instance{}).
Owns(&kudov1beta1.Instance{}).
Expand All @@ -118,11 +103,31 @@ func (r *Reconciler) SetupWithManager(
Owns(&batchv1.Job{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Pod{}).
WithEventFilter(resPredicate).
WithEventFilter(eventFilter()).
Watches(&source.Kind{Type: &kudov1beta1.OperatorVersion{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: addOvRelatedInstancesToReconcile}).
Complete(r)
}

// eventFilter ignores DeleteEvents for pipe-pods only (marked with task.PipePodAnnotation). This is due to an
// inherent race that was described in detail in #1116 (https://github.com/kudobuilder/kudo/issues/1116)
// tl;dr: pipe-task will delete the pipe pod at the end of the execution. this would normally trigger another
// Instance reconciliation which might end up copying pipe files twice. we avoid this by explicitly ignoring
// DeleteEvents for pipe-pods.
func eventFilter() predicate.Funcs {
return predicate.Funcs{
CreateFunc: func(event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool {
return !isForPipePod(e)
},
UpdateFunc: func(event.UpdateEvent) bool { return true },
GenericFunc: func(event.GenericEvent) bool { return true },
}
}

func isForPipePod(e event.DeleteEvent) bool {
return e.Meta.GetAnnotations() != nil && funk.Contains(e.Meta.GetAnnotations(), task.PipePodAnnotation)
}

// Reconcile is the main controller method that gets called every time something about the instance changes
//
// +-------------------------------+
Expand Down
32 changes: 32 additions & 0 deletions pkg/controller/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"testing"
"time"

"github.com/kudobuilder/kudo/pkg/engine/task"
"sigs.k8s.io/controller-runtime/pkg/event"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -348,3 +351,32 @@ func TestSpecParameterDifference(t *testing.T) {
assert.Equal(t, test.diff, diff)
}
}

func TestEventFilterForDelete(t *testing.T) {
var testParams = []struct {
name string
allowed bool
e event.DeleteEvent
}{
{"A Pod without annotations", true, event.DeleteEvent{
Meta: &v1.Pod{},
Object: nil,
DeleteStateUnknown: false,
}},
{"A Pod with pipePod annotation", false, event.DeleteEvent{
Meta: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{task.PipePodAnnotation: "true"},
},
},
Object: nil,
DeleteStateUnknown: false,
}},
}

filter := eventFilter()
for _, test := range testParams {
diff := filter.Delete(test.e)
assert.Equal(t, test.allowed, diff, test.name)
}
}

0 comments on commit 1c0f33c

Please sign in to comment.