Skip to content

Commit

Permalink
Merge pull request #4213 from zs-ko/fix-gatewayapi-protomatch
Browse files Browse the repository at this point in the history
Fix Gateway API TLS TCP Route
  • Loading branch information
k8s-ci-robot committed Feb 14, 2024
2 parents 484ab50 + 9795c0b commit 70c20b3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,18 @@ func uniqueTargets(targets endpoint.Targets) endpoint.Targets {

// gwProtocolMatches returns whether a and b are the same protocol,
// where HTTP and HTTPS are considered the same.
// and TLS and TCP are considered the same.
func gwProtocolMatches(a, b v1.ProtocolType) bool {
if a == v1.HTTPSProtocolType {
a = v1.HTTPProtocolType
}
if b == v1.HTTPSProtocolType {
b = v1.HTTPProtocolType
}
// if Listener is TLS and Route is TCP set Listener type to TCP as to pass true and return valid match
if a == v1.TCPProtocolType && b == v1.TLSProtocolType {
b = v1.TCPProtocolType
}
return a == b
}

Expand Down
56 changes: 56 additions & 0 deletions source/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package source
import (
"strings"
"testing"

v1 "sigs.k8s.io/gateway-api/apis/v1"
)

func TestGatewayMatchingHost(t *testing.T) {
Expand Down Expand Up @@ -105,6 +107,60 @@ func TestGatewayMatchingHost(t *testing.T) {
}
}

func TestGatewayMatchingProtocol(t *testing.T) {
tests := []struct {
route, lis string
desc string
ok bool
}{
{
desc: "protocol-matches-lis-https-route-http",
route: "HTTP",
lis: "HTTPS",
ok: true,
},
{
desc: "protocol-match-invalid-list-https-route-tcp",
route: "TCP",
lis: "HTTPS",
ok: false,
},
{
desc: "protocol-match-valid-lis-tls-route-tls",
route: "TLS",
lis: "TLS",
ok: true,
},
{
desc: "protocol-match-valid-lis-TLS-route-TCP",
route: "TCP",
lis: "TLS",
ok: true,
},
{
desc: "protocol-match-valid-lis-TLS-route-TCP",
route: "TLS",
lis: "TCP",
ok: false,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
for i := 0; i < 2; i++ {
if ok := gwProtocolMatches(v1.ProtocolType(tt.route), v1.ProtocolType(tt.lis)); ok != tt.ok {
t.Errorf(
"gwProtocolMatches(%q, %q); got: %v; want: %v",
tt.route, tt.lis, ok, tt.ok,
)
}
//tt.a, tt.b = tt.b, tt.a
}
})

}
}

func TestIsDNS1123Domain(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 70c20b3

Please sign in to comment.