Skip to content

Commit

Permalink
feat: for k8s version >= 1.21, only use endpoints for headless servic…
Browse files Browse the repository at this point in the history
…e without selector (#323)

Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh authored Aug 7, 2024
1 parent 27a65f0 commit 0587e5b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/gateway/cache/endpoints_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/gateway/cache/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions pkg/gateway/cache/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/gateway/cache/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 0587e5b

Please sign in to comment.