コード例 #1
0
ファイル: network.go プロジェクト: abhinandanpb/netplugin
// detachServiceContainer detaches the service container's endpoint during network delete
//      - detach happens only if all other endpoints in the network are already removed
func detachServiceContainer(tenantName, networkName string) error {
	docker, err := utils.GetDockerClient()
	if err != nil {
		log.Errorf("Unable to connect to docker. Error %v", err)
		return errors.New("Unable to connect to docker")
	}

	dnsContName := getDNSName(tenantName)
	cinfo, err := docker.InspectContainer(dnsContName)
	if err != nil {
		log.Errorf("Error inspecting the container %s. Err: %v", dnsContName, err)
		return err
	}

	// Trim default tenant
	dnetName := docknet.GetDocknetName(tenantName, networkName, "")

	// inspect docker network
	nwState, err := docker.InspectNetwork(dnetName)
	if err != nil {
		log.Errorf("Error while inspecting network: %+v", dnetName)
		return err
	}

	log.Infof("Containers in network: %+v are {%+v}", dnetName, nwState.Containers)
	dnsServerIP := strings.Split(nwState.Containers[cinfo.Id].IPv4Address, "/")[0]

	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		log.Errorf("Could not get StateDriver while trying to disconnect dnsContainer from %+v", networkName)
		return err
	}

	// Read network config and get DNSServer information
	nwCfg := &mastercfg.CfgNetworkState{}
	nwCfg.StateDriver = stateDriver
	networkID := networkName + "." + tenantName
	err = nwCfg.Read(networkID)
	if err != nil {
		return err
	}

	log.Infof("dnsServerIP: %+v, nwCfg.dnsip: %+v", dnsServerIP, nwCfg.DNSServer)
	// Remove dns container from network if all other endpoints are withdrawn
	if len(nwState.Containers) == 1 && (dnsServerIP == nwCfg.DNSServer) {
		log.Infof("Disconnecting dns container from network as all other endpoints are removed: %+v", networkName)
		err = docker.DisconnectNetwork(dnetName, dnsContName, false)
		if err != nil {
			log.Errorf("Could not detach container(%s) from network %s. Error: %s",
				dnsContName, dnetName, err)
			return err
		}
	}

	return nil
}
コード例 #2
0
ファイル: network.go プロジェクト: abhinandanpb/netplugin
func attachServiceContainer(tenantName, networkName string, stateDriver core.StateDriver) error {
	contName := getDNSName(tenantName)
	docker, err := utils.GetDockerClient()
	if err != nil {
		log.Errorf("Unable to connect to docker. Error %v", err)
		return err
	}

	cinfo, err := docker.InspectContainer(contName)
	if err != nil {
		if strings.Contains(err.Error(), "no such id") {
			// DNS container not started for this tenant. Start skydns container
			err = startServiceContainer(tenantName)
			if err != nil {
				log.Warnf("Error starting service container. "+
					"Continuing without DNS provider. Error: %v", err)
				return nil
			}
			cinfo, err = docker.InspectContainer(contName)
			if err != nil {
				log.Warnf("Error fetching container info after starting %s"+
					"Continuing without DNS provider. Error: %s", contName, err)
				return nil
			}
		}
	}

	// If it's not in running state, restart the container.
	// This case can occur if the host is reloaded
	if !cinfo.State.Running {
		log.Debugf("Container %s not running. Restarting the container", contName)
		err = docker.RestartContainer(contName, 0)
		if err != nil {
			log.Warnf("Error restarting service container %s. "+
				"Continuing without DNS provider. Error: %v",
				contName, err)
			return nil
		}

		// Refetch container info after restart
		cinfo, err = docker.InspectContainer(contName)
		if err != nil {
			log.Warnf("Error fetching container info after restarting %s"+
				"Continuing without DNS provider. Error: %s", contName, err)
			return nil
		}
	}

	log.Debugf("Container info: %+v\n Hostconfig: %+v", cinfo, cinfo.HostConfig)

	// Trim default tenant
	dnetName := docknet.GetDocknetName(tenantName, networkName, "")

	err = docker.ConnectNetwork(dnetName, contName)
	if err != nil {
		log.Warnf("Could not attach container(%s) to network %s. "+
			"Continuing without DNS provider. Error: %s",
			contName, dnetName, err)
		return nil
	}

	ninfo, err := docker.InspectNetwork(dnetName)
	if err != nil {
		log.Errorf("Error getting network info for %s. Err: %v", dnetName, err)
		return err
	}

	log.Debugf("Network info: %+v", ninfo)

	// find the container in network info
	epInfo, ok := ninfo.Containers[cinfo.Id]
	if !ok {
		log.Errorf("Could not find container %s in network info", cinfo.Id)
		return errors.New("Endpoint not found")
	}

	// read network Config
	nwCfg := &mastercfg.CfgNetworkState{}
	networkID := networkName + "." + tenantName
	nwCfg.StateDriver = stateDriver
	err = nwCfg.Read(networkID)
	if err != nil {
		return err
	}

	// set the dns server Info
	nwCfg.DNSServer = strings.Split(epInfo.IPv4Address, "/")[0]
	log.Infof("Dns server for network %s: %s", networkName, nwCfg.DNSServer)

	// write the network config
	err = nwCfg.Write()
	if err != nil {
		return err
	}

	return nil
}