-
Notifications
You must be signed in to change notification settings - Fork 3.1k
compat: populate IPRange in IPAM config when listing networks #28396
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
crawfordxx
wants to merge
5
commits into
containers:main
Choose a base branch
from
crawfordxx:compat-add-iprange-to-ipam-config
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.
+185
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a388d72
compat: populate IPRange in IPAM config when listing networks
crawfordxx b880c51
compat: fix leaseRangeToIPRangePrefix to account for first-host offset
crawfordxx 7954f92
compat: refactor leaseRangeToIPRangePrefix and add unit tests
crawfordxx a0fb5cc
compat: use encoding/binary and Masked() check in leaseRangeToIPRange…
crawfordxx b36b97e
compat: use encoding/binary and Masked() check in leaseRangeToIPRange…
crawfordxx 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //go:build !remote && (linux || freebsd) | ||
|
|
||
| package compat | ||
|
|
||
| import ( | ||
| "net" | ||
| "net/netip" | ||
| "testing" | ||
|
|
||
| nettypes "go.podman.io/common/libnetwork/types" | ||
| ) | ||
|
|
||
| func TestLeaseRangeToIPRangePrefix(t *testing.T) { | ||
| mustPrefix := func(s string) netip.Prefix { | ||
| p, err := netip.ParsePrefix(s) | ||
| if err != nil { | ||
| t.Fatalf("invalid prefix %q: %v", s, err) | ||
| } | ||
| return p | ||
| } | ||
| leaseRange := func(start, end string) *nettypes.LeaseRange { | ||
| return &nettypes.LeaseRange{ | ||
| StartIP: net.ParseIP(start), | ||
| EndIP: net.ParseIP(end), | ||
| } | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| lr *nettypes.LeaseRange | ||
| want netip.Prefix | ||
| wantOK bool | ||
| }{ | ||
| { | ||
| name: "nil LeaseRange", | ||
| lr: nil, | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "nil StartIP", | ||
| lr: &nettypes.LeaseRange{EndIP: net.ParseIP("192.168.1.1")}, | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "nil EndIP", | ||
| lr: &nettypes.LeaseRange{StartIP: net.ParseIP("192.168.1.1")}, | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| // /24: StartIP = network+1 = .1, EndIP = broadcast = .255 | ||
| name: "/24 subnet", | ||
| lr: leaseRange("192.168.1.1", "192.168.1.255"), | ||
| want: mustPrefix("192.168.1.0/24"), | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| // /25: StartIP = .129, EndIP = .255 | ||
| name: "/25 subnet", | ||
| lr: leaseRange("10.10.61.129", "10.10.61.255"), | ||
| want: mustPrefix("10.10.61.128/25"), | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| // /16: StartIP = .0.1, EndIP = .255.255 | ||
| name: "/16 subnet", | ||
| lr: leaseRange("10.10.0.1", "10.10.255.255"), | ||
| want: mustPrefix("10.10.0.0/16"), | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| // /32: single host, StartIP == EndIP (FirstIPInSubnet returns address unchanged) | ||
| name: "/32 single host", | ||
| lr: leaseRange("10.0.0.5", "10.0.0.5"), | ||
| want: mustPrefix("10.0.0.5/32"), | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| // misaligned: StartIP and EndIP do not form a valid CIDR block | ||
| name: "misaligned range", | ||
| lr: leaseRange("192.168.1.2", "192.168.1.255"), | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| // non-power-of-two size: 3 addresses | ||
| name: "non-power-of-two size", | ||
| lr: leaseRange("192.168.1.1", "192.168.1.3"), | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| // end before start | ||
| name: "end before start", | ||
| lr: leaseRange("192.168.1.5", "192.168.1.1"), | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| // IPv6 not supported | ||
| name: "IPv6 not supported", | ||
| lr: &nettypes.LeaseRange{ | ||
| StartIP: net.ParseIP("::1"), | ||
| EndIP: net.ParseIP("::1"), | ||
| }, | ||
| wantOK: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, ok := leaseRangeToIPRangePrefix(tt.lr) | ||
| if ok != tt.wantOK { | ||
| t.Errorf("leaseRangeToIPRangePrefix() ok = %v, want %v", ok, tt.wantOK) | ||
| return | ||
| } | ||
| if ok && got != tt.want { | ||
| t.Errorf("leaseRangeToIPRangePrefix() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
I have a few thoughts on this!
First, this function could be a lot shorter and cleaner. I'm not a huge fan of using raw bitwise operations here; we can make the logic much more elegant and readable. Here is a quick pseudocode mockup of what I mean:
Second, we definitely need to add unit tests for this function.
Lastly (and this isn't a blocker), something in the back of my mind is screaming that this function actually belongs in
libnetworkundercontainer-libs. WDYT? @Luap99There 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.
I am fine with having this code here, I don't think we will need it elsewhere. most of that libnetwork code is going into netavark now so it is not like we could resuse it from there.