Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,11 @@ func validateDestinationSettings(destinationSettings *ir.DestinationSetting, end
status.RouteReasonUnsupportedAddressType)
}
case resource.KindService, resource.KindServiceImport:
if endpointRoutingDisabled && isHeadlessService(destinationSettings) {
return status.NewRouteStatusError(
fmt.Errorf("service %s is headless Service, please set routingType=Endpoint", destinationSettings.Name),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Errorf("service %s is headless Service, please set routingType=Endpoint", destinationSettings.Name),
fmt.Errorf("service %s is a headless Service, please set routingType=Endpoint", destinationSettings.Name),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status.RouteReasonUnsupportedSetting)
}
if !endpointRoutingDisabled && destinationSettings.AddressType != nil && *destinationSettings.AddressType == ir.MIXED {
return status.NewRouteStatusError(
fmt.Errorf("mixed endpointslice address type for the same backendRef is not supported"),
Expand All @@ -1411,6 +1416,17 @@ func validateDestinationSettings(destinationSettings *ir.DestinationSetting, end
return nil
}

// isHeadlessService reports true when any DestinationEndpoint corresponds to
// a headless Kubernetes Service (ClusterIP="None").
func isHeadlessService(ds *ir.DestinationSetting) bool {
for _, ep := range ds.Endpoints {
if ep.Host == "None" {
return true
}
}
return false
}

func (t *Translator) processServiceImportDestinationSetting(
name string,
backendRef gwapiv1.BackendObjectReference,
Expand Down
98 changes: 98 additions & 0 deletions internal/gatewayapi/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
"github.com/envoyproxy/gateway/internal/gatewayapi/status"
"github.com/envoyproxy/gateway/internal/ir"
)

Expand Down Expand Up @@ -226,3 +229,98 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
})
}
}

func TestValidateDestinationSettings(t *testing.T) {
svcKind := gwapiv1.Kind(resource.KindService)

tests := []struct {
name string
ds *ir.DestinationSetting
endpointRoutingDisabled bool
kind *gwapiv1.Kind
wantErr bool
wantReason gwapiv1.RouteConditionReason
}{
{
name: "headless service rejected when endpointRoutingDisabled=true",
ds: &ir.DestinationSetting{
Name: "headless",
Endpoints: []*ir.DestinationEndpoint{{Host: "None"}},
},
endpointRoutingDisabled: true,
kind: &svcKind,
wantErr: true,
wantReason: status.RouteReasonUnsupportedSetting,
},
{
name: "normal service allowed with ClusterIP routing",
ds: &ir.DestinationSetting{
Name: "normal",
Endpoints: []*ir.DestinationEndpoint{{Host: "10.0.0.1"}},
},
endpointRoutingDisabled: true,
kind: &svcKind,
wantErr: false,
},
{
name: "mixed address type rejected when EndpointSlice routing",
ds: &ir.DestinationSetting{
Name: "mixed",
Endpoints: []*ir.DestinationEndpoint{{Host: "10.0.0.1"}},
AddressType: ptr.To(ir.MIXED),
},
endpointRoutingDisabled: false,
kind: &svcKind,
wantErr: true,
wantReason: status.RouteReasonUnsupportedAddressType,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateDestinationSettings(tt.ds, tt.endpointRoutingDisabled, tt.kind)
if tt.wantErr {
require.Error(t, err)
require.Equal(t, tt.wantReason, err.Reason())
} else {
require.NoError(t, err)
}
})
}
}

func TestIsHeadlessService(t *testing.T) {
tests := []struct {
name string
endpoints []*ir.DestinationEndpoint
want bool
}{
{
name: "headless Service",
endpoints: []*ir.DestinationEndpoint{
{Host: "None"},
},
want: true,
},
{
name: "non headless Service with valid ClusterIP",
endpoints: []*ir.DestinationEndpoint{
{Host: "10.0.0.1"},
},
want: false,
},
{
name: "empty slice",
endpoints: nil,
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ds := &ir.DestinationSetting{Endpoints: tt.endpoints}
got := isHeadlessService(ds)
require.Equal(t, tt.want, got)
})
}
}