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

kubeovn / kube-ovn / 26878340903

03 Jun 2026 10:15AM UTC coverage: 25.084% (-0.02%) from 25.104%
26878340903

push

github

oilbeater
PR introduces changes to correctly support multiple network interfaces from same subnet (#6766)

* PR introduces changes to allow kube-ovn-cni and kube-ovn-controller to
correctly handle multiple network attachments from same subnets.

Please refer to GH issue https://github.com/kubeovn/kube-ovn/issues/6762
for more details.

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>

* fine tune annotation lookup to ignore default interface name or eth0 as the case may be when trying to lookup kubeovn annotations

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>

* fix provide name elements lookup

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>

* simplify interface logic to leverage pod annotations to identify if interface name needs to be used for lookup

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>

* include pr review feedback, drop extraneous comments and methods

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>

---------

Signed-off-by: Gaurav Mehta <gaurav.mehta@suse.com>
(cherry picked from commit 7a2b2a246)

8 of 93 new or added lines in 4 files covered. (8.6%)

2 existing lines in 1 file now uncovered.

14232 of 56737 relevant lines covered (25.08%)

0.29 hits per line

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

0.0
/pkg/daemon/handler.go
1
package daemon
2

3
import (
4
        "context"
5
        "encoding/json"
6
        "fmt"
7
        "net"
8
        "net/http"
9
        "strconv"
10
        "strings"
11
        "time"
12

13
        "github.com/emicklei/go-restful/v3"
14
        v1 "k8s.io/api/core/v1"
15
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
16
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
        "k8s.io/apimachinery/pkg/labels"
18
        "k8s.io/client-go/kubernetes"
19
        "k8s.io/klog/v2"
20
        kubevirtv1 "kubevirt.io/api/core/v1"
21

22
        kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
23
        clientset "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
24
        "github.com/kubeovn/kube-ovn/pkg/ovs"
25
        "github.com/kubeovn/kube-ovn/pkg/request"
26
        "github.com/kubeovn/kube-ovn/pkg/util"
27
)
28

29
const (
30
        gatewayCheckModeDisabled = iota
31
        gatewayCheckModePing
32
        gatewayCheckModeArping
33
        gatewayCheckModePingNotConcerned
34
        gatewayCheckModeArpingNotConcerned
35
)
36

37
type cniServerHandler struct {
38
        Config        *Configuration
39
        KubeClient    kubernetes.Interface
40
        KubeOvnClient clientset.Interface
41
        Controller    *Controller
42
}
43

44
func createCniServerHandler(config *Configuration, controller *Controller) *cniServerHandler {
×
45
        csh := &cniServerHandler{KubeClient: config.KubeClient, KubeOvnClient: config.KubeOvnClient, Config: config, Controller: controller}
×
46
        return csh
×
47
}
×
48

NEW
49
func (csh cniServerHandler) providerExists(provider, ifName string) (*kubeovnv1.Subnet, bool) {
×
50
        if util.IsOvnProvider(provider) {
×
51
                return nil, true
×
52
        }
×
53
        subnets, _ := csh.Controller.subnetsLister.List(labels.Everything())
×
NEW
54
        // for multi interface attachments the ifname is included in provider, for example, vm-overlay.default.ovn.net1
×
NEW
55
        // as a result if ifname is set, we need to append it to subnet provider when comparing with request provider
×
NEW
56
        // else no subnet will be found
×
NEW
57
        providerName, _ := strings.CutSuffix(provider, fmt.Sprintf(".%s", ifName))
×
58
        for _, subnet := range subnets {
×
NEW
59
                if subnet.Spec.Provider == providerName {
×
60
                        return subnet.DeepCopy(), true
×
61
                }
×
62
        }
63
        return nil, false
×
64
}
65

66
func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Response) {
×
67
        podRequest := request.CniRequest{}
×
68
        if err := req.ReadEntity(&podRequest); err != nil {
×
69
                errMsg := fmt.Errorf("parse add request failed %w", err)
×
70
                klog.Error(errMsg)
×
71
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
72
                        klog.Errorf("failed to write response, %v", err)
×
73
                }
×
74
                return
×
75
        }
76
        klog.V(5).Infof("request body is %v", podRequest)
×
NEW
77
        podSubnet, exist := csh.providerExists(podRequest.Provider, podRequest.IfName)
×
78
        if !exist {
×
79
                errMsg := fmt.Errorf("provider %s not bind to any subnet", podRequest.Provider)
×
80
                klog.Error(errMsg)
×
81
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
82
                        klog.Errorf("failed to write response, %v", err)
×
83
                }
×
84
                return
×
85
        }
86

87
        klog.Infof("add port request: %v", podRequest)
×
88
        if err := csh.validatePodRequest(&podRequest); err != nil {
×
89
                klog.Error(err)
×
90
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: err.Error()}); err != nil {
×
91
                        klog.Errorf("failed to write response, %v", err)
×
92
                }
×
93
                return
×
94
        }
95

96
        var gatewayCheckMode int
×
97
        var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress, providerNetwork, ifName, nicType, podNicName, vmName, latency, limit, loss, jitter, u2oInterconnectionIP, oldPodName string
×
98
        var routes []request.Route
×
99
        var isDefaultRoute, noIPAM bool
×
100
        var pod *v1.Pod
×
101
        var err error
×
NEW
102

×
NEW
103
        providerWithIfName := fmt.Sprintf("%s.%s", podRequest.Provider, podRequest.IfName)
×
NEW
104
        var appendIfName bool
×
NEW
105

×
106
        for range 20 {
×
107
                if pod, err = csh.Controller.podsLister.Pods(podRequest.PodNamespace).Get(podRequest.PodName); err != nil {
×
108
                        errMsg := fmt.Errorf("get pod %s/%s failed %w", podRequest.PodNamespace, podRequest.PodName, err)
×
109
                        klog.Error(errMsg)
×
110
                        if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
111
                                klog.Errorf("failed to write response, %v", err)
×
112
                        }
×
113
                        return
×
114
                }
115

116
                // in case of multiple nics from same subnet
NEW
117
                _, ok := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, providerWithIfName)]
×
NEW
118
                if ok {
×
NEW
119
                        appendIfName = true
×
NEW
120
                }
×
121

NEW
122
                ip = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.IPAddressAnnotationTemplate, appendIfName)
×
NEW
123
                if ip == "" {
×
124
                        klog.Infof("wait address for pod %s/%s provider %s", podRequest.PodNamespace, podRequest.PodName, podRequest.Provider)
×
125
                        // wait controller assign an address
×
126
                        cniWaitAddressResult.WithLabelValues(nodeName).Inc()
×
127
                        time.Sleep(1 * time.Second)
×
128
                        continue
×
129
                }
130

131
                if err := util.ValidatePodNetwork(pod.Annotations); err != nil {
×
132
                        klog.Errorf("validate pod %s/%s failed, %v", podRequest.PodNamespace, podRequest.PodName, err)
×
133
                        // wait controller assign an address
×
134
                        cniWaitAddressResult.WithLabelValues(nodeName).Inc()
×
135
                        time.Sleep(1 * time.Second)
×
136
                        continue
×
137
                }
138

NEW
139
                cidr = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.CidrAnnotationTemplate, appendIfName)
×
NEW
140
                gw = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.GatewayAnnotationTemplate, appendIfName)
×
NEW
141
                subnet = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.LogicalSwitchAnnotationTemplate, appendIfName)
×
NEW
142
                ingress = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.IngressRateAnnotationTemplate, appendIfName)
×
NEW
143
                egress = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.EgressRateAnnotationTemplate, appendIfName)
×
NEW
144
                latency = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.NetemQosLatencyAnnotationTemplate, appendIfName)
×
NEW
145
                limit = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.NetemQosLimitAnnotationTemplate, appendIfName)
×
NEW
146
                loss = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.NetemQosLossAnnotationTemplate, appendIfName)
×
NEW
147
                jitter = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.NetemQosJitterAnnotationTemplate, appendIfName)
×
NEW
148
                providerNetwork = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.ProviderNetworkTemplate, appendIfName)
×
NEW
149
                vmName = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.VMAnnotationTemplate, appendIfName)
×
150
                ipAddr, noIPAM, err = util.GetIPAddrWithMaskForCNI(ip, cidr)
×
151
                if err != nil {
×
152
                        errMsg := fmt.Errorf("failed to get ip address with mask, %w", err)
×
153
                        klog.Error(errMsg)
×
154
                        if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
155
                                klog.Errorf("failed to write response, %v", err)
×
156
                        }
×
157
                        return
×
158
                }
159

160
                oldPodName = podRequest.PodName
×
161
                if s := pod.Annotations[fmt.Sprintf(util.RoutesAnnotationTemplate, podRequest.Provider)]; s != "" {
×
162
                        if err = json.Unmarshal([]byte(s), &routes); err != nil {
×
163
                                errMsg := fmt.Errorf("invalid routes for pod %s/%s: %w", pod.Namespace, pod.Name, err)
×
164
                                klog.Error(errMsg)
×
165
                                if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
166
                                        klog.Errorf("failed to write response: %v", err)
×
167
                                }
×
168
                                return
×
169
                        }
170
                }
171
                if ifName = podRequest.IfName; ifName == "" {
×
172
                        ifName = "eth0"
×
173
                }
×
174

175
                // To support KubeVirt hotplug dpdk nic, forbidden set the volume name
176
                if podRequest.VhostUserSocketConsumption == util.ConsumptionKubevirt {
×
177
                        podRequest.VhostUserSocketVolumeName = util.VhostUserSocketVolumeName
×
178
                }
×
179

180
                switch {
×
181
                case podRequest.DeviceID != "":
×
182
                        nicType = util.OffloadType
×
183
                case podRequest.VhostUserSocketVolumeName != "":
×
184
                        nicType = util.DpdkType
×
185
                        if err = createShortSharedDir(pod, podRequest.VhostUserSocketVolumeName, podRequest.VhostUserSocketConsumption, csh.Config.KubeletDir); err != nil {
×
186
                                klog.Error(err.Error())
×
187
                                if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
×
188
                                        klog.Errorf("failed to write response: %v", err)
×
189
                                }
×
190
                                return
×
191
                        }
192
                default:
×
193
                        nicType = pod.Annotations[fmt.Sprintf(util.PodNicAnnotationTemplate, podRequest.Provider)]
×
194
                }
195

196
                switch pod.Annotations[fmt.Sprintf(util.DefaultRouteAnnotationTemplate, podRequest.Provider)] {
×
197
                case "true":
×
198
                        isDefaultRoute = true
×
199
                case "false":
×
200
                        isDefaultRoute = false
×
201
                default:
×
202
                        isDefaultRoute = ifName == "eth0"
×
203
                }
204

205
                if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && util.IsOvnProvider(podRequest.Provider) {
×
206
                        klog.Infof("wait route ready for pod %s/%s provider %s", podRequest.PodNamespace, podRequest.PodName, podRequest.Provider)
×
207
                        cniWaitRouteResult.WithLabelValues(nodeName).Inc()
×
208
                        time.Sleep(1 * time.Second)
×
209
                        continue
×
210
                }
211

212
                if vmName != "" {
×
213
                        podRequest.PodName = vmName
×
214
                }
×
215

216
                break
×
217
        }
218

NEW
219
        if util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.IPAddressAnnotationTemplate, appendIfName) == "" {
×
220
                err := fmt.Errorf("no address allocated to pod %s/%s provider %s, please see kube-ovn-controller logs to find errors", pod.Namespace, pod.Name, podRequest.Provider)
×
221
                klog.Error(err)
×
222
                if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
×
223
                        klog.Errorf("failed to write response, %v", err)
×
224
                }
×
225
                return
×
226
        }
227

228
        if subnet == "" && podSubnet != nil {
×
229
                subnet = podSubnet.Name
×
230
        }
×
231
        if !noIPAM {
×
NEW
232
                if err := csh.UpdateIPCR(podRequest, subnet, ip, appendIfName); err != nil {
×
233
                        if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
×
234
                                klog.Errorf("failed to write response, %v", err)
×
235
                        }
×
236
                        return
×
237
                }
238
        }
239

240
        if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && util.IsOvnProvider(podRequest.Provider) {
×
241
                err := fmt.Errorf("route is not ready for pod %s/%s provider %s, please see kube-ovn-controller logs to find errors", pod.Namespace, pod.Name, podRequest.Provider)
×
242
                klog.Error(err)
×
243
                if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
×
244
                        klog.Errorf("failed to write response, %v", err)
×
245
                }
×
246
                return
×
247
        }
248

249
        var mtu int
×
250
        routes = append(podRequest.Routes, routes...)
×
251
        if strings.HasSuffix(podRequest.Provider, util.OvnProvider) && subnet != "" {
×
252
                podSubnet, err := csh.Controller.subnetsLister.Get(subnet)
×
253
                if err != nil {
×
254
                        errMsg := fmt.Errorf("failed to get subnet %s: %w", subnet, err)
×
255
                        klog.Error(errMsg)
×
256
                        if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
257
                                klog.Errorf("failed to write response: %v", err)
×
258
                        }
×
259
                        return
×
260
                }
261

262
                if podSubnet.Status.U2OInterconnectionIP == "" && podSubnet.Spec.U2OInterconnection {
×
263
                        errMsg := fmt.Errorf("failed to generate u2o ip on subnet %s", podSubnet.Name)
×
264
                        klog.Error(errMsg)
×
265
                        return
×
266
                }
×
267

268
                if podSubnet.Status.U2OInterconnectionIP != "" && podSubnet.Spec.U2OInterconnection {
×
269
                        u2oInterconnectionIP = podSubnet.Status.U2OInterconnectionIP
×
270
                }
×
271

272
                var vmMigration bool
×
273
                subnetHasVlan := podSubnet.Spec.Vlan != ""
×
274
                // skip ping check gateway for pods during live migration
×
275
                if pod.Annotations[kubevirtv1.MigrationJobNameAnnotation] == "" {
×
276
                        if subnetHasVlan && !podSubnet.Spec.LogicalGateway {
×
277
                                if podSubnet.Spec.DisableGatewayCheck {
×
278
                                        gatewayCheckMode = gatewayCheckModeArpingNotConcerned
×
279
                                } else {
×
280
                                        gatewayCheckMode = gatewayCheckModeArping
×
281
                                }
×
282
                        } else {
×
283
                                if podSubnet.Spec.DisableGatewayCheck {
×
284
                                        gatewayCheckMode = gatewayCheckModePingNotConcerned
×
285
                                } else {
×
286
                                        gatewayCheckMode = gatewayCheckModePing
×
287
                                }
×
288
                        }
289
                } else {
×
290
                        vmMigration = true
×
291
                }
×
292
                if pod.Annotations[fmt.Sprintf(util.ActivationStrategyTemplate, podRequest.Provider)] != "" {
×
293
                        gatewayCheckMode = gatewayCheckModeDisabled
×
294
                }
×
295

296
                if podSubnet.Spec.Mtu > 0 {
×
297
                        mtu = int(podSubnet.Spec.Mtu)
×
298
                } else {
×
299
                        if providerNetwork != "" && !podSubnet.Spec.LogicalGateway && !podSubnet.Spec.U2OInterconnection {
×
300
                                node, err := csh.Controller.nodesLister.Get(csh.Config.NodeName)
×
301
                                if err != nil {
×
302
                                        errMsg := fmt.Errorf("failed to get node %s: %w", csh.Config.NodeName, err)
×
303
                                        klog.Error(errMsg)
×
304
                                        if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
305
                                                klog.Errorf("failed to write response: %v", err)
×
306
                                        }
×
307
                                        return
×
308
                                }
309
                                mtuStr := node.Labels[fmt.Sprintf(util.ProviderNetworkMtuTemplate, providerNetwork)]
×
310
                                if mtuStr != "" {
×
311
                                        if mtu, err = strconv.Atoi(mtuStr); err != nil || mtu <= 0 {
×
312
                                                errMsg := fmt.Errorf("failed to parse provider network MTU %s: %w", mtuStr, err)
×
313
                                                klog.Error(errMsg)
×
314
                                                if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
315
                                                        klog.Errorf("failed to write response: %v", err)
×
316
                                                }
×
317
                                                return
×
318
                                        }
319
                                }
320
                        } else {
×
321
                                mtu = csh.Config.MTU
×
322
                        }
×
323
                }
324

NEW
325
                macAddr = util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.MacAddressAnnotationTemplate, appendIfName)
×
326
                klog.Infof("create container interface %s mac %s, ip %s, cidr %s, gw %s, custom routes %v", ifName, macAddr, ipAddr, cidr, gw, routes)
×
327
                podNicName = ifName
×
328

×
329
                var encapIP string
×
330
                if podSubnet.Spec.NodeNetwork != "" {
×
331
                        encapIP, err = csh.Config.GetEncapIPByNetwork(podSubnet.Spec.NodeNetwork)
×
332
                        if err != nil {
×
333
                                errMsg := fmt.Errorf("failed to get encap IP for node network %s: %w", podSubnet.Spec.NodeNetwork, err)
×
334
                                klog.Error(errMsg)
×
335
                                if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
336
                                        klog.Errorf("failed to write response: %v", err)
×
337
                                }
×
338
                                return
×
339
                        }
340
                }
341

342
                switch nicType {
×
343
                case util.DpdkType:
×
344
                        err = csh.configureDpdkNic(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, mtu, ipAddr, gw, ingress, egress, getShortSharedDir(pod.UID, podRequest.VhostUserSocketVolumeName), podRequest.VhostUserSocketName, podRequest.VhostUserSocketConsumption)
×
345
                        routes = nil
×
346
                default:
×
NEW
347
                        routes, err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, podRequest.VfDriver, ifName, macAddr, mtu, ipAddr, gw, isDefaultRoute, vmMigration, routes, podRequest.DNS.Nameservers, podRequest.DNS.Search, ingress, egress, podRequest.DeviceID, latency, limit, loss, jitter, gatewayCheckMode, u2oInterconnectionIP, oldPodName, encapIP, appendIfName)
×
348
                }
349
                if err != nil {
×
350
                        errMsg := fmt.Errorf("configure nic %s for pod %s/%s failed: %w", ifName, podRequest.PodName, podRequest.PodNamespace, err)
×
351
                        klog.Error(errMsg)
×
352
                        if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
353
                                klog.Errorf("failed to write response, %v", err)
×
354
                        }
×
355
                        return
×
356
                }
357

358
                ifaceID := ovs.PodNameToPortName(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider)
×
359
                if err = ovs.ConfigInterfaceMirror(csh.Config.EnableMirror, pod.Annotations[fmt.Sprintf(util.MirrorControlAnnotationTemplate, podRequest.Provider)], ifaceID); err != nil {
×
360
                        klog.Errorf("failed mirror to mirror0, %v", err)
×
361
                        return
×
362
                }
×
363

364
                if err = csh.Controller.addEgressConfig(podSubnet, ip); err != nil {
×
365
                        errMsg := fmt.Errorf("failed to add egress configuration: %w", err)
×
366
                        klog.Error(errMsg)
×
367
                        if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
368
                                klog.Errorf("failed to write response, %v", err)
×
369
                        }
×
370
                        return
×
371
                }
372
        } else if len(routes) != 0 {
×
373
                hasDefaultRoute := make(map[string]bool, 2)
×
374
                for _, r := range routes {
×
375
                        if r.Destination == "" {
×
376
                                hasDefaultRoute[util.CheckProtocol(r.Gateway)] = true
×
377
                                continue
×
378
                        }
379
                        if _, cidr, err := net.ParseCIDR(r.Destination); err == nil {
×
380
                                if ones, _ := cidr.Mask.Size(); ones == 0 {
×
381
                                        hasDefaultRoute[util.CheckProtocol(r.Gateway)] = true
×
382
                                }
×
383
                        }
384
                }
385
                if len(hasDefaultRoute) != 0 {
×
386
                        // remove existing default route so other CNI plugins, such as macvlan, can add the new default route correctly
×
387
                        if err = csh.removeDefaultRoute(podRequest.NetNs, hasDefaultRoute[kubeovnv1.ProtocolIPv4], hasDefaultRoute[kubeovnv1.ProtocolIPv6]); err != nil {
×
388
                                errMsg := fmt.Errorf("failed to remove existing default route for interface %s of pod %s/%s: %w", podRequest.IfName, podRequest.PodNamespace, podRequest.PodName, err)
×
389
                                klog.Error(errMsg)
×
390
                                if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
391
                                        klog.Errorf("failed to write response: %v", err)
×
392
                                }
×
393
                                return
×
394
                        }
395
                }
396
        }
397

398
        v4IP, v6IP := util.SplitStringIP(ip)
×
399
        v4CIDR, v6CIDR := util.SplitStringIP(cidr)
×
400
        v4GW, v6GW := util.SplitStringIP(gw)
×
401

×
402
        var ips []request.IPConfig
×
403
        if v4IP != "" {
×
404
                cfg := request.IPConfig{Protocol: kubeovnv1.ProtocolIPv4, IP: v4IP, CIDR: v4CIDR}
×
405
                if isDefaultRoute {
×
406
                        cfg.Gateway = v4GW
×
407
                }
×
408
                ips = append(ips, cfg)
×
409
        }
410
        if v6IP != "" {
×
411
                cfg := request.IPConfig{Protocol: kubeovnv1.ProtocolIPv6, IP: v6IP, CIDR: v6CIDR}
×
412
                if isDefaultRoute {
×
413
                        cfg.Gateway = v6GW
×
414
                }
×
415
                ips = append(ips, cfg)
×
416
        }
417

418
        response := &request.CniResponse{
×
419
                IPs:        ips,
×
420
                MacAddress: macAddr,
×
421
                PodNicName: podNicName,
×
422
                Routes:     routes,
×
423
                Mtu:        mtu,
×
424
        }
×
425
        if err := resp.WriteHeaderAndEntity(http.StatusOK, response); err != nil {
×
426
                klog.Errorf("failed to write response, %v", err)
×
427
        }
×
428
}
429

NEW
430
func (csh cniServerHandler) UpdateIPCR(podRequest request.CniRequest, subnet, ip string, appendIfName bool) error {
×
NEW
431
        klog.V(4).Infof("found subnet %s", subnet)
×
432
        ipCRName := ovs.PodNameToPortName(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider)
×
NEW
433

×
NEW
434
        // for backward compatibility we will check if annotation ip address exists with ifName first and subsequently with ifname
×
NEW
435
        if appendIfName {
×
NEW
436
                ipCRName = fmt.Sprintf("%s.%s", ipCRName, podRequest.IfName)
×
NEW
437
        }
×
438
        for range 20 {
×
439
                ipCR, err := csh.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), ipCRName, metav1.GetOptions{})
×
440
                if err != nil {
×
441
                        err = fmt.Errorf("failed to get ip crd for %s, %w", ip, err)
×
442
                        // maybe create a backup pod with previous annotations
×
443
                        klog.Error(err)
×
444
                } else if ipCR.Spec.NodeName != csh.Config.NodeName {
×
445
                        ipCR := ipCR.DeepCopy()
×
446
                        if ipCR.Labels == nil {
×
447
                                ipCR.Labels = map[string]string{}
×
448
                        }
×
449
                        ipCR.Spec.NodeName = csh.Config.NodeName
×
450
                        ipCR.Spec.AttachIPs = []string{}
×
451
                        ipCR.Labels[subnet] = ""
×
452
                        ipCR.Labels[util.NodeNameLabel] = csh.Config.NodeName
×
453
                        ipCR.Spec.AttachSubnets = []string{}
×
454
                        ipCR.Spec.AttachMacs = []string{}
×
455
                        if _, err := csh.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), ipCR, metav1.UpdateOptions{}); err != nil {
×
456
                                err = fmt.Errorf("failed to update ip crd for %s, %w", ip, err)
×
457
                                klog.Error(err)
×
458
                        } else {
×
459
                                return nil
×
460
                        }
×
461
                }
462
                if err != nil {
×
463
                        klog.Warningf("wait pod ip %s to be ready", ipCRName)
×
464
                        time.Sleep(1 * time.Second)
×
465
                } else {
×
466
                        return nil
×
467
                }
×
468
        }
469
        // update ip spec node is not that necessary, so we just log the error
470
        return nil
×
471
}
472

473
func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Response) {
×
474
        var podRequest request.CniRequest
×
NEW
475
        var appendIfName bool
×
NEW
476

×
477
        if err := req.ReadEntity(&podRequest); err != nil {
×
478
                errMsg := fmt.Errorf("parse del request failed %w", err)
×
479
                klog.Error(errMsg)
×
480
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
481
                        klog.Errorf("failed to write response, %v", err)
×
482
                }
×
483
                return
×
484
        }
485

486
        // Try to get the Pod, but if it fails due to not being found, log a warning and continue.
487
        pod, err := csh.Controller.podsLister.Pods(podRequest.PodNamespace).Get(podRequest.PodName)
×
488
        if err != nil && !k8serrors.IsNotFound(err) {
×
489
                errMsg := fmt.Errorf("failed to retrieve Pod %s/%s: %w", podRequest.PodNamespace, podRequest.PodName, err)
×
490
                klog.Error(errMsg)
×
491
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
492
                        klog.Errorf("failed to write response, %v", err)
×
493
                }
×
494
                return
×
495
        }
496

NEW
497
        providerWithIfName := fmt.Sprintf("%s.%s", podRequest.Provider, podRequest.IfName)
×
498
        if podRequest.NetNs == "" {
×
499
                klog.Infof("skip del port request: %v", podRequest)
×
500
                resp.WriteHeader(http.StatusNoContent)
×
501
                return
×
502
        }
×
503

504
        klog.Infof("del port request: %v", podRequest)
×
505
        if err := csh.validatePodRequest(&podRequest); err != nil {
×
506
                klog.Error(err)
×
507
                if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: err.Error()}); err != nil {
×
508
                        klog.Errorf("failed to write response, %v", err)
×
509
                }
×
510
                return
×
511
        }
512

513
        var nicType string
×
514
        var vmName string
×
515

×
516
        // If the Pod was found, process its annotations and labels.
×
517
        if pod != nil {
×
518
                if pod.Annotations != nil && (util.IsOvnProvider(podRequest.Provider) || podRequest.CniType == util.CniTypeName) {
×
NEW
519
                        _, ok := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, providerWithIfName)]
×
NEW
520
                        if ok {
×
NEW
521
                                appendIfName = true
×
NEW
522
                        }
×
NEW
523
                        subnet := util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.LogicalSwitchAnnotationTemplate, appendIfName)
×
524
                        if subnet != "" {
×
NEW
525
                                ip := util.GetAnnotationWithIfNameOverride(pod.Annotations, podRequest.Provider, podRequest.IfName, util.IPAddressAnnotationTemplate, appendIfName)
×
526
                                if err = csh.Controller.removeEgressConfig(subnet, ip); err != nil {
×
527
                                        errMsg := fmt.Errorf("failed to remove egress configuration: %w", err)
×
528
                                        klog.Error(errMsg)
×
529
                                        if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
530
                                                klog.Errorf("failed to write response, %v", err)
×
531
                                        }
×
532
                                        return
×
533
                                }
534
                        }
535

536
                        switch {
×
537
                        case podRequest.DeviceID != "":
×
538
                                nicType = util.OffloadType
×
539
                        case podRequest.VhostUserSocketVolumeName != "":
×
540
                                nicType = util.DpdkType
×
541
                                if err = removeShortSharedDir(pod, podRequest.VhostUserSocketVolumeName, podRequest.VhostUserSocketConsumption); err != nil {
×
542
                                        klog.Error(err.Error())
×
543
                                        if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
×
544
                                                klog.Errorf("failed to write response: %v", err)
×
545
                                        }
×
546
                                        return
×
547
                                }
548
                        default:
×
549
                                nicType = pod.Annotations[fmt.Sprintf(util.PodNicAnnotationTemplate, podRequest.Provider)]
×
550
                        }
551

552
                        vmName = pod.Annotations[fmt.Sprintf(util.VMAnnotationTemplate, podRequest.Provider)]
×
553
                        if vmName != "" {
×
554
                                podRequest.PodName = vmName
×
555
                        }
×
556
                }
557
        } else {
×
558
                // If the Pod is not found, assign a default value.
×
559
                klog.Warningf("Pod %s not found, proceeding with NIC deletion using ContainerID and NetNs", podRequest.PodName)
×
560
                switch {
×
561
                case podRequest.DeviceID != "":
×
562
                        nicType = util.OffloadType
×
563
                case podRequest.VhostUserSocketVolumeName != "":
×
564
                        nicType = util.DpdkType
×
565
                default:
×
566
                        nicType = util.VethType
×
567
                }
568
        }
569

570
        // To support KubeVirt hotplug dpdk nic, forbidden set the volume name
571
        if podRequest.VhostUserSocketConsumption == util.ConsumptionKubevirt {
×
572
                podRequest.VhostUserSocketVolumeName = util.VhostUserSocketVolumeName
×
573
        }
×
574

575
        // Proceed to delete the NIC regardless of whether the Pod was found or not.
576
        err = csh.deleteNic(podRequest.PodName, podRequest.PodNamespace, podRequest.ContainerID, podRequest.NetNs, podRequest.DeviceID, podRequest.IfName, nicType)
×
577
        if err != nil {
×
578
                errMsg := fmt.Errorf("del nic failed %w", err)
×
579
                klog.Error(errMsg)
×
580
                if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
×
581
                        klog.Errorf("failed to write response, %v", err)
×
582
                }
×
583
                return
×
584
        }
585
        resp.WriteHeader(http.StatusNoContent)
×
586
}
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