Ejemplo n.º 1
0
func (m *ServiceManagerImpl) locatePolicyRule(policy *types.NetworkPolicy, lhs, rhs *types.VirtualNetwork) error {
	lhsName := strings.Join(lhs.GetFQName(), ":")
	rhsName := strings.Join(rhs.GetFQName(), ":")

	entries := policy.GetNetworkPolicyEntries()
	for _, rule := range entries.PolicyRule {
		if rule.SrcAddresses[0].VirtualNetwork == lhsName &&
			rule.DstAddresses[0].VirtualNetwork == rhsName {
			return nil
		}
	}
	rule := new(types.PolicyRuleType)
	rule.Protocol = "any"
	rule.Direction = "<>"
	rule.SrcAddresses = []types.AddressType{types.AddressType{
		VirtualNetwork: lhsName,
	}}
	rule.DstAddresses = []types.AddressType{types.AddressType{
		VirtualNetwork: rhsName,
	}}
	rule.SrcPorts = []types.PortType{types.PortType{-1, -1}}
	rule.DstPorts = []types.PortType{types.PortType{-1, -1}}
	rule.ActionList = &types.ActionListType{
		SimpleAction: "pass",
	}

	entries.AddPolicyRule(rule)
	policy.SetNetworkPolicyEntries(&entries)
	err := m.client.Update(policy)
	if err != nil {
		glog.Errorf("policy-rule: %v", err)
		return err
	}
	return nil
}
Ejemplo n.º 2
0
func (m *NetworkManagerImpl) DeleteConnections(network *types.VirtualNetwork, policies map[string]string) error {
	for policyID, _ := range policies {
		network.DeleteNetworkPolicy(policyID)
	}
	err := m.client.Update(network)

	for policyID, targetName := range policies {
		policy, policyErr := types.NetworkPolicyByUuid(m.client, policyID)
		if policyErr != nil {
			glog.Error(policyErr)
			continue
		}
		networkRefs, policyErr := policy.GetVirtualNetworkBackRefs()
		if len(networkRefs) > 1 {
			continue
		}
		if len(networkRefs) == 1 {
			if strings.Join(networkRefs[0].To, ":") == targetName {
				policyErr = m.disconnectNetworkFromPolicy(policy, targetName)
				if policyErr != nil {
					glog.Error(policyErr)
					continue
				}
			} else {
				continue
			}
		}
		policyErr = m.client.Delete(policy)
		if policyErr != nil {
			glog.Error(policyErr)
		}
	}

	return err
}
Ejemplo n.º 3
0
func (m *ServiceManagerImpl) DeleteConnections(network *types.VirtualNetwork, purgeList []string) error {
	if len(purgeList) == 0 {
		return nil
	}
	for _, policyId := range purgeList {
		network.DeleteNetworkPolicy(policyId)
	}
	err := m.client.Update(network)
	if err != nil {
		return err
	}
	for _, policyId := range purgeList {
		policy, err := types.NetworkPolicyByUuid(m.client, policyId)
		if err != nil {
			glog.Error(err)
			continue
		}
		refs, err := policy.GetVirtualNetworkBackRefs()
		if err != nil {
			glog.Error(err)
			continue
		}
		if len(refs) == 0 {
			err = m.client.Delete(policy)
			if err != nil {
				glog.Error(err)
			}
		}
	}
	return nil
}
Ejemplo n.º 4
0
func TestReadChildren(t *testing.T) {
	client := new(ApiClient)
	client.Init()

	project := new(types.Project)
	project.SetFQName("domain", []string{"default-domain", "p1"})
	assert.NoError(t, client.Create(project))

	net := new(types.VirtualNetwork)
	net.SetFQName("project", []string{"default-domain", "p1", "n1"})
	assert.NoError(t, client.Create(net))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"})
	vmi1.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi1))

	vmi2 := new(types.VirtualMachineInterface)
	vmi2.SetFQName("project", []string{"default-domain", "p1", "port2"})
	vmi2.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi2))

	refs, err := project.GetVirtualMachineInterfaces()
	assert.NoError(t, err)
	assert.Len(t, refs, 2)
}
Ejemplo n.º 5
0
func (c *consistencyChecker) globalNetworkCheckPolicyAttachment(network *types.VirtualNetwork) bool {
	policyName := makeGlobalNetworkPolicyName(c.config, network.GetFQName())
	policy, err := types.NetworkPolicyByName(c.client, strings.Join(policyName, ":"))
	if err != nil {
		glog.V(3).Infof("No network policy for %s", network.GetName())
		return true
	}
	policyRefs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Error(err)
		return true
	}

	for _, ref := range policyRefs {
		if ref.Uuid == policy.GetUuid() {
			glog.V(5).Infof("Network %s attached to %s", network.GetName(), policy.GetUuid())
			return true
		}
	}

	err = policyAttach(c.client, network, policy)
	if err != nil {
		glog.Error(err)
	} else {
		glog.Infof("attached global network %s to policy", strings.Join(network.GetFQName(), ":"))
	}
	return false
}
Ejemplo n.º 6
0
func (m *InstanceManager) LocateInterface(
	network *types.VirtualNetwork, instance *types.VirtualMachine) *types.VirtualMachineInterface {
	tenant := network.GetFQName()[len(network.GetFQName())-2]
	fqn := interfaceFQName(m.config.DefaultDomain, tenant, instance.GetName())

	obj, err := m.client.FindByName(
		"virtual-machine-interface", strings.Join(fqn, ":"))

	if err == nil {
		nic := obj.(*types.VirtualMachineInterface)
		// TODO(prm): ensure network is as expected, else update.
		return nic
	}

	nic := new(types.VirtualMachineInterface)
	nic.SetFQName("project", fqn)
	nic.AddVirtualMachine(instance)
	if network != nil {
		nic.AddVirtualNetwork(network)
	}
	err = m.client.Create(nic)
	if err != nil {
		glog.Errorf("Create interface %s: %v", instance.GetName(), err)
		return nil
	}
	obj, err = m.client.FindByUuid(nic.GetType(), nic.GetUuid())
	if err != nil {
		glog.Errorf("Get vmi %s: %v", nic.GetUuid(), err)
		return nil
	}
	return obj.(*types.VirtualMachineInterface)
}
Ejemplo n.º 7
0
func TestPropertyUpdate(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)
	net := types.VirtualNetwork{}
	net.SetName("test")

	err := client.Create(&net)
	if err != nil {
		t.Error(err)
	}

	defer client.Delete(&net)

	props := net.GetVirtualNetworkProperties()
	if len(props.ForwardingMode) > 0 {
		t.Error(props.ForwardingMode)
	}
	props.ForwardingMode = "l2_l3"
	net.SetVirtualNetworkProperties(&props)
	err = client.Update(&net)
	if err != nil {
		t.Error(err)
	}

	obj, err := client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Fatal(err)
	}
	netPtr := obj.(*types.VirtualNetwork)
	p2 := netPtr.GetVirtualNetworkProperties()
	if p2.ForwardingMode != "l2_l3" {
		t.Errorf("Expected: l2_l3 got: %s", p2.ForwardingMode)
	}
}
Ejemplo n.º 8
0
func (m *NetworkManagerImpl) DeleteFloatingIpPool(network *types.VirtualNetwork, cascade bool) error {
	obj, err := m.client.FindByName("floating-ip-pool", makePoolName(network))
	if err != nil {
		glog.Errorf("Get floating-ip-pool %s: %v", network.GetName(), err)
		return err
	}
	if cascade {
		pool := obj.(*types.FloatingIpPool)
		m.floatingIpPoolDeleteChildren(pool)
	}
	m.client.Delete(obj)
	return nil
}
Ejemplo n.º 9
0
func (c *Controller) globalNetworkConnectionUpdate(network *types.VirtualNetwork) {
	if !networkAccessGlobalNetworks(c.config, network.GetFQName()) {
		return
	}
	// connect to each of the global-networks
	for _, gbl := range c.config.GlobalNetworks {
		err := c.networkMgr.Connect(network, gbl)
		if err != nil {
			glog.Error(err)
			continue
		}
		glog.V(2).Infof("Connected %s to %s", strings.Join(network.GetFQName(), ":"), gbl)
	}
}
func TestPodCreate(t *testing.T) {
	kube := mocks.NewKubeClient()

	client := new(contrail_mocks.ApiClient)
	client.Init()

	client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{})
	allocator := new(mocks.AddressAllocator)
	networkMgr := new(mocks.NetworkManager)

	controller := NewTestController(kube, client, allocator, networkMgr)
	pod := &api.Pod{
		ObjectMeta: api.ObjectMeta{
			Name:      "test",
			Namespace: "testns",
			UID:       kubetypes.UID(uuid.New()),
			Labels: map[string]string{
				"name": "testnet",
			},
		},
	}

	netnsProject := new(types.Project)
	netnsProject.SetUuid(uuid.New())
	netnsProject.SetFQName("", []string{"default-domain", "testns"})
	client.Create(netnsProject)

	testnet := new(types.VirtualNetwork)
	testnet.SetFQName("project", []string{"default-domain", "testns", "testnet"})
	client.Create(testnet)

	allocator.On("LocateIpAddress", string(pod.ObjectMeta.UID)).Return("10.0.0.42", nil)
	networkMgr.On("LocateNetwork", "testns", "testnet",
		controller.config.PrivateSubnet).Return(testnet, nil)
	networkMgr.On("GetGatewayAddress", testnet).Return("10.0.255.254", nil)

	kube.PodInterface.On("Update", pod).Return(pod, nil)

	shutdown := make(chan struct{})
	go controller.Run(shutdown)

	controller.AddPod(pod)

	time.Sleep(100 * time.Millisecond)
	type shutdownMsg struct {
	}
	shutdown <- shutdownMsg{}

	kube.PodInterface.AssertExpectations(t)
}
Ejemplo n.º 11
0
func CreateNetworkWithSubnet(
	client contrail.ApiClient, project_id, name, prefix string) (
	string, error) {

	expr := regexp.MustCompile(`(([0-9]{1,3}\.){3}[0-9]{1,3})/([0-9]{1,2})`)
	match := expr.FindStringSubmatch(prefix)
	if match == nil {
		return "", fmt.Errorf("Invalid subnet prefix %s", prefix)
	}
	address := match[1]
	prefixlen, _ := strconv.Atoi(match[3])

	obj, err := client.FindByUuid("project", project_id)
	if err != nil {
		return "", err
	}

	project := obj.(*types.Project)
	refList, err := project.GetNetworkIpams()
	if err != nil {
		return "", err
	}

	var ipam *types.NetworkIpam
	if len(refList) > 0 {
		obj, err := client.FindByUuid("network-ipam", refList[0].Uuid)
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	} else {
		obj, err := client.FindByName("network-ipam",
			"default-domain:default-project:default-network-ipam")
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	}

	net := new(types.VirtualNetwork)
	net.SetParent(project)
	net.SetName(name)

	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{address, prefixlen}})
	net.AddNetworkIpam(ipam, subnets)
	err = client.Create(net)
	if err != nil {
		return "", err
	}
	return net.GetUuid(), nil
}
Ejemplo n.º 12
0
func TestCreate(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)
	net := types.VirtualNetwork{}
	net.SetName("test")
	ipam, err := client.FindByName("network-ipam",
		"default-domain:default-project:default-network-ipam")
	if err != nil {
		t.Fatal(err)
	}
	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{"10.0.0.0", 8}})
	net.AddNetworkIpam(ipam.(*types.NetworkIpam), subnets)
	err = client.Create(&net)
	if err != nil {
		t.Error(err)
	}
	if len(net.GetUuid()) == 0 {
		t.Error("No uuid assigned")
	}

	netPtr, err := client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Error(err)
	} else {
		xnet := netPtr.(*types.VirtualNetwork)
		refs, err := xnet.GetNetworkIpamRefs()
		if err != nil {
			t.Error(err)
		} else if len(refs) > 0 {
			xattr := refs[0].Attr.(types.VnSubnetsType)
			if len(xattr.IpamSubnets) == 1 {
				isubnet := xattr.IpamSubnets[0]
				if isubnet.Subnet.IpPrefix != "10.0.0.0" ||
					isubnet.Subnet.IpPrefixLen != 8 {
					t.Errorf("Bad subnet %s/%d",
						isubnet.Subnet.IpPrefix,
						isubnet.Subnet.IpPrefixLen)
				}
			} else {
				t.Errorf("%d subnets", len(xattr.IpamSubnets))
			}
		} else {
			t.Error("Empty network-ipam reference list")
		}
	}

	err = client.Delete(&net)
	if err != nil {
		t.Error(err)
	}
}
Ejemplo n.º 13
0
func (m *NetworkManagerImpl) LocateFloatingIpPool(network *types.VirtualNetwork) (*types.FloatingIpPool, error) {
	obj, err := m.client.FindByName("floating-ip-pool", makePoolName(network))
	if err == nil {
		return obj.(*types.FloatingIpPool), nil
	}

	pool := new(types.FloatingIpPool)
	pool.SetName(network.GetName())
	pool.SetParent(network)
	err = m.client.Create(pool)
	if err != nil {
		glog.Errorf("Create floating-ip-pool %s: %v", network.GetName(), err)
		return nil, err
	}
	return pool, nil
}
Ejemplo n.º 14
0
func (m *ServiceManagerImpl) PurgeStalePolicyRefs(network *types.VirtualNetwork, services ServiceIdList,
	doDelete func(string, string) bool) error {
	purgeList := make([]string, 0)
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		return err
	}
	for _, ref := range refs {
		if len(ref.To) < 3 {
			glog.Errorf("unexpected policy id %+v", ref.To)
			continue
		}
		namespace := ref.To[1]
		serviceName := ref.To[len(ref.To)-1]
		if !services.Contains(namespace, serviceName) && doDelete(namespace, serviceName) {
			purgeList = append(purgeList, ref.Uuid)
		}
	}
	if len(purgeList) == 0 {
		return nil
	}
	for _, policyId := range purgeList {
		network.DeleteNetworkPolicy(policyId)
	}
	err = m.client.Update(network)
	if err != nil {
		return err
	}
	for _, policyId := range purgeList {
		policy, err := types.NetworkPolicyByUuid(m.client, policyId)
		if err != nil {
			glog.Error(err)
			continue
		}
		refs, err := policy.GetVirtualNetworkBackRefs()
		if err != nil {
			glog.Error(err)
		}
		if len(refs) == 0 {
			err = m.client.Delete(policy)
			if err != nil {
				glog.Error(err)
			}
		}
	}
	return nil
}
Ejemplo n.º 15
0
//
// Retrieve the list of IP prefixes associated with this network.
//
func (m *NetworkManagerImpl) NetworkIPPrefixList(network *types.VirtualNetwork) ([]string, error) {
	prefixList := make([]string, 0)
	refList, err := network.GetNetworkIpamRefs()
	if err != nil {
		return nil, err
	}

	for _, ref := range refList {
		attr := ref.Attr.(types.VnSubnetsType)
		for _, ipamSubnet := range attr.IpamSubnets {
			prefixList = append(prefixList,
				fmt.Sprintf("%s/%d", ipamSubnet.Subnet.IpPrefix, ipamSubnet.Subnet.IpPrefixLen))
		}
	}

	return prefixList, nil
}
Ejemplo n.º 16
0
func TestReadModifiedPolicy(t *testing.T) {
	client := new(ApiClient)
	client.Init()

	project := new(types.Project)
	project.SetFQName("domain", []string{"default-domain", "p1"})
	assert.NoError(t, client.Create(project))

	policy := new(types.NetworkPolicy)
	policy.SetFQName("project", []string{"default-domain", "p1", "x"})
	assert.NoError(t, client.Create(policy))

	net1 := new(types.VirtualNetwork)
	net1.SetFQName("project", []string{"default-domain", "p1", "n1"})
	net1.AddNetworkPolicy(policy,
		types.VirtualNetworkPolicyType{
			Sequence: &types.SequenceType{10, 0},
		})
	assert.NoError(t, client.Create(net1))

	net2 := new(types.VirtualNetwork)
	net2.SetFQName("project", []string{"default-domain", "p1", "n2"})
	net2.AddNetworkPolicy(policy,
		types.VirtualNetworkPolicyType{
			Sequence: &types.SequenceType{10, 0},
		})
	assert.NoError(t, client.Create(net2))

	refs, err := policy.GetVirtualNetworkBackRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 2)

	assert.NoError(t, client.Delete(net1))

	refs, err = policy.GetVirtualNetworkBackRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 1)

	assert.NoError(t, client.Delete(net2))
	refs, err = policy.GetVirtualNetworkBackRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 0)
}
Ejemplo n.º 17
0
func TestChildrenRefs(t *testing.T) {
	db := NewInMemDatabase()
	project := new(types.Project)
	project.SetUuid(uuid.New())
	project.SetName("p1")
	assert.NoError(t, db.Put(project, nil, GetReferenceList(project)))

	net := new(types.VirtualNetwork)
	net.SetUuid(uuid.New())
	net.SetFQName("project", []string{"p1", "n1"})
	assert.NoError(t, db.Put(net, project, UIDList{}))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetUuid(uuid.New())
	vmi1.SetName("port1")
	vmi1.AddVirtualNetwork(net)
	assert.NoError(t, db.Put(vmi1, project, GetReferenceList(vmi1)))

	vmi2 := new(types.VirtualMachineInterface)
	vmi2.SetUuid(uuid.New())
	vmi2.SetName("port2")
	vmi2.AddVirtualNetwork(net)
	assert.NoError(t, db.Put(vmi2, project, GetReferenceList(vmi2)))

	result, err := db.GetChildren(parseUID(project.GetUuid()), "virtual_network")
	assert.NoError(t, err)
	assert.Len(t, result, 1)

	result, err = db.GetChildren(parseUID(project.GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, result, 2)
	assert.Contains(t, result, parseUID(vmi1.GetUuid()))
	assert.Contains(t, result, parseUID(vmi2.GetUuid()))

	assert.Error(t, db.Delete(project))

	assert.NoError(t, db.Delete(vmi1))
	assert.Error(t, db.Delete(net))
	assert.NoError(t, db.Delete(vmi2))

	assert.NoError(t, db.Delete(net))
	assert.NoError(t, db.Delete(project))
}
Ejemplo n.º 18
0
// connectionShouldDelete returns true when a connection should be deleted.
// When a connection is first considered to be stale it is added to a GC list; if it is considered to be stale
// in two consistency runs, it is deleted.
func (c *consistencyChecker) connectionShouldDelete(network *types.VirtualNetwork, lastIterationMap networkConnectionMap, policyName []string) bool {
	networkName := strings.Join(network.GetFQName(), ":")
	policyCSN := strings.Join(policyName, ":")
	if entry, ok := lastIterationMap[networkName]; ok {
		if _, exists := entry[policyCSN]; exists {
			glog.Infof("%s network connection %s delete", networkName, policyCSN)
			return true
		}
	}

	gcPolicies, exists := c.staleConnectionMap[networkName]
	if !exists {
		gcPolicies = make(policySet, 0)
		c.staleConnectionMap[networkName] = gcPolicies
	}
	glog.Infof("%s network connection %s not used by pod specifications", networkName, policyCSN)
	gcPolicies[policyCSN] = true
	return false
}
Ejemplo n.º 19
0
func (m *NetworkManagerImpl) DeleteNetwork(network *types.VirtualNetwork) error {
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Errorf("Get %s policy refs: %v", network.GetName(), err)
	}
	m.client.Delete(network)

	for _, ref := range refs {
		obj, err := m.client.FindByUuid("network-policy", ref.Uuid)
		if err != nil {
			glog.Errorf("Get policy %s: %v", ref.Uuid, err)
		}
		policy := obj.(*types.NetworkPolicy)
		npRefs, err := policy.GetVirtualNetworkBackRefs()
		if len(npRefs) == 0 {
			m.client.Delete(policy)
		}
	}
	return nil
}
Ejemplo n.º 20
0
func (m *NetworkManagerImpl) GetGatewayAddress(network *types.VirtualNetwork) (string, error) {
	refs, err := network.GetNetworkIpamRefs()
	if err != nil {
		glog.Errorf("Get network %s network-ipam refs: %v", network.GetName(), err)
		return "", err
	}

	attr := refs[0].Attr.(types.VnSubnetsType)
	if len(attr.IpamSubnets) == 0 {
		glog.Errorf("Network %s has no subnets configured", network.GetName())
		return "", fmt.Errorf("Network %s: empty subnet list", network.GetName())
	}

	gateway := attr.IpamSubnets[0].DefaultGateway
	if gateway == "" {
		glog.Errorf("Gateway for %s is empty", network.GetName())
		return "", fmt.Errorf("Gateway is empty: %+v", attr.IpamSubnets)
	}

	return gateway, nil
}
Ejemplo n.º 21
0
func TestReadRefs(t *testing.T) {
	client := new(ApiClient)
	client.Init()

	project := new(types.Project)
	project.SetFQName("domain", []string{"default-domain", "p1"})
	assert.NoError(t, client.Create(project))

	net := new(types.VirtualNetwork)
	net.SetFQName("project", []string{"default-domain", "p1", "n1"})
	assert.NoError(t, client.Create(net))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"})
	vmi1.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi1))

	refs, err := vmi1.GetVirtualNetworkRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 1)
	assert.Equal(t, net.GetUuid(), refs[0].Uuid)
}
Ejemplo n.º 22
0
// Subnets can only be deleted if there are no instance-ips or floating-ips associated
// with the subnet.
func (m *NetworkManagerImpl) updateSubnetConfig(network *types.VirtualNetwork, prefix string) error {
	prefixList, err := m.NetworkIPPrefixList(network)
	if err != nil {
		glog.Fatal(err)
	}
	add := true
	for _, pfx := range prefixList {
		if pfx == prefix {
			add = false
			continue
		}

		ipRefs, err := network.GetInstanceIpBackRefs()
		if err != nil {
			glog.Error(err)
			continue
		}

		// TODO: filter the IP addresses associated with the subnet that we are
		// attempting to delete.
		if len(ipRefs) > 0 {
			glog.Warningf(
				"network %s %s is stale but there are %d instance-ips present",
				network.GetName(), pfx, len(ipRefs))
			continue
		}

		pool, err := m.LookupFloatingIpPool(network)
		if err == nil {
			floatRefs, err := pool.GetFloatingIps()
			if err != nil {
				glog.Error(err)
				continue
			}
			// TODO: filter the floating-ips associated with the subnet that we
			// are attempting to delete.
			if len(floatRefs) > 0 {
				glog.Warningf(
					"network %s %s is stale but there are %d floating-ip addresses present",
					network.GetName(), pfx, len(floatRefs))
				continue
			}
		}

		err = config.RemoveSubnet(m.client, network, pfx)
		if err != nil {
			glog.Error(err)
		}
	}
	if add {
		_, err := config.AddSubnet(m.client, network, m.config.PublicSubnet)
		if err != nil {
			glog.Error(err)
		}
		return err
	}
	return nil
}
Ejemplo n.º 23
0
func (m *NetworkManagerImpl) LocateFloatingIpPool(
	network *types.VirtualNetwork, subnet string) (*types.FloatingIpPool, error) {
	obj, err := m.client.FindByName(
		"floating-ip-pool", makePoolName(network))
	if err == nil {
		return obj.(*types.FloatingIpPool), nil
	}

	address, prefixlen := PrefixToAddressLen(subnet)

	pool := new(types.FloatingIpPool)
	pool.SetName(network.GetName())
	pool.SetParent(network)
	pool.SetFloatingIpPoolPrefixes(
		&types.FloatingIpPoolType{
			Subnet: []types.SubnetType{types.SubnetType{address, prefixlen}}})
	err = m.client.Create(pool)
	if err != nil {
		glog.Errorf("Create floating-ip-pool %s: %v", network.GetName(), err)
		return nil, err
	}
	return pool, nil
}
Ejemplo n.º 24
0
// Connect creates a network-policy and corresponding policy rule (when they do not exist) in order to
// connect the source network with the target. The target network may or not exist yet.
func (m *NetworkManagerImpl) Connect(network *types.VirtualNetwork, targetCSN string) error {
	targetName := strings.Split(targetCSN, ":")
	policy, err := m.locatePolicy(targetName)
	if err != nil {
		return err
	}

	policyAttach(m.client, network, policy)

	target, err := types.VirtualNetworkByName(m.client, targetCSN)
	if err == nil {
		err = policyAttach(m.client, target, policy)
		if err != nil {
			glog.Error(err)
		}
	}

	err = policyLocateRuleByFQN(m.client, policy, network.GetFQName(), targetName)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 25
0
func policyDetach(client contrail.ApiClient, network *types.VirtualNetwork, policyName string) error {
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Errorf("get network policy-refs %s: %v", network.GetName(), err)
		return err
	}
	for _, ref := range refs {
		if strings.Join(ref.To, ":") == policyName {
			network.DeleteNetworkPolicy(ref.Uuid)
			err := client.Update(network)
			if err != nil {
				glog.Errorf("Update network %s policies: %v", network.GetName(), err)
			}
			return err
		}
	}
	return nil
}
Ejemplo n.º 26
0
func (m *ServiceManagerImpl) detachPolicy(network *types.VirtualNetwork, policyName string) error {
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Errorf("get network policy-refs %s: %v", network.GetName(), err)
		return err
	}
	for _, ref := range refs {
		if strings.Join(ref.To, ":") == policyName {
			network.DeleteNetworkPolicy(ref.Uuid)
			err := m.client.Update(network)
			if err != nil {
				glog.Errorf("Update network %s policies: %v", network.GetName(), err)
			}
			return err
		}
	}
	return nil
}
Ejemplo n.º 27
0
func TestReadBackRefs(t *testing.T) {
	client := new(ApiClient)
	client.Init()

	project := new(types.Project)
	project.SetFQName("domain", []string{"default-domain", "p1"})
	assert.NoError(t, client.Create(project))

	net := new(types.VirtualNetwork)
	net.SetFQName("project", []string{"default-domain", "p1", "n1"})
	props := new(types.VirtualNetworkType)
	props.ForwardingMode = "turbo"
	net.SetVirtualNetworkProperties(props)
	assert.NoError(t, client.Create(net))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"})
	vmi1.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi1))

	vmi2 := new(types.VirtualMachineInterface)
	vmi2.SetFQName("project", []string{"default-domain", "p1", "port2"})
	vmi2.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi2))

	refs, err := net.GetVirtualMachineInterfaceBackRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 2)
	idList := make([]string, len(refs))
	for i, ref := range refs {
		idList[i] = ref.Uuid
	}

	assert.Contains(t, idList, vmi1.GetUuid())
	assert.Contains(t, idList, vmi2.GetUuid())

	rProps := net.GetVirtualNetworkProperties()
	assert.Equal(t, "turbo", rProps.ForwardingMode)
}
Ejemplo n.º 28
0
func TestReference(t *testing.T) {
	db := NewInMemDatabase()

	ipam := new(types.NetworkIpam)
	ipam.SetUuid(uuid.New())
	ipam.SetFQName("project", strings.Split("default-domain:p1:ipam", ":"))
	assert.NoError(t, db.Put(ipam, nil, UIDList{}))

	net := new(types.VirtualNetwork)
	net.SetUuid(uuid.New())
	net.SetFQName("project", strings.Split("default-domain:p1:network", ":"))
	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{"10.0.0.0", 8}})

	net.AddNetworkIpam(ipam, subnets)
	refs := GetReferenceList(net)
	assert.NoError(t, db.Put(net, nil, refs))

	result, err := db.GetBackReferences(parseUID(ipam.GetUuid()), "virtual_network")
	assert.NoError(t, err)
	assert.Contains(t, result, parseUID(net.GetUuid()))
}
Ejemplo n.º 29
0
func (m *ServiceManagerImpl) attachPolicy(network *types.VirtualNetwork, policy *types.NetworkPolicy) error {
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Errorf("get network policy-refs %s: %v", network.GetName(), err)
		return err
	}
	for _, ref := range refs {
		if ref.Uuid == policy.GetUuid() {
			return nil
		}
	}
	network.AddNetworkPolicy(policy,
		types.VirtualNetworkPolicyType{
			Sequence: &types.SequenceType{10, 0},
		})
	err = m.client.Update(network)
	if err != nil {
		glog.Errorf("Update network %s policies: %v", network.GetName(), err)
		return err
	}
	return nil
}
Ejemplo n.º 30
0
func policyAttach(client contrail.ApiClient, network *types.VirtualNetwork, policy *types.NetworkPolicy) error {
	refs, err := network.GetNetworkPolicyRefs()
	if err != nil {
		glog.Errorf("get network policy-refs %s: %v", network.GetName(), err)
		return err
	}
	for _, ref := range refs {
		if ref.Uuid == policy.GetUuid() {
			return nil
		}
	}
	network.AddNetworkPolicy(policy,
		types.VirtualNetworkPolicyType{
			Sequence: &types.SequenceType{Major: 10, Minor: 0},
		})
	err = client.Update(network)
	if err != nil {
		glog.Errorf("Update network %s policies: %v", network.GetName(), err)
		return err
	}
	return nil
}