コード例 #1
0
ファイル: remote.go プロジェクト: hyperhq/hypernetes
func (plugin *RemoteNetworkPlugin) getNetworkOfNamespace(nsName string) (*types.Network, error) {
	// get namespace info
	namespace, err := plugin.client.Core().Namespaces().Get(nsName)
	if err != nil {
		glog.Errorf("Couldn't get info of namespace %s: %v", nsName, err)
		return nil, err
	}
	if namespace.Spec.Network == "" {
		glog.Warningf("There is no network associated with namespace %s", nsName)
		return nil, nil
	}

	// get network of namespace
	network, err := plugin.client.Core().Networks().Get(namespace.Spec.Network)
	if err != nil {
		glog.Errorf("Couldn't get network for namespace %s: %v", namespace.Name, err)
		return nil, err
	}

	var networkInfo *types.Network
	if network.Spec.ProviderNetworkID != "" {
		networkInfo, err = plugin.provider.Networks().GetNetworkByID(network.Spec.ProviderNetworkID)
	} else {
		networkName := networkprovider.BuildNetworkName(network.Name, network.Spec.TenantID)
		networkInfo, err = plugin.provider.Networks().GetNetwork(networkName)
	}
	if err != nil {
		glog.Errorf("Couldn't get network info from networkprovider: %v", err)
		return nil, err
	}

	return networkInfo, nil
}
コード例 #2
0
// Check network by networkprovider
// If network doesn't exist, then create one.
func (e *NetworkController) addNetwork(obj interface{}) {
	net := obj.(*api.Network)
	newNetwork := *net
	newNetworkStatus := api.NetworkActive

	glog.V(4).Infof("NetworkController: add network %s", net.Name)

	/*// Check if tenant id exist
	check, err := e.netProvider.CheckTenantID(net.Tenant)
	if err != nil {
		glog.Errorf("NetworkController: check tenantID failed: %v", err)
	}
	if !check {
		glog.Warningf("NetworkController: tenantID %s doesn't exit in network provider", net.Tenant)
		newNetwork.Status = api.NetworkStatus{Phase: api.NetworkFailed}
		_, err := e.client.Networks().Status(&newNetwork)
		if err != nil {
			glog.Errorf("NetworkController: failed to update network status: %v", err)
		}
		return
	}*/

	// Check if provider network id exist
	if net.Spec.ProviderNetworkID != "" {
		_, err := e.netProvider.Networks().GetNetworkByID(net.Spec.ProviderNetworkID)
		if err != nil {
			glog.Warningf("NetworkController: network %s doesn't exit in network provider", net.Spec.ProviderNetworkID)
			newNetworkStatus = api.NetworkFailed
		}
	} else {
		if len(net.Spec.Subnets) == 0 {
			glog.Warningf("NetworkController: subnets of %s is null", net.Name)
			newNetworkStatus = api.NetworkFailed
		} else {
			// Check if provider network has already created
			networkName := networkprovider.BuildNetworkName(net.Name, net.Tenant)
			_, err := e.netProvider.Networks().GetNetwork(networkName)
			if err == nil {
				glog.Infof("NetworkController: network %s has already created", networkName)
			} else if err.Error() == networkprovider.ErrNotFound.Error() {
				// Create a new network by network provider
				err := e.netProvider.Networks().CreateNetwork(networkprovider.ApiNetworkToProviderNetwork(net))
				if err != nil {
					glog.Warningf("NetworkController: create network %s failed: %v", net.Name, err)
					newNetworkStatus = api.NetworkFailed
				}
			} else {
				glog.Warningf("NetworkController: get network failed: %v", err)
				newNetworkStatus = api.NetworkFailed
			}
		}
	}

	newNetwork.Status = api.NetworkStatus{Phase: newNetworkStatus}
	_, err := e.client.Networks().Status(&newNetwork)
	if err != nil {
		glog.Errorf("NetworkController: failed to update network status: %v", err)
	}

}
コード例 #3
0
// DeleteNetwork deletes networks created by networkprovider
func (e *NetworkController) deleteNetwork(obj interface{}) {
	if net, ok := obj.(*api.Network); ok {
		glog.V(4).Infof("NetworkController: network %s deleted", net.Name)
		// Only delete network created by networkprovider
		if net.Spec.ProviderNetworkID == "" {
			networkName := networkprovider.BuildNetworkName(net.Name, net.Tenant)
			err := e.netProvider.Networks().DeleteNetwork(networkName)
			if err != nil {
				glog.Errorf("NetworkController: delete network %s failed in networkprovider: %v", networkName, err)
			} else {
				glog.V(4).Infof("NetworkController: network %s deleted in networkprovider", networkName)
			}
		}
		return
	}

	key, err := keyFunc(obj)
	if err != nil {
		glog.Errorf("Couldn't get key for object %+v: %v", obj, err)
	}
	glog.V(5).Infof("Network %q was deleted but we don't have a record of its final state", key)
}
コード例 #4
0
// UpdateNetwork update network'by networkprovider
func (e *NetworkController) updateNetwork(old, cur interface{}) {
	if api.Semantic.DeepEqual(old, cur) {
		return
	}

	var failed bool
	oldNetwork := old.(*api.Network)
	newNetwork := cur.(*api.Network)

	if newNetwork.Spec.ProviderNetworkID != "" {
		// If oldNetwork has created network, delete it
		if oldNetwork.Spec.ProviderNetworkID == "" {
			// delete old tenant network
			networkName := networkprovider.BuildNetworkName(oldNetwork.Name, oldNetwork.Tenant)
			err := e.netProvider.Networks().DeleteNetwork(networkName)
			if err != nil {
				glog.Errorf("NetworkController: delete old network %s from networkprovider failed: %v", oldNetwork.Name, err)
				failed = true
			}
		}
	} else {
		// Update network's subnet
		net := networkprovider.ApiNetworkToProviderNetwork(newNetwork)
		err := e.netProvider.Networks().UpdateNetwork(net)
		if err != nil {
			glog.Errorf("NetworkController: update network %s failed: %v", newNetwork.Name, err)
			failed = true
		}
	}

	// If updated failed, update network status
	if failed {
		oldNetwork.Status = api.NetworkStatus{Phase: api.NetworkFailed}
		_, err := e.client.Networks().Status(oldNetwork)
		if err != nil {
			glog.Errorf("NetworkController: failed to update network status: %v", err)
		}
	}
}
コード例 #5
0
func (e *NetworkController) createLoadBalancer(service *api.Service) (*api.LoadBalancerStatus, error) {
	newHosts, _ := e.getEndpointHosts(service)
	if len(newHosts) == 0 {
		glog.V(4).Infof("NetworkController: no endpoints on service %s", service.Name)
		return nil, nil
	}

	// get namespace of svc
	namespace, err := e.client.Namespaces().Get(service.Namespace)
	if err != nil {
		glog.Errorf("NetworkController: couldn't get namespace for service %s: %v", service.Name, err)
		return nil, err
	}
	if namespace.Spec.Network == "" {
		glog.Warningf("NetworkController: there is no network associated with namespace %s", namespace.Name)
		return nil, nil
	}

	// get network of namespace
	network, err := e.client.Networks().Get(namespace.Spec.Network)
	if err != nil {
		glog.Errorf("NetworkController: couldn't get network for namespace %s: %v", namespace.Name, err)
		return nil, err
	}

	var networkInfo *networkprovider.Network
	if network.Spec.ProviderNetworkID != "" {
		networkInfo, err = e.netProvider.Networks().GetNetworkByID(network.Spec.ProviderNetworkID)
	} else {
		networkName := networkprovider.BuildNetworkName(network.Name, network.Tenant)
		networkInfo, err = e.netProvider.Networks().GetNetwork(networkName)
	}
	if err != nil {
		glog.Errorf("NetworkController: couldn't get network from networkprovider: %v", err)
		return nil, err
	}

	// create loadbalancer for service
	loadBalancerFullName := networkprovider.BuildLoadBalancerName(service.Name, service.Namespace)
	providerLoadBalancer := networkprovider.LoadBalancer{
		Name: loadBalancerFullName,
		// TODO: support more loadbalancer type
		Type:        networkprovider.LoadBalancerTypeTCP,
		TenantID:    networkInfo.TenantID,
		Subnets:     networkInfo.Subnets,
		Hosts:       newHosts,
		ExternalIPs: service.Spec.ExternalIPs,
	}

	vip, err := e.netProvider.LoadBalancers().CreateLoadBalancer(&providerLoadBalancer, service.Spec.SessionAffinity)
	if err != nil {
		glog.Errorf("NetworkController: create load balancer %s failed: %v", loadBalancerFullName, err)
		return nil, err
	}

	glog.V(4).Infof("NetworkController: load balancer for service %s created", service.Name)

	status := &api.LoadBalancerStatus{}
	status.Ingress = []api.LoadBalancerIngress{{IP: vip}}
	return status, nil
}