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 (#324)

Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh authored Aug 7, 2024
1 parent e3cde37 commit 4f28448
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
12 changes: 6 additions & 6 deletions pkg/gateway/processor/mock_processor_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/gateway/processor/triggers/k8s/endpoints_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (p *EndpointsTrigger) Insert(obj interface{}, processor processor.Processor
key := client.ObjectKeyFromObject(ep)

if processor.UseEndpointSlices() {
return processor.IsHeadlessService(key) && processor.IsRoutableService(key)
return processor.IsHeadlessServiceWithoutSelector(key) && processor.IsRoutableService(key)
} else {
return processor.IsRoutableService(key)
}
Expand All @@ -38,7 +38,7 @@ func (p *EndpointsTrigger) Delete(obj interface{}, processor processor.Processor
key := client.ObjectKeyFromObject(ep)

if processor.UseEndpointSlices() {
return processor.IsHeadlessService(key) && processor.IsRoutableService(key)
return processor.IsHeadlessServiceWithoutSelector(key) && processor.IsRoutableService(key)
} else {
return processor.IsRoutableService(key)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Processor interface {
BuildConfigs()
IsEffectiveRoute(parentRefs []gwv1.ParentReference) bool
IsRoutableService(service client.ObjectKey) bool
IsHeadlessService(key client.ObjectKey) bool
IsHeadlessServiceWithoutSelector(key client.ObjectKey) bool
IsEffectiveTargetRef(policy client.Object, targetRef gwv1alpha2.NamespacedPolicyTargetReference) bool
IsRoutableTargetService(policy client.Object, targetRef gwv1alpha2.NamespacedPolicyTargetReference) bool
IsRoutableNamespacedTargetServices(policy client.Object, targetRefs []gwv1alpha2.NamespacedPolicyTargetReference) bool
Expand Down
6 changes: 2 additions & 4 deletions pkg/gateway/processor/v2/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/flomesh-io/fsm/pkg/k8s"
)

func (c *ConfigGenerator) processBackends() []interface{} {
Expand Down Expand Up @@ -49,8 +47,8 @@ func (c *ConfigGenerator) processBackends() []interface{} {
}

func (c *ConfigGenerator) calculateEndpoints(svc *corev1.Service, port *int32) []fgwv2.BackendTarget {
// If the Service is headless, use the Endpoints to get the list of backends
if k8s.IsHeadlessService(*svc) {
// If the Service is headless and has no selector, use Endpoints to get the list of backends
if isHeadlessServiceWithoutSelector(svc) {
return c.upstreamsByEndpoints(svc, port)
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/gateway/processor/v2/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

corev1 "k8s.io/api/core/v1"

"github.com/flomesh-io/fsm/pkg/k8s"

"k8s.io/apimachinery/pkg/types"

"k8s.io/apimachinery/pkg/fields"
Expand Down Expand Up @@ -287,14 +285,14 @@ func (c *GatewayProcessor) isFilterReferredByGRPCRoute(filter client.ObjectKey)
return len(list.Items) > 0
}

func (c *GatewayProcessor) IsHeadlessService(key client.ObjectKey) bool {
func (c *GatewayProcessor) IsHeadlessServiceWithoutSelector(key client.ObjectKey) bool {
service, err := c.getServiceFromCache(key)
if err != nil {
log.Error().Msgf("failed to get service from processor: %v", err)
return false
}

return k8s.IsHeadlessService(*service)
return isHeadlessServiceWithoutSelector(service)
}

func (c *GatewayProcessor) getServiceFromCache(key client.ObjectKey) (*corev1.Service, error) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/gateway/processor/v2/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package v2

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"

"github.com/flomesh-io/fsm/pkg/k8s"

fgwv2 "github.com/flomesh-io/fsm/pkg/gateway/fgw"

gwv1 "sigs.k8s.io/gateway-api/apis/v1"
Expand All @@ -28,3 +31,7 @@ func backendWeight(bk gwv1.BackendRef) int32 {

return 1
}

func isHeadlessServiceWithoutSelector(service *corev1.Service) bool {
return k8s.IsHeadlessService(*service) && len(service.Spec.Selector) == 0
}

0 comments on commit 4f28448

Please sign in to comment.