コード例 #1
0
ファイル: context.go プロジェクト: kjplatz/vic
func reservePools(space *AddressSpace, ipam *IPAM) ([]*AddressSpace, error) {
	if ipam.pools == nil || len(ipam.pools) == 0 {
		// pool not specified so use the default
		ipam.pools = []string{space.Network.String()}
		return []*AddressSpace{space}, nil
	}

	var err error
	subSpaces := make([]*AddressSpace, len(ipam.pools))
	defer func() {
		if err == nil {
			return
		}

		for _, s := range subSpaces {
			if s == nil {
				continue
			}
			space.ReleaseIP4Range(s)

		}
	}()

	for i, p := range ipam.pools {
		var nw *net.IPNet
		_, nw, err = net.ParseCIDR(p)
		if err == nil {
			subSpaces[i], err = space.ReserveIP4Net(nw)
			if err != nil {
				break
			}

			continue
		}

		// ip range
		r := ip.ParseRange(p)
		if r == nil {
			err = fmt.Errorf("error in pool spec")
			break
		}

		var ss *AddressSpace
		ss, err = space.ReserveIP4Range(r.FirstIP, r.LastIP)
		if err != nil {
			break
		}

		subSpaces[i] = ss
	}

	if err != nil {
		return nil, err
	}

	return subSpaces, nil
}
コード例 #2
0
ファイル: scope.go プロジェクト: vmware/vic
func (s *Scope) pools() []*ip.Range {
	pools := make([]*ip.Range, len(s.spaces))
	for i := range s.spaces {
		sp := s.spaces[i]
		if sp.Network != nil {
			r := ip.ParseRange(sp.Network.String())
			if r == nil {
				continue
			}
			pools[i] = r
			continue
		}

		pools[i] = sp.Pool
	}

	return pools
}
コード例 #3
0
ファイル: context.go プロジェクト: vmware/vic
func (c *Context) newScopeCommon(id uid.UID, name, scopeType string, subnet *net.IPNet, gateway net.IP, dns []net.IP, pools []string, network object.NetworkReference) (*Scope, error) {
	defer trace.End(trace.Begin(""))

	newScope := newScope(id, name, scopeType, subnet, gateway, dns, network)
	newScope.spaces = make([]*AddressSpace, len(pools))
	for i, p := range pools {
		r := ip.ParseRange(p)
		if r == nil {
			return nil, fmt.Errorf("invalid pool %s specified for scope %s", p, name)
		}

		newScope.spaces[i] = NewAddressSpaceFromRange(r.FirstIP, r.LastIP)
	}

	if err := c.addScope(newScope); err != nil {
		return nil, err
	}

	return newScope, nil
}
コード例 #4
0
ファイル: context_test.go プロジェクト: vmware/vic
func TestMapExternalNetworks(t *testing.T) {
	conf := testConfig()
	ctx, err := NewContext(conf, nil)
	if err != nil {
		t.Fatalf("NewContext() => (nil, %s), want (ctx, nil)", err)
	}

	// check if external networks were loaded
	for n, nn := range conf.ContainerNetworks {
		scopes, err := ctx.findScopes(&n)
		if err != nil || len(scopes) != 1 {
			t.Fatalf("external network %s was not loaded", n)
		}

		s := scopes[0]
		pools := s.Pools()
		if !ip.IsUnspecifiedIP(nn.Gateway.IP) {
			subnet := &net.IPNet{IP: nn.Gateway.IP.Mask(nn.Gateway.Mask), Mask: nn.Gateway.Mask}
			if ip.IsUnspecifiedSubnet(s.Subnet()) || !s.Subnet().IP.Equal(subnet.IP) || !bytes.Equal(s.Subnet().Mask, subnet.Mask) {
				t.Fatalf("external network %s was loaded with wrong subnet, got: %s, want: %s", n, s.Subnet(), subnet)
			}

			if ip.IsUnspecifiedIP(s.Gateway()) || !s.Gateway().Equal(nn.Gateway.IP) {
				t.Fatalf("external network %s was loaded with wrong gateway, got: %s, want: %s", n, s.Gateway(), nn.Gateway.IP)
			}

			if len(nn.Pools) == 0 {
				// only one pool corresponding to the subnet
				if len(pools) != 1 || !pools[0].Equal(ip.ParseRange(subnet.String())) {
					t.Fatalf("external network %s was loaded with wrong pool, got: %+v, want %+v", n, pools, []*net.IPNet{subnet})
				}
			}
		}

		for _, d := range nn.Nameservers {
			found := false
			for _, d2 := range s.DNS() {
				if d2.Equal(d) {
					found = true
					break
				}
			}

			if !found {
				t.Fatalf("external network %s was loaded with wrong nameservers, got: %+v, want: %+v", n, s.DNS(), nn.Nameservers)
			}
		}

		for _, p := range nn.Pools {
			found := false
			for _, p2 := range pools {
				if p2.Equal(&p) {
					found = true
					break
				}
			}

			if !found {
				t.Fatalf("external network %s was loaded with wrong pools, got: %+v, want: %+v", n, s.Pools(), nn.Pools)
			}
		}
	}
}