Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg committed Sep 19, 2023
1 parent dd2aed5 commit a3ffab3
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 139 deletions.
14 changes: 8 additions & 6 deletions internal/cmd/egctl/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ func translateGatewayAPIToGatewayAPI(resources *gatewayapi.Resources) (gatewayap

// Translate from Gateway API to Xds IR
gTranslator := &gatewayapi.Translator{
GatewayControllerName: egv1alpha1.GatewayControllerName,
GatewayClassName: v1beta1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: true,
GatewayControllerName: egv1alpha1.GatewayControllerName,
GatewayClassName: v1beta1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: true,
EndpointRoutingDisabled: true,
}
gRes := gTranslator.Translate(resources)
// Update the status of the GatewayClass based on EnvoyProxy validation
Expand Down Expand Up @@ -292,9 +293,10 @@ func translateGatewayAPIToXds(dnsDomain string, resourceType string, resources *

// Translate from Gateway API to Xds IR
gTranslator := &gatewayapi.Translator{
GatewayControllerName: egv1alpha1.GatewayControllerName,
GatewayClassName: v1beta1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: true,
GatewayControllerName: egv1alpha1.GatewayControllerName,
GatewayClassName: v1beta1.ObjectName(resources.GatewayClass.Name),
GlobalRateLimitEnabled: true,
EndpointRoutingDisabled: true,
}
gRes := gTranslator.Translate(resources)

Expand Down
28 changes: 0 additions & 28 deletions internal/gatewayapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ func SectionNamePtr(name string) *v1beta1.SectionName {
return &sectionName
}

func TLSModeTypePtr(mode v1beta1.TLSModeType) *v1beta1.TLSModeType {
return &mode
}

func BoolPtr(val bool) *bool {
return &val
}

func StringPtr(val string) *string {
return &val
}

func Int32Ptr(val int32) *int32 {
return &val
}

func PortNumPtr(val int32) *v1beta1.PortNumber {
portNum := v1beta1.PortNumber(val)
return &portNum
Expand All @@ -83,18 +67,6 @@ func ObjectNamePtr(val string) *v1alpha2.ObjectName {
return &objectName
}

func PathMatchTypePtr(pType v1beta1.PathMatchType) *v1beta1.PathMatchType {
return &pType
}

func GatewayAddressTypePtr(addr v1beta1.AddressType) *v1beta1.AddressType {
return &addr
}

func ProtocolPtr(val v1.Protocol) *v1.Protocol {
return &val
}

func PathMatchTypeDerefOr(matchType *v1beta1.PathMatchType, defaultType v1beta1.PathMatchType) v1beta1.PathMatchType {
if matchType != nil {
return *matchType
Expand Down
98 changes: 57 additions & 41 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/utils/ptr"
)

var (
Expand Down Expand Up @@ -227,12 +228,12 @@ func (t *Translator) processHTTPRouteRule(httpRoute *HTTPRouteContext, ruleIdx i
case v1beta1.HeaderMatchExact:
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: string(headerMatch.Name),
Exact: StringPtr(headerMatch.Value),
Exact: ptr.To(headerMatch.Value),
})
case v1beta1.HeaderMatchRegularExpression:
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: string(headerMatch.Name),
SafeRegex: StringPtr(headerMatch.Value),
SafeRegex: ptr.To(headerMatch.Value),
})
}
}
Expand All @@ -241,20 +242,20 @@ func (t *Translator) processHTTPRouteRule(httpRoute *HTTPRouteContext, ruleIdx i
case v1beta1.QueryParamMatchExact:
irRoute.QueryParamMatches = append(irRoute.QueryParamMatches, &ir.StringMatch{
Name: string(queryParamMatch.Name),
Exact: StringPtr(queryParamMatch.Value),
Exact: ptr.To(queryParamMatch.Value),
})
case v1beta1.QueryParamMatchRegularExpression:
irRoute.QueryParamMatches = append(irRoute.QueryParamMatches, &ir.StringMatch{
Name: string(queryParamMatch.Name),
SafeRegex: StringPtr(queryParamMatch.Value),
SafeRegex: ptr.To(queryParamMatch.Value),
})
}
}

if match.Method != nil {
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: ":method",
Exact: StringPtr(string(*match.Method)),
Exact: ptr.To(string(*match.Method)),
})
}
applyHTTPFiltersContextToIRRoute(httpFiltersContext, irRoute)
Expand Down Expand Up @@ -425,12 +426,12 @@ func (t *Translator) processGRPCRouteRule(grpcRoute *GRPCRouteContext, ruleIdx i
case v1beta1.HeaderMatchExact:
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: string(headerMatch.Name),
Exact: StringPtr(headerMatch.Value),
Exact: ptr.To(headerMatch.Value),
})
case v1beta1.HeaderMatchRegularExpression:
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: string(headerMatch.Name),
SafeRegex: StringPtr(headerMatch.Value),
SafeRegex: ptr.To(headerMatch.Value),
})
}
}
Expand All @@ -455,17 +456,17 @@ func (t *Translator) processGRPCRouteMethodExact(method *v1alpha2.GRPCMethodMatc
switch {
case method.Service != nil && method.Method != nil:
irRoute.PathMatch = &ir.StringMatch{
Exact: StringPtr(fmt.Sprintf("/%s/%s", *method.Service, *method.Method)),
Exact: ptr.To(fmt.Sprintf("/%s/%s", *method.Service, *method.Method)),
}
case method.Method != nil:
// Use a header match since the PathMatch doesn't support Suffix matching
irRoute.HeaderMatches = append(irRoute.HeaderMatches, &ir.StringMatch{
Name: ":path",
Suffix: StringPtr(fmt.Sprintf("/%s", *method.Method)),
Suffix: ptr.To(fmt.Sprintf("/%s", *method.Method)),
})
case method.Service != nil:
irRoute.PathMatch = &ir.StringMatch{
Prefix: StringPtr(fmt.Sprintf("/%s", *method.Service)),
Prefix: ptr.To(fmt.Sprintf("/%s", *method.Service)),
}
}
}
Expand All @@ -474,15 +475,15 @@ func (t *Translator) processGRPCRouteMethodRegularExpression(method *v1alpha2.GR
switch {
case method.Service != nil && method.Method != nil:
irRoute.PathMatch = &ir.StringMatch{
SafeRegex: StringPtr(fmt.Sprintf("/%s/%s", *method.Service, *method.Method)),
SafeRegex: ptr.To(fmt.Sprintf("/%s/%s", *method.Service, *method.Method)),
}
case method.Method != nil:
irRoute.PathMatch = &ir.StringMatch{
SafeRegex: StringPtr(fmt.Sprintf("/%s/%s", validServiceName, *method.Method)),
SafeRegex: ptr.To(fmt.Sprintf("/%s/%s", validServiceName, *method.Method)),
}
case method.Service != nil:
irRoute.PathMatch = &ir.StringMatch{
SafeRegex: StringPtr(fmt.Sprintf("/%s/%s", *method.Service, validMethodName)),
SafeRegex: ptr.To(fmt.Sprintf("/%s/%s", *method.Service, validMethodName)),
}
}
}
Expand Down Expand Up @@ -1000,7 +1001,6 @@ func (t *Translator) processDestEndpoints(backendRef v1beta1.BackendRef,
}
endpoints = append(endpoints, ep)
}
return endpoints, weight
case KindService:
service := resources.GetService(backendNamespace, string(backendRef.Name))
var servicePort corev1.ServicePort
Expand All @@ -1011,40 +1011,56 @@ func (t *Translator) processDestEndpoints(backendRef v1beta1.BackendRef,
}
}

endpointSlices := resources.GetEndpointSlicesForService(backendNamespace, string(backendRef.Name))

for _, endpointSlice := range endpointSlices {
for _, endpoint := range endpointSlice.Endpoints {
for _, endpointPort := range endpointSlice.Ports {
// Check if the endpoint port matches the service port
// and if endpoint is Ready
if *endpointPort.Port == servicePort.Port &&
*endpointPort.Protocol == servicePort.Protocol &&
*endpoint.Conditions.Ready {
for _, address := range endpoint.Addresses {
var ep *ir.DestinationEndpoint
// Weights are not relevant for TCP and UDP Routes
if routeType == KindTCPRoute ||
routeType == KindUDPRoute {
ep = ir.NewDestEndpoint(
address,
uint32(servicePort.TargetPort.IntVal))
} else {
ep = ir.NewDestEndpointWithWeight(
address,
uint32(servicePort.TargetPort.IntVal),
weight)
// Route to endpoints by default
if !t.EndpointRoutingDisabled {
endpointSlices := resources.GetEndpointSlicesForService(backendNamespace, string(backendRef.Name))

for _, endpointSlice := range endpointSlices {
for _, endpoint := range endpointSlice.Endpoints {
for _, endpointPort := range endpointSlice.Ports {
// Check if the endpoint port matches the service port
// and if endpoint is Ready
if *endpointPort.Port == servicePort.Port &&
*endpointPort.Protocol == servicePort.Protocol &&
*endpoint.Conditions.Ready {
for _, address := range endpoint.Addresses {
var ep *ir.DestinationEndpoint
// Weights are not relevant for TCP and UDP Routes
if routeType == KindTCPRoute ||
routeType == KindUDPRoute {
ep = ir.NewDestEndpoint(
address,
uint32(servicePort.TargetPort.IntVal))
} else {
ep = ir.NewDestEndpointWithWeight(
address,
uint32(servicePort.TargetPort.IntVal),
weight)
}
endpoints = append(endpoints, ep)
}
endpoints = append(endpoints, ep)
}
}
}
}
} else {
// Fall back to Service CluserIP routing
var ep *ir.DestinationEndpoint
if routeType == KindTCPRoute ||
routeType == KindUDPRoute {
ep = ir.NewDestEndpoint(
service.Spec.ClusterIP,
uint32(*backendRef.Port))
} else {
ep = ir.NewDestEndpointWithWeight(
service.Spec.ClusterIP,
uint32(*backendRef.Port),
weight)
}
endpoints = append(endpoints, ep)
}
return endpoints, weight
}

return nil, 0 // shouldnt reach here
return endpoints, weight
}

// processAllowedListenersForParentRefs finds out if the route attaches to one of our
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ endpointSlices:
kind: EndpointSlice
metadata:
name: endpointslice-service-1
namespace: envoy-gateway
namespace: envoy-gateway
labels:
kubernetes.io/service-name: service-1
kubernetes.io/service-name: service-1
addressType: IPv4
ports:
- name: http
Expand All @@ -59,4 +59,4 @@ endpointSlices:
- addresses:
- "7.7.7.7"
conditions:
ready: true
ready: true
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ endpointSlices:
kind: EndpointSlice
metadata:
name: endpointslice-service-1
namespace: backends
namespace: backends
labels:
kubernetes.io/service-name: service-1
kubernetes.io/service-name: service-1
addressType: IPv4
ports:
- name: http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ services:
namespace: envoy-gateway
name: service-2
spec:
clusterIP: 2.2.2.2
clusterIP: 2.2.2.2
ports:
- port: 8080
protocol: TCP
Expand All @@ -155,9 +155,9 @@ endpointSlices:
kind: EndpointSlice
metadata:
name: endpointslice-service-1
namespace: envoy-gateway
namespace: envoy-gateway
labels:
kubernetes.io/service-name: service-1
kubernetes.io/service-name: service-1
addressType: IPv4
ports:
- name: http
Expand All @@ -172,7 +172,7 @@ endpointSlices:
kind: EndpointSlice
metadata:
name: endpointslice-service-2
namespace: envoy-gateway
namespace: envoy-gateway
labels:
kubernetes.io/service-name: service-2
addressType: IPv4
Expand All @@ -184,4 +184,4 @@ endpointSlices:
- addresses:
- "8.8.8.8"
conditions:
ready: true
ready: true
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ endpointSlices:
name: endpointslice-service-1
namespace: test-service-namespace
labels:
kubernetes.io/service-name: service-1
kubernetes.io/service-name: service-1
addressType: IPv4
ports:
- name: http
Expand Down
5 changes: 5 additions & 0 deletions internal/gatewayapi/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ type Translator struct {
// ratelimiting has been configured by the admin.
GlobalRateLimitEnabled bool

// EndpointRoutingDisabled can be set to true to use
// the Service Cluser IP for routing to the backend
// instead.
EndpointRoutingDisabled bool

// ExtensionGroupKinds stores the group/kind for all resources
// introduced by an Extension so that the translator can
// store referenced resources in the IR for later use.
Expand Down
Loading

0 comments on commit a3ffab3

Please sign in to comment.