Skip to content

Commit

Permalink
api-gateway: when multiple listeners have the same port, only add to …
Browse files Browse the repository at this point in the history
…K8s Service once (#2413)

* Modify unit tests to include multiple listeners w/ same port

Running the tests on this commit will demonstrate the bug

* When multiple listeners have the same port, only add to K8s Service once

* Add changelog entry
  • Loading branch information
nathancoleman authored Jul 11, 2023
1 parent 8582286 commit 4f06479
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/2413.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api-gateway: Fix creation of invalid Kubernetes Service when multiple Gateway listeners have the same port.
```
7 changes: 7 additions & 0 deletions control-plane/api-gateway/gatekeeper/gatekeeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ var (
Name: "Listener 1",
Port: 8080,
Protocol: "TCP",
Hostname: common.PointerTo(gwv1beta1.Hostname("example.com")),
},
{
Name: "Listener 2",
Port: 8081,
Protocol: "TCP",
},
{
Name: "Listener 3",
Port: 8080,
Protocol: "TCP",
Hostname: common.PointerTo(gwv1beta1.Hostname("example.net")),
},
}
)

Expand Down
8 changes: 8 additions & 0 deletions control-plane/api-gateway/gatekeeper/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,22 @@ func (g *Gatekeeper) deleteService(ctx context.Context, gwName types.NamespacedN
}

func (g *Gatekeeper) service(gateway gwv1beta1.Gateway, gcc v1alpha1.GatewayClassConfig) *corev1.Service {
seenPorts := map[gwv1beta1.PortNumber]struct{}{}
ports := []corev1.ServicePort{}
for _, listener := range gateway.Spec.Listeners {
if _, seen := seenPorts[listener.Port]; seen {
// We've already added this listener's port to the Service
continue
}

ports = append(ports, corev1.ServicePort{
Name: string(listener.Name),
// only TCP-based services are supported for now
Protocol: corev1.ProtocolTCP,
Port: int32(listener.Port),
})

seenPorts[listener.Port] = struct{}{}
}

// Copy annotations from the Gateway, filtered by those allowed by the GatewayClassConfig.
Expand Down

0 comments on commit 4f06479

Please sign in to comment.