From c19a30ca5cbfa80948d563a0eca411d42bd51a14 Mon Sep 17 00:00:00 2001 From: Wanxian Yang Date: Tue, 26 Jul 2022 14:23:08 -0700 Subject: [PATCH 1/2] add extra container port to the main container only if nlb target container is the main contaier (fix #3768) --- .../stack/testdata/workloads/svc-nlb-prod.stack.yml | 2 -- .../workloads/partials/cf/workload-container.yml | 11 ++++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml b/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml index a9977455de4..30e55b2809a 100644 --- a/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml +++ b/internal/pkg/deploy/cloudformation/stack/testdata/workloads/svc-nlb-prod.stack.yml @@ -95,8 +95,6 @@ Resources: # If a bucket URL is specified, that means the template exists. awslogs-stream-prefix: copilot PortMappings: - ContainerPort: !Ref ContainerPort - - ContainerPort: 82 - Protocol: tcp - Name: tls Image: 1234567890.dkr.ecr.us-west-2.amazonaws.com/proxy:cicdtest PortMappings: diff --git a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml index 8d68f89be06..5620ece143f 100644 --- a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml +++ b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml @@ -29,17 +29,18 @@ {{- if eq .WorkloadType "Load Balanced Web Service"}} PortMappings: - ContainerPort: !Ref ContainerPort -{{- if .NLB}} - {{if ne .NLB.Listener.TargetPort .NLB.MainContainerPort}} {{/*No need to add additional port if the target port is the same as image port*/}} - - ContainerPort: {{.NLB.Listener.TargetPort}} - {{- if eq .NLB.Listener.Protocol "UDP" }} +{{- if .NLB}} {{ $nlbListener := .NLB.Listener }} + {{- if and (eq $nlbListener.TargetContainer .WorkloadName) (ne $nlbListener.TargetPort .NLB.MainContainerPort)}} + {{/*No need to add additional port if the target port is the same as image port*/}} + - ContainerPort: {{$nlbListener.TargetPort}} + {{- if eq $nlbListener.Protocol "UDP" }} Protocol: udp {{- else }} Protocol: tcp {{- end }} {{- end}} {{- end}} -{{- end}} +{{- end}} {{/* end if eq .WorkloadType "Load Balanced Web Service"*/}} {{- if eq .WorkloadType "Backend Service"}} PortMappings: !If [ExposePort, [{ContainerPort: !Ref ContainerPort}], !Ref "AWS::NoValue"] {{- end}} From 13ca12cb16f6b27bb67fa8cdcabfd2518e6ad03e Mon Sep 17 00:00:00 2001 From: Wanxian Yang Date: Tue, 26 Jul 2022 14:47:23 -0700 Subject: [PATCH 2/2] small refactor --- .../templates/workloads/partials/cf/workload-container.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml index 5620ece143f..0baab5c7fb6 100644 --- a/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml +++ b/internal/pkg/template/templates/workloads/partials/cf/workload-container.yml @@ -33,11 +33,7 @@ {{- if and (eq $nlbListener.TargetContainer .WorkloadName) (ne $nlbListener.TargetPort .NLB.MainContainerPort)}} {{/*No need to add additional port if the target port is the same as image port*/}} - ContainerPort: {{$nlbListener.TargetPort}} - {{- if eq $nlbListener.Protocol "UDP" }} - Protocol: udp - {{- else }} - Protocol: tcp - {{- end }} + Protocol: {{if eq $nlbListener.Protocol "UDP"}}udp{{else}}tcp{{end}} {{- end}} {{- end}} {{- end}} {{/* end if eq .WorkloadType "Load Balanced Web Service"*/}}