Exemple #1
0
// InitDockerDefault registers the built-in ipam service with libnetwork
func InitDockerDefault(ic ipamapi.Callback, l, g interface{}) error {
	var (
		ok                bool
		localDs, globalDs datastore.DataStore
	)

	if l != nil {
		if localDs, ok = l.(datastore.DataStore); !ok {
			return errors.New("incorrect local datastore passed to built-in ipam init")
		}
	}

	if g != nil {
		if globalDs, ok = g.(datastore.DataStore); !ok {
			return errors.New("incorrect global datastore passed to built-in ipam init")
		}
	}

	ipamutils.InitNetworks()

	a, err := ipam.NewAllocator(localDs, globalDs)
	if err != nil {
		return err
	}

	cps := &ipamapi.Capability{RequiresRequestReplay: true}

	return ic.RegisterIpamDriverWithCapabilities(ipamapi.DefaultIPAM, a, cps)
}
Exemple #2
0
// Init registers the built-in ipam service with libnetwork
func Init(ic ipamapi.Callback, l, g interface{}) error {
	var (
		ok                bool
		localDs, globalDs datastore.DataStore
	)

	if l != nil {
		if localDs, ok = l.(datastore.DataStore); !ok {
			return fmt.Errorf("incorrect local datastore passed to built-in ipam init")
		}
	}

	if g != nil {
		if globalDs, ok = g.(datastore.DataStore); !ok {
			return fmt.Errorf("incorrect global datastore passed to built-in ipam init")
		}
	}

	ipamutils.InitNetworks()

	a, err := ipam.NewAllocator(localDs, globalDs)
	if err != nil {
		return err
	}

	return ic.RegisterIpamDriver(ipamapi.DefaultIPAM, a)
}
Exemple #3
0
func TestNetworkRequest(t *testing.T) {
	defer testutils.SetupTestOSContext(t)()
	ipamutils.InitNetworks()

	nw, err := FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
	if err != nil {
		t.Fatal(err)
	}

	var found bool
	for _, exp := range ipamutils.PredefinedBroadNetworks {
		if types.CompareIPNet(exp, nw) {
			found = true
			break
		}
	}

	if !found {
		t.Fatalf("Found unexpected broad network %s", nw)
	}

	nw, err = FindAvailableNetwork(ipamutils.PredefinedGranularNetworks)
	if err != nil {
		t.Fatal(err)
	}

	found = false
	for _, exp := range ipamutils.PredefinedGranularNetworks {
		if types.CompareIPNet(exp, nw) {
			found = true
			break
		}
	}

	if !found {
		t.Fatalf("Found unexpected granular network %s", nw)
	}

	// Add iface and ssert returned address on request
	createInterface(t, "test", "172.17.42.1/16")

	_, exp, err := net.ParseCIDR("172.18.0.0/16")
	if err != nil {
		t.Fatal(err)
	}
	nw, err = FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
	if err != nil {
		t.Fatal(err)
	}
	if !types.CompareIPNet(exp, nw) {
		t.Fatalf("exected %s. got %s", exp, nw)
	}
}
func getAllocator() (*Allocator, error) {
	ipamutils.InitNetworks()
	ds, err := randomLocalStore()
	if err != nil {
		return nil, err
	}
	a, err := NewAllocator(ds, nil)
	if err != nil {
		return nil, err
	}
	return a, nil
}
Exemple #5
0
func TestNetworkRequest(t *testing.T) {
	defer testutils.SetupTestOSContext(t)()
	ipamutils.InitNetworks()

	_, exp, err := net.ParseCIDR("172.17.0.0/16")
	if err != nil {
		t.Fatal(err)
	}

	nw, err := FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
	if err != nil {
		t.Fatal(err)
	}
	if !types.CompareIPNet(exp, nw) {
		t.Fatalf("exected %s. got %s", exp, nw)
	}

	_, exp, err = net.ParseCIDR("10.0.0.0/24")
	if err != nil {
		t.Fatal(err)
	}
	nw, err = FindAvailableNetwork(ipamutils.PredefinedGranularNetworks)
	if err != nil {
		t.Fatal(err)
	}
	if !types.CompareIPNet(exp, nw) {
		t.Fatalf("exected %s. got %s", exp, nw)
	}

	// Add iface and ssert returned address on request
	createInterface(t, "test", "172.17.42.1/16")

	_, exp, err = net.ParseCIDR("172.18.0.0/16")
	if err != nil {
		t.Fatal(err)
	}
	nw, err = FindAvailableNetwork(ipamutils.PredefinedBroadNetworks)
	if err != nil {
		t.Fatal(err)
	}
	if !types.CompareIPNet(exp, nw) {
		t.Fatalf("exected %s. got %s", exp, nw)
	}
}
Exemple #6
0
func TestElectInterfaceAddress(t *testing.T) {
	defer testutils.SetupTestOSContext(t)()
	ipamutils.InitNetworks()

	nws := "172.101.202.254/16"
	createInterface(t, "test", nws)

	ipv4Nw, ipv6Nw, err := ElectInterfaceAddresses("test")
	if err != nil {
		t.Fatal(err)
	}

	if ipv4Nw == nil {
		t.Fatalf("unexpected empty ipv4 network addresses")
	}

	if len(ipv6Nw) == 0 {
		t.Fatalf("unexpected empty ipv4 network addresses")
	}

	if nws != ipv4Nw.String() {
		t.Fatalf("expected %s. got %s", nws, ipv4Nw)
	}
}
Exemple #7
0
func TestElectInterfaceAddressMultipleAddresses(t *testing.T) {
	defer testutils.SetupTestOSContext(t)()
	ipamutils.InitNetworks()

	nws := []string{"172.101.202.254/16", "172.102.202.254/16"}
	createInterface(t, "test", nws...)

	ipv4NwList, ipv6NwList, err := ElectInterfaceAddresses("test")
	if err != nil {
		t.Fatal(err)
	}

	if len(ipv4NwList) == 0 {
		t.Fatalf("unexpected empty ipv4 network addresses")
	}

	if len(ipv6NwList) == 0 {
		t.Fatalf("unexpected empty ipv6 network addresses")
	}

	nwList := []string{}
	for _, ipv4Nw := range ipv4NwList {
		nwList = append(nwList, ipv4Nw.String())
	}
	sort.Strings(nws)
	sort.Strings(nwList)

	if len(nws) != len(nwList) {
		t.Fatalf("expected %v. got %v", nws, nwList)
	}
	for i, nw := range nws {
		if nw != nwList[i] {
			t.Fatalf("expected %v. got %v", nw, nwList[i])
		}
	}
}
Exemple #8
0
func init() {
	ipamutils.InitNetworks()
}