Skip to content

Commit

Permalink
update listener functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jm96441n committed Feb 6, 2024
1 parent cff47ca commit f1e6a71
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
18 changes: 16 additions & 2 deletions control-plane/api/mesh/v2beta1/api_gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func (in *APIGateway) Validate(tenancy common.ConsulTenancyConfig) error {
// DefaultNamespaceFields is required as part of the common.MeshConfig interface.
func (in *APIGateway) DefaultNamespaceFields(tenancy common.ConsulTenancyConfig) {}

// ListenersToPorts converts the APIGateway listeners to ServicePorts.
func (in *APIGateway) ListenersToPorts(portModifier int32) []corev1.ServicePort {
// ListenersToServicePorts converts the APIGateway listeners to ServicePorts.
func (in *APIGateway) ListenersToServicePorts(portModifier int32) []corev1.ServicePort {
ports := []corev1.ServicePort{}

for _, listener := range in.Spec.Listeners {
Expand All @@ -182,3 +182,17 @@ func (in *APIGateway) ListenersToPorts(portModifier int32) []corev1.ServicePort

return ports
}

func (in *APIGateway) ListenersToContainerPorts(portModifier int32) []corev1.ContainerPort {
ports := []corev1.ContainerPort{}

for _, listener := range in.Spec.Listeners {
port := int32(listener.Port)
ports = append(ports, corev1.ContainerPort{
Name: listener.Name,
ContainerPort: port + portModifier,
Protocol: corev1.Protocol(listener.Protocol),
})
}
return ports
}
34 changes: 19 additions & 15 deletions control-plane/api/mesh/v2beta1/mesh_gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,15 @@ func (in *MeshGateway) Validate(tenancy common.ConsulTenancyConfig) error {
// DefaultNamespaceFields is required as part of the common.MeshConfig interface.
func (in *MeshGateway) DefaultNamespaceFields(tenancy common.ConsulTenancyConfig) {}

// ListenersToPorts converts the MeshGateway listeners to ServicePorts.
func (in *MeshGateway) ListenersToPorts(portModifier int32) []corev1.ServicePort {
// ListenersToServicePorts converts the MeshGateway listeners to ServicePorts.
func (in *MeshGateway) ListenersToServicePorts(portModifier int32) []corev1.ServicePort {
ports := []corev1.ServicePort{}

if len(in.Spec.Listeners) == 0 {
// If empty use the default value. This should always be set, but in case it's not, this check
// will prevent a panic.
return []corev1.ServicePort{
{
Name: "wan",
Port: constants.DefaultWANPort,
TargetPort: intstr.IntOrString{
IntVal: constants.DefaultWANPort + portModifier,
},
},
}
}
for _, listener := range in.Spec.Listeners {
port := int32(listener.Port)
if listener.Name == "wan" {
port = constants.DefaultWANPort
}
ports = append(ports, corev1.ServicePort{
Name: listener.Name,
Port: port,
Expand All @@ -177,3 +167,17 @@ func (in *MeshGateway) ListenersToPorts(portModifier int32) []corev1.ServicePort
}
return ports
}

func (in *MeshGateway) ListenersToContainerPorts(portModifier int32) []corev1.ContainerPort {
ports := []corev1.ContainerPort{}

for _, listener := range in.Spec.Listeners {
port := int32(listener.Port)
ports = append(ports, corev1.ContainerPort{
Name: listener.Name,
ContainerPort: port + portModifier,
Protocol: corev1.Protocol(listener.Protocol),
})
}
return ports
}
3 changes: 2 additions & 1 deletion control-plane/gateways/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Gateway interface {
*meshv2beta1.MeshGateway | *meshv2beta1.APIGateway
GetName() string
GetNamespace() string
ListenersToPorts(int32) []corev1.ServicePort
ListenersToServicePorts(int32) []corev1.ServicePort
ListenersToContainerPorts(int32) []corev1.ContainerPort
GetAnnotations() map[string]string
GetLabels() map[string]string
}
Expand Down
11 changes: 1 addition & 10 deletions control-plane/gateways/deployment_dataplane_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,7 @@ func (b *gatewayBuilder[T]) consulDataplaneContainer(containerConfig v2beta1.Gat
ContainerPort: int32(constants.ProxyDefaultHealthPort),
})

// Configure the wan port.
wanPort := corev1.ContainerPort{
Name: "wan",
ContainerPort: int32(constants.DefaultWANPort),
HostPort: containerConfig.HostPort,
}

wanPort.ContainerPort = 443 + containerConfig.PortModifier

container.Ports = append(container.Ports, wanPort)
container.Ports = append(container.Ports, b.gateway.ListenersToContainerPorts(containerConfig.PortModifier)...)

// Configure the resource requests and limits for the proxy if they are set.
if resources != nil {
Expand Down
9 changes: 1 addition & 8 deletions control-plane/gateways/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,7 @@ func (b *gatewayBuilder[T]) Service() *corev1.Service {
Spec: corev1.ServiceSpec{
Selector: b.labelsForDeployment(),
Type: serviceType,
Ports: b.Ports(portModifier),
Ports: b.gateway.ListenersToServicePorts(portModifier),
},
}
}

// Ports build a list of ports from the listener objects. In theory there should only ever be a WAN port on
// mesh gateway but building the ports from a list of listeners will allow for easier compatability with other
// gateway patterns in the future.
func (b *gatewayBuilder[T]) Ports(portModifier int32) []corev1.ServicePort {
return b.gateway.ListenersToPorts(portModifier)
}

0 comments on commit f1e6a71

Please sign in to comment.