Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
IndexFieldVMMACByAddress = "spec.address|status.address"

IndexFieldVMMACLeaseByVMMAC = "spec.virtualMachineMACAddressRef.Name"

IndexFieldVMIPLeaseByVMIP = "spec.virtualMachineIPAddressRef"
)

var IndexGetters = []IndexGetter{
Expand All @@ -79,6 +81,7 @@ var IndexGetters = []IndexGetter{
IndexVMMACByVM,
IndexVMMACByAddress,
IndexVMMACLeaseByVMMAC,
IndexVMIPLeaseByVMIP,
}

type IndexGetter func() (obj client.Object, field string, extractValue client.IndexerFunc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package indexer

import (
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

func IndexVMIPLeaseByVMIP() (obj client.Object, field string, extractValue client.IndexerFunc) {
return &v1alpha2.VirtualMachineIPAddressLease{}, IndexFieldVMIPLeaseByVMIP, func(object client.Object) []string {
lease, ok := object.(*v1alpha2.VirtualMachineIPAddressLease)
if !ok || lease == nil {
return nil
}
vmipRef := lease.Spec.VirtualMachineIPAddressRef
if vmipRef == nil || vmipRef.Name == "" {
return nil
}

return []string{fmt.Sprintf("%s/%s", vmipRef.Namespace, vmipRef.Name)}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/virtualization-controller/pkg/common/ip"
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand Down Expand Up @@ -63,28 +64,34 @@ func (w VirtualMachineIPAddressWatcher) Watch(mgr manager.Manager, ctr controlle
}

func (w VirtualMachineIPAddressWatcher) enqueueRequests(ctx context.Context, vmip *v1alpha2.VirtualMachineIPAddress) (requests []reconcile.Request) {
requestMap := make(map[string]struct{})

if vmip.Status.Address != "" {
leaseName := ip.IPToLeaseName(vmip.Status.Address)
if leaseName != "" {
requestMap[leaseName] = struct{}{}
}
}

var leases v1alpha2.VirtualMachineIPAddressLeaseList
err := w.client.List(ctx, &leases, &client.ListOptions{})
err := w.client.List(ctx, &leases, &client.MatchingFields{
indexer.IndexFieldVMIPLeaseByVMIP: fmt.Sprintf("%s/%s", vmip.GetNamespace(), vmip.GetName()),
})
if err != nil {
w.logger.Error(fmt.Sprintf("failed to list leases: %s", err))
return
}

for _, lease := range leases.Items {
if vmip.Status.Address != "" && vmip.Status.Address == ip.LeaseNameToIP(lease.Name) {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{Name: lease.Name},
})
continue
}

vmipRef := lease.Spec.VirtualMachineIPAddressRef
if vmipRef != nil && vmipRef.Name == vmip.GetName() && vmipRef.Namespace == vmip.GetNamespace() {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{Name: lease.Name},
})
requestMap[lease.Name] = struct{}{}
}
}

for leaseName := range requestMap {
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: leaseName}})
}

return requests
}
Loading