Skip to content

Commit

Permalink
Allow OurChild to make decisions with the parent and child objects (#221
Browse files Browse the repository at this point in the history
)
  • Loading branch information
scothis committed May 4, 2022
1 parent 3382f72 commit 91fe232
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
11 changes: 6 additions & 5 deletions reconcilers/reconcilers.go
Expand Up @@ -538,7 +538,7 @@ type ChildReconciler struct {
// children match.
//
// Expected function signature:
// func(child client.Object) bool
// func(parent, child client.Object) bool
//
// +optional
OurChild interface{}
Expand Down Expand Up @@ -666,13 +666,14 @@ func (r *ChildReconciler) validate(ctx context.Context) error {

// validate OurChild function signature:
// nil
// func(child client.Object) interface{}
// func(parent, child client.Object) bool
if r.OurChild != nil {
fn := reflect.TypeOf(r.OurChild)
if fn.NumIn() != 1 || fn.NumOut() != 1 ||
!reflect.TypeOf(r.ChildType).AssignableTo(fn.In(0)) ||
if fn.NumIn() != 2 || fn.NumOut() != 1 ||
!reflect.TypeOf(castParentType).AssignableTo(fn.In(0)) ||
!reflect.TypeOf(r.ChildType).AssignableTo(fn.In(1)) ||
fn.Out(0).Kind() != reflect.Bool {
return fmt.Errorf("ChildReconciler must implement OurChild: nil | func(%s) bool, found: %s", reflect.TypeOf(r.ChildType), fn)
return fmt.Errorf("ChildReconciler must implement OurChild: nil | func(%s, %s) bool, found: %s", reflect.TypeOf(castParentType), reflect.TypeOf(r.ChildType), fn)
}
}

Expand Down
30 changes: 22 additions & 8 deletions reconcilers/reconcilers_validate_test.go
Expand Up @@ -550,7 +550,7 @@ func TestChildReconciler_validate(t *testing.T) {
ReflectChildStatusOnParent: func(parent *corev1.ConfigMap, child *corev1.Pod, err error) {},
MergeBeforeUpdate: func(current, desired *corev1.Pod) {},
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func(child *corev1.Pod) bool { return false },
OurChild: func(parent *corev1.ConfigMap, child *corev1.Pod) bool { return false },
},
},
{
Expand All @@ -565,7 +565,7 @@ func TestChildReconciler_validate(t *testing.T) {
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func() bool { return false },
},
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.Pod) bool, found: func() bool",
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.ConfigMap, *v1.Pod) bool, found: func() bool",
},
{
name: "OurChild in 1",
Expand All @@ -577,9 +577,23 @@ func TestChildReconciler_validate(t *testing.T) {
ReflectChildStatusOnParent: func(parent *corev1.ConfigMap, child *corev1.Pod, err error) {},
MergeBeforeUpdate: func(current, desired *corev1.Pod) {},
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func(child *corev1.Secret) bool { return false },
OurChild: func(parent *corev1.ConfigMap, child *corev1.Secret) bool { return false },
},
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.Pod) bool, found: func(*v1.Secret) bool",
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.ConfigMap, *v1.Pod) bool, found: func(*v1.ConfigMap, *v1.Secret) bool",
},
{
name: "OurChild in 2",
parent: &corev1.ConfigMap{},
reconciler: &ChildReconciler{
ChildType: &corev1.Pod{},
ChildListType: &corev1.PodList{},
DesiredChild: func(ctx context.Context, parent *corev1.ConfigMap) (*corev1.Pod, error) { return nil, nil },
ReflectChildStatusOnParent: func(parent *corev1.ConfigMap, child *corev1.Pod, err error) {},
MergeBeforeUpdate: func(current, desired *corev1.Pod) {},
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func(parent *corev1.Secret, child *corev1.Pod) bool { return false },
},
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.ConfigMap, *v1.Pod) bool, found: func(*v1.Secret, *v1.Pod) bool",
},
{
name: "OurChild num out",
Expand All @@ -591,9 +605,9 @@ func TestChildReconciler_validate(t *testing.T) {
ReflectChildStatusOnParent: func(parent *corev1.ConfigMap, child *corev1.Pod, err error) {},
MergeBeforeUpdate: func(current, desired *corev1.Pod) {},
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func(child *corev1.Pod) {},
OurChild: func(parent *corev1.ConfigMap, child *corev1.Pod) {},
},
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.Pod) bool, found: func(*v1.Pod)",
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.ConfigMap, *v1.Pod) bool, found: func(*v1.ConfigMap, *v1.Pod)",
},
{
name: "OurChild out 1",
Expand All @@ -605,9 +619,9 @@ func TestChildReconciler_validate(t *testing.T) {
ReflectChildStatusOnParent: func(parent *corev1.ConfigMap, child *corev1.Pod, err error) {},
MergeBeforeUpdate: func(current, desired *corev1.Pod) {},
SemanticEquals: func(a1, a2 *corev1.Pod) bool { return false },
OurChild: func(child *corev1.Pod) *corev1.Pod { return child },
OurChild: func(parent *corev1.ConfigMap, child *corev1.Pod) *corev1.Pod { return child },
},
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.Pod) bool, found: func(*v1.Pod) *v1.Pod",
shouldErr: "ChildReconciler must implement OurChild: nil | func(*v1.ConfigMap, *v1.Pod) bool, found: func(*v1.ConfigMap, *v1.Pod) *v1.Pod",
},
{
name: "Sanitize",
Expand Down

0 comments on commit 91fe232

Please sign in to comment.