Skip to content

Commit

Permalink
Merge pull request #456 from gunjan5/pool-validation
Browse files Browse the repository at this point in the history
validate all the requested pools
  • Loading branch information
gunjan5 authored Jun 21, 2017
2 parents e40a090 + 22d5d04 commit 8ddc30e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/client/ipam_block_reader_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net"
"reflect"

"fmt"

log "github.com/Sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
Expand Down Expand Up @@ -71,8 +73,7 @@ func (rw blockReaderWriter) getAffineBlocks(host string, ver ipVersion, pools []
return ids, nil
}

func (rw blockReaderWriter) claimNewAffineBlock(
host string, version ipVersion, requestedPools []cnet.IPNet, config IPAMConfig) (*cnet.IPNet, error) {
func (rw blockReaderWriter) claimNewAffineBlock(host string, version ipVersion, requestedPools []cnet.IPNet, config IPAMConfig) (*cnet.IPNet, error) {

// If requestedPools is not empty, use it. Otherwise, default to
// all configured pools.
Expand All @@ -92,6 +93,20 @@ func (rw blockReaderWriter) claimNewAffineBlock(
}
}

// Build a map so we can lookup existing pools.
pm := map[string]bool{}
for _, ap := range allPools.Items {
pm[ap.Metadata.CIDR.String()] = true
}

// Make sure each requested pool exists.
for _, rp := range requestedPools {
if _, ok := pm[rp.String()]; !ok {
// The requested pool doesn't exist.
return nil, fmt.Errorf("The given pool (%s) does not exist", rp.IPNet.String())
}
}

// If there are no pools, we cannot assign addresses.
if len(pools) == 0 {
return nil, goerrors.New("No configured Calico pools")
Expand Down
20 changes: 20 additions & 0 deletions lib/client/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ var _ = testutils.E2eDatastoreDescribe("IPAM tests", testutils.DatastoreEtcdV2,
pool2 := testutils.MustParseNetwork("20.0.0.0/24")
pool3 := testutils.MustParseNetwork("30.0.0.0/24")
pool4_v6 := testutils.MustParseNetwork("fe80::11/120")
pool5_doesnot_exist := testutils.MustParseNetwork("40.0.0.0/24")

testutils.CreateNewIPPool(*c, "10.0.0.0/24", false, false, true)
testutils.CreateNewIPPool(*c, "20.0.0.0/24", false, false, true)
Expand Down Expand Up @@ -431,6 +432,25 @@ var _ = testutils.E2eDatastoreDescribe("IPAM tests", testutils.DatastoreEtcdV2,
Expect(len(v4)).NotTo(Equal(209))
})
})

// Step-6: AutoAssign an IPv4 address. Request the IP from 2 IP Pools one of which doesn't exist.
// This should return an error.
Context("AutoAssign an IP from two pools - one valid one doesn't exist", func() {
args := client.AutoAssignArgs{
Num4: 1,
Num6: 0,
Hostname: host,
IPv4Pools: []cnet.IPNet{pool1, pool5_doesnot_exist},
}

v4, _, err := ic.AutoAssign(args)
log.Println("v4: %d IPs", len(v4))

It("should return an error and no IPs", func() {
Expect(err.Error()).Should(Equal("The given pool (40.0.0.0/24) does not exist"))
Expect(len(v4)).To(Equal(0))
})
})
})

DescribeTable("AutoAssign: requested IPs vs returned IPs",
Expand Down

0 comments on commit 8ddc30e

Please sign in to comment.