forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ipv6_dhcp-* types of network. Apply static IPs in all supported network types. Signed-off-by: Serge Logvinov <[email protected]> Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
1 parent
5ec4e90
commit 63f23e9
Showing
4 changed files
with
155 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
internal/app/machined/pkg/runtime/v1alpha1/platform/utils/utils.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
"inet.af/netaddr" | ||
) | ||
|
||
// IPPrefixFrom make netaddr.IPPrefix from cidr-address and netmask strings. | ||
// address can be IP or CIDR (1.1.1.1 or 1.1.1.1/8 or 1.1.1.1/255.0.0.0) | ||
// netmask can be IP or number (255.255.255.0 or 24 or empty). | ||
func IPPrefixFrom(address, netmask string) (netaddr.IPPrefix, error) { | ||
cidr := strings.SplitN(address, "/", 2) | ||
if len(cidr) == 1 { | ||
address = cidr[0] | ||
} else { | ||
address = cidr[0] | ||
netmask = cidr[1] | ||
} | ||
|
||
ip, err := netaddr.ParseIP(address) | ||
if err != nil { | ||
return netaddr.IPPrefix{}, fmt.Errorf("failed to parse ip address: %w", err) | ||
} | ||
|
||
if netmask == "" { | ||
if ip.Is4() { | ||
netmask = "32" | ||
} else { | ||
netmask = "128" | ||
} | ||
} | ||
|
||
bits, err := strconv.Atoi(netmask) | ||
if err != nil { | ||
netmask, err := netaddr.ParseIP(netmask) | ||
if err != nil { | ||
return netaddr.IPPrefix{}, fmt.Errorf("failed to parse netmask: %w", err) | ||
} | ||
|
||
mask, _ := netmask.MarshalBinary() //nolint:errcheck // never fails | ||
bits, _ = net.IPMask(mask).Size() | ||
} | ||
|
||
if ip.Is4() && bits > 32 { | ||
return netaddr.IPPrefix{}, fmt.Errorf("failed netmask should be the same address family") | ||
} | ||
|
||
return netaddr.IPPrefixFrom(ip, uint8(bits)), nil | ||
} |