-
Notifications
You must be signed in to change notification settings - Fork 818
feature: Populate NetworkSettings.Gateway #4771
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
Open
must108
wants to merge
1
commit into
containerd:main
Choose a base branch
from
must108:inspect-gateway
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
pkg/containerinspector/containerinspector_linux_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package containerinspector | ||
|
|
||
| import ( | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/vishvananda/netlink" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| // defaultRoute mimics what netlink returns for a default route: Dst is not nil, | ||
| // it is synthesized as 0.0.0.0/0. | ||
| func defaultRoute(linkIndex int, gw string) netlink.Route { | ||
| return netlink.Route{ | ||
| LinkIndex: linkIndex, | ||
| Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)}, | ||
| Gw: net.ParseIP(gw), | ||
| } | ||
| } | ||
|
|
||
| func subnetRoute(linkIndex int, cidr string) netlink.Route { | ||
| _, dst, _ := net.ParseCIDR(cidr) | ||
| return netlink.Route{LinkIndex: linkIndex, Dst: dst} | ||
| } | ||
|
|
||
| func TestSelectDefaultGateway(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| routes []netlink.Route | ||
| primary int | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "default route on the primary interface", | ||
| routes: []netlink.Route{subnetRoute(2, "10.4.1.0/24"), defaultRoute(2, "10.4.1.1")}, | ||
| primary: 2, | ||
| expected: "10.4.1.1", | ||
| }, | ||
| { | ||
| name: "prefers the primary interface over another default route", | ||
| routes: []netlink.Route{defaultRoute(3, "192.168.0.1"), defaultRoute(2, "10.4.1.1")}, | ||
| primary: 2, | ||
| expected: "10.4.1.1", | ||
| }, | ||
| { | ||
| name: "falls back when no default route matches the primary interface", | ||
| routes: []netlink.Route{defaultRoute(3, "192.168.0.1")}, | ||
| primary: 2, | ||
| expected: "192.168.0.1", | ||
| }, | ||
| { | ||
| name: "nil Dst is still treated as a default route", | ||
| routes: []netlink.Route{{LinkIndex: 2, Gw: net.ParseIP("10.4.1.1")}}, | ||
| primary: 2, | ||
| expected: "10.4.1.1", | ||
| }, | ||
| { | ||
| name: "no default route", | ||
| routes: []netlink.Route{subnetRoute(2, "10.4.1.0/24")}, | ||
| primary: 2, | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "onlink default route without a gateway is skipped", | ||
| routes: []netlink.Route{{LinkIndex: 2, Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)}}}, | ||
| primary: 2, | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "IPv6 gateway is ignored", | ||
| routes: []netlink.Route{{LinkIndex: 2, Gw: net.ParseIP("fe80::1")}}, | ||
| primary: 2, | ||
| expected: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, tc.expected, selectDefaultGateway(tc.routes, tc.primary)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,7 @@ type NetNS struct { | |||||
| // Zero means unset. | ||||||
| PrimaryInterface int `json:"PrimaryInterface,omitempty"` | ||||||
| Interfaces []NetInterface `json:"Interfaces,omitempty"` | ||||||
| Gateway string `json:"Gateway,omitempty"` | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| PortMappings []cni.PortMapping | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you already checked CI failures related to this fix?