Extract the port from the service for Kubernetes auto discovery

This commit is contained in:
TwinProduction 2020-11-11 19:47:53 -05:00
parent aeb0fb35d8
commit 7699552c5b
3 changed files with 23 additions and 16 deletions

View File

@ -327,15 +327,15 @@ func TestParseAndValidateConfigBytesWithNoServicesOrAutoDiscovery(t *testing.T)
func TestParseAndValidateConfigBytesWithKubernetesAutoDiscovery(t *testing.T) { func TestParseAndValidateConfigBytesWithKubernetesAutoDiscovery(t *testing.T) {
var kubernetesServices []v1.Service var kubernetesServices []v1.Service
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-1", "default")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-1", "default", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2", "default")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2", "default", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2-canary", "default")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-2-canary", "default", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-3", "kube-system")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-3", "kube-system", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-4", "tools")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-4", "tools", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-5", "tools")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-5", "tools", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-6", "tools")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-6", "tools", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7", "metrics")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7", "metrics", 8080))
kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7-canary", "metrics")) kubernetesServices = append(kubernetesServices, k8stest.CreateTestServices("service-7-canary", "metrics", 8080))
k8stest.InitializeMockedKubernetesClient(kubernetesServices) k8stest.InitializeMockedKubernetesClient(kubernetesServices)
config, err := parseAndValidateConfigBytes([]byte(` config, err := parseAndValidateConfigBytes([]byte(`
debug: true debug: true
@ -391,8 +391,8 @@ kubernetes:
if len(service.Conditions) == 1 && *service.Conditions[0] != "[STATUS] == 200" { 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) 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") { 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/health'", service.Name) t.Errorf("service '%s' should've had an URL with the suffix '.svc.cluster.local:8080/health'", service.Name)
} }
} }
} }

View File

@ -31,11 +31,16 @@ func DiscoverServices(kubernetesConfig *Config) ([]*core.Service, error) {
continue skipExcluded 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, "/") { 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 { } 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{ services = append(services, &core.Service{
Name: service.Name, Name: service.Name,

View File

@ -42,12 +42,14 @@ func InitializeMockedKubernetesClient(services []v1.Service) {
} }
// CreateTestServices creates a mocked service for testing purposes // 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{ return v1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
Namespace: namespace, Namespace: namespace,
}, },
Spec: v1.ServiceSpec{}, Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{{Name: "http", Protocol: v1.ProtocolTCP, Port: port}},
},
} }
} }