diff --git a/rancher/service.go b/rancher/service.go index 91a4beb6..4eb15f36 100644 --- a/rancher/service.go +++ b/rancher/service.go @@ -364,11 +364,11 @@ func (r *RancherService) getLbLinks() ([]interface{}, error) { func (r *RancherService) getLbLinkPorts(name string) ([]string, error) { labelName := "io.rancher.loadbalancer.target." + name v := r.serviceConfig.Labels.MapParts()[labelName] - if len(v) == 0 { + if v == "" { if len(r.serviceConfig.Ports) != 1 { return nil, fmt.Errorf("Failed to find target ports for %s, add label %s", name, labelName) } - parts := strings.SplitN(r.serviceConfig.Ports[0], ",", 2) + parts := TrimSplit(TrimSplit(r.serviceConfig.Ports[0], "/", 2)[0], ":", 2) if len(parts) == 2 { return []string{parts[1]}, nil } else { @@ -376,7 +376,7 @@ func (r *RancherService) getLbLinkPorts(name string) ([]string, error) { } } - return strings.Split(v, ","), nil + return TrimSplit(v, ",", -1), nil } func (r *RancherService) getServiceLinks() ([]interface{}, error) { @@ -710,3 +710,12 @@ func (r *RancherService) DependentServices() []project.ServiceRelationship { func (r *RancherService) Pull() error { return nil } + +func TrimSplit(str, sep string, count int) []string { + result := []string{} + for _, i := range strings.SplitN(strings.TrimSpace(str), sep, count) { + result = append(result, strings.TrimSpace(i)) + } + + return result +} diff --git a/tests/integration/cattletest/core/test_compose.py b/tests/integration/cattletest/core/test_compose.py index 174bb01d..6a0f8634 100644 --- a/tests/integration/cattletest/core/test_compose.py +++ b/tests/integration/cattletest/core/test_compose.py @@ -379,8 +379,129 @@ def test_lb(client, compose): project = find_one(client.list_environment, name=project_name) assert len(project.services()) == 3 lb = _get_service(project.services(), 'lb') - _get_service(project.services(), 'web') - _get_service(project.services(), 'web2') + web = _get_service(project.services(), 'web') + web2 = _get_service(project.services(), 'web2') + + maps = client.list_service_consume_map(serviceId=lb.id) + assert len(maps) == 2 + + for map in maps: + if map.consumedServiceId == web.id: + assert map.ports == ['80'] + elif map.consumedServiceId == web2.id: + assert map.ports == ['80'] + else: + assert False + + assert lb.type == 'loadBalancerService' + + +def test_lb_default_port_http(client, compose): + template = ''' + lb: + image: rancher/load-balancer-service + ports: + - 7900:80/tcp + links: + - web + web: + image: nginx + ''' + + project_name = create_project(compose, input=template) + + project = find_one(client.list_environment, name=project_name) + assert len(project.services()) == 2 + lb = _get_service(project.services(), 'lb') + web = _get_service(project.services(), 'web') + assert lb.launchConfig.ports == ['7900:80/tcp'] + + map = find_one(client.list_service_consume_map, serviceId=lb.id) + assert map.consumedServiceId == web.id + assert map.ports == ['80'] + + +def test_lb_default_port_with_mapped_tcp(client, compose): + template = ''' + lb: + image: rancher/load-balancer-service + ports: + - 80:8080/tcp + links: + - web + web: + image: nginx + ''' + + project_name = create_project(compose, input=template) + + project = find_one(client.list_environment, name=project_name) + assert len(project.services()) == 2 + lb = _get_service(project.services(), 'lb') + assert lb.launchConfig.ports == ['80:8080/tcp'] + + web = _get_service(project.services(), 'web') + + map = find_one(client.list_service_consume_map, serviceId=lb.id) + assert map.consumedServiceId == web.id + assert map.ports == ['8080'] + + +def test_lb_default_port_with_tcp(client, compose): + template = ''' + lb: + image: rancher/load-balancer-service + ports: + - 80/tcp + links: + - web + web: + image: nginx + ''' + + project_name = create_project(compose, input=template) + + project = find_one(client.list_environment, name=project_name) + assert len(project.services()) == 2 + lb = _get_service(project.services(), 'lb') + web = _get_service(project.services(), 'web') + + map = find_one(client.list_service_consume_map, serviceId=lb.id) + assert map.consumedServiceId == web.id + assert map.ports == ['80'] + + +def test_lb_path_space_target(client, compose): + template = ''' + lb: + image: rancher/load-balancer-service + ports: + - 80:8080 + labels: + io.rancher.loadbalancer.target.web: "6000:hostname/path, + 7000" + links: + - web + web: + image: nginx + ''' + + project_name = create_project(compose, input=template) + + project = find_one(client.list_environment, name=project_name) + assert len(project.services()) == 2 + lb = _get_service(project.services(), 'lb') + web = _get_service(project.services(), 'web') + + maps = client.list_service_consume_map(serviceId=lb.id) + assert len(maps) == 1 + + for map in maps: + if map.consumedServiceId == web.id: + assert map.ports == ['6000:hostname/path', + '7000'] + else: + assert False assert lb.type == 'loadBalancerService'