Ejemplo n.º 1
0
// attempt to convert the slave hostname to an IP4 address. if that fails, then
// sanitize the hostname for DNS compat.
func sanitizedSlaveAddress(hostname string) string {
	address, ok := hostToIP4(hostname)
	if !ok {
		address = labels.AsDomainFrag(address)
	}
	return address
}
Ejemplo n.º 2
0
// InsertState transforms a StateJSON into RecordGenerator RRs
func (rg *RecordGenerator) InsertState(sj StateJSON, domain string, ns string,
	listener string, masters []string, spec labels.HostNameSpec) error {

	// creates a map with slave IP addresses (IPv4)
	rg.SlaveIPs = make(map[string]string)
	for _, slave := range sj.Slaves {
		rg.SlaveIPs[slave.Id] = sanitizedSlaveAddress(slave.Hostname, spec)
	}

	rg.SRVs = make(rrs)
	rg.As = make(rrs)

	// complete crap - refactor me
	for _, f := range sj.Frameworks {
		fname := labels.AsDomainFrag(f.Name, spec)
		for _, task := range f.Tasks {
			hostIP, ok := rg.SlaveIPs[task.SlaveId]
			// skip not running or not discoverable tasks
			if !ok || (task.State != "TASK_RUNNING") {
				continue
			}

			tname := spec.Mangle(task.Name)
			sid := slaveIdTail(task.SlaveId)
			tag := hashString(task.Id)
			tail := fname + "." + domain + "."

			// A records for task and task-sid
			arec := tname + "." + tail
			rg.insertRR(arec, hostIP, "A")
			trec := tname + "-" + tag + "-" + sid + "." + tail
			rg.insertRR(trec, hostIP, "A")

			// A records with container IP
			if containerIP := task.containerIP(); containerIP != "" {
				rg.insertRR("_container."+arec, containerIP, "A")
				rg.insertRR("_container."+trec, containerIP, "A")
			}

			// SRV records
			if task.Resources.Ports != "" {
				ports := yankPorts(task.Resources.Ports)
				for _, port := range ports {
					var srvhost string = trec + ":" + port
					tcp := "_" + tname + "._tcp." + tail
					udp := "_" + tname + "._udp." + tail
					rg.insertRR(tcp, srvhost, "SRV")
					rg.insertRR(udp, srvhost, "SRV")
				}
			}
		}
	}

	rg.listenerRecord(listener, ns)
	rg.masterRecord(domain, masters, sj.Leader)
	return nil
}