Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Draft: duck type support for builder calls - still need to deal with client.… #450

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion duck/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func (c *duckAwareClientWrapper) Create(ctx context.Context, obj client.Object,
return c.client.Create(ctx, obj, opts...)
}

return fmt.Errorf("Create is not supported for the duck typed objects")
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return err
}
u := &unstructured.Unstructured{Object: uObj}
return c.client.Create(ctx, u, opts...)
}

func (c *duckAwareClientWrapper) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
Expand Down
23 changes: 20 additions & 3 deletions reconcilers/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"sync"

"github.com/go-logr/logr"
"github.com/vmware-labs/reconciler-runtime/duck"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -197,10 +200,24 @@ func (r *ChildReconciler[T, CT, CLT]) SetupWithManager(ctx context.Context, mgr
return err
}

if r.SkipOwnerReference {
bldr.Watches(r.ChildType, EnqueueTracked(ctx))
if duck.IsDuck(r.ChildType, c.Scheme()) {
uns, err := runtime.DefaultUnstructuredConverter.ToUnstructured(r.ChildType)
if err != nil {
return err
}
funs := &unstructured.Unstructured{Object: uns}
if r.SkipOwnerReference {
bldr.Watches(funs, EnqueueTracked(ctx))
} else {
bldr.Owns(funs)
}

} else {
bldr.Owns(r.ChildType)
if r.SkipOwnerReference {
bldr.Watches(r.ChildType, EnqueueTracked(ctx))
} else {
bldr.Owns(r.ChildType)
}
}

if r.Setup == nil {
Expand Down
Loading