From 56976dea702b34e54d8d3b35be3605b3a6d03d18 Mon Sep 17 00:00:00 2001 From: "dmitry.lopatin" Date: Fri, 13 Dec 2024 00:03:53 +0300 Subject: [PATCH] test Signed-off-by: dmitry.lopatin --- .../controller/watchers/object_ref_watcher.go | 27 ++++++++++++- .../pkg/controller/watchers/vd_enqueuer.go | 38 ++++++++++++------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/images/virtualization-artifact/pkg/controller/watchers/object_ref_watcher.go b/images/virtualization-artifact/pkg/controller/watchers/object_ref_watcher.go index 8fbe9a169..441db45e5 100644 --- a/images/virtualization-artifact/pkg/controller/watchers/object_ref_watcher.go +++ b/images/virtualization-artifact/pkg/controller/watchers/object_ref_watcher.go @@ -39,6 +39,8 @@ type ObjectRefWatcher struct { type RequestEnqueuer interface { EnqueueRequests(context.Context, client.Object) []reconcile.Request + EnqueueRequestsFromVIs(client.Object) []reconcile.Request + EnqueueRequestsFromCVIs(client.Object) []reconcile.Request GetEnqueueFrom() client.Object } @@ -60,7 +62,30 @@ func NewObjectRefWatcher( func (w ObjectRefWatcher) Run(mgr manager.Manager, ctr controller.Controller) error { return ctr.Watch( source.Kind(mgr.GetCache(), w.enqueuer.GetEnqueueFrom()), - handler.EnqueueRequestsFromMapFunc(w.enqueuer.EnqueueRequests), + handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request { + vds := w.enqueuer.EnqueueRequests(ctx, obj) + vdsFromVIs := w.enqueuer.EnqueueRequestsFromVIs(obj) + vdsFromCVIs := w.enqueuer.EnqueueRequestsFromCVIs(obj) + + uniqueRequests := make(map[reconcile.Request]struct{}) + + for _, req := range vds { + uniqueRequests[req] = struct{}{} + } + for _, req := range vdsFromVIs { + uniqueRequests[req] = struct{}{} + } + for _, req := range vdsFromCVIs { + uniqueRequests[req] = struct{}{} + } + + var aggregatedResults []reconcile.Request + for req := range uniqueRequests { + aggregatedResults = append(aggregatedResults, req) + } + + return aggregatedResults + }), predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return true }, DeleteFunc: func(e event.DeleteEvent) bool { return true }, diff --git a/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go b/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go index bda8a2859..f0299e9cc 100644 --- a/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go +++ b/images/virtualization-artifact/pkg/controller/watchers/vd_enqueuer.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/deckhouse/deckhouse/pkg/log" + "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" @@ -51,40 +52,49 @@ func (w VirtualDiskRequestEnqueuer) GetEnqueueFrom() client.Object { return w.enqueueFromObj } -func (w VirtualDiskRequestEnqueuer) EnqueueRequests(ctx context.Context, obj client.Object) (requests []reconcile.Request) { - // Enqueue CVI or VI specified by the object ref. - if w.enqueueFromKind == virtv2.VirtualDiskObjectRefKindVirtualImage { - vi, ok := obj.(*virtv2.VirtualImage) +func (w VirtualDiskRequestEnqueuer) EnqueueRequestsFromCVIs(obj client.Object) (requests []reconcile.Request) { + if w.enqueueFromKind == virtv2.VirtualDiskObjectRefKindClusterVirtualImage { + cvi, ok := obj.(*virtv2.ClusterVirtualImage) if !ok { - w.logger.Error(fmt.Sprintf("expected a VirtualImage but got a %T", obj)) + w.logger.Error(fmt.Sprintf("expected a ClusterVirtualImage but got a %T", obj)) return } - if vi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && vi.Spec.DataSource.ObjectRef != nil && vi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind { + if cvi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && cvi.Spec.DataSource.ObjectRef != nil && cvi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind { requests = append(requests, reconcile.Request{ NamespacedName: types.NamespacedName{ - Name: vi.Spec.DataSource.ObjectRef.Name, - Namespace: vi.Namespace, + Name: cvi.Spec.DataSource.ObjectRef.Name, + Namespace: cvi.Spec.DataSource.ObjectRef.Namespace, }, }) } - } else if w.enqueueFromKind == virtv2.VirtualDiskObjectRefKindClusterVirtualImage { - cvi, ok := obj.(*virtv2.ClusterVirtualImage) + } + + return +} + +func (w VirtualDiskRequestEnqueuer) EnqueueRequestsFromVIs(obj client.Object) (requests []reconcile.Request) { + if w.enqueueFromKind == virtv2.VirtualDiskObjectRefKindVirtualImage { + vi, ok := obj.(*virtv2.VirtualImage) if !ok { - w.logger.Error(fmt.Sprintf("expected a ClusterVirtualImage but got a %T", obj)) + w.logger.Error(fmt.Sprintf("expected a VirtualImage but got a %T", obj)) return } - if cvi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && cvi.Spec.DataSource.ObjectRef != nil && cvi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind { + if vi.Spec.DataSource.Type == virtv2.DataSourceTypeObjectRef && vi.Spec.DataSource.ObjectRef != nil && vi.Spec.DataSource.ObjectRef.Kind == virtv2.VirtualDiskKind { requests = append(requests, reconcile.Request{ NamespacedName: types.NamespacedName{ - Name: cvi.Spec.DataSource.ObjectRef.Name, - Namespace: cvi.Spec.DataSource.ObjectRef.Namespace, + Name: vi.Spec.DataSource.ObjectRef.Name, + Namespace: vi.Namespace, }, }) } } + return +} + +func (w VirtualDiskRequestEnqueuer) EnqueueRequests(ctx context.Context, obj client.Object) (requests []reconcile.Request) { var vds virtv2.VirtualDiskList err := w.client.List(ctx, &vds) if err != nil {