@@ -11,6 +11,7 @@ import (
11
11
"syscall"
12
12
"time"
13
13
14
+ "github.com./ccoveille/go-safecast"
14
15
"github.com./cloudnativelabs/kube-router/v2/pkg/metrics"
15
16
"github.com./cloudnativelabs/kube-router/v2/pkg/utils"
16
17
"github.com./moby/ipvs"
@@ -135,6 +136,10 @@ func (nsc *NetworkServicesController) setupClusterIPServices(serviceInfoMap serv
135
136
if err != nil {
136
137
return fmt .Errorf ("failed creating dummy interface: %v" , err )
137
138
}
139
+ sPort , err := safecast .ToUint16 (svc .port )
140
+ if err != nil {
141
+ return fmt .Errorf ("failed to convert service port to uint16: %v" , err )
142
+ }
138
143
139
144
for family , famClusIPs := range clusterIPs {
140
145
var nodeIP string
@@ -159,7 +164,7 @@ func (nsc *NetworkServicesController) setupClusterIPServices(serviceInfoMap serv
159
164
160
165
// create IPVS service for the service to be exposed through the cluster ip
161
166
ipvsSvcs , svcID , ipvsSvc = nsc .addIPVSService (ipvsSvcs , activeServiceEndpointMap , svc , clusterIP ,
162
- protocol , uint16 ( svc . port ) )
167
+ protocol , sPort )
163
168
// We weren't able to create the IPVS service, so we won't be able to add endpoints to it
164
169
if svcID == "" {
165
170
// not logging an error here because it was already logged in the addIPVSService function
@@ -241,13 +246,19 @@ func (nsc *NetworkServicesController) addEndpointsToIPVSService(endpoints []endp
241
246
syscallINET = syscall .AF_INET6
242
247
}
243
248
249
+ ePort , err := safecast .ToUint16 (endpoint .port )
250
+ if err != nil {
251
+ klog .Errorf ("failed to convert endpoint port to uint16: %v" , err )
252
+ continue
253
+ }
254
+
244
255
dst := ipvs.Destination {
245
256
Address : eIP ,
246
257
AddressFamily : syscallINET ,
247
- Port : uint16 ( endpoint . port ) ,
258
+ Port : ePort ,
248
259
Weight : 1 ,
249
260
}
250
- err : = nsc .ln .ipvsAddServer (ipvsSvc , & dst )
261
+ err = nsc .ln .ipvsAddServer (ipvsSvc , & dst )
251
262
if err != nil {
252
263
klog .Errorf ("encountered error adding endpoint to service: %v" , err )
253
264
continue
@@ -282,6 +293,11 @@ func (nsc *NetworkServicesController) setupNodePortServices(serviceInfoMap servi
282
293
continue
283
294
}
284
295
296
+ nPort , err := safecast .ToUint16 (svc .nodePort )
297
+ if err != nil {
298
+ return fmt .Errorf ("failed to convert node port to uint16: %v" , err )
299
+ }
300
+
285
301
var svcID string
286
302
var ipvsSvc * ipvs.Service
287
303
if nsc .nodeportBindOnAllIP {
@@ -312,7 +328,7 @@ func (nsc *NetworkServicesController) setupNodePortServices(serviceInfoMap servi
312
328
for _ , addr := range addrs {
313
329
314
330
ipvsSvcs , svcID , ipvsSvc = nsc .addIPVSService (ipvsSvcs , activeServiceEndpointMap , svc , addr ,
315
- protocol , uint16 ( svc . nodePort ) )
331
+ protocol , nPort )
316
332
// We weren't able to create the IPVS service, so we won't be able to add endpoints to it
317
333
if svcID == "" {
318
334
continue
@@ -322,7 +338,7 @@ func (nsc *NetworkServicesController) setupNodePortServices(serviceInfoMap servi
322
338
}
323
339
} else {
324
340
ipvsSvcs , svcID , ipvsSvc = nsc .addIPVSService (ipvsSvcs , activeServiceEndpointMap , svc ,
325
- nsc .krNode .GetPrimaryNodeIP (), protocol , uint16 ( svc . nodePort ) )
341
+ nsc .krNode .GetPrimaryNodeIP (), protocol , nPort )
326
342
// We weren't able to create the IPVS service, so we won't be able to add endpoints to it
327
343
if svcID == "" {
328
344
continue
@@ -416,6 +432,11 @@ func (nsc *NetworkServicesController) setupExternalIPForService(svc *serviceInfo
416
432
return fmt .Errorf ("failed get list of IPVS services due to: %v" , err )
417
433
}
418
434
435
+ sPort , err := safecast .ToUint16 (svc .port )
436
+ if err != nil {
437
+ return fmt .Errorf ("failed to convert service port to uint16: %v" , err )
438
+ }
439
+
419
440
// ensure director with vip assigned
420
441
err = nsc .ln .ipAddrAdd (dummyVipInterface , externalIP .String (), nodeIP .String (), true )
421
442
if err != nil && err .Error () != IfaceHasAddr {
@@ -424,8 +445,7 @@ func (nsc *NetworkServicesController) setupExternalIPForService(svc *serviceInfo
424
445
}
425
446
426
447
// create IPVS service for the service to be exposed through the external ip
427
- _ , svcID , ipvsExternalIPSvc = nsc .addIPVSService (ipvsSvcs , svcEndpointMap , svc , externalIP , protocol ,
428
- uint16 (svc .port ))
448
+ _ , svcID , ipvsExternalIPSvc = nsc .addIPVSService (ipvsSvcs , svcEndpointMap , svc , externalIP , protocol , sPort )
429
449
if svcID == "" {
430
450
return fmt .Errorf ("failed to create ipvs service for external ip: %s" , externalIP )
431
451
}
@@ -510,7 +530,12 @@ func (nsc *NetworkServicesController) setupExternalIPForDSRService(svcIn *servic
510
530
return fmt .Errorf ("failed to generate FW mark" )
511
531
}
512
532
513
- ipvsExternalIPSvc , err := nsc .ln .ipvsAddFWMarkService (ipvsSvcs , fwMark , sysFamily , protocol , uint16 (svcIn .port ),
533
+ sInPort , err := safecast .ToUint16 (svcIn .port )
534
+ if err != nil {
535
+ return fmt .Errorf ("failed to convert serviceIn port to uint16: %v" , err )
536
+ }
537
+
538
+ ipvsExternalIPSvc , err := nsc .ln .ipvsAddFWMarkService (ipvsSvcs , fwMark , sysFamily , protocol , sInPort ,
514
539
svcIn .sessionAffinity , svcIn .sessionAffinityTimeoutSeconds , svcIn .scheduler , svcIn .flags )
515
540
if err != nil {
516
541
return fmt .Errorf ("failed to create IPVS service for FWMark service: %d (external IP: %s) due to: %s" ,
@@ -570,12 +595,17 @@ func (nsc *NetworkServicesController) setupExternalIPForDSRService(svcIn *servic
570
595
syscallINET = syscall .AF_INET6
571
596
}
572
597
598
+ ePort , err := safecast .ToUint16 (endpoint .port )
599
+ if err != nil {
600
+ return fmt .Errorf ("failed to convert endpoint port to uint16: %v" , err )
601
+ }
602
+
573
603
// create the basic IPVS destination record
574
604
dst := ipvs.Destination {
575
605
Address : eIP ,
576
606
AddressFamily : syscallINET ,
577
607
ConnectionFlags : ipvs .ConnectionFlagTunnel ,
578
- Port : uint16 ( endpoint . port ) ,
608
+ Port : ePort ,
579
609
Weight : 1 ,
580
610
}
581
611
0 commit comments