Skip to content

Commit b83b2ce

Browse files
authored
chore(vm): optimize vmip watcher (#1664)
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
1 parent 7a02ef2 commit b83b2ce

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

images/virtualization-artifact/pkg/controller/indexer/indexer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const (
5757
IndexFieldVMMACByAddress = "spec.address|status.address"
5858

5959
IndexFieldVMMACLeaseByVMMAC = "spec.virtualMachineMACAddressRef.Name"
60+
61+
IndexFieldVMIPLeaseByVMIP = "spec.virtualMachineIPAddressRef"
6062
)
6163

6264
var IndexGetters = []IndexGetter{
@@ -79,6 +81,7 @@ var IndexGetters = []IndexGetter{
7981
IndexVMMACByVM,
8082
IndexVMMACByAddress,
8183
IndexVMMACLeaseByVMMAC,
84+
IndexVMIPLeaseByVMIP,
8285
}
8386

8487
type IndexGetter func() (obj client.Object, field string, extractValue client.IndexerFunc)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2025 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package indexer
18+
19+
import (
20+
"fmt"
21+
22+
"sigs.k8s.io/controller-runtime/pkg/client"
23+
24+
"github.com/deckhouse/virtualization/api/core/v1alpha2"
25+
)
26+
27+
func IndexVMIPLeaseByVMIP() (obj client.Object, field string, extractValue client.IndexerFunc) {
28+
return &v1alpha2.VirtualMachineIPAddressLease{}, IndexFieldVMIPLeaseByVMIP, func(object client.Object) []string {
29+
lease, ok := object.(*v1alpha2.VirtualMachineIPAddressLease)
30+
if !ok || lease == nil {
31+
return nil
32+
}
33+
vmipRef := lease.Spec.VirtualMachineIPAddressRef
34+
if vmipRef == nil || vmipRef.Name == "" {
35+
return nil
36+
}
37+
38+
return []string{fmt.Sprintf("%s/%s", vmipRef.Namespace, vmipRef.Name)}
39+
}
40+
}

images/virtualization-artifact/pkg/controller/vmiplease/internal/watcher/vmip_watcher.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/deckhouse/deckhouse/pkg/log"
3535
"github.com/deckhouse/virtualization-controller/pkg/common/ip"
36+
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
3637
"github.com/deckhouse/virtualization/api/core/v1alpha2"
3738
)
3839

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

6566
func (w VirtualMachineIPAddressWatcher) enqueueRequests(ctx context.Context, vmip *v1alpha2.VirtualMachineIPAddress) (requests []reconcile.Request) {
67+
requestMap := make(map[string]struct{})
68+
69+
if vmip.Status.Address != "" {
70+
leaseName := ip.IPToLeaseName(vmip.Status.Address)
71+
if leaseName != "" {
72+
requestMap[leaseName] = struct{}{}
73+
}
74+
}
75+
6676
var leases v1alpha2.VirtualMachineIPAddressLeaseList
67-
err := w.client.List(ctx, &leases, &client.ListOptions{})
77+
err := w.client.List(ctx, &leases, &client.MatchingFields{
78+
indexer.IndexFieldVMIPLeaseByVMIP: fmt.Sprintf("%s/%s", vmip.GetNamespace(), vmip.GetName()),
79+
})
6880
if err != nil {
6981
w.logger.Error(fmt.Sprintf("failed to list leases: %s", err))
7082
return
7183
}
7284

7385
for _, lease := range leases.Items {
74-
if vmip.Status.Address != "" && vmip.Status.Address == ip.LeaseNameToIP(lease.Name) {
75-
requests = append(requests, reconcile.Request{
76-
NamespacedName: types.NamespacedName{Name: lease.Name},
77-
})
78-
continue
79-
}
80-
8186
vmipRef := lease.Spec.VirtualMachineIPAddressRef
8287
if vmipRef != nil && vmipRef.Name == vmip.GetName() && vmipRef.Namespace == vmip.GetNamespace() {
83-
requests = append(requests, reconcile.Request{
84-
NamespacedName: types.NamespacedName{Name: lease.Name},
85-
})
88+
requestMap[lease.Name] = struct{}{}
8689
}
8790
}
8891

92+
for leaseName := range requestMap {
93+
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: leaseName}})
94+
}
95+
8996
return requests
9097
}

0 commit comments

Comments
 (0)