@@ -42,6 +42,8 @@ const (
42
42
NLBHealthCheckIntervalMax = 1800000
43
43
)
44
44
45
+ const ProtocolTypeMixed = "TCP_AND_UDP"
46
+
45
47
const (
46
48
// ServiceAnnotationLoadBalancerInternal is a service annotation for
47
49
// specifying that a load balancer should be internal.
@@ -199,6 +201,8 @@ const (
199
201
// ServiceAnnotationNetworkLoadBalancerNodeFilter is a service annotation to select specific nodes as your backend in the NLB
200
202
// based on label selector.
201
203
ServiceAnnotationNetworkLoadBalancerNodeFilter = "oci-network-load-balancer.oraclecloud.com/node-label-selector"
204
+
205
+ ServiceAnnotationNetworkLoadBalancerMixedProtocol = "oci-network-load-balancer.oraclecloud.com/node-label-selector"
202
206
)
203
207
204
208
// certificateData is a structure containing the data about a K8S secret required
@@ -487,13 +491,28 @@ func getBackendSetName(protocol string, port int) string {
487
491
488
492
func getPorts (svc * v1.Service ) (map [string ]portSpec , error ) {
489
493
ports := make (map [string ]portSpec )
494
+ portsMap := make (map [int ][]string )
495
+ mixedProtocolsPortSet := make (map [int ]bool )
490
496
for _ , servicePort := range svc .Spec .Ports {
491
- name := getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
497
+ portsMap [int (servicePort .Port )] = append (portsMap [int (servicePort .Port )], string (servicePort .Protocol ))
498
+ }
499
+ for _ , servicePort := range svc .Spec .Ports {
500
+ port := int (servicePort .Port )
501
+ backendSetName := ""
502
+ if len (portsMap [port ]) > 1 {
503
+ if mixedProtocolsPortSet [port ] {
504
+ continue
505
+ }
506
+ backendSetName = getBackendSetName (ProtocolTypeMixed , port )
507
+ mixedProtocolsPortSet [port ] = true
508
+ } else {
509
+ backendSetName = getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
510
+ }
492
511
healthChecker , err := getHealthChecker (svc )
493
512
if err != nil {
494
513
return nil , err
495
514
}
496
- ports [name ] = portSpec {
515
+ ports [backendSetName ] = portSpec {
497
516
BackendPort : int (servicePort .NodePort ),
498
517
ListenerPort : int (servicePort .Port ),
499
518
HealthCheckerPort : * healthChecker .Port ,
@@ -532,9 +551,23 @@ func getBackendSets(logger *zap.SugaredLogger, svc *v1.Service, nodes []*v1.Node
532
551
if err != nil {
533
552
return nil , err
534
553
}
554
+ portsMap := make (map [int ][]string )
555
+ mixedProtocolsPortSet := make (map [int ]bool )
556
+ for _ , servicePort := range svc .Spec .Ports {
557
+ portsMap [int (servicePort .Port )] = append (portsMap [int (servicePort .Port )], string (servicePort .Protocol ))
558
+ }
535
559
for _ , servicePort := range svc .Spec .Ports {
536
- name := getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
537
560
port := int (servicePort .Port )
561
+ backendSetName := ""
562
+ if len (portsMap [port ]) > 1 {
563
+ if mixedProtocolsPortSet [port ] {
564
+ continue
565
+ }
566
+ backendSetName = getBackendSetName (ProtocolTypeMixed , port )
567
+ mixedProtocolsPortSet [port ] = true
568
+ } else {
569
+ backendSetName = getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
570
+ }
538
571
var secretName string
539
572
if sslCfg != nil && len (sslCfg .BackendSetSSLSecretName ) != 0 {
540
573
secretName = sslCfg .BackendSetSSLSecretName
@@ -543,7 +576,7 @@ func getBackendSets(logger *zap.SugaredLogger, svc *v1.Service, nodes []*v1.Node
543
576
if err != nil {
544
577
return nil , err
545
578
}
546
- backendSets [name ] = client.GenericBackendSetDetails {
579
+ backendSets [backendSetName ] = client.GenericBackendSetDetails {
547
580
Policy : & loadbalancerPolicy ,
548
581
Backends : getBackends (logger , nodes , servicePort .NodePort ),
549
582
HealthChecker : healthChecker ,
@@ -789,6 +822,11 @@ func getListenersOciLoadBalancer(svc *v1.Service, sslCfg *SSLConfig) (map[string
789
822
790
823
func getListenersNetworkLoadBalancer (svc * v1.Service ) (map [string ]client.GenericListener , error ) {
791
824
listeners := make (map [string ]client.GenericListener )
825
+ portsMap := make (map [int ][]string )
826
+ mixedProtocolsPortSet := make (map [int ]bool )
827
+ for _ , servicePort := range svc .Spec .Ports {
828
+ portsMap [int (servicePort .Port )] = append (portsMap [int (servicePort .Port )], string (servicePort .Protocol ))
829
+ }
792
830
for _ , servicePort := range svc .Spec .Ports {
793
831
protocol := string (servicePort .Protocol )
794
832
@@ -801,17 +839,31 @@ func getListenersNetworkLoadBalancer(svc *v1.Service) (map[string]client.Generic
801
839
}
802
840
803
841
port := int (servicePort .Port )
804
- name := getListenerName (protocol , port )
842
+ listenerName := ""
843
+ backendSetName := ""
844
+ if len (portsMap [port ]) > 1 {
845
+ if mixedProtocolsPortSet [port ] {
846
+ continue
847
+ }
848
+ listenerName = getListenerName (ProtocolTypeMixed , port )
849
+ backendSetName = getBackendSetName (ProtocolTypeMixed , port )
850
+ protocol = ProtocolTypeMixed
851
+ mixedProtocolsPortSet [port ] = true
852
+ } else {
853
+ listenerName = getListenerName (protocol , port )
854
+ backendSetName = getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
855
+ }
805
856
806
857
listener := client.GenericListener {
807
- Name : & name ,
808
- DefaultBackendSetName : common .String (getBackendSetName ( string ( servicePort . Protocol ), int ( servicePort . Port )) ),
858
+ Name : & listenerName ,
859
+ DefaultBackendSetName : common .String (backendSetName ),
809
860
Protocol : & protocol ,
810
861
Port : & port ,
811
862
}
812
863
813
- listeners [name ] = listener
864
+ listeners [listenerName ] = listener
814
865
}
866
+
815
867
return listeners , nil
816
868
}
817
869
0 commit comments