Skip to content
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

Allow OurChild to make decisions with the parent and child objects #221

Merged
merged 1 commit into from May 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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