Skip to content

Commit 77f9db6

Browse files
vbhargav875l-technicore
authored andcommitted
Allow ports on UDP/TCP protocol
1 parent 3b5c6e6 commit 77f9db6

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

pkg/cloudprovider/providers/oci/load_balancer_spec.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
NLBHealthCheckIntervalMax = 1800000
4343
)
4444

45+
const ProtocolTypeMixed = "TCP_AND_UDP"
46+
4547
const (
4648
// ServiceAnnotationLoadBalancerInternal is a service annotation for
4749
// specifying that a load balancer should be internal.
@@ -199,6 +201,8 @@ const (
199201
// ServiceAnnotationNetworkLoadBalancerNodeFilter is a service annotation to select specific nodes as your backend in the NLB
200202
// based on label selector.
201203
ServiceAnnotationNetworkLoadBalancerNodeFilter = "oci-network-load-balancer.oraclecloud.com/node-label-selector"
204+
205+
ServiceAnnotationNetworkLoadBalancerMixedProtocol = "oci-network-load-balancer.oraclecloud.com/node-label-selector"
202206
)
203207

204208
// certificateData is a structure containing the data about a K8S secret required
@@ -487,13 +491,28 @@ func getBackendSetName(protocol string, port int) string {
487491

488492
func getPorts(svc *v1.Service) (map[string]portSpec, error) {
489493
ports := make(map[string]portSpec)
494+
portsMap := make(map[int][]string)
495+
mixedProtocolsPortSet := make(map[int]bool)
490496
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+
}
492511
healthChecker, err := getHealthChecker(svc)
493512
if err != nil {
494513
return nil, err
495514
}
496-
ports[name] = portSpec{
515+
ports[backendSetName] = portSpec{
497516
BackendPort: int(servicePort.NodePort),
498517
ListenerPort: int(servicePort.Port),
499518
HealthCheckerPort: *healthChecker.Port,
@@ -532,9 +551,23 @@ func getBackendSets(logger *zap.SugaredLogger, svc *v1.Service, nodes []*v1.Node
532551
if err != nil {
533552
return nil, err
534553
}
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+
}
535559
for _, servicePort := range svc.Spec.Ports {
536-
name := getBackendSetName(string(servicePort.Protocol), int(servicePort.Port))
537560
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+
}
538571
var secretName string
539572
if sslCfg != nil && len(sslCfg.BackendSetSSLSecretName) != 0 {
540573
secretName = sslCfg.BackendSetSSLSecretName
@@ -543,7 +576,7 @@ func getBackendSets(logger *zap.SugaredLogger, svc *v1.Service, nodes []*v1.Node
543576
if err != nil {
544577
return nil, err
545578
}
546-
backendSets[name] = client.GenericBackendSetDetails{
579+
backendSets[backendSetName] = client.GenericBackendSetDetails{
547580
Policy: &loadbalancerPolicy,
548581
Backends: getBackends(logger, nodes, servicePort.NodePort),
549582
HealthChecker: healthChecker,
@@ -789,6 +822,11 @@ func getListenersOciLoadBalancer(svc *v1.Service, sslCfg *SSLConfig) (map[string
789822

790823
func getListenersNetworkLoadBalancer(svc *v1.Service) (map[string]client.GenericListener, error) {
791824
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+
}
792830
for _, servicePort := range svc.Spec.Ports {
793831
protocol := string(servicePort.Protocol)
794832

@@ -801,17 +839,31 @@ func getListenersNetworkLoadBalancer(svc *v1.Service) (map[string]client.Generic
801839
}
802840

803841
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+
}
805856

806857
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),
809860
Protocol: &protocol,
810861
Port: &port,
811862
}
812863

813-
listeners[name] = listener
864+
listeners[listenerName] = listener
814865
}
866+
815867
return listeners, nil
816868
}
817869

0 commit comments

Comments
 (0)