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

Include init NetworkSettings within inspect response #3318

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

chews93319
Copy link
Contributor

Related to containerd#3310

Replacing Previous Pull Request 3311

New behavior will always initialize a NetworkSettings entity for the inspect response, including Ports child member.

If inspecting a running container with published ports, then all information will be correctly returned.
If inspecting a running container without published ports, then NetworkSettings will include an initialized Ports member. If inspecting a stopped/exited container,
then an entirely initialized, "empty-value" NetworkSettings is returned.

Background

Research related to Scenario 1:
According to dockercompat line189 and the logical evaluation of Port Annotations, the Ports are not assigned within the response when values are present within the Annotations.
Therefore, containers without published ports yield an NetworkSettings structure without a Ports key-value pair.

Research related to Scenario 2:
According to dockercompat line401, the native.NetNS of a stopped/exited container is a null pointer, which would return nil.

Solution

To resolve this issue, the NetworkSettings and PortMap are initialized at the beginning of the method to ensure that at least "Empty-value" structs are always included within the inspect response.
When "Port Annotations" are available, the new logic will add the key-value pairs into the initialized PortMap of the response.

Since Ports should not be omitted, this change also removes the json:",omitempty" decorator for the NetworkSettings struct.

Additional Tasks

This bug is also present within v1.7.6 and the change can be easily reconciled with the released version.

  • Please consider this PR for Back Porting into Release/1.7 branch.

@chews93319
Copy link
Contributor Author

@apostasie

I have added the requested test cases as well as caught an update to the pre-existing test, which I was responsible for.
Unfortunately, I did something to "close" the previous Pull Request, so this is a "fresh replacement".

PTAL at your convenience.

@chews93319
Copy link
Contributor Author

@AkihiroSuda

PTAL at your convenience.
We would also like to discuss this change as another Back Port into Release/1.7 branch.

},
},
}

for _, tc := range testcase {
fmt.Fprintln(os.Stderr, tc.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove the printline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This print statement was actually an attempt to clarify the sub-test cases, similar to the precedence of other unit tests. (See reference example below)

I will remove the print statements.
If I can easily iterate on the test to uniquely report the sub-case PASS, then I will do that too.

--- PASS: TestBuildKitFile (0.00s)
    --- PASS: TestBuildKitFile/only_Dockerfile_is_present (0.00s)
    --- PASS: TestBuildKitFile/only_Containerfile_is_present (0.00s)
    --- PASS: TestBuildKitFile/both_Dockerfile_and_Containerfile_are_present (0.00s)
    --- PASS: TestBuildKitFile/Dockerfile_and_Containerfile_have_different_contents (0.00s)
    --- PASS: TestBuildKitFile/Custom_file_is_specfied (0.00s)
    --- PASS: TestBuildKitFile/Absolute_path_is_specified_along_with_custom_file (0.00s)
    --- PASS: TestBuildKitFile/Absolute_path_is_specified_along_with_Docker_file (0.00s)
    --- PASS: TestBuildKitFile/Absolute_path_is_specified_with_Container_file_in_the_path (0.00s)

Copy link
Contributor

Choose a reason for hiding this comment

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

ah ok. its fine then

Copy link
Contributor Author

@chews93319 chews93319 Aug 16, 2024

Choose a reason for hiding this comment

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

@Shubhranshu153 within commit adf4035, successfully corrected the syntax to execute the sub-case test names.

// Given native.NetNS with single Interface without Port Annotations, Return populated NetworkSettings
// UseCase: Inspect a Running Container without published ports
{
name: "Given NetNS with single Interface without Port Annotations, Return populated NetworkSettings",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: initialized network setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a difficult comment and test case name to write.
Technically, a "Running Container without published ports" will still have a populated NetworkSettings entity that contains an "initialized Ports" child entity.

I will try to update the comment to reflect this statement, if this clarification makes sense to you.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Shubhranshu153 within commit adf4035, I also updated the test description/name to reflect this conversation.

Related to
[containerd#3310](containerd#3310)

New behavior will always initialize a NetworkSettings entity
for the inspect response, including Ports child member.

If inspecting a running container with published ports,
then all information will be correctly returned.
If inspecting a running container without published ports,
then NetworkSettings will include an initialized Ports member.
If inspecting a stopped/exited container,
then an entirely initialized, "empty-value" NetworkSettings
is returned.

Signed-off-by: Sam Chew <[email protected]>
@@ -186,7 +186,7 @@ type ContainerState struct {
}

type NetworkSettings struct {
Ports *nat.PortMap `json:",omitempty"`
Ports *nat.PortMap
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit b54de25 into containerd:main Aug 17, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants