Skip to content

Commit

Permalink
add test for listener with same port
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthyparty committed Jul 25, 2024
1 parent 811196c commit efa3923
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func createManager(cfg config.Config, nginxChecker *nginxConfiguredOnStartChecke
// Note: when the leadership is lost, the manager will return an error in the Start() method.
// However, it will not wait for any Runnable it starts to finish, meaning any in-progress operations
// might get terminated half-way.
LeaderElection: false,
LeaderElection: true,
LeaderElectionNamespace: cfg.GatewayPodConfig.Namespace,
LeaderElectionID: cfg.LeaderElection.LockName,
// We're not enabling LeaderElectionReleaseOnCancel because when the Manager stops gracefully, it waits
Expand Down
19 changes: 13 additions & 6 deletions internal/mode/static/state/graph/gateway_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func newListenerConfiguratorFactory(
valErr := field.NotSupported(
field.NewPath("protocol"),
listener.Protocol,
[]string{string(v1.HTTPProtocolType), string(v1.HTTPSProtocolType)},
[]string{string(v1.HTTPProtocolType), string(v1.HTTPSProtocolType), string(v1.TLSProtocolType)},
)
return staticConds.NewListenerUnsupportedProtocol(valErr.Error()), false /* not attachable */
},
Expand Down Expand Up @@ -130,7 +130,9 @@ func newListenerConfiguratorFactory(
validateListenerLabelSelector,
validateListenerHostname,
},
conflictResolvers: []listenerConflictResolver{},
conflictResolvers: []listenerConflictResolver{
sharedPortConflictResolver,
},
externalReferenceResolvers: []listenerExternalReferenceResolver{},
},
}
Expand Down Expand Up @@ -434,8 +436,13 @@ func createHTTPSListenerValidator(protectedPorts ProtectedPorts) listenerValidat
}

func createPortConflictResolver() listenerConflictResolver {
protocolGoups := map[v1.ProtocolType]string{
v1.TLSProtocolType: "Secure",
v1.HTTPProtocolType: "Unsecure",
v1.HTTPSProtocolType: "Secure",
}
conflictedPorts := make(map[v1.PortNumber]bool)
portProtocolOwner := make(map[v1.PortNumber]v1.ProtocolType)
portProtocolOwner := make(map[v1.PortNumber]string)
listenersByPort := make(map[v1.PortNumber][]*Listener)

format := "Multiple listeners for the same port %d specify incompatible protocols; " +
Expand All @@ -458,15 +465,15 @@ func createPortConflictResolver() listenerConflictResolver {

listenersByPort[port] = append(listenersByPort[port], l)

protocol, ok := portProtocolOwner[port]
protocolGroup, ok := portProtocolOwner[port]
if !ok {
portProtocolOwner[port] = l.Source.Protocol
portProtocolOwner[port] = protocolGoups[l.Source.Protocol]
return
}

// if protocol owner doesn't match the listener's protocol we mark the port as conflicted,
// and invalidate all listeners we've seen for this port.
if protocol != l.Source.Protocol {
if protocolGroup != protocolGoups[l.Source.Protocol] {
conflictedPorts[port] = true
for _, l := range listenersByPort[port] {
l.Valid = false
Expand Down
42 changes: 42 additions & 0 deletions internal/mode/static/state/graph/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func TestBuildGateway(t *testing.T) {
createTCPListener := func(name, hostname string, port int) v1.Listener {
return createListener(name, hostname, port, v1.TCPProtocolType, nil)
}
createTLSListener := func(name, hostname string, port int) v1.Listener {
return createListener(name, hostname, port, v1.TLSProtocolType, nil)
}
createHTTPSListener := func(name, hostname string, port int, tls *v1.GatewayTLSConfig) v1.Listener {
return createListener(name, hostname, port, v1.HTTPSProtocolType, tls)
}
Expand Down Expand Up @@ -280,6 +283,9 @@ func TestBuildGateway(t *testing.T) {
gatewayTLSConfigDiffNs,
)

// tls listeners
foo443TLSListener := createTLSListener("foo-443-tls", "foo.example.com", 443)

// invalid listeners
invalidProtocolListener := createTCPListener("invalid-protocol", "bar.example.com", 80)
invalidPortListener := createHTTPListener("invalid-port", "invalid-port", 0)
Expand Down Expand Up @@ -948,6 +954,42 @@ func TestBuildGateway(t *testing.T) {
},
name: "nil gatewayclass",
},
{
gateway: createGateway(
gatewayCfg{listeners: []v1.Listener{foo443TLSListener, foo443Listener}},
),
gatewayClass: validGC,
expected: &Gateway{
Source: getLastCreatedGetaway(),
Valid: true,
Listeners: []*Listener{
{
Name: "foo-443-tls",
Source: foo443TLSListener,
Valid: false,
Attachable: true,
Routes: map[RouteKey]*L7Route{}, L4Routes: map[L4RouteKey]*L4Route{},
Conditions: staticConds.NewListenerProtocolConflict(conflict443PortMsg),
SupportedKinds: []v1.RouteGroupKind{
{Kind: kinds.TLSRoute},
},
},
{
Name: "foo-443",
Source: foo443Listener,
Valid: false,
Attachable: true,
Routes: map[RouteKey]*L7Route{}, L4Routes: map[L4RouteKey]*L4Route{},
Conditions: staticConds.NewListenerProtocolConflict(conflict443PortMsg),
SupportedKinds: []v1.RouteGroupKind{
{Kind: kinds.HTTPRoute},
{Kind: kinds.GRPCRoute},
},
},
},
},
name: "http listener and tls listener port conflicting",
},
}

secretResolver := newSecretResolver(
Expand Down
4 changes: 4 additions & 0 deletions internal/mode/static/state/graph/route_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func CreateRouteKey(obj client.Object) RouteKey {
}
}

// RemoveDuplicateHostnameRoutes will invalidate the older routes with duplicate routes and only keep the latest.
func RemoveDuplicateHostnameRoutes() {
}

// CreateRouteKeyL4 takes a client.Object and creates a L4RouteKey.
func CreateRouteKeyL4(obj client.Object) L4RouteKey {
nsName := types.NamespacedName{
Expand Down

0 comments on commit efa3923

Please sign in to comment.