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

kubeovn / kube-ovn / 20363501253

19 Dec 2025 07:49AM UTC coverage: 22.591% (-0.001%) from 22.592%
20363501253

push

github

web-flow
fix: correct typo in Makefile and enhance IP pool status event logging (#6071)

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

0 of 3 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

12050 of 53340 relevant lines covered (22.59%)

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())
×
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, "")
×
NEW
180
                // to observe ippool status change to normal
×
NEW
181
                c.recorder.Eventf(ippool, corev1.EventTypeNormal, reason, "")
×
UNCOV
182
        }
×
183

184
        return c.patchIPPoolStatus(ippool)
×
185
}
186

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

198
        return nil
×
199
}
200

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

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

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

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

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

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

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

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

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

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

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

304
        return nil
×
305
}
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