func checkDocknetDelete(t *testing.T, tenantName, networkName, serviceName string) { docknetName := GetDocknetName(tenantName, networkName, serviceName) // delete the docknet err := DeleteDockNet(tenantName, networkName, serviceName) if err != nil { t.Fatalf("Error deleting docker network %s. Err: %v", docknetName, err) } // verify docknet state is deleted dnetOper := getDocknetState(tenantName, networkName, serviceName) if dnetOper != nil { t.Fatalf("docknet state for %s was not deleted", docknetName) } // make sure docker has removed the state docker, err := utils.GetDockerClient() if err != nil { t.Fatalf("Unable to connect to docker. Error %v", err) } _, err = docker.InspectNetwork(docknetName) if err == nil { t.Fatalf("docker net %s was not deleted. Err: %v", docknetName, err) } }
// 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 }
func checkDocknetCreate(t *testing.T, tenantName, networkName, serviceName, subnet, gw string) { docknetName := GetDocknetName(tenantName, networkName, serviceName) subnetIP, subnetLen, _ := netutils.ParseCIDR(subnet) nwcfg := mastercfg.CfgNetworkState{ Tenant: tenantName, NetworkName: networkName, PktTagType: "vlan", PktTag: 1, ExtPktTag: 1, SubnetIP: subnetIP, SubnetLen: subnetLen, Gateway: gw, } // create a docker network err := CreateDockNet(tenantName, networkName, serviceName, &nwcfg) if err != nil { t.Fatalf("Error creating docker ntework. Err: %v", err) } // verify docknet state is created dnetOper := getDocknetState(tenantName, networkName, serviceName) if dnetOper == nil { t.Fatalf("Error finding docknet state for %s", docknetName) } // check if docker has the state docker, err := utils.GetDockerClient() if err != nil { t.Fatalf("Unable to connect to docker. Error %v", err) } ninfo, err := docker.InspectNetwork(docknetName) if err != nil { t.Fatalf("Error getting network info for %s. Err: %v", docknetName, err) } // verify params are correct if ninfo.Scope != "local" || ninfo.Driver != netDriverName || ninfo.IPAM.Driver != ipamDriverName || ninfo.IPAM.Config[0].Subnet != subnet || ninfo.IPAM.Config[0].Gateway != gw { t.Fatalf("Docker network {%+v} does not match expected values", ninfo) } // make sure FindDocknetByUUID returns correct UUID tmpOper, err := FindDocknetByUUID(dnetOper.DocknetUUID) if err != nil { t.Fatalf("Error getting docknet by UUID") } if tmpOper.TenantName != tenantName || tmpOper.NetworkName != networkName || tmpOper.ServiceName != serviceName { t.Fatalf("Got unexpected docknet oper state %+v for network UUID %s", tmpOper, dnetOper.DocknetUUID) } }
func startServiceContainer(tenantName string) error { // do nothing in test mode if testMode { return nil } var err error docker, err := utils.GetDockerClient() if err != nil { log.Errorf("Unable to connect to docker. Error %v", err) return err } // pull the skydns image if it does not exist imageName := defaultSkyDNSImage _, err = docker.InspectImage(imageName) if err != nil { pullOperation := func() error { err := docker.PullImage(imageName, nil) if err != nil { log.Errorf("Retrying to pull image: %s", imageName) return err } return nil } err = backoff.Retry(pullOperation, backoff.NewExponentialBackOff()) if err != nil { log.Errorf("Unable to pull image: %s", imageName) return err } } containerConfig := &dockerclient.ContainerConfig{ Image: imageName, Env: []string{"ETCD_MACHINES=http://172.17.0.1:4001", "SKYDNS_NAMESERVERS=8.8.8.8:53", "SKYDNS_ADDR=0.0.0.0:53", "SKYDNS_DOMAIN=" + tenantName}} containerID, err := docker.CreateContainer(containerConfig, tenantName+"dns") if err != nil { log.Errorf("Error creating DNS container for tenant: %s. Error: %s", tenantName, err) } // Start the container err = docker.StartContainer(containerID, nil) if err != nil { log.Errorf("Error starting DNS container for tenant: %s. Error: %s", tenantName, err) } return err }
func stopAndRemoveServiceContainer(tenantName string) error { var err error docker, err := utils.GetDockerClient() if err != nil { log.Errorf("Unable to connect to docker. Error %v", err) return err } dnsContName := getDNSName(tenantName) // Stop the container err = docker.StopContainer(dnsContName, 10) if err != nil { log.Errorf("Error stopping DNS container for tenant: %s. Error: %s", tenantName, err) return err } err = docker.RemoveContainer(dnsContName, true, true) if err != nil { log.Errorf("Error removing DNS container for tenant: %s. Error: %s", tenantName, err) return err } return err }
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 }
func attachServiceContainer(tenantName, networkName string, stateDriver core.StateDriver) error { // do nothing in test mode if testMode { return nil } contName := tenantName + "dns" docker, err := utils.GetDockerClient() if err != nil { log.Errorf("Unable to connect to docker. Error %v", err) return err } // Trim default tenant dnetName := getDocknetName(tenantName, networkName, "") err = docker.ConnectNetwork(dnetName, contName) if err != nil { log.Errorf("Could not attach container(%s) to network %s. Error: %s", contName, dnetName, err) return err } // inspect the container cinfo, err := docker.InspectContainer(contName) if err != nil { log.Errorf("Error inspecting the container %s. Err: %v", contName, err) return err } log.Debugf("Container info: %+v\n Hostconfig: %+v", cinfo, cinfo.HostConfig) 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 }