-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial work for customizable service CIDR * add service cidr to interactive bootstrap; add first ip4 service ip to sans; test service cidrs * handle ip6 service cidrs in addition to ip4 * Update src/k8s/pkg/utils/cidr.go Co-authored-by: Angelos Kolaitis <[email protected]> * rework invalid tests into subtests * Update src/k8s/pkg/k8sd/types/cluster_config_test.go Co-authored-by: Angelos Kolaitis <[email protected]> * lets tell some stories --------- Co-authored-by: Angelos Kolaitis <[email protected]>
- Loading branch information
1 parent
43966cb
commit 004a1af
Showing
9 changed files
with
179 additions
and
24 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"net" | ||
"strings" | ||
) | ||
|
||
// GetFirstIP returns the first IP address of a subnet. Use big.Int so that it can handle both IPv4 and IPv6 addreses. | ||
func GetFirstIP(subnet string) (net.IP, error) { | ||
_, cidr, err := net.ParseCIDR(subnet) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q is not a valid subnet CIDR: %w", subnet, err) | ||
} | ||
r := big.NewInt(0).Add( | ||
big.NewInt(0).SetBytes(cidr.IP.To16()), | ||
big.NewInt(1), | ||
).Bytes() | ||
r = append(make([]byte, 16), r...) | ||
return net.IP(r[len(r)-16:]), nil | ||
} | ||
|
||
// GetKubernetesServiceIPsFromServiceCIDRs returns a list of the first IP addrs from a given service cidr string. | ||
func GetKubernetesServiceIPsFromServiceCIDRs(serviceCIDR string) ([]net.IP, error) { | ||
var firstIPs []net.IP | ||
cidrs := strings.Split(serviceCIDR, ",") | ||
if v := len(cidrs); v != 1 && v != 2 { | ||
return nil, fmt.Errorf("invalid ServiceCIDR value: %v", cidrs) | ||
} | ||
for _, cidr := range cidrs { | ||
ip, err := GetFirstIP(cidr) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get IP from CIDR %q: %w", cidr, err) | ||
} | ||
firstIPs = append(firstIPs, ip) | ||
} | ||
return firstIPs, nil | ||
} |
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,70 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/canonical/k8s/pkg/utils" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGetFirstIP(t *testing.T) { | ||
for _, tc := range []struct { | ||
cidr string | ||
ip string | ||
}{ | ||
{cidr: "10.152.183.0/24", ip: "10.152.183.1"}, | ||
{cidr: "10.152.183.10/24", ip: "10.152.183.1"}, | ||
{cidr: "10.100.0.0/16", ip: "10.100.0.1"}, | ||
{cidr: "fd01::/64", ip: "fd01::1"}, | ||
// TODO: do we need more test cases? | ||
} { | ||
t.Run(tc.cidr, func(t *testing.T) { | ||
g := NewWithT(t) | ||
ip, err := utils.GetFirstIP(tc.cidr) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(ip.String()).To(Equal(tc.ip)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetKubernetesServiceIPsFromServiceCIDRs(t *testing.T) { | ||
// Test valid subnet cidr strings | ||
t.Run("ValidCIDR", func(t *testing.T) { | ||
for _, tc := range []struct { | ||
cidr string | ||
ips []string | ||
}{ | ||
{cidr: "10.152.183.0/24", ips: []string{"10.152.183.1"}}, | ||
{cidr: "fd01::/64", ips: []string{"fd01::1"}}, | ||
{cidr: "10.152.183.0/24,fd01::/64", ips: []string{"10.152.183.1", "fd01::1"}}, | ||
} { | ||
t.Run(tc.cidr, func(t *testing.T) { | ||
g := NewWithT(t) | ||
i, err := utils.GetKubernetesServiceIPsFromServiceCIDRs(tc.cidr) | ||
ips := make([]string, len(i)) | ||
for idx, v := range i { | ||
ips[idx] = v.String() | ||
} | ||
|
||
g.Expect(err).To(BeNil()) | ||
g.Expect(ips).To(Equal(tc.ips)) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("InvalidCIDR", func(t *testing.T) { | ||
for _, tc := range []struct { | ||
cidr string | ||
}{ | ||
{cidr: "fd01::/64,fd02::/64,fd03::/64"}, | ||
{cidr: "bananas"}, | ||
} { | ||
t.Run(tc.cidr, func(t *testing.T) { | ||
g := NewWithT(t) | ||
_, err := utils.GetKubernetesServiceIPsFromServiceCIDRs(tc.cidr) | ||
|
||
g.Expect(err).ToNot(BeNil()) | ||
}) | ||
} | ||
}) | ||
} |