diff --git a/config/config_test.go b/config/config_test.go index f06fb9da..8e97c87d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -327,15 +327,15 @@ func TestParseAndValidateConfigBytesWithNoServicesOrAutoDiscovery(t *testing.T) func TestParseAndValidateConfigBytesWithKubernetesAutoDiscovery(t *testing.T) { var kubernetesServices []v1.Service - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-1", "default")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2", "default")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2-canary", "default")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-3", "kube-system")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-4", "tools")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-5", "tools")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-6", "tools")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7", "metrics")) - kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7-canary", "metrics")) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-1", "default", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2", "default", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2-canary", "default", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-3", "kube-system", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-4", "tools", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-5", "tools", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-6", "tools", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7", "metrics", 8080)) + kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7-canary", "metrics", 8080)) k8stest.InitializeMockedKubernetesClient(kubernetesServices) config, err := parseAndValidateConfigBytes([]byte(` debug: true @@ -391,8 +391,8 @@ kubernetes: if len(service.Conditions) == 1 && *service.Conditions[0] != "[STATUS] == 200" { t.Errorf("service '%s' should've had the condition '[STATUS] == 200', because the template is configured for it", service.Name) } - if !strings.HasSuffix(service.URL, ".svc.cluster.local/health") { - t.Errorf("service '%s' should've had an URL with the suffix '.svc.cluster.local/health'", service.Name) + if !strings.HasSuffix(service.URL, ".svc.cluster.local:8080/health") { + t.Errorf("service '%s' should've had an URL with the suffix '.svc.cluster.local:8080/health'", service.Name) } } } diff --git a/k8s/discovery.go b/k8s/discovery.go index 113ca2e3..e168c952 100644 --- a/k8s/discovery.go +++ b/k8s/discovery.go @@ -31,11 +31,16 @@ func DiscoverServices(kubernetesConfig *Config) ([]*core.Service, error) { continue skipExcluded } } - var url string + // XXX: try to extract health from liveness probe endpoint? + var url, port string + if len(service.Spec.Ports) > 0 && !strings.Contains(ns.HostnameSuffix, ":") && strings.HasSuffix(ns.HostnameSuffix, ".svc.cluster.local") { + port = fmt.Sprintf(":%d", service.Spec.Ports[0].Port) + } + // If the path starts with a / or starts with a port if strings.HasPrefix(ns.TargetPath, "/") { - url = fmt.Sprintf("http://%s%s%s", service.Name, ns.HostnameSuffix, ns.TargetPath) + url = fmt.Sprintf("http://%s%s%s%s", service.Name, ns.HostnameSuffix, port, ns.TargetPath) } else { - url = fmt.Sprintf("http://%s%s/%s", service.Name, ns.HostnameSuffix, ns.TargetPath) + url = fmt.Sprintf("http://%s%s%s/%s", service.Name, ns.HostnameSuffix, port, ns.TargetPath) } services = append(services, &core.Service{ Name: service.Name, diff --git a/k8stest/k8stest.go b/k8stest/k8stest.go index 4d8d0182..90a974f0 100644 --- a/k8stest/k8stest.go +++ b/k8stest/k8stest.go @@ -42,12 +42,14 @@ func InitializeMockedKubernetesClient(services []v1.Service) { } // CreateTestServices creates a mocked service for testing purposes -func CreateTestServices(name, namespace string) v1.Service { +func CreateTestServices(name, namespace string, port int32) v1.Service { return v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, }, - Spec: v1.ServiceSpec{}, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{{Name: "http", Protocol: v1.ProtocolTCP, Port: port}}, + }, } }