Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Move address range helpers into address package
Browse files Browse the repository at this point in the history
This means that users don't drag in a dependency on aws-sdk.
  • Loading branch information
bboreham committed Oct 29, 2019
1 parent 196d18d commit bbe48bb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 65 deletions.
2 changes: 1 addition & 1 deletion ipam/tracker/awsvpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewAWSVPCTracker(bridgeName string) (*AWSVPCTracker, error) {
func (t *AWSVPCTracker) HandleUpdate(prevRanges, currRanges []address.Range, local bool) error {
t.debugf("replacing %q by %q; local(%t)", prevRanges, currRanges, local)

prev, curr := RemoveCommon(address.NewCIDRs(Merge(prevRanges)), address.NewCIDRs(Merge(currRanges)))
prev, curr := address.RemoveCommon(address.NewCIDRs(address.Merge(prevRanges)), address.NewCIDRs(address.Merge(currRanges)))

// It might make sense to do the removal first and then add entries
// because of the 50 routes limit. However, in such case a container might
Expand Down
52 changes: 0 additions & 52 deletions ipam/tracker/helpers_test.go

This file was deleted.

12 changes: 4 additions & 8 deletions ipam/tracker/helpers.go → net/address/helpers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package tracker

import (
"github.com/weaveworks/weave/net/address"
)
package address

// Merge merges adjacent range entries.
// The given slice has to be sorted in increasing order.
func Merge(r []address.Range) []address.Range {
var merged []address.Range
func Merge(r []Range) []Range {
var merged []Range

for i := range r {
if prev := len(merged) - 1; prev >= 0 && merged[prev].End == r[i].Start {
Expand All @@ -22,7 +18,7 @@ func Merge(r []address.Range) []address.Range {

// RemoveCommon filters out CIDR ranges which are contained in both a and b slices.
// Both slices have to be sorted in increasing order.
func RemoveCommon(a, b []address.CIDR) (newA, newB []address.CIDR) {
func RemoveCommon(a, b []CIDR) (newA, newB []CIDR) {
i, j := 0, 0

for i < len(a) && j < len(b) {
Expand Down
43 changes: 43 additions & 0 deletions net/address/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package address

import (
"testing"

"github.com/stretchr/testify/require"
)

var (
r0to127 = cidr2("10.0.0.0", "10.0.0.127")
r128to255 = cidr2("10.0.0.128", "10.0.0.255")
r0to255 = cidr2("10.0.0.0", "10.0.0.255")
r1dot0to255 = cidr2("10.0.1.0", "10.0.1.255")
r2dot0to255 = cidr2("10.0.2.0", "10.0.2.255")
)

func TestRemoveCommon(t *testing.T) {
a := []CIDR{r0to127, r1dot0to255}
b := []CIDR{r1dot0to255, r2dot0to255}
newA, newB := RemoveCommon(a, b)
require.Equal(t, []CIDR{r0to127}, newA)
require.Equal(t, []CIDR{r2dot0to255}, newB)
}

func TestMerge(t *testing.T) {
ranges := []Range{
r0to127.Range(),
r128to255.Range(),
r2dot0to255.Range(),
}
require.Equal(t, []Range{r0to255.Range(), r2dot0to255.Range()}, Merge(ranges))
}

// Helper

// [start; end]
func cidr2(start, end string) CIDR {
c := Range{Start: ip(start), End: ip(end) + 1}.CIDRs()
if len(c) != 1 {
panic("invalid cidr")
}
return c[0]
}
7 changes: 3 additions & 4 deletions net/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/chains"
"github.com/weaveworks/weave/common/odp"
"github.com/weaveworks/weave/ipam/tracker"
"github.com/weaveworks/weave/net/address"
"github.com/weaveworks/weave/net/ipset"
)
Expand Down Expand Up @@ -565,9 +564,9 @@ func (t *NoMasqLocalTracker) HandleUpdate(prevRanges, currRanges []address.Range
return nil
}

prev, curr := tracker.RemoveCommon(
address.NewCIDRs(tracker.Merge(prevRanges)),
address.NewCIDRs(tracker.Merge(currRanges)))
prev, curr := address.RemoveCommon(
address.NewCIDRs(address.Merge(prevRanges)),
address.NewCIDRs(address.Merge(currRanges)))

for _, cidr := range curr {
if err := t.ips.AddEntry(t.owner, NoMasqLocalIpset, cidr.String(), ""); err != nil {
Expand Down

0 comments on commit bbe48bb

Please sign in to comment.