func TestAddControlPlaneEndpoints(t *testing.T) { agent := &HostAgent{} agent.master = "127.0.0.1:0" agent.uiport = ":443" endpoints := make(map[string][]dao.ApplicationEndpoint) consumer_endpoint := dao.ApplicationEndpoint{} consumer_endpoint.ServiceID = "controlplane_consumer" consumer_endpoint.Application = "controlplane_consumer" consumer_endpoint.ContainerIP = "127.0.0.1" consumer_endpoint.ContainerPort = 8443 consumer_endpoint.ProxyPort = 8444 consumer_endpoint.HostPort = 8443 consumer_endpoint.HostIP = "127.0.0.1" consumer_endpoint.Protocol = "tcp" controlplane_endpoint := dao.ApplicationEndpoint{} controlplane_endpoint.ServiceID = "controlplane" controlplane_endpoint.Application = "controlplane" controlplane_endpoint.ContainerIP = "127.0.0.1" controlplane_endpoint.ContainerPort = 443 controlplane_endpoint.ProxyPort = 443 controlplane_endpoint.HostPort = 443 controlplane_endpoint.HostIP = "127.0.0.1" controlplane_endpoint.Protocol = "tcp" agent.addControlPlaneEndpoint(endpoints) agent.addControlPlaneConsumerEndpoint(endpoints) if _, ok := endpoints["tcp:8444"]; !ok { t.Fatalf(" mapping failed missing key[\"tcp:8444\"]") } if _, ok := endpoints["tcp:443"]; !ok { t.Fatalf(" mapping failed missing key[\"tcp:443\"]") } if len(endpoints["tcp:8444"]) != 1 { t.Fatalf(" mapping failed len(\"tcp:8444\"])=%d expected 1", len(endpoints["tcp:8444"])) } if len(endpoints["tcp:443"]) != 1 { t.Fatalf(" mapping failed len(\"tcp:443\"])=%d expected 1", len(endpoints["tcp:443"])) } if endpoints["tcp:8444"][0] != consumer_endpoint { t.Fatalf(" mapping failed %+v expected %+v", endpoints["tcp:8444"][0], consumer_endpoint) } if endpoints["tcp:443"][0] != controlplane_endpoint { t.Fatalf(" mapping failed %+v expected %+v", endpoints["tcp:443"][0], controlplane_endpoint) } }
// 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 }
// addControlPlaneConsumerEndpoint adds an application endpoint mapping for the master control center api func (a *HostAgent) addControlPlaneConsumerEndpoint(endpoints map[string][]dao.ApplicationEndpoint) { key := "tcp:8444" endpoint := dao.ApplicationEndpoint{} endpoint.ServiceID = "controlplane_consumer" endpoint.Application = "controlplane_consumer" endpoint.ContainerIP = "127.0.0.1" endpoint.ContainerPort = 8443 endpoint.ProxyPort = 8444 endpoint.HostPort = 8443 endpoint.HostIP = strings.Split(a.master, ":")[0] endpoint.Protocol = "tcp" a.addEndpoint(key, endpoint, endpoints) }
// addControlPlaneEndpoint adds an application endpoint mapping for the master control center api func (a *HostAgent) addControlPlaneEndpoint(endpoints map[string][]dao.ApplicationEndpoint) { key := "tcp" + a.uiport endpoint := dao.ApplicationEndpoint{} endpoint.ServiceID = "controlplane" endpoint.Application = "controlplane" endpoint.ContainerIP = "127.0.0.1" port, err := strconv.Atoi(a.uiport[1:]) if err != nil { glog.Errorf("Unable to interpret ui port.") return } endpoint.ContainerPort = uint16(port) endpoint.ProxyPort = uint16(port) endpoint.HostPort = uint16(port) endpoint.HostIP = strings.Split(a.master, ":")[0] endpoint.Protocol = "tcp" a.addEndpoint(key, endpoint, endpoints) }