WIP: net, libs: Refactor random ip address helpers#4944
Conversation
|
Report bugs in Issues Welcome! 🎉This pull request will be automatically processed with the following features: 🔄 Automatic Actions
📋 Available CommandsPR Status Management
Review & Approval
Testing & Validation
Container Operations
Cherry-pick Operations
Label Management
✅ Merge RequirementsThis PR will be automatically approved when the following conditions are met:
📊 Review ProcessApprovers and ReviewersApprovers:
Reviewers:
Available Labels
AI Features
💡 Tips
For more information, please refer to the project documentation or contact the maintainers. |
📝 WalkthroughWalkthroughIPv4 and IPv6 address generation functions now accept a ChangesIP Address Generation Contract Refactor
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Rationale: The change is a focused refactoring of a shared utility function contract. While the scope is broad (30+ files touched), the pattern is highly repetitive and consistent: either adding Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
e59ba4d to
39f096b
Compare
| """ | ||
| third_octets = _random_octets(count=_MAX_NUM_OF_RANDOM_OCTETS_PER_SESSION) | ||
| return f"{_IPV4_ADDRESS_SUBNET_PREFIX_VMI}.{third_octets[net_seed]}.{host_address}" | ||
| return f"{_IPV4_ADDRESS_SUBNET_PREFIX_VMI}.{third_octets[net_seed]}.{host_address}/24" |
There was a problem hiding this comment.
First - if the caller is now responsible for removing the subnet length suffix if they need a plain IP (instead of a complete CIDR), then it might be worth explaining it in the doctring (just like you did very clearly in the PR description).
Second - looking at places where the plain IP is needed (for example here), it looks a bit hacky, Maybe it's worth to add a flag to this function, which defaults to True, so it will become something like this:
def random_ipv4_address(net_seed: int, host_address: int, cidr_required: bool = True) -> str:
...
subnet_length = "/24" if cidr_required else ""
...
return f"{_IPV4_ADDRESS_SUBNET_PREFIX_VMI}.{third_octets[net_seed]}.{host_address}{subnet_length}"
and then, when the subnet length is not needed, it can be called this way
ip_address=f"{random_ipv4_address(net_seed=0, host_address=123, cidr_required=False)/{SPECIFIC_HOST_MASK}"
and leave out the cidr_required in all other places (which will leave most of the contents of this PR as it is now).
There was a problem hiding this comment.
Great idea, thanks!
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4856. Overlapping filestests/conftest.py |
Return CIDR strings directly from the low-level helpers. Callers that need a plain IP call the relevant helper with cidr_required=False. callers that need a non-standard mask may strip the cidr and reapply it. Remove random_ip_addresses_by_family — it became identical to random_cidr_addresses_by_family after this change. Signed-off-by: Asia Khromov <azhivovk@redhat.com> Assisted-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
39f096b to
223c404
Compare
|
Change: add optional cidr param |
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4896. Overlapping filestests/conftest.py |
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4950. Overlapping filestests/network/kubemacpool/utils.py |
|
|
||
|
|
||
| def random_ipv4_address(net_seed: int, host_address: int) -> str: | ||
| def random_ipv4_address(net_seed: int, host_address: int, cidr_required: bool = True) -> str: |
There was a problem hiding this comment.
Passing booleans as arguments is not not elegant and many times makes things unclear when reading the code. E.g. assuming a 24 bit mask is not nice, but defining it as the default value makes it a bit better.
This helper was created when the need was basic, now that we can observe the usage, it makes sense to make it fit the usages in general and by that make it nice and clean.
I suggest something like this:
random_ipv4_address(net_seed: int, host_address: int, subnet_length=24) -> ipaddress.IPv4Interface:It should raise in case the subnet len is lower than 24 (Bad value).
Note that it is better to return the IP structure, not a plain string.
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4962. Overlapping filestests/network/l2_bridge/libl2bridge.py |
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4934. Overlapping filestests/conftest.py |
|
/retest all Auto-triggered: Files in this PR were modified by merged PR #4867. Overlapping filestests/network/l2_bridge/libl2bridge.py |
|
/wip |
What this PR does / why we need it:
random_ipv4_address and random_ipv6_address returned plain IPs, forcing every caller to manually append /24 or /64. Since CIDR is the common case (cloud-init, subnet assignments), the helpers now
return CIDR strings directly. Callers that need a plain IP strip the prefix with .split("/")[0]; callers with a non-standard mask strip and reapply it.
Which issue(s) this PR fixes: -
Special notes for reviewer:
Reuested follow-up PR by approvers
jira-ticket: -
Summary by CodeRabbit
Release Notes