Ejemplo n.º 1
0
func (s Service) prepareAciList(sources []string) (string, error) {
	if len(s.manifest.Containers) == 0 {
		return "", nil
	}

	override := s.sources(sources)
	s.log.WithField("data", override).Debug("Local resolved sources")

	var acis string
	for _, aci := range s.manifest.Containers {
		containerLog := s.log.WithField("container", aci.String())
		containerLog.Debug("Processing container")
		if strings.HasPrefix(aci.ShortName(), "pod-") {
			var podAcis []cntspec.ACFullname
			if override[aci.Name()] != nil {
				containerLog.Debug("Using local source to resolve")
				podAcis = override[aci.Name()]
			} else {
				containerLog.Debug("Using remote source to resolve")
				podAcis = s.discoverPod(aci)
			}
			for _, aci := range podAcis {
				acis += aci.String() + " "
			}
		} else {
			var taci cntspec.ACFullname
			if override[aci.Name()] != nil {
				containerLog.Debug("Using local source to resolve")
				taci = override[aci.Name()][0]
			} else {
				containerLog.Debug("Using remote source to resolve")
				aciTmp, err := aci.FullyResolved()
				taci = *aciTmp
				if err != nil {
					containerLog.Fatal("Cannot resolve aci")
					return "", err
				}
			}
			acis += taci.String() + " "
		}
	}
	if acis == "" {
		s.log.Error("Aci list is empty after discovery")
	}
	return acis, nil
}
Ejemplo n.º 2
0
func (s Service) discoverPod(name cntspec.ACFullname) []cntspec.ACFullname {
	logAci := s.log.WithField("pod", name)

	app, err := discovery.NewAppFromString(name.String())
	if app.Labels["os"] == "" {
		app.Labels["os"] = "linux"
	}
	if app.Labels["arch"] == "" {
		app.Labels["arch"] = "amd64"
	}

	endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
	if err != nil {
		logAci.WithError(err).Fatal("pod discovery failed")
	}

	url := endpoint.ACIEndpoints[0].ACI
	url = strings.Replace(url, "=aci", "=pod", 1) // TODO this is nexus specific

	logUrl := logAci.WithField("url", url)
	response, err := http.Get(url)
	if err != nil {
		logUrl.WithError(err).Fatal("Cannot get pod manifest content")
		return nil
	} else {
		if response.StatusCode != 200 {
			logUrl.WithField("status_code", response.StatusCode).WithField("status_message", response.Status).
				Fatal("Receive response error for discovery")
		}
		defer response.Body.Close()
		content, err := ioutil.ReadAll(response.Body)
		if err != nil {
			logUrl.WithError(err).Fatal("Cannot read pod manifest content")
		}
		tmpMap := make(map[string][]cntspec.ACFullname, 1)
		if err := s.podManifestToMap(tmpMap, content); err != nil {
			logUrl.WithError(err).Fatal("Cannot read pod content")
		}
		acis := tmpMap[name.Name()]
		if acis == nil {
			logUrl.Fatal("Discovered pod name does not match requested")
		}
		return acis
	}
}