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. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (28)
📝 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 |
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