Skip to content

Commit c64e34c

Browse files
committed
fix: use corev1.Protocol directly in port maps instead of wrapping in PortInfo
1 parent 7f1ff18 commit c64e34c

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

internal/controller/provisioner/objects.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ const (
4444
defaultInitialDelaySeconds = int32(3)
4545
)
4646

47-
type PortInfo struct {
48-
Port int32
49-
Protocol corev1.Protocol
50-
}
51-
5247
var emptyDirVolumeSource = corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}
5348

5449
//nolint:gocyclo // will refactor at some point
@@ -152,7 +147,7 @@ func (p *NginxProvisioner) buildNginxResourceObjects(
152147
openshiftObjs = p.buildOpenshiftObjects(objectMeta)
153148
}
154149

155-
ports := make(map[int32]PortInfo)
150+
ports := make(map[int32]corev1.Protocol)
156151
for _, listener := range gateway.Spec.Listeners {
157152
var protocol corev1.Protocol
158153
switch listener.Protocol {
@@ -163,7 +158,7 @@ func (p *NginxProvisioner) buildNginxResourceObjects(
163158
default:
164159
protocol = corev1.ProtocolTCP
165160
}
166-
ports[int32(listener.Port)] = PortInfo{Port: int32(listener.Port), Protocol: protocol}
161+
ports[int32(listener.Port)] = protocol
167162
}
168163

169164
// Create separate copies of objectMeta for service and deployment to avoid shared map references
@@ -529,7 +524,7 @@ func (p *NginxProvisioner) buildOpenshiftObjects(objectMeta metav1.ObjectMeta) [
529524
func buildNginxService(
530525
objectMeta metav1.ObjectMeta,
531526
nProxyCfg *graph.EffectiveNginxProxy,
532-
ports map[int32]PortInfo,
527+
ports map[int32]corev1.Protocol,
533528
selectorLabels map[string]string,
534529
addresses []gatewayv1.GatewaySpecAddress,
535530
) (*corev1.Service, error) {
@@ -552,17 +547,17 @@ func buildNginxService(
552547
}
553548

554549
servicePorts := make([]corev1.ServicePort, 0, len(ports))
555-
for _, portInfo := range ports {
550+
for port, protocol := range ports {
556551
servicePort := corev1.ServicePort{
557-
Name: fmt.Sprintf("port-%d", portInfo.Port),
558-
Port: portInfo.Port,
559-
TargetPort: intstr.FromInt32(portInfo.Port),
560-
Protocol: portInfo.Protocol,
552+
Name: fmt.Sprintf("port-%d", port),
553+
Port: port,
554+
TargetPort: intstr.FromInt32(port),
555+
Protocol: protocol,
561556
}
562557

563558
if serviceType != corev1.ServiceTypeClusterIP {
564559
for _, nodePort := range serviceCfg.NodePorts {
565-
if nodePort.ListenerPort == portInfo.Port {
560+
if nodePort.ListenerPort == port {
566561
servicePort.NodePort = nodePort.Port
567562
}
568563
}
@@ -640,7 +635,7 @@ func (p *NginxProvisioner) buildNginxDeployment(
640635
nProxyCfg *graph.EffectiveNginxProxy,
641636
ngxIncludesConfigMapName string,
642637
ngxAgentConfigMapName string,
643-
ports map[int32]PortInfo,
638+
ports map[int32]corev1.Protocol,
644639
selectorLabels map[string]string,
645640
agentTLSSecretName string,
646641
dockerSecretNames map[string]string,
@@ -794,7 +789,7 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
794789
nProxyCfg *graph.EffectiveNginxProxy,
795790
ngxIncludesConfigMapName string,
796791
ngxAgentConfigMapName string,
797-
ports map[int32]PortInfo,
792+
ports map[int32]corev1.Protocol,
798793
agentTLSSecretName string,
799794
dockerSecretNames map[string]string,
800795
jwtSecretName string,
@@ -803,11 +798,11 @@ func (p *NginxProvisioner) buildNginxPodTemplateSpec(
803798
dataplaneKeySecretName string,
804799
) corev1.PodTemplateSpec {
805800
containerPorts := make([]corev1.ContainerPort, 0, len(ports))
806-
for _, portInfo := range ports {
801+
for port, protocol := range ports {
807802
containerPort := corev1.ContainerPort{
808-
Name: fmt.Sprintf("port-%d", portInfo.Port),
809-
ContainerPort: portInfo.Port,
810-
Protocol: portInfo.Protocol,
803+
Name: fmt.Sprintf("port-%d", port),
804+
ContainerPort: port,
805+
Protocol: protocol,
811806
}
812807
containerPorts = append(containerPorts, containerPort)
813808
}

internal/controller/provisioner/objects_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,20 @@ func TestBuildNginxResourceObjects(t *testing.T) {
177177
{
178178
Port: 80,
179179
Name: "port-80",
180+
Protocol: corev1.ProtocolTCP,
180181
TargetPort: intstr.FromInt(80),
181182
NodePort: 30000,
182183
},
183184
{
184185
Port: 8888,
185186
Name: "port-8888",
187+
Protocol: corev1.ProtocolTCP,
186188
TargetPort: intstr.FromInt(8888),
187189
},
188190
{
189191
Port: 9999,
190192
Name: "port-9999",
193+
Protocol: corev1.ProtocolTCP,
191194
TargetPort: intstr.FromInt(9999),
192195
},
193196
}))
@@ -208,10 +211,12 @@ func TestBuildNginxResourceObjects(t *testing.T) {
208211
{
209212
ContainerPort: 80,
210213
Name: "port-80",
214+
Protocol: corev1.ProtocolTCP,
211215
},
212216
{
213217
ContainerPort: 8888,
214218
Name: "port-8888",
219+
Protocol: corev1.ProtocolTCP,
215220
},
216221
{
217222
ContainerPort: config.DefaultNginxMetricsPort,
@@ -220,6 +225,7 @@ func TestBuildNginxResourceObjects(t *testing.T) {
220225
{
221226
ContainerPort: 9999,
222227
Name: "port-9999",
228+
Protocol: corev1.ProtocolTCP,
223229
},
224230
}))
225231

@@ -374,6 +380,7 @@ func TestBuildNginxResourceObjects_NginxProxyConfig(t *testing.T) {
374380
g.Expect(container.Ports).To(ContainElement(corev1.ContainerPort{
375381
ContainerPort: 8443,
376382
Name: "port-8443",
383+
Protocol: corev1.ProtocolTCP,
377384
HostPort: 8443,
378385
}))
379386

0 commit comments

Comments
 (0)