Пример #1
0
// buildApplicationEndpoint converts a ServiceEndpoint to an ApplicationEndpoint
func buildApplicationEndpoint(state *servicestate.ServiceState, endpoint *service.ServiceEndpoint) (dao.ApplicationEndpoint, error) {
	var ae dao.ApplicationEndpoint

	ae.ServiceID = state.ServiceID
	ae.Application = endpoint.Application
	ae.Protocol = endpoint.Protocol
	ae.ContainerIP = state.PrivateIP
	if endpoint.PortTemplate != "" {
		// Evaluate the PortTemplate field and use it for the port
		t := template.Must(template.New("PortTemplate").Funcs(funcmap).Parse(endpoint.PortTemplate))
		b := bytes.Buffer{}
		err := t.Execute(&b, state)
		if err == nil {
			i, err := strconv.Atoi(b.String())
			if err != nil {
				glog.Errorf("%+v", err)
			} else {
				ae.ContainerPort = uint16(i)
			}
		}
	} else {
		// No dynamic port, just use the specified PortNumber
		ae.ContainerPort = endpoint.PortNumber
	}
	ae.HostIP = state.HostIP
	if len(state.PortMapping) > 0 {
		pmKey := fmt.Sprintf("%d/%s", ae.ContainerPort, ae.Protocol)
		pm := state.PortMapping[pmKey]
		if len(pm) > 0 {
			port, err := strconv.Atoi(pm[0].HostPort)
			if err != nil {
				glog.Errorf("Unable to interpret HostPort: %s", pm[0].HostPort)
				return ae, err
			}
			ae.HostPort = uint16(port)
		}
	}
	ae.VirtualAddress = endpoint.VirtualAddress
	ae.InstanceID = state.InstanceID

	glog.V(2).Infof("  built ApplicationEndpoint: %+v", ae)

	return ae, nil
}