pkg/net: add ip arithmetic functions
To address #3142, I implemented a proof-of-concept API to perform arithmetic operations on IP addresses. Since this PR is a proof-of-concept, I welcome any feedback on naming conventions, interface design, or any other areas where improvements could be made.
This change introduces two functions for performing arithmetic operations on IP addresses and CIDR networks. These functions may be useful for use cases such as IP address management in CUE.
net.AddIPOffset
AddIPOffset adds a numerical offset to a given IP address.
This function is useful for automatically generating IP addresses for a large number of hosts, as demonstrated in the example below:
let networks = [
"192.168.0.0/24",
"2001:db8::/64",
]
for i in list.Range(1, 10, 1) {
"testhost-\(i)": {
interfaces: eth0: [for network in networks {net.AddIPOffset(network, i)}]
}
}
net.AddIPCIDROffset
AddIPCIDROffset adds a numerical offset to a given CIDR subnet.
This function is useful for generating network prefixes based on specific rules, as demonstrated in the example below:
#VLAN: {
id: >=1 & <=4096
prefix: net.AddIPCIDROffset("10.0.0.0/24", id)
}
vlanA: #VLAN & {id: 1} // prefix: 10.0.1.0/24
vlanB: #VLAN & {id: 2} // prefix: 10.0.2.0/24
vlanC: #VLAN & {id: 3} // prefix: 10.0.3.0/24
vlanX: #VLAN & {id: 1000} // prefix: 10.3.232.0/24
@takonomura thanks for raising this. We are extremely busy in the run up to KubeCon so please bear with us!
Hi @myitcv, any updates on this PR? Let me know if I can do anything. Thanks!
Hi @myitcv, just wanted to follow up on this PR again.
If there are any concerns about the implementation or if you'd prefer a different approach, I'm happy to discuss alternatives. Otherwise, please let me know if there's anything I can do to help move this forward.
Thanks for the reminder @takonomura - I have asked @rogpeppe for thoughts on the API here.
@rogpeppe just a friendly ping! This is ready for re-review when you have a moment.