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

kubeovn / kube-ovn / 14547474456

19 Apr 2025 08:37AM UTC coverage: 21.73% (-0.007%) from 21.737%
14547474456

push

github

web-flow
modernize: simplify code by using modern constructs (#5163)

Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>

87 of 376 new or added lines in 94 files covered. (23.14%)

1 existing line in 1 file now uncovered.

10251 of 47175 relevant lines covered (21.73%)

0.25 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
        "reflect"
6
        "slices"
7

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

16
        kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
17
        "github.com/kubeovn/kube-ovn/pkg/util"
18
)
19

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

NEW
26
func (c *Controller) enqueueDeleteIPPool(obj any) {
×
27
        ippool := obj.(*kubeovnv1.IPPool)
×
28
        klog.V(3).Infof("enqueue delete ippool %s", cache.MetaObjectToName(ippool).String())
×
29
        c.deleteIPPoolQueue.Add(ippool)
×
30
}
×
31

NEW
32
func (c *Controller) enqueueUpdateIPPool(oldObj, newObj any) {
×
33
        oldIPPool := oldObj.(*kubeovnv1.IPPool)
×
34
        newIPPool := newObj.(*kubeovnv1.IPPool)
×
35
        if !slices.Equal(oldIPPool.Spec.Namespaces, newIPPool.Spec.Namespaces) ||
×
36
                !slices.Equal(oldIPPool.Spec.IPs, newIPPool.Spec.IPs) {
×
37
                key := cache.MetaObjectToName(newIPPool).String()
×
38
                klog.V(3).Infof("enqueue update ippool %s", key)
×
39
                c.addOrUpdateIPPoolQueue.Add(key)
×
40
        }
×
41
}
42

43
func (c *Controller) handleAddOrUpdateIPPool(key string) error {
×
44
        c.ippoolKeyMutex.LockKey(key)
×
45
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(key) }()
×
46

47
        cachedIPPool, err := c.ippoolLister.Get(key)
×
48
        if err != nil {
×
49
                if k8serrors.IsNotFound(err) {
×
50
                        return nil
×
51
                }
×
52
                klog.Error(err)
×
53
                return err
×
54
        }
55
        klog.Infof("handle add/update ippool %s", cachedIPPool.Name)
×
56

×
57
        ippool := cachedIPPool.DeepCopy()
×
58
        ippool.Status.EnsureStandardConditions()
×
59
        if err = c.ipam.AddOrUpdateIPPool(ippool.Spec.Subnet, ippool.Name, ippool.Spec.IPs); err != nil {
×
60
                klog.Errorf("failed to add/update ippool %s with IPs %v in subnet %s: %v", ippool.Name, ippool.Spec.IPs, ippool.Spec.Subnet, err)
×
61
                if patchErr := c.patchIPPoolStatusCondition(ippool, "UpdateIPAMFailed", err.Error()); patchErr != nil {
×
62
                        klog.Error(patchErr)
×
63
                }
×
64
                return err
×
65
        }
66

67
        v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us := c.ipam.IPPoolStatistics(ippool.Spec.Subnet, ippool.Name)
×
68
        ippool.Status.V4AvailableIPs = v4a
×
69
        ippool.Status.V4UsingIPs = v4u
×
70
        ippool.Status.V6AvailableIPs = v6a
×
71
        ippool.Status.V6UsingIPs = v6u
×
72
        ippool.Status.V4AvailableIPRange = v4as
×
73
        ippool.Status.V4UsingIPRange = v4us
×
74
        ippool.Status.V6AvailableIPRange = v6as
×
75
        ippool.Status.V6UsingIPRange = v6us
×
76

×
77
        if err = c.patchIPPoolStatusCondition(ippool, "UpdateIPAMSucceeded", ""); err != nil {
×
78
                klog.Error(err)
×
79
                return err
×
80
        }
×
81

82
        for _, ns := range ippool.Spec.Namespaces {
×
83
                c.addNamespaceQueue.Add(ns)
×
84
        }
×
85

86
        return nil
×
87
}
88

89
func (c *Controller) handleDeleteIPPool(ippool *kubeovnv1.IPPool) error {
×
90
        c.ippoolKeyMutex.LockKey(ippool.Name)
×
91
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(ippool.Name) }()
×
92

93
        klog.Infof("handle delete ippool %s", ippool.Name)
×
94
        c.ipam.RemoveIPPool(ippool.Spec.Subnet, ippool.Name)
×
95

×
96
        namespaces, err := c.namespacesLister.List(labels.Everything())
×
97
        if err != nil {
×
98
                klog.Errorf("failed to list namespaces: %v", err)
×
99
                return err
×
100
        }
×
101

102
        for _, ns := range namespaces {
×
103
                if len(ns.Annotations) == 0 {
×
104
                        continue
×
105
                }
106
                if ns.Annotations[util.IPPoolAnnotation] == ippool.Name {
×
107
                        c.enqueueAddNamespace(ns)
×
108
                }
×
109
        }
110

111
        return nil
×
112
}
113

114
func (c *Controller) handleUpdateIPPoolStatus(key string) error {
×
115
        c.ippoolKeyMutex.LockKey(key)
×
116
        defer func() { _ = c.ippoolKeyMutex.UnlockKey(key) }()
×
117

118
        cachedIPPool, err := c.ippoolLister.Get(key)
×
119
        if err != nil {
×
120
                if k8serrors.IsNotFound(err) {
×
121
                        return nil
×
122
                }
×
123
                klog.Error(err)
×
124
                return err
×
125
        }
126

127
        ippool := cachedIPPool.DeepCopy()
×
128
        v4a, v4u, v6a, v6u, v4as, v4us, v6as, v6us := c.ipam.IPPoolStatistics(ippool.Spec.Subnet, ippool.Name)
×
129
        ippool.Status.V4AvailableIPs = v4a
×
130
        ippool.Status.V4UsingIPs = v4u
×
131
        ippool.Status.V6AvailableIPs = v6a
×
132
        ippool.Status.V6UsingIPs = v6u
×
133
        ippool.Status.V4AvailableIPRange = v4as
×
134
        ippool.Status.V4UsingIPRange = v4us
×
135
        ippool.Status.V6AvailableIPRange = v6as
×
136
        ippool.Status.V6UsingIPRange = v6us
×
137
        if reflect.DeepEqual(ippool.Status, cachedIPPool.Status) {
×
138
                return nil
×
139
        }
×
140

141
        return c.patchIPPoolStatus(ippool)
×
142
}
143

144
func (c Controller) patchIPPoolStatusCondition(ippool *kubeovnv1.IPPool, reason, errMsg string) error {
×
145
        if errMsg != "" {
×
146
                ippool.Status.SetError(reason, errMsg)
×
147
                ippool.Status.NotReady(reason, errMsg)
×
148
                c.recorder.Eventf(ippool, corev1.EventTypeWarning, reason, errMsg)
×
149
        } else {
×
150
                ippool.Status.Ready(reason, "")
×
151
                c.recorder.Eventf(ippool, corev1.EventTypeNormal, reason, errMsg)
×
152
        }
×
153

154
        return c.patchIPPoolStatus(ippool)
×
155
}
156

157
func (c Controller) patchIPPoolStatus(ippool *kubeovnv1.IPPool) error {
×
158
        bytes, err := ippool.Status.Bytes()
×
159
        if err != nil {
×
160
                klog.Errorf("failed to generate json representation for status of ippool %s: %v", ippool.Name, err)
×
161
                return err
×
162
        }
×
163
        if _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status"); err != nil {
×
164
                klog.Errorf("failed to patch status of ippool %s: %v", ippool.Name, err)
×
165
                return err
×
166
        }
×
167

168
        return nil
×
169
}
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