Skip to content
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

pkg/net: add ip arithmetic functions #3815

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

takonomura
Copy link

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

This change introduces two functions to adding numerical offsets to IP
addresses or CIDR networks.

Signed-off-by: takonomura <[email protected]>
@takonomura takonomura requested a review from cueckoo as a code owner March 13, 2025 06:46
@takonomura takonomura mentioned this pull request Mar 13, 2025
@myitcv
Copy link
Member

myitcv commented Mar 20, 2025

@takonomura thanks for raising this. We are extremely busy in the run up to KubeCon so please bear with us!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants