@@ -150,7 +150,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
150150
151151 // Populate non-overlapping subnets into consolidation map
152152 for _ , s := range options .subnets {
153- // TODO(thaJeztah): is all this validation needed on the CLI-side?
154153 for k := range iData {
155154 ok1 , err := subnetMatches (s , k )
156155 if err != nil {
@@ -164,7 +163,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
164163 return nil , errors .New ("multiple overlapping subnet configuration is not supported" )
165164 }
166165 }
167- sn , err := parsePrefixOrAddr (s )
166+ sn , err := netip . ParsePrefix (s )
168167 if err != nil {
169168 return nil , err
170169 }
@@ -173,7 +172,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
173172
174173 // Validate and add valid ip ranges
175174 for _ , r := range options .ipRanges {
176- // TODO(thaJeztah): is all this validation needed on the CLI-side?
177175 match := false
178176 for _ , s := range options .subnets {
179177 ok , err := subnetMatches (s , r .String ())
@@ -188,9 +186,10 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
188186 if iData [s ].IPRange .IsValid () {
189187 return nil , fmt .Errorf ("cannot configure multiple ranges (%s, %s) on the same subnet (%s)" , r .String (), iData [s ].IPRange .String (), s )
190188 }
191- d := iData [s ]
192- d .IPRange = ipNetToPrefix (r )
193- match = true
189+ if ipRange , ok := toPrefix (r ); ok {
190+ iData [s ].IPRange = ipRange
191+ match = true
192+ }
194193 }
195194 if ! match {
196195 return nil , fmt .Errorf ("no matching subnet for range %s" , r .String ())
@@ -201,7 +200,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
201200 for _ , g := range options .gateways {
202201 match := false
203202 for _ , s := range options .subnets {
204- // TODO(thaJeztah): is all this validation needed on the CLI-side?
205203 ok , err := subnetMatches (s , g .String ())
206204 if err != nil {
207205 return nil , err
@@ -278,20 +276,3 @@ func subnetMatches(subnet, data string) (bool, error) {
278276
279277 return s .Contains (ip ), nil
280278}
281-
282- // parsePrefixOrAddr parses s as a subnet in CIDR notation (e.g. "10.0.0.0/24").
283- // If s does not include a prefix length, it is interpreted as a single-address
284- // subnet using the full address width (/32 for IPv4 or /128 for IPv6).
285- //
286- // It returns the resulting netip.Prefix or an error if the input is invalid.
287- func parsePrefixOrAddr (s string ) (netip.Prefix , error ) {
288- pfx , err := netip .ParsePrefix (s )
289- if err != nil {
290- addr , err := netip .ParseAddr (s )
291- if err != nil {
292- return netip.Prefix {}, fmt .Errorf ("invalid address: %w" , err )
293- }
294- pfx = netip .PrefixFrom (addr , addr .BitLen ())
295- }
296- return pfx , nil
297- }
0 commit comments