Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: exposed port validation #5303

Merged

Conversation

CaptainCarpensir
Copy link
Contributor

Refactors the validation for exposed ports to make code more readable, preserve functionality, and include protocol validation.

Step in #4767

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.

@CaptainCarpensir CaptainCarpensir requested a review from a team as a code owner September 14, 2023 18:09
@CaptainCarpensir CaptainCarpensir requested review from huanjani and removed request for a team September 14, 2023 18:09
@github-actions
Copy link

github-actions bot commented Sep 14, 2023

🍕 Here are the new binary sizes!

Name New size (kiB) size (kiB) Delta (%)
macOS (amd) 51828 51740 +0.17
macOS (arm) 52684 52596 +0.17
linux (amd) 45580 45496 +0.18
linux (arm) 44868 44804 +0.14
windows (amd) 43044 42968 +0.18

@codecov-commenter
Copy link

codecov-commenter commented Sep 14, 2023

Codecov Report

Patch coverage: 94.73% and no project coverage change.

Comparison is base (79e54ae) 69.76% compared to head (1e4fd9d) 69.77%.
Report is 3 commits behind head on mainline.

Additional details and impacted files
@@            Coverage Diff            @@
##           mainline    #5303   +/-   ##
=========================================
  Coverage     69.76%   69.77%           
=========================================
  Files           296      296           
  Lines         44581    44610   +29     
  Branches        286      286           
=========================================
+ Hits          31102    31125   +23     
- Misses        11971    11975    +4     
- Partials       1508     1510    +2     
Files Changed Coverage Δ
internal/pkg/manifest/validate.go 77.29% <94.59%> (+<0.01%) ⬆️
internal/pkg/manifest/errors.go 52.57% <100.00%> (+0.99%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@KollaAdithya KollaAdithya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@KollaAdithya KollaAdithya added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Sep 15, 2023
Copy link
Contributor

@huanjani huanjani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a unit test for errContainerPortExposedWithMultipleProtocol?

@CaptainCarpensir
Copy link
Contributor Author

CaptainCarpensir commented Sep 18, 2023

Is there a unit test for errContainerPortExposedWithMultipleProtocol?

Good catch! There is now 😄

Copy link
Contributor

@huanjani huanjani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@CaptainCarpensir CaptainCarpensir removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Sep 18, 2023
@mergify mergify bot merged commit 0a4097d into aws:mainline Sep 18, 2023
Copy link
Contributor

@Lou1415926 Lou1415926 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!! Sorry for the late feedbacks : ( Thankfully, I don't have any major comments!

}
containerNameFor[aws.Uint16Value(opts.mainContainerPort)] = opts.mainContainerName

targetPort := aws.Uint16Value(opts.mainContainerPort)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just sharing my thought here, this is not a request 👀 I personally would refrain from using the term target in the variable names, because up until this point, target means that a port a) not only is exposed, b) but also functions as a port that receives traffic from an NLB/ALB.

A port could be exposed, but only receive traffic from within the task - for example, a sidecar could communicate with another sidecar through a port. These port satisfy a), but not b). They are generally not considered "a target port", because they are not the target of anything.

http:
  target_container: nginx
sidecars:
  logging:
    port: 81 # Exposed, but not an HTTP target nor an NLB target.
  nginx:
    port: 82

Comment on lines +2087 to +2089
if existingContainerNameAndProtocol, ok := portExposedTo[targetPort]; ok {
targetProtocol = existingContainerNameAndProtocol.containerProtocol
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this added so that populateAndValidateMainContainerPort works no matter what order it is called in relative to the other populateAndValidateXPort functions? Based on the order in which these functions are called, it seems like ok would never be true 👀 !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for the case where the NLB specifies a protocol that's not TCP to be used for the main container. In this case the NLB specifies that and it gets populated in the map.
An example manifest:

image:
  port: 80
nlb:
  port: 80/udp

Without this statement here the program would error out saying container "mockMainContainer" is exposing the same port 80 with protocol TCP and UDP

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I just realized you moved the function call from the first to the last! Then I think we are good 👍🏼

// Prefer `nlb.port`, then existing exposed port mapping, then fallback on default protocol
targetProtocol := defaultProtocol
if existingContainerNameAndProtocol, ok := portExposedTo[targetPort]; ok {
targetProtocol = existingContainerNameAndProtocol.containerProtocol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like nlb is a little different than the other. Without protocol, default protocol for nlb should be tcp, instead of any protocol that is already exposed. Consider this manifest:

nlb:
  port: 81

sidecars:
  nginx:
    port: 81/udp

I would expect this manifest to error out, honestly. The nlb.port being 81 means it listens on 81 for TCP port, and route the traffic to port 81 of the nginx container, and expect that target port to be tcp. However, the same port is already exposed as udp. So error.

I think basically, the question is, when we think of the protocol for nlb, do we think of it in a NLB-first way, or a container-first way. My proposal above is NLB-first, the code that I'm commenting on is container-first.

mergify bot pushed a commit that referenced this pull request Sep 20, 2023
Addresses feedback on #5303 and #5284 from after they were merged



By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
@CaptainCarpensir CaptainCarpensir deleted the refactor/exposed-port-validation branch October 9, 2023 16:12
KollaAdithya pushed a commit to KollaAdithya/copilot-cli that referenced this pull request Oct 18, 2023
Refactors the validation for exposed ports to make code more readable, preserve functionality, and include protocol validation.


Step in aws#4767 

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
KollaAdithya pushed a commit to KollaAdithya/copilot-cli that referenced this pull request Oct 18, 2023
Addresses feedback on aws#5303 and aws#5284 from after they were merged



By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants