Пример #1
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)
	}

}
Пример #2
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)
		}
	}
}