Skip to content

Commit

Permalink
fix udp/http listener validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
j1m-ryan committed Sep 12, 2024
1 parent f6f0aaa commit d996555
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/apis/configuration/validation/globalconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ func (gcv *GlobalConfigurationValidator) checkIPPortProtocolConflicts(combinatio
}

existingProtocol, exists := combinations[ip][listener.Port]
if exists {
if existingProtocol == listener.Protocol {
return field.Duplicate(fieldPath, fmt.Sprintf("Listener %s: Duplicated port/protocol combination %d/%s", listener.Name, listener.Port, listener.Protocol))
} else if listener.Protocol == "HTTP" || existingProtocol == "HTTP" {
return field.Invalid(fieldPath.Child("port"), listener.Port, fmt.Sprintf("Listener %s: Port %d is used with a different protocol (current: %s, new: %s)", listener.Name, listener.Port, existingProtocol, listener.Protocol))
if !exists {
return nil
}

switch listener.Protocol {
case "HTTP", "TCP":
if existingProtocol == "HTTP" || existingProtocol == "TCP" {
return field.Invalid(fieldPath.Child("protocol"), listener.Protocol, fmt.Sprintf("Listener %s: Duplicated ip:port protocol combination %d/%s", listener.Name, listener.Port, listener.Protocol))
}
case "UDP":
if existingProtocol == "UDP" {
return field.Invalid(fieldPath.Child("protocol"), listener.Protocol, fmt.Sprintf("Listener %s: Duplicated ip:port protocol combination %d/%s", listener.Name, listener.Port, listener.Protocol))
}
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/configuration/validation/globalconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ func TestValidateListeners_PassesOnValidIPListeners(t *testing.T) {
{Name: "listener-2", IPv6IP: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", Port: 8080, Protocol: "HTTP"},
},
},
{
name: "UDP and HTTP Listeners with Same Port",
listeners: []conf_v1.Listener{
{Name: "listener-1", IPv4: "127.0.0.1", Port: 8080, Protocol: "UDP"},

Check failure on line 240 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Lint

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}

Check failure on line 240 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}
{Name: "listener-2", IPv4: "127.0.0.1", Port: 8080, Protocol: "HTTP"},

Check failure on line 241 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Lint

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}

Check failure on line 241 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}
},
},
{
name: "UDP and TCP Listeners with Same Port",
listeners: []conf_v1.Listener{
{Name: "listener-1", IPv4: "127.0.0.1", Port: 8080, Protocol: "UDP"},

Check failure on line 247 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Lint

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}

Check failure on line 247 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}
{Name: "listener-2", IPv4: "127.0.0.1", Port: 8080, Protocol: "TCP"},

Check failure on line 248 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Lint

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""} (typecheck)

Check failure on line 248 in pkg/apis/configuration/validation/globalconfiguration_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

unknown field IPv4 in struct literal of type struct{Name string "json:\"name\""; Port int "json:\"port\""; IPv4IP string "json:\"ipv4\""; IPv6IP string "json:\"ipv6\""; Protocol string "json:\"protocol\""; Ssl bool "json:\"ssl\""}
},
},
}

gcv := createGlobalConfigurationValidator()
Expand Down

0 comments on commit d996555

Please sign in to comment.