1
0
Fork 0

Add HealthCheck for KubernetesCRD ExternalName services

This commit is contained in:
Marc Mognol 2024-05-30 17:18:05 +02:00 committed by GitHub
parent c0a2e6b4b6
commit 7fc56454ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 885 additions and 22 deletions

View file

@ -0,0 +1,22 @@
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: test.route
namespace: default
spec:
entryPoints:
- foo
routes:
- match: Host(`foo.com`) && PathPrefix(`/foo`)
kind: Rule
priority: 12
services:
- name: external-svc
port: 443
healthCheck:
path: /health
interval: 15s

View file

@ -0,0 +1,27 @@
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: test.route
namespace: default
spec:
entryPoints:
- foo
routes:
- match: Host(`foo.com`) && PathPrefix(`/foo`)
kind: Rule
priority: 12
services:
- name: external-svc
port: 443
healthCheck:
path: /health1
interval: 15s
- name: whoami2
port: 443
healthCheck:
path: /health3
interval: 30s

View file

@ -0,0 +1,27 @@
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: test.route
namespace: default
spec:
entryPoints:
- foo
routes:
- match: Host(`foo.com`) && PathPrefix(`/foo`)
kind: Rule
priority: 12
services:
- name: external-svc
port: 443
healthCheck:
path: /health1
interval: 15s
- name: external-svc-with-https
port: 443
healthCheck:
path: /health2
interval: 20s

View file

@ -305,6 +305,7 @@ func (c configBuilder) buildServersLB(namespace string, svc traefikv1alpha1.Load
lb := &dynamic.ServersLoadBalancer{}
lb.SetDefaults()
lb.Servers = servers
lb.HealthCheck = svc.HealthCheck
conf := svc
lb.PassHostHeader = conf.PassHostHeader
@ -380,6 +381,10 @@ func (c configBuilder) loadServers(parentNamespace string, svc traefikv1alpha1.L
}
var servers []dynamic.Server
if service.Spec.Type != corev1.ServiceTypeExternalName && svc.HealthCheck != nil {
return nil, fmt.Errorf("HealthCheck allowed only for ExternalName services: %s/%s", namespace, sanitizedName)
}
if service.Spec.Type == corev1.ServiceTypeExternalName {
if !c.allowExternalNameServices {
return nil, fmt.Errorf("externalName services not allowed: %s/%s", namespace, sanitizedName)

View file

@ -2432,6 +2432,174 @@ func TestLoadIngressRoutes(t *testing.T) {
},
},
},
{
desc: "with one external service and health check",
paths: []string{"services.yml", "with_one_external_service_and_health_check.yml"},
expected: &dynamic.Configuration{
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"default-test-route-77c62dfe9517144aeeaa": {
EntryPoints: []string{"foo"},
Service: "default-test-route-77c62dfe9517144aeeaa",
Rule: "Host(`foo.com`) && PathPrefix(`/foo`)",
Priority: 12,
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"default-test-route-77c62dfe9517144aeeaa": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "https://external.domain:443",
},
},
PassHostHeader: Bool(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
HealthCheck: &dynamic.ServerHealthCheck{
Path: "/health",
Interval: 15000000000,
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{},
},
},
{
desc: "with two external services and health check",
paths: []string{"services.yml", "with_two_external_services_and_health_check.yml"},
expected: &dynamic.Configuration{
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"default-test-route-77c62dfe9517144aeeaa": {
EntryPoints: []string{"foo"},
Service: "default-test-route-77c62dfe9517144aeeaa",
Rule: "Host(`foo.com`) && PathPrefix(`/foo`)",
Priority: 12,
},
},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"default-test-route-77c62dfe9517144aeeaa": {
Weighted: &dynamic.WeightedRoundRobin{
Services: []dynamic.WRRService{
{
Name: "default-external-svc-443",
Weight: func(i int) *int { return &i }(1),
},
{
Name: "default-external-svc-with-https-443",
Weight: func(i int) *int { return &i }(1),
},
},
},
},
"default-external-svc-443": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "https://external.domain:443",
},
},
PassHostHeader: Bool(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
HealthCheck: &dynamic.ServerHealthCheck{
Path: "/health1",
Interval: 15000000000,
},
},
},
"default-external-svc-with-https-443": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "https://external.domain:443",
},
},
PassHostHeader: Bool(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
HealthCheck: &dynamic.ServerHealthCheck{
Path: "/health2",
Interval: 20000000000,
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{},
},
},
{
desc: "with one external service and one regular service and health check",
paths: []string{"services.yml", "with_one_external_svc_and_regular_svc_health_check.yml"},
expected: &dynamic.Configuration{
UDP: &dynamic.UDPConfiguration{
Routers: map[string]*dynamic.UDPRouter{},
Services: map[string]*dynamic.UDPService{},
},
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{},
Middlewares: map[string]*dynamic.TCPMiddleware{},
Services: map[string]*dynamic.TCPService{},
ServersTransports: map[string]*dynamic.TCPServersTransport{},
},
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{},
Middlewares: map[string]*dynamic.Middleware{},
Services: map[string]*dynamic.Service{
"default-external-svc-443": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "https://external.domain:443",
},
},
PassHostHeader: Bool(true),
ResponseForwarding: &dynamic.ResponseForwarding{
FlushInterval: ptypes.Duration(100 * time.Millisecond),
},
HealthCheck: &dynamic.ServerHealthCheck{
Path: "/health1",
Interval: 15000000000,
},
},
},
},
ServersTransports: map[string]*dynamic.ServersTransport{},
},
TLS: &dynamic.TLSConfiguration{},
},
},
{
desc: "services lb, servers lb, and mirror service, all in a wrr with different namespaces",
allowCrossNamespace: true,

View file

@ -131,6 +131,8 @@ type LoadBalancerSpec struct {
// It allows services to be reachable when Traefik runs externally from the Kubernetes cluster but within the same network of the nodes.
// By default, NodePortLB is false.
NodePortLB bool `json:"nodePortLB,omitempty"`
// Healthcheck defines health checks for the service.
HealthCheck *dynamic.ServerHealthCheck `json:"healthCheck,omitempty"`
}
type ResponseForwarding struct {

View file

@ -582,6 +582,11 @@ func (in *LoadBalancerSpec) DeepCopyInto(out *LoadBalancerSpec) {
*out = new(bool)
**out = **in
}
if in.HealthCheck != nil {
in, out := &in.HealthCheck, &out.HealthCheck
*out = new(dynamic.ServerHealthCheck)
(*in).DeepCopyInto(*out)
}
return
}