• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubeovn / kube-ovn / 20331782627

18 Dec 2025 09:10AM UTC coverage: 22.603% (-0.06%) from 22.661%
20331782627

push

github

web-flow
Add tests for VIP finalizer handling and subnet status updates (#6068)

* Add tests for VIP finalizer handling and subnet status updates

- Introduced a new test to verify that the subnet status is correctly updated when a VIP is created and deleted, ensuring that finalizers are properly handled.
- Added checks for both IPv4 and IPv6 protocols, including dual stack scenarios, to confirm that available and using IP counts and ranges are updated as expected.
- Enhanced the existing VIP creation test to wait for the finalizer to be added before proceeding with subnet status verification.
- Updated sleep durations to ensure sufficient time for status updates after VIP operations.

Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>

* fix after review

Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>

---------

Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>

0 of 312 new or added lines in 10 files covered. (0.0%)

21 existing lines in 6 files now uncovered.

12052 of 53320 relevant lines covered (22.6%)

0.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/controller/ippool.go
1
package controller
2

3
import (
4
        "context"
5
        "fmt"
6
        "reflect"
7
        "slices"
8

9
        corev1 "k8s.io/api/core/v1"
10
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
11
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
        "k8s.io/apimachinery/pkg/labels"
13
        "k8s.io/apimachinery/pkg/types"
14
        "k8s.io/client-go/tools/cache"
15
        "k8s.io/klog/v2"
16
        "sigs.k8s.io/controller-runtime/pkg/client"
17
        "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
18

19
        kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
20
        "github.com/kubeovn/kube-ovn/pkg/util"
21
)
22

23
func (c *Controller) enqueueAddIPPool(obj any) {
×
24
        key := cache.MetaObjectToName(obj.(*kubeovnv1.IPPool)).String()
×
25
        klog.V(3).Infof("enqueue add ippool %s", key)
×
26
        c.addOrUpdateIPPoolQueue.Add(key)
×
27
}
×
28

29
func (c *Controller) enqueueDeleteIPPool(obj any) {
×
30
        var ippool *kubeovnv1.IPPool
×
31
        switch t := obj.(type) {
×
32
        case *kubeovnv1.IPPool:
×
33
                ippool = t
×
34
        case cache.DeletedFinalStateUnknown:
×
35
                i, ok := t.Obj.(*kubeovnv1.IPPool)
×
36
                if !ok {
×
37
                        klog.Warningf("unexpected object type: %T", t.Obj)
×
38
                        return
×
39
                }
×
40
                ippool = i
×
41
        default:
×
42
                klog.Warningf("unexpected type: %T", obj)
×
43
                return
×
44
        }
45

46
        klog.V(3).Infof("enqueue delete ippool %s", cache.MetaObjectToName(ippool).String())
×
47
        c.deleteIPPoolQueue.Add(ippool)
×
48
}
49

50
func (c *Controller) enqueueUpdateIPPool(oldObj, newObj any) {
×
51
        oldIPPool := oldObj.(*kubeovnv1.IPPool)
×
52
        newIPPool := newObj.(*kubeovnv1.IPPool)
×
53
        if !newIPPool.DeletionTimestamp.IsZero() {
×
54
                klog.V(3).Infof("enqueue delete ippool %s due to deletion timestamp", cache.MetaObjectToName(newIPPool).String())
×
NEW
55
                c.deleteIPPoolQueue.Add(newIPPool)
×
56
                return
×
57
        }
×
58
        if !slices.Equal(oldIPPool.Spec.Namespaces, newIPPool.Spec.Namespaces) ||
×
59
                !slices.Equal(oldIPPool.Spec.IPs, newIPPool.Spec.IPs) ||
×
60
                oldIPPool.Spec.EnableAddressSet != newIPPool.Spec.EnableAddressSet {
×
61
                key := cache.MetaObjectToName(newIPPool).String()
×
62
                klog.V(3).Infof("enqueue update ippool %s", key)
×
63
                c.addOrUpdateIPPoolQueue.Add(key)
×
64
        }
×
65
}
66

67
func (c *Controller) handleAddOrUpdateIPPool(key string) error {
×
68
        c.ippoolKeyMutex.LockKey(key)
×
69
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(key) }()
×
70

71
        cachedIPPool, err := c.ippoolLister.Get(key)
×
72
        if err != nil {
×
73
                if k8serrors.IsNotFound(err) {
×
74
                        return nil
×
75
                }
×
76
                klog.Error(err)
×
77
                return err
×
78
        }
79
        klog.Infof("handle add/update ippool %s", cachedIPPool.Name)
×
80

×
81
        ippool := cachedIPPool.DeepCopy()
×
82
        if err = c.handleAddIPPoolFinalizer(ippool); err != nil {
×
83
                klog.Errorf("failed to add finalizer for ippool %s: %v", ippool.Name, err)
×
84
                return err
×
85
        }
×
86
        if !ippool.DeletionTimestamp.IsZero() {
×
87
                klog.Infof("ippool %s is being deleted, skip add/update handling", ippool.Name)
×
88
                return nil
×
89
        }
×
90
        ippool.Status.EnsureStandardConditions()
×
91
        if err = c.reconcileIPPoolAddressSet(ippool); err != nil {
×
92
                klog.Errorf("failed to reconcile address set for ippool %s: %v", ippool.Name, err)
×
93
                if patchErr := c.patchIPPoolStatusCondition(ippool, "ReconcileAddressSetFailed", err.Error()); patchErr != nil {
×
94
                        klog.Error(patchErr)
×
95
                }
×
96
                return err
×
97
        }
98
        if err = c.ipam.AddOrUpdateIPPool(ippool.Spec.Subnet, ippool.Name, ippool.Spec.IPs); err != nil {
×
99
                klog.Errorf("failed to add/update ippool %s with IPs %v in subnet %s: %v", ippool.Name, ippool.Spec.IPs, ippool.Spec.Subnet, err)
×
100
                if patchErr := c.patchIPPoolStatusCondition(ippool, "UpdateIPAMFailed", err.Error()); patchErr != nil {
×
101
                        klog.Error(patchErr)
×
102
                }
×
103
                return err
×
104
        }
105

106
        c.updateIPPoolStatistics(ippool)
×
107

×
108
        if err = c.patchIPPoolStatusCondition(ippool, "UpdateIPAMSucceeded", ""); err != nil {
×
109
                klog.Error(err)
×
110
                return err
×
111
        }
×
112

113
        for _, ns := range ippool.Spec.Namespaces {
×
114
                c.addNamespaceQueue.Add(ns)
×
115
        }
×
116

117
        return nil
×
118
}
119

120
func (c *Controller) handleDeleteIPPool(ippool *kubeovnv1.IPPool) error {
×
121
        c.ippoolKeyMutex.LockKey(ippool.Name)
×
122
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(ippool.Name) }()
×
123

124
        klog.Infof("handle delete ippool %s", ippool.Name)
×
125
        c.ipam.RemoveIPPool(ippool.Spec.Subnet, ippool.Name)
×
126
        if err := c.OVNNbClient.DeleteAddressSet(util.IPPoolAddressSetName(ippool.Name)); err != nil {
×
127
                klog.Errorf("failed to delete address set for ippool %s: %v", ippool.Name, err)
×
128
                return err
×
129
        }
×
130

131
        namespaces, err := c.namespacesLister.List(labels.Everything())
×
132
        if err != nil {
×
133
                klog.Errorf("failed to list namespaces: %v", err)
×
134
                return err
×
135
        }
×
136

137
        for _, ns := range namespaces {
×
138
                if ns.Annotations[util.IPPoolAnnotation] == ippool.Name {
×
139
                        c.enqueueAddNamespace(ns)
×
140
                }
×
141
        }
142

143
        if err := c.handleDelIPPoolFinalizer(ippool); err != nil {
×
144
                klog.Errorf("failed to remove finalizer for ippool %s: %v", ippool.Name, err)
×
145
                return err
×
146
        }
×
147

148
        return nil
×
149
}
150

151
func (c *Controller) handleUpdateIPPoolStatus(key string) error {
×
152
        c.ippoolKeyMutex.LockKey(key)
×
153
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(key) }()
×
154

155
        cachedIPPool, err := c.ippoolLister.Get(key)
×
156
        if err != nil {
×
157
                if k8serrors.IsNotFound(err) {
×
158
                        return nil
×
159
                }
×
160
                klog.Error(err)
×
161
                return err
×
162
        }
163

164
        ippool := cachedIPPool.DeepCopy()
×
165
        c.updateIPPoolStatistics(ippool)
×
166
        if reflect.DeepEqual(ippool.Status, cachedIPPool.Status) {
×
167
                return nil
×
168
        }
×
169

170
        return c.patchIPPoolStatus(ippool)
×
171
}
172

173
func (c *Controller) patchIPPoolStatusCondition(ippool *kubeovnv1.IPPool, reason, errMsg string) error {
×
174
        if errMsg != "" {
×
175
                ippool.Status.SetError(reason, errMsg)
×
176
                ippool.Status.NotReady(reason, errMsg)
×
177
                c.recorder.Eventf(ippool, corev1.EventTypeWarning, reason, errMsg)
×
178
        } else {
×
179
                ippool.Status.Ready(reason, "")
×
180
        }
×
181

182
        return c.patchIPPoolStatus(ippool)
×
183
}
184

185
func (c *Controller) patchIPPoolStatus(ippool *kubeovnv1.IPPool) error {
×
186
        bytes, err := ippool.Status.Bytes()
×
187
        if err != nil {
×
188
                klog.Errorf("failed to generate json representation for status of ippool %s: %v", ippool.Name, err)
×
189
                return err
×
190
        }
×
191
        if _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status"); err != nil {
×
192
                klog.Errorf("failed to patch status of ippool %s: %v", ippool.Name, err)
×
193
                return err
×
194
        }
×
195

196
        return nil
×
197
}
198

199
func (c *Controller) syncIPPoolFinalizer(cl client.Client) error {
×
200
        ippools := &kubeovnv1.IPPoolList{}
×
201
        return migrateFinalizers(cl, ippools, func(i int) (client.Object, client.Object) {
×
202
                if i < 0 || i >= len(ippools.Items) {
×
203
                        return nil, nil
×
204
                }
×
205
                return ippools.Items[i].DeepCopy(), ippools.Items[i].DeepCopy()
×
206
        })
207
}
208

209
func (c *Controller) handleAddIPPoolFinalizer(ippool *kubeovnv1.IPPool) error {
×
210
        if ippool == nil || !ippool.DeletionTimestamp.IsZero() {
×
211
                return nil
×
212
        }
×
213
        if controllerutil.ContainsFinalizer(ippool, util.KubeOVNControllerFinalizer) {
×
214
                return nil
×
215
        }
×
216

217
        newIPPool := ippool.DeepCopy()
×
218
        controllerutil.AddFinalizer(newIPPool, util.KubeOVNControllerFinalizer)
×
219
        patch, err := util.GenerateMergePatchPayload(ippool, newIPPool)
×
220
        if err != nil {
×
221
                klog.Errorf("failed to generate patch payload for ippool %s: %v", ippool.Name, err)
×
222
                return err
×
223
        }
×
224
        if _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name,
×
225
                types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
×
226
                if k8serrors.IsNotFound(err) {
×
227
                        return nil
×
228
                }
×
229
                klog.Errorf("failed to add finalizer for ippool %s: %v", ippool.Name, err)
×
230
                return err
×
231
        }
232
        return nil
×
233
}
234

235
func (c *Controller) handleDelIPPoolFinalizer(ippool *kubeovnv1.IPPool) error {
×
236
        if ippool == nil || len(ippool.GetFinalizers()) == 0 {
×
237
                return nil
×
238
        }
×
239

240
        newIPPool := ippool.DeepCopy()
×
241
        controllerutil.RemoveFinalizer(newIPPool, util.DepreciatedFinalizerName)
×
242
        controllerutil.RemoveFinalizer(newIPPool, util.KubeOVNControllerFinalizer)
×
243
        patch, err := util.GenerateMergePatchPayload(ippool, newIPPool)
×
244
        if err != nil {
×
245
                klog.Errorf("failed to generate patch payload for ippool %s: %v", ippool.Name, err)
×
246
                return err
×
247
        }
×
248
        if _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name,
×
249
                types.MergePatchType, patch, metav1.PatchOptions{}, ""); err != nil {
×
250
                if k8serrors.IsNotFound(err) {
×
251
                        return nil
×
252
                }
×
253
                klog.Errorf("failed to remove finalizer from ippool %s: %v", ippool.Name, err)
×
254
                return err
×
255
        }
256
        return nil
×
257
}
258

259
func (c *Controller) updateIPPoolStatistics(ippool *kubeovnv1.IPPool) {
×
260
        v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us := c.ipam.IPPoolStatistics(ippool.Spec.Subnet, ippool.Name)
×
261
        ippool.Status.V4AvailableIPs = v4a
×
262
        ippool.Status.V4UsingIPs = v4u
×
263
        ippool.Status.V6AvailableIPs = v6a
×
264
        ippool.Status.V6UsingIPs = v6u
×
265
        ippool.Status.V4AvailableIPRange = v4as
×
266
        ippool.Status.V4UsingIPRange = v4us
×
267
        ippool.Status.V6AvailableIPRange = v6as
×
268
        ippool.Status.V6UsingIPRange = v6us
×
269
}
×
270

271
func (c *Controller) reconcileIPPoolAddressSet(ippool *kubeovnv1.IPPool) error {
×
272
        asName := util.IPPoolAddressSetName(ippool.Name)
×
273

×
274
        if !ippool.Spec.EnableAddressSet {
×
275
                if err := c.OVNNbClient.DeleteAddressSet(asName); err != nil {
×
276
                        err = fmt.Errorf("failed to delete address set %s: %w", asName, err)
×
277
                        klog.Error(err)
×
278
                        return err
×
279
                }
×
280
                return nil
×
281
        }
282

283
        addresses, err := util.ExpandIPPoolAddressesForOVN(ippool.Spec.IPs)
×
284
        if err != nil {
×
285
                err = fmt.Errorf("failed to build address set entries for ippool %s: %w", ippool.Name, err)
×
286
                klog.Error(err)
×
287
                return err
×
288
        }
×
289

290
        if err := c.OVNNbClient.CreateAddressSet(asName, map[string]string{ippoolKey: ippool.Name}); err != nil {
×
291
                err = fmt.Errorf("failed to create address set for ippool %s: %w", ippool.Name, err)
×
292
                klog.Error(err)
×
293
                return err
×
294
        }
×
295

296
        if err := c.OVNNbClient.AddressSetUpdateAddress(asName, addresses...); err != nil {
×
297
                err = fmt.Errorf("failed to update address set for ippool %s: %w", ippool.Name, err)
×
298
                klog.Error(err)
×
299
                return err
×
300
        }
×
301

302
        return nil
×
303
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc