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

Support for dualstack loadbalancer services #71

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ The `kube-vip-cloud-provider` will only implement the `loadBalancer` functionali
- IP ranges [start address - end address]
- Multiple pools by CIDR per namespace
- Multiple IP ranges per namespace (handles overlapping ranges)
- Support for mixed IP families when specifying multiple pools or ranges
- Setting of static addresses through `--load-balancer-ip=x.x.x.x` or through annotations `kube-vip.io/loadbalancerIPs: x.x.x.x`
- Setting the special IP `0.0.0.0` for DHCP workflow.
- Support single stack IPv6 or IPv4
- Support for dualstack via the annotation: `kube-vip.io/loadbalancerIPs: 192.168.10.10,2001:db8::1`
- Support ascending and descending search order by setting search-order=desc

## Installing the `kube-vip-cloud-provider`
Expand Down Expand Up @@ -87,7 +89,8 @@ kubectl create configmap --namespace kube-system kubevip --from-literal range-gl

## Multiple pools or ranges

We can apply multiple pools or ranges by seperating them with commas.. i.e. `192.168.0.200/30,192.168.0.200/29` or `2001::12/127,2001::10/127` or `192.168.0.10-192.168.0.11,192.168.0.10-192.168.0.13` or `2001::10-2001::14,2001::20-2001::24`
We can apply multiple pools or ranges by seperating them with commas.. i.e. `192.168.0.200/30,192.168.0.200/29` or `2001::12/127,2001::10/127` or `192.168.0.10-192.168.0.11,192.168.0.10-192.168.0.13` or `2001::10-2001::14,2001::20-2001::24` or `192.168.0.200/30,2001::10/127`


## Special DHCP CIDR

Expand Down
66 changes: 63 additions & 3 deletions pkg/ipam/addressbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"go4.org/netipx"
)

// buildHostsFromCidr - Builds a IPSet constructed from the cidr
func buildHostsFromCidr(cidr string) (*netipx.IPSet, error) {
// parseCidr - Builds an IPSet constructed from the cidrs
func parseCidrs(cidr string) (*netipx.IPSet, error) {
// Split the ipranges (comma separated)
cidrs := strings.Split(cidr, ",")
if len(cidrs) == 0 {
Expand All @@ -23,6 +23,21 @@ func buildHostsFromCidr(cidr string) (*netipx.IPSet, error) {
if err != nil {
return nil, err
}
builder.AddPrefix(prefix)
}
return builder.IPSet()
}

// buildHostsFromCidr - Builds a IPSet constructed from the cidr and filters out
// the broadcast IP and network IP for IPv4 networks
func buildHostsFromCidr(cidr string) (*netipx.IPSet, error) {
unfilteredSet, err := parseCidrs(cidr)
if err != nil {
return nil, err
}

builder := &netipx.IPSetBuilder{}
for _, prefix := range unfilteredSet.Prefixes() {
if prefix.IsSingleIP() {
builder.Add(prefix.Addr())
continue
Expand All @@ -31,7 +46,6 @@ func buildHostsFromCidr(cidr string) (*netipx.IPSet, error) {
builder.AddPrefix(prefix)
continue
}

if r := netipx.RangeOfPrefix(prefix); r.IsValid() {
if prefix.Bits() == 31 {
// rfc3021 Using 31-Bit Prefixes on IPv4 Point-to-Point Links
Expand Down Expand Up @@ -77,3 +91,49 @@ func buildAddressesFromRange(ipRangeString string) (*netipx.IPSet, error) {

return builder.IPSet()
}

// SplitCIDRsByIPFamily splits the cidrs into separate lists of ipv4
// and ipv6 CIDRs
func SplitCIDRsByIPFamily(cidrs string) (ipv4 string, ipv6 string, err error) {
ipPools, err := parseCidrs(cidrs)
if err != nil {
return "", "", err
}
ipv4Cidrs := strings.Builder{}
ipv6Cidrs := strings.Builder{}
for _, prefix := range ipPools.Prefixes() {
cidrsToEdit := &ipv4Cidrs
if prefix.Addr().Is6() {
cidrsToEdit = &ipv6Cidrs
}
if cidrsToEdit.Len() > 0 {
cidrsToEdit.WriteByte(',')
}
_, _ = cidrsToEdit.WriteString(prefix.String())
}
return ipv4Cidrs.String(), ipv6Cidrs.String(), nil
}

// SplitRangesByIPFamily splits the ipRangeString into separate lists of ipv4
// and ipv6 ranges
func SplitRangesByIPFamily(ipRangeString string) (ipv4 string, ipv6 string, err error) {
ipPools, err := buildAddressesFromRange(ipRangeString)
if err != nil {
return "", "", err
}
ipv4Ranges := strings.Builder{}
ipv6Ranges := strings.Builder{}
for _, ipRange := range ipPools.Ranges() {
rangeToEdit := &ipv4Ranges
if ipRange.From().Is6() {
rangeToEdit = &ipv6Ranges
}
if rangeToEdit.Len() > 0 {
rangeToEdit.WriteByte(',')
}
_, _ = rangeToEdit.WriteString(ipRange.From().String())
_ = rangeToEdit.WriteByte('-')
_, _ = rangeToEdit.WriteString(ipRange.To().String())
}
return ipv4Ranges.String(), ipv6Ranges.String(), nil
}
192 changes: 192 additions & 0 deletions pkg/ipam/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,198 @@ func Test_buildHostsFromCidr(t *testing.T) {
}
}

func TestSplitCIDRsByIPFamily(t *testing.T) {
type args struct {
cidrs string
}
type output struct {
ipv4Cidrs string
ipv6Cidrs string
}
tests := []struct {
name string
args args
want output
wantErr bool
}{
{
name: "single ipv4 cidr",
args: args{
"192.168.0.200/30",
},
want: output{
ipv4Cidrs: "192.168.0.200/30",
ipv6Cidrs: "",
},
wantErr: false,
},
{
name: "multiple ipv4 cidrs",
args: args{
"192.168.0.200/30,192.168.1.200/30",
},
want: output{
ipv4Cidrs: "192.168.0.200/30,192.168.1.200/30",
ipv6Cidrs: "",
},
wantErr: false,
},
{
name: "single ipv6 cidr",
args: args{
"fe80::10/127",
},
want: output{
ipv4Cidrs: "",
ipv6Cidrs: "fe80::10/127",
},
wantErr: false,
},
{
name: "multiple ipv6 cidrs",
args: args{
"fe80::10/127,fe80::fe/127",
},
want: output{
ipv4Cidrs: "",
ipv6Cidrs: "fe80::10/127,fe80::fe/127",
},
wantErr: false,
},
{
name: "one ipv4 cidr and one ipv6 cidr",
args: args{
"192.168.0.200/30,fe80::10/127",
},
want: output{
ipv4Cidrs: "192.168.0.200/30",
ipv6Cidrs: "fe80::10/127",
},
wantErr: false,
},
{
name: "multiple ipv4 cidrs and multiple ipv6 cidrs",
args: args{
"192.168.0.200/30,192.168.1.200/30,fe80::10/127,fe80::fe/127",
},
want: output{
ipv4Cidrs: "192.168.0.200/30,192.168.1.200/30",
ipv6Cidrs: "fe80::10/127,fe80::fe/127",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv4Cidrs, ipv6Cidrs, err := SplitCIDRsByIPFamily(tt.args.cidrs)
if (err != nil) != tt.wantErr {
t.Errorf("SplitCIDRsByIPFamily() error = %v, wantErr %v", err, tt.wantErr)
return
}

if ipv4Cidrs != tt.want.ipv4Cidrs || ipv6Cidrs != tt.want.ipv6Cidrs {
t.Errorf("SplitCIDRsByIPFamily() = {ipv4Cidrs: %v, ipv6Cidrs: %v}, want %+v", ipv4Cidrs, ipv6Cidrs, tt.want)
}
})
}
}

func TestSplitRangesByIPFamily(t *testing.T) {
type args struct {
ipRangeString string
}
type output struct {
ipv4Ranges string
ipv6Ranges string
}
tests := []struct {
name string
args args
want output
wantErr bool
}{
{
name: "single ipv4 range",
args: args{
"192.168.0.10-192.168.0.12",
},
want: output{
ipv4Ranges: "192.168.0.10-192.168.0.12",
ipv6Ranges: "",
},
wantErr: false,
},
{
name: "multiple ipv4 ranges",
args: args{
"192.168.0.10-192.168.0.12,192.168.0.100-192.168.0.120",
},
want: output{
ipv4Ranges: "192.168.0.10-192.168.0.12,192.168.0.100-192.168.0.120",
ipv6Ranges: "",
},
wantErr: false,
},
{
name: "single ipv6 range",
args: args{
"fe80::13-fe80::14",
},
want: output{
ipv4Ranges: "",
ipv6Ranges: "fe80::13-fe80::14",
},
wantErr: false,
},
{
name: "multiple ipv6 ranges",
args: args{
"fe80::13-fe80::14,fe80::130-fe80::140",
},
want: output{
ipv4Ranges: "",
ipv6Ranges: "fe80::13-fe80::14,fe80::130-fe80::140",
},
wantErr: false,
},
{
name: "one ipv4 range and one ipv6 range",
args: args{
"192.168.0.10-192.168.0.12,fe80::13-fe80::14",
},
want: output{
ipv4Ranges: "192.168.0.10-192.168.0.12",
ipv6Ranges: "fe80::13-fe80::14",
},
wantErr: false,
},
{
name: "multiple ipv4 ranges and multiple ipv6 ranges",
args: args{
"192.168.0.10-192.168.0.12,192.168.0.100-192.168.0.120,fe80::13-fe80::14,fe80::130-fe80::140",
},
want: output{
ipv4Ranges: "192.168.0.10-192.168.0.12,192.168.0.100-192.168.0.120",
ipv6Ranges: "fe80::13-fe80::14,fe80::130-fe80::140",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ipv4Ranges, ipv6Ranges, err := SplitRangesByIPFamily(tt.args.ipRangeString)
if (err != nil) != tt.wantErr {
t.Errorf("SplitRangesByIPFamily() error = %v, wantErr %v", err, tt.wantErr)
return
}

if ipv4Ranges != tt.want.ipv4Ranges || ipv6Ranges != tt.want.ipv6Ranges {
t.Errorf("SplitRangesByIPFamily() = {ipv4Ranges: %v, ipv6Ranges: %v}, want %+v", ipv4Ranges, ipv6Ranges, tt.want)
}
})
}
}

func TestFindAvailableHostFromRange(t *testing.T) {
type args struct {
namespace string
Expand Down
Loading