Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forwardingrules.GetILBProtocol func for mixed protocol #2746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkg/forwardingrules/protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package forwardingrules

import api_v1 "k8s.io/api/core/v1"

const (
// ProtocolTCP is the value used for Protocol when creating TCP Forwarding Rule
ProtocolTCP = "TCP"
// ProtocolUDP is the value used for Protocol when creating UDP Forwarding Rule
ProtocolUDP = "UDP"
// ProtocolL3 is the value used for Protocol when creating L3 Forwarding Rule.
// L3 Forwarding rule allows all the traffic to flow through it regardless of ports
// and protocol. When using ProtocolL3 AllPorts option should be used as well.
ProtocolL3 = "L3_DEFAULT"
)

// GetILBProtocol returns the protocol to be used for a Forwarding Rule for an ILB.
// For ELB please use forwardingrules.NeedsTCP or forwardingrules.NeedsUDP functions.
func GetILBProtocol(svcPorts []api_v1.ServicePort) string {
protocolSet := make(map[api_v1.Protocol]struct{})
for _, port := range svcPorts {
protocol := port.Protocol
if protocol == "" {
// Protocol is optional, defaults to TCP
protocol = api_v1.ProtocolTCP
}
protocolSet[protocol] = struct{}{}
}

_, okTCP := protocolSet[api_v1.ProtocolTCP]
_, okUDP := protocolSet[api_v1.ProtocolUDP]

switch {
case okTCP && okUDP:
return ProtocolL3
case okUDP:
return ProtocolUDP
case okTCP:
return ProtocolTCP
default:
return ProtocolTCP
}
}
75 changes: 75 additions & 0 deletions pkg/forwardingrules/protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package forwardingrules_test

import (
"testing"

api_v1 "k8s.io/api/core/v1"
"k8s.io/ingress-gce/pkg/forwardingrules"
)

func TestGetProtocol(t *testing.T) {
tcpPort := api_v1.ServicePort{
Protocol: api_v1.ProtocolTCP,
}
udpPort := api_v1.ServicePort{
Protocol: api_v1.ProtocolUDP,
}
emptyPort := api_v1.ServicePort{}

testCases := []struct {
ports []api_v1.ServicePort
want string
desc string
}{
{
desc: "no ports",
ports: []api_v1.ServicePort{},
want: forwardingrules.ProtocolTCP,
},
{
desc: "udp single",
ports: []api_v1.ServicePort{udpPort},
want: forwardingrules.ProtocolUDP,
},
{
desc: "udp multiple",
ports: []api_v1.ServicePort{udpPort, udpPort},
want: forwardingrules.ProtocolUDP,
},
{
desc: "tcp single",
ports: []api_v1.ServicePort{tcpPort},
want: forwardingrules.ProtocolTCP,
},
{
desc: "tcp multiple",
ports: []api_v1.ServicePort{tcpPort, tcpPort},
want: forwardingrules.ProtocolTCP,
},
{
desc: "tcp default",
ports: []api_v1.ServicePort{emptyPort},
want: forwardingrules.ProtocolTCP,
},
{
desc: "mixed, udp first",
ports: []api_v1.ServicePort{udpPort, tcpPort},
want: forwardingrules.ProtocolL3,
},
{
desc: "mixed, tcp first",
ports: []api_v1.ServicePort{tcpPort, udpPort},
want: forwardingrules.ProtocolL3,
},
}

for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
got := forwardingrules.GetILBProtocol(tC.ports)

if got != tC.want {
t.Errorf("GetProtocol(_) = %v, want %v", got, tC.want)
}
})
}
}