• 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/namespace.go
1
package controller
2

3
import (
4
        "maps"
5
        "slices"
6
        "strings"
7

8
        "github.com/scylladb/go-set/strset"
9
        v1 "k8s.io/api/core/v1"
10
        "k8s.io/apimachinery/pkg/api/errors"
11
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
        "k8s.io/apimachinery/pkg/labels"
13
        "k8s.io/client-go/tools/cache"
14
        "k8s.io/klog/v2"
15

16
        "github.com/kubeovn/kube-ovn/pkg/util"
17
)
18

NEW
19
func (c *Controller) enqueueAddNamespace(obj any) {
×
20
        if c.config.EnableNP {
×
21
                for _, np := range c.namespaceMatchNetworkPolicies(obj.(*v1.Namespace)) {
×
22
                        c.updateNpQueue.Add(np)
×
23
                }
×
24
        }
25

26
        key := cache.MetaObjectToName(obj.(*v1.Namespace)).String()
×
27
        c.addNamespaceQueue.Add(key)
×
28
}
29

NEW
30
func (c *Controller) enqueueDeleteNamespace(obj any) {
×
31
        if c.config.EnableNP {
×
32
                for _, np := range c.namespaceMatchNetworkPolicies(obj.(*v1.Namespace)) {
×
33
                        c.updateNpQueue.Add(np)
×
34
                }
×
35
        }
36
        if c.config.EnableANP {
×
37
                c.updateAnpsByLabelsMatch(obj.(*v1.Namespace).Labels, nil)
×
38
        }
×
39
}
40

NEW
41
func (c *Controller) enqueueUpdateNamespace(oldObj, newObj any) {
×
42
        oldNs := oldObj.(*v1.Namespace)
×
43
        newNs := newObj.(*v1.Namespace)
×
44
        if oldNs.ResourceVersion == newNs.ResourceVersion {
×
45
                return
×
46
        }
×
47

48
        if !maps.Equal(oldNs.Labels, newNs.Labels) {
×
49
                if c.config.EnableNP {
×
50
                        oldNp := c.namespaceMatchNetworkPolicies(oldNs)
×
51
                        newNp := c.namespaceMatchNetworkPolicies(newNs)
×
52
                        for _, np := range util.DiffStringSlice(oldNp, newNp) {
×
53
                                c.updateNpQueue.Add(np)
×
54
                        }
×
55
                }
56

57
                if c.config.EnableANP {
×
58
                        c.updateAnpsByLabelsMatch(newObj.(*v1.Namespace).Labels, nil)
×
59
                }
×
60

61
                expectSubnets, err := c.getNsExpectSubnets(newNs)
×
62
                if err != nil {
×
63
                        klog.Errorf("failed to list expected subnets for namespace %s, %v", newNs.Name, err)
×
64
                        return
×
65
                }
×
66

67
                expectSubnetsSet := strset.New(expectSubnets...)
×
68
                existSubnetsSet := strset.New(strings.Split(newNs.Annotations[util.LogicalSwitchAnnotation], ",")...)
×
69
                if !expectSubnetsSet.IsEqual(existSubnetsSet) {
×
70
                        c.addNamespaceQueue.Add(newNs.Name)
×
71
                }
×
72
        }
73

74
        // in case annotations are removed by other controllers
75
        if newNs.Annotations == nil || newNs.Annotations[util.LogicalSwitchAnnotation] == "" {
×
76
                klog.Warningf("no logical switch annotation for ns %s", newNs.Name)
×
77
                c.addNamespaceQueue.Add(newNs.Name)
×
78
        }
×
79
}
80

81
func (c *Controller) handleAddNamespace(key string) error {
×
82
        c.nsKeyMutex.LockKey(key)
×
83
        defer func() { _ = c.nsKeyMutex.UnlockKey(key) }()
×
84
        klog.Infof("handle add/update namespace %s", key)
×
85

×
86
        cachedNs, err := c.namespacesLister.Get(key)
×
87
        if err != nil {
×
88
                if errors.IsNotFound(err) {
×
89
                        return nil
×
90
                }
×
91
                klog.Error(err)
×
92
                return err
×
93
        }
94
        namespace := cachedNs.DeepCopy()
×
95

×
96
        var ls string
×
97
        var lss, cidrs, excludeIps, ipPoolsAnnotation []string
×
98
        subnets, err := c.subnetsLister.List(labels.Everything())
×
99
        if err != nil {
×
100
                klog.Errorf("failed to list subnets %v", err)
×
101
                return err
×
102
        }
×
103
        ipPoolList, err := c.ippoolLister.List(labels.Everything())
×
104
        if err != nil {
×
105
                klog.Errorf("failed to list ippools: %v", err)
×
106
                return err
×
107
        }
×
108

109
        // check if subnet bind ns
110
        for _, s := range subnets {
×
NEW
111
                if slices.Contains(s.Spec.Namespaces, key) {
×
NEW
112
                        lss = append(lss, s.Name)
×
NEW
113
                        cidrs = append(cidrs, s.Spec.CIDRBlock)
×
NEW
114
                        excludeIps = append(excludeIps, strings.Join(s.Spec.ExcludeIps, ","))
×
UNCOV
115
                }
×
116

117
                // bind subnet with namespaceLabelSeletcor which select the namespace
118
                for _, nsSelector := range s.Spec.NamespaceSelectors {
×
119
                        matchSelector, err := metav1.LabelSelectorAsSelector(&nsSelector)
×
120
                        if err != nil {
×
121
                                klog.Errorf("failed to convert label selector, %v", err)
×
122
                                return err
×
123
                        }
×
124

125
                        if matchSelector.Matches(labels.Set(namespace.Labels)) {
×
126
                                if slices.Contains(lss, s.Name) {
×
127
                                        break
×
128
                                }
129
                                lss = append(lss, s.Name)
×
130
                                cidrs = append(cidrs, s.Spec.CIDRBlock)
×
131
                                excludeIps = append(excludeIps, strings.Join(s.Spec.ExcludeIps, ","))
×
132
                                break
×
133
                        }
134
                }
135

136
                // check if subnet is in custom vpc with configured defaultSubnet, then annotate the namespace with this subnet
137
                if s.Spec.Vpc != "" && s.Spec.Vpc != c.config.ClusterRouter {
×
138
                        vpc, err := c.vpcsLister.Get(s.Spec.Vpc)
×
139
                        if err != nil {
×
140
                                if errors.IsNotFound(err) {
×
141
                                        // this subnet is broken (it references a non-existent VPC) - we just ignore it.
×
142
                                        klog.Errorf("vpc %q is not found. Ignoring subnet %q: %v", s.Spec.Vpc, s.Name, err)
×
143
                                        break
×
144
                                }
145
                                klog.Errorf("failed to get vpc %q: %v", s.Spec.Vpc, err)
×
146
                                return err
×
147
                        }
148
                        if s.Name == vpc.Spec.DefaultSubnet {
×
149
                                if slices.Contains(vpc.Spec.Namespaces, key) && key != metav1.NamespaceSystem {
×
150
                                        lss = append([]string{s.Name}, lss...)
×
151
                                }
×
152
                        }
153
                }
154
        }
155

156
        for _, ipPool := range ipPoolList {
×
157
                if slices.Contains(ipPool.Spec.Namespaces, key) {
×
158
                        ipPoolsAnnotation = append(ipPoolsAnnotation, ipPool.Name)
×
159
                }
×
160
        }
161

162
        if lss == nil {
×
163
                // If NS does not belong to any custom VPC, then this NS belongs to the default VPC
×
164
                vpc, err := c.vpcsLister.Get(c.config.ClusterRouter)
×
165
                if err != nil {
×
166
                        klog.Errorf("failed to get default vpc %v", err)
×
167
                        return err
×
168
                }
×
169
                vpcs, err := c.vpcsLister.List(labels.Everything())
×
170
                if err != nil {
×
171
                        klog.Errorf("failed to list vpc %v", err)
×
172
                        return err
×
173
                }
×
174
                for _, v := range vpcs {
×
175
                        if slices.Contains(v.Spec.Namespaces, key) {
×
176
                                vpc = v
×
177
                                break
×
178
                        }
179
                }
180

181
                if vpc.Status.DefaultLogicalSwitch != "" {
×
182
                        ls = vpc.Status.DefaultLogicalSwitch
×
183
                } else {
×
184
                        ls = c.config.DefaultLogicalSwitch
×
185
                }
×
186
                subnet, err := c.subnetsLister.Get(ls)
×
187
                if err != nil {
×
188
                        klog.Errorf("failed to get default subnet %v", err)
×
189
                        return err
×
190
                }
×
191
                lss = append(lss, subnet.Name)
×
192
                cidrs = append(cidrs, subnet.Spec.CIDRBlock)
×
193
                excludeIps = append(excludeIps, strings.Join(subnet.Spec.ExcludeIps, ","))
×
194
        }
195

196
        if namespace.Annotations[util.LogicalSwitchAnnotation] == strings.Join(lss, ",") &&
×
197
                namespace.Annotations[util.CidrAnnotation] == strings.Join(cidrs, ";") &&
×
198
                namespace.Annotations[util.ExcludeIpsAnnotation] == strings.Join(excludeIps, ";") &&
×
199
                namespace.Annotations[util.IPPoolAnnotation] == strings.Join(ipPoolsAnnotation, ",") {
×
200
                return nil
×
201
        }
×
202

203
        patch := util.KVPatch{
×
204
                util.LogicalSwitchAnnotation: strings.Join(lss, ","),
×
205
                util.CidrAnnotation:          strings.Join(cidrs, ";"),
×
206
                util.ExcludeIpsAnnotation:    strings.Join(excludeIps, ";"),
×
207
        }
×
208

×
209
        if len(ipPoolsAnnotation) == 0 {
×
210
                patch[util.IPPoolAnnotation] = nil
×
211
        } else {
×
212
                patch[util.IPPoolAnnotation] = strings.Join(ipPoolsAnnotation, ",")
×
213
        }
×
214

215
        if err = util.PatchAnnotations(c.config.KubeClient.CoreV1().Namespaces(), key, patch); err != nil {
×
216
                klog.Errorf("patch namespace %s failed %v", key, err)
×
217
        }
×
218
        return err
×
219
}
220

221
func (c *Controller) getNsExpectSubnets(newNs *v1.Namespace) ([]string, error) {
×
222
        var expectSubnets []string
×
223

×
224
        subnets, err := c.subnetsLister.List(labels.Everything())
×
225
        if err != nil {
×
226
                klog.Errorf("failed to list subnets %v", err)
×
227
                return expectSubnets, err
×
228
        }
×
229
        for _, subnet := range subnets {
×
230
                // ns labels match subnet's selector
×
231
                for _, nsSelector := range subnet.Spec.NamespaceSelectors {
×
232
                        matchSelector, err := metav1.LabelSelectorAsSelector(&nsSelector)
×
233
                        if err != nil {
×
234
                                klog.Errorf("failed to convert label selector, %v", err)
×
235
                                return expectSubnets, err
×
236
                        }
×
237

238
                        if matchSelector.Matches(labels.Set(newNs.Labels)) {
×
239
                                expectSubnets = append(expectSubnets, subnet.Name)
×
240
                                break
×
241
                        }
242
                }
243

244
                // ns included in subnet's namespaces
245
                if slices.Contains(subnet.Spec.Namespaces, newNs.Name) && !slices.Contains(expectSubnets, subnet.Name) {
×
246
                        expectSubnets = append(expectSubnets, subnet.Name)
×
247
                }
×
248
        }
249

250
        return expectSubnets, nil
×
251
}
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