Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: dmitry.lopatin <[email protected]>
  • Loading branch information
LopatinDmitr committed Dec 12, 2024
1 parent d75eaf6 commit 56976de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 56976de

Please sign in to comment.