Skip to content

Conversation

@takonomura
Copy link
Contributor

@takonomura takonomura commented Mar 13, 2025

To address #3142, this PR 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.AddIP

AddIP 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.AddIP(network, i)}]
        }
}

net.AddIPCIDR

AddIPCIDR 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.AddIPCIDR("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 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!

@takonomura
Copy link
Contributor Author

Hi @myitcv, any updates on this PR? Let me know if I can do anything. Thanks!

@takonomura
Copy link
Contributor Author

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.

@myitcv
Copy link
Member

myitcv commented Jun 19, 2025

Thanks for the reminder @takonomura - I have asked @rogpeppe for thoughts on the API here.

Copy link
Member

@rogpeppe rogpeppe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that having this functionality available is a good thing. I just wonder if we'd maybe be better off adding something a bit more general: for example, a way to convert IP / CIDR addresses to bits or a number, and vice versa, and then we'd gain more than just this specific functionality. That said, the functions proposed here would still be more convenient to use even if we had something more general, so in general I'm in favour of this change.

One thing: I think the Offset part of the name is probably redundant: how about AddIP and AddIPCIDR as names? Adding addresses themselves together doesn't really make sense, so I don't think there's any potential future ambiguity there.

}

// AddIPOffset adds a numerical offset to a given IP address.
// The address can be provided as a string, byte array, or CIDR subnet notation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit odd that we can provide a CIDR string here. What's the use case for that?
For example, net.AddIPOffset("100.0.0.0/8", 1) returns "100.0.0.1/8" but that's not generally useful, as all the bits after the prefix are conventionally zero and ignored.

Copy link
Contributor Author

@takonomura takonomura Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case is generating network interface configurations, where IP addresses are written in CIDR notation with a prefix length. Since many configuration formats support this form, allowing AddIPOffset to take a CIDR string makes it easier to produce such values.

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

Signed-off-by: takonomura <takonomura@users.noreply.github.com>
Copy link
Contributor Author

@takonomura takonomura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogpeppe Thank you for the review. I've made the changes to address your comments.


I just wonder if we'd maybe be better off adding something a bit more general: for example, a way to convert IP / CIDR addresses to bits or a number, and vice versa, and then we'd gain more than just this specific functionality.

I agree that having a more general way to convert IP/CIDR addresses to numbers or bits could be broadly useful.
However, converting IP addresses to numbers can be problematic for IPv6, as IPv6 addresses are typically 39 digits long.
As discussed in #3787, this can cause the value to be interpreted as a float during arithmetic operations, which leads to unexpected behavior.

}

// AddIPOffset adds a numerical offset to a given IP address.
// The address can be provided as a string, byte array, or CIDR subnet notation.
Copy link
Contributor Author

@takonomura takonomura Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case is generating network interface configurations, where IP addresses are written in CIDR notation with a prefix length. Since many configuration formats support this form, allowing AddIPOffset to take a CIDR string makes it easier to produce such values.

@takonomura takonomura requested a review from rogpeppe July 22, 2025 00:57
@takonomura
Copy link
Contributor Author

@rogpeppe just a friendly ping! This is ready for re-review when you have a moment.

@cueckoo cueckoo closed this in f374d29 Aug 7, 2025
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.

3 participants