コード例 #1
0
func (m *InstanceManager) AttachFloatingIp(
	podName, projectName string, floatingIp *types.FloatingIp) {

	fqn := []string{m.config.DefaultDomain, projectName, podName}
	obj, err := m.client.FindByName(
		"virtual-machine-interface", strings.Join(fqn, ":"))
	if err != nil {
		glog.Errorf("GET vmi %s: %v", podName, err)
		return
	}

	vmi := obj.(*types.VirtualMachineInterface)

	refs, err := floatingIp.GetVirtualMachineInterfaceRefs()
	if err != nil {
		glog.Errorf("GET floating-ip %s: %v", floatingIp.GetUuid(), err)
		return
	}
	for _, ref := range refs {
		if ref.Uuid == vmi.GetUuid() {
			return
		}
	}

	floatingIp.AddVirtualMachineInterface(vmi)
	err = m.client.Update(floatingIp)
	if err != nil {
		glog.Errorf("Update floating-ip %s: %v", podName, err)
	}
}
コード例 #2
0
func getFloatingIpToInstanceList(client contrail.ApiClient, fip *types.FloatingIp) ([]string, error) {
	vmList := make([]string, 0)
	refs, err := fip.GetVirtualMachineInterfaceRefs()
	if err != nil {
		return vmList, err
	}
	for _, ref := range refs {
		vmi, err := types.VirtualMachineInterfaceByUuid(client, ref.Uuid)
		if err != nil {
			continue
		}
		instanceRefs, err := vmi.GetVirtualMachineRefs()
		if err != nil || len(instanceRefs) == 0 {
			continue
		}
		vmList = append(vmList, instanceRefs[0].Uuid)
	}
	return vmList, nil
}
コード例 #3
0
func (c *Controller) updateService(service *api.Service) {
	glog.Infof("Update Service %s", service.Name)
	serviceName := ServiceName(c.config, service.Labels)
	err := c.serviceMgr.Create(service.Namespace, serviceName)
	if err != nil {
		return
	}

	pods, err := c.kube.Pods(service.Namespace).List(makeListOptSelector(service.Spec.Selector))
	if err != nil {
		glog.Errorf("List pods by service %s: %v", service.Name, err)
		return
	}

	var serviceIp *types.FloatingIp = nil
	if service.Spec.ClusterIP != "" {
		serviceNetwork, err := c.serviceMgr.LocateServiceNetwork(service.Namespace, serviceName)
		if err == nil {
			serviceIp, err = c.networkMgr.LocateFloatingIp(
				serviceNetwork, service.Name, service.Spec.ClusterIP)
		}
	} else {
		serviceNetwork, err := c.serviceMgr.LookupServiceNetwork(service.Namespace, serviceName)
		if err == nil {
			c.networkMgr.DeleteFloatingIp(serviceNetwork, service.Name)
		}
	}

	publicIp, err := c.updateServicePublicIP(service)
	if err == nil && publicIp == nil {
		resourceName := publicIpResourceName(service)
		c.networkMgr.DeleteFloatingIp(c.networkMgr.GetPublicNetwork(), resourceName)
	}

	podIdMap := make(map[string]*api.Pod)
	for _, pod := range pods.Items {
		podIdMap[string(pod.UID)] = &pod
		if serviceIp != nil {
			// Connect serviceIp to VMI.
			c.instanceMgr.AttachFloatingIp(pod.Name, pod.Namespace, serviceIp)
		}
		if publicIp != nil {
			c.instanceMgr.AttachFloatingIp(pod.Name, pod.Namespace, publicIp)
		}
	}

	// Detach the VIPs from pods which are no longer selected.
	if serviceIp != nil {
		refs, err := serviceIp.GetVirtualMachineInterfaceRefs()
		if err == nil {
			c.purgeStaleServiceRefs(serviceIp, refs, podIdMap)
		}
	}

	if publicIp != nil {
		refs, err := publicIp.GetVirtualMachineInterfaceRefs()
		if err == nil {
			c.purgeStaleServiceRefs(publicIp, refs, podIdMap)
		}
	}
}