Skip to content

Commit 3ad35c6

Browse files
author
Valeriy Khorunzhin
committed
add index
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
1 parent 85d9410 commit 3ad35c6

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
const defaultNamespaceName = "default"
30+
31+
lease, ok := object.(*v1alpha2.VirtualMachineIPAddressLease)
32+
if !ok || lease == nil {
33+
return nil
34+
}
35+
vmipRef := lease.Spec.VirtualMachineIPAddressRef
36+
if vmipRef == nil || vmipRef.Name == "" {
37+
return nil
38+
}
39+
40+
return []string{GetVMIPLeaseIndexKeyByVMIP(vmipRef.Name, vmipRef.Namespace)}
41+
}
42+
}
43+
44+
func GetVMIPLeaseIndexKeyByVMIP(name, namespace string) string {
45+
if name == "" {
46+
return ""
47+
}
48+
49+
if namespace == "" {
50+
return name
51+
}
52+
53+
return fmt.Sprintf("%s/%s", namespace, name)
54+
}

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

Lines changed: 13 additions & 8 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,21 +64,25 @@ 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+
if vmip.Status.Address != "" {
68+
leaseName := ip.IPToLeaseName(vmip.Status.Address)
69+
if leaseName != "" {
70+
requests = append(requests, reconcile.Request{
71+
NamespacedName: types.NamespacedName{Name: leaseName},
72+
})
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: indexer.GetVMIPLeaseIndexKeyByVMIP(vmip.GetName(), vmip.GetNamespace()),
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() {
8388
requests = append(requests, reconcile.Request{

0 commit comments

Comments
 (0)