mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 23:43:27 +01:00
Extract the port from the service for Kubernetes auto discovery
This commit is contained in:
parent
aeb0fb35d8
commit
7699552c5b
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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}},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user