diff --git a/pkg/gateway/cache/endpoints_trigger.go b/pkg/gateway/cache/endpoints_trigger.go index 23ae462d4..1135f919e 100644 --- a/pkg/gateway/cache/endpoints_trigger.go +++ b/pkg/gateway/cache/endpoints_trigger.go @@ -20,7 +20,7 @@ func (p *EndpointsTrigger) Insert(obj interface{}, cache *GatewayCache) bool { key := utils.ObjectKey(ep) if cache.useEndpointSlices { - return cache.isHeadlessService(key) && cache.isRoutableService(key) + return cache.isHeadlessServiceWithoutSelector(key) && cache.isRoutableService(key) } else { return cache.isRoutableService(key) } @@ -37,7 +37,7 @@ func (p *EndpointsTrigger) Delete(obj interface{}, cache *GatewayCache) bool { key := utils.ObjectKey(ep) if cache.useEndpointSlices { - return cache.isHeadlessService(key) && cache.isRoutableService(key) + return cache.isHeadlessServiceWithoutSelector(key) && cache.isRoutableService(key) } else { return cache.isRoutableService(key) } diff --git a/pkg/gateway/cache/methods.go b/pkg/gateway/cache/methods.go index 14dd52213..dfbc195a8 100644 --- a/pkg/gateway/cache/methods.go +++ b/pkg/gateway/cache/methods.go @@ -3,8 +3,6 @@ package cache import ( "k8s.io/utils/ptr" - "github.com/flomesh-io/fsm/pkg/k8s" - gwtypes "github.com/flomesh-io/fsm/pkg/gateway/types" "github.com/flomesh-io/fsm/pkg/k8s/informers" @@ -370,14 +368,14 @@ func (c *GatewayCache) getServiceFromCache(key client.ObjectKey) (*corev1.Servic return obj, nil } -func (c *GatewayCache) isHeadlessService(key client.ObjectKey) bool { +func (c *GatewayCache) isHeadlessServiceWithoutSelector(key client.ObjectKey) bool { service, err := c.getServiceFromCache(key) if err != nil { log.Error().Msgf("failed to get service from cache: %v", err) return false } - return k8s.IsHeadlessService(*service) + return isHeadlessServiceWithoutSelector(service) } func (c *GatewayCache) isRefToService(referer client.Object, ref gwv1.BackendObjectReference, service client.ObjectKey) bool { diff --git a/pkg/gateway/cache/processor.go b/pkg/gateway/cache/processor.go index 84be789fe..184c06426 100644 --- a/pkg/gateway/cache/processor.go +++ b/pkg/gateway/cache/processor.go @@ -3,8 +3,6 @@ package cache import ( "fmt" - "github.com/flomesh-io/fsm/pkg/k8s" - discoveryv1 "k8s.io/api/discovery/v1" "k8s.io/utils/ptr" @@ -295,7 +293,7 @@ func (c *GatewayProcessor) serviceConfigs() map[string]fgw.ServiceConfig { func (c *GatewayProcessor) calculateEndpoints(svc *corev1.Service, port *int32) map[string]fgw.Endpoint { // If the Service is headless, use the Endpoints to get the list of backends - if k8s.IsHeadlessService(*svc) { + if isHeadlessServiceWithoutSelector(svc) { return c.upstreamsByEndpoints(svc, port) } diff --git a/pkg/gateway/cache/utils.go b/pkg/gateway/cache/utils.go index 1eda30f57..27a028c9a 100644 --- a/pkg/gateway/cache/utils.go +++ b/pkg/gateway/cache/utils.go @@ -4,6 +4,8 @@ import ( "fmt" "reflect" + "github.com/flomesh-io/fsm/pkg/k8s" + gwtypes "github.com/flomesh-io/fsm/pkg/gateway/types" "github.com/flomesh-io/fsm/pkg/utils" @@ -583,3 +585,7 @@ func toFGWEndpoints(endpointSet map[endpointContext]struct{}) map[string]fgw.End return endpoints } + +func isHeadlessServiceWithoutSelector(service *corev1.Service) bool { + return k8s.IsHeadlessService(*service) && len(service.Spec.Selector) == 0 +}