diff --git a/ipams/null/null.go b/ipams/null/null.go index 339b5308d1..f623ce048d 100644 --- a/ipams/null/null.go +++ b/ipams/null/null.go @@ -24,19 +24,24 @@ func (a *allocator) GetDefaultAddressSpaces() (string, string, error) { } func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) { - if addressSpace != defaultAS { - return "", nil, nil, types.BadRequestErrorf("unknown address space: %s", addressSpace) + if addressSpace == "" { + addressSpace = defaultAS } - if pool != "" { - return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address pool requests") - } - if subPool != "" { - return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address subpool requests") + + if pool == "" && subPool != "" { + return "", nil, nil, fmt.Errorf("subpool requires a pool to be configured") } - if v6 { - return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle IPv6 address pool pool requests") + + retPool := defaultPool + if pool != "" { + var err error + retPool, err = types.ParseCIDR(pool) + if err != nil { + return "", nil, nil, err + } } - return defaultPoolID, defaultPool, nil, nil + retPoolID := fmt.Sprintf("%s/%s", addressSpace, retPool.String()) + return retPoolID, retPool, nil, nil } func (a *allocator) ReleasePool(poolID string) error { diff --git a/ipams/null/null_test.go b/ipams/null/null_test.go index 79b31d50de..4ff2e4ceb6 100644 --- a/ipams/null/null_test.go +++ b/ipams/null/null_test.go @@ -21,14 +21,23 @@ func TestPoolRequest(t *testing.T) { t.Fatalf("Unexpected pool id returned. Expected: %s. Got: %s", defaultPoolID, pid) } - _, _, _, err = a.RequestPool("default", "", "", nil, false) - if err == nil { - t.Fatal("Unexpected success") + id, _, _, err := a.RequestPool("foo", "", "", nil, false) + if err != nil { + t.Fatal("Unexpected error") + } + if id != "foo/0.0.0.0/0" { + t.Fatal("Wrong id") } - _, _, _, err = a.RequestPool(defaultAS, "192.168.0.0/16", "", nil, false) - if err == nil { - t.Fatal("Unexpected success") + id, p, _, err := a.RequestPool(defaultAS, "192.168.0.0/16", "", nil, false) + if err != nil { + t.Fatal("Unexpected error") + } + if id != defaultAS+"/192.168.0.0/16" { + t.Fatalf("Wrong id") + } + if p.String() != "192.168.0.0/16" { + t.Fatalf("Wrong pool") } _, _, _, err = a.RequestPool(defaultAS, "", "192.168.0.0/24", nil, false) @@ -36,9 +45,20 @@ func TestPoolRequest(t *testing.T) { t.Fatal("Unexpected success") } + id, p, _, err = a.RequestPool(defaultAS, "192.168.0.0/16", "192.168.0.0/24", nil, false) + if err != nil { + t.Fatal("Unexpected error") + } + if id != defaultAS+"/192.168.0.0/16" { + t.Fatalf("Wrong id") + } + if p.String() != "192.168.0.0/16" { + t.Fatalf("Wrong pool") + } + _, _, _, err = a.RequestPool(defaultAS, "", "", nil, true) - if err == nil { - t.Fatal("Unexpected success") + if err != nil { + t.Fatal("Unexpected error") } }