From 7603592e010069e6cb5925cdbf6ba426c9f26086 Mon Sep 17 00:00:00 2001 From: zirain Date: Thu, 4 Jul 2024 14:15:54 +0800 Subject: [PATCH] support grpc Signed-off-by: zirain --- internal/gatewayapi/route.go | 12 ++++++++---- .../envoyproxy-accesslog-als-json.in.yaml | 2 ++ .../envoyproxy-accesslog-als-json.out.yaml | 4 ++-- .../envoyproxy-accesslog-backend.out.yaml | 2 +- .../testdata/envoyproxy-accesslog.in.yaml | 2 ++ .../testdata/envoyproxy-accesslog.out.yaml | 4 ++-- .../envoyproxy-tracing-backend.out.yaml | 2 +- internal/gatewayapi/translator_test.go | 18 +++++++++++++++--- 8 files changed, 33 insertions(+), 13 deletions(-) diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index 98e998c7f95..c6719015d1c 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -1307,10 +1307,14 @@ func (t *Translator) processServiceDestinationSetting( } } - // support HTTPRouteBackendProtocolH2C - if servicePort.AppProtocol != nil && - *servicePort.AppProtocol == "kubernetes.io/h2c" { - protocol = ir.HTTP2 + // support HTTPRouteBackendProtocolH2C/GRPC + if servicePort.AppProtocol != nil { + switch *servicePort.AppProtocol { + case "kubernetes.io/h2c": + protocol = ir.HTTP2 + case "grpc": + protocol = ir.GRPC + } } // Route to endpoints by default diff --git a/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.in.yaml b/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.in.yaml index 28850133563..4b5caa73f17 100644 --- a/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.in.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.in.yaml @@ -110,6 +110,7 @@ services: ports: - name: grpc port: 9000 + appProtocol: grpc protocol: TCP targetPort: 9000 endpointSlices: @@ -123,6 +124,7 @@ endpointSlices: addressType: IPv4 ports: - name: grpc + appProtocol: grpc protocol: TCP port: 9090 endpoints: diff --git a/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.out.yaml b/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.out.yaml index 109bbcdc48a..8695f47ecfa 100755 --- a/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.out.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-accesslog-als-json.out.yaml @@ -158,7 +158,7 @@ xdsIR: endpoints: - host: 10.240.0.10 port: 9090 - protocol: TCP + protocol: GRPC http: requestHeaders: - x-client-ip-address @@ -178,7 +178,7 @@ xdsIR: endpoints: - host: 10.240.0.10 port: 9090 - protocol: TCP + protocol: GRPC name: envoy-gateway-system/test type: TCP http: diff --git a/internal/gatewayapi/testdata/envoyproxy-accesslog-backend.out.yaml b/internal/gatewayapi/testdata/envoyproxy-accesslog-backend.out.yaml index 8e81d6356d1..94763fd2522 100644 --- a/internal/gatewayapi/testdata/envoyproxy-accesslog-backend.out.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-accesslog-backend.out.yaml @@ -143,7 +143,7 @@ xdsIR: endpoints: - host: 8.7.6.5 port: 4317 - protocol: TCP + protocol: GRPC resources: k8s.cluster.name: cluster-1 text: | diff --git a/internal/gatewayapi/testdata/envoyproxy-accesslog.in.yaml b/internal/gatewayapi/testdata/envoyproxy-accesslog.in.yaml index ea6b29424ca..0d634113f4f 100644 --- a/internal/gatewayapi/testdata/envoyproxy-accesslog.in.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-accesslog.in.yaml @@ -118,6 +118,7 @@ services: ports: - name: grpc port: 9000 + appProtocol: grpc protocol: TCP targetPort: 9000 endpointSlices: @@ -132,6 +133,7 @@ endpointSlices: ports: - name: grpc protocol: TCP + appProtocol: grpc port: 9090 endpoints: - addresses: diff --git a/internal/gatewayapi/testdata/envoyproxy-accesslog.out.yaml b/internal/gatewayapi/testdata/envoyproxy-accesslog.out.yaml index 8a7cc1aec73..9694dd07ad7 100644 --- a/internal/gatewayapi/testdata/envoyproxy-accesslog.out.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-accesslog.out.yaml @@ -163,7 +163,7 @@ xdsIR: endpoints: - host: 10.240.0.10 port: 9090 - protocol: TCP + protocol: GRPC http: requestHeaders: - x-client-ip-address @@ -182,7 +182,7 @@ xdsIR: endpoints: - host: 10.240.0.10 port: 9090 - protocol: TCP + protocol: GRPC name: envoy-gateway-system/test text: | [%START_TIME%] "%REQ(:METHOD)% %PROTOCOL%" %RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION% %RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQ(X-FORWARDED-FOR)%" "%REQ(USER-AGENT)%" "%REQ(X-REQUEST-ID)%" "%REQ(:AUTHORITY)%" "%UPSTREAM_HOST%"\n diff --git a/internal/gatewayapi/testdata/envoyproxy-tracing-backend.out.yaml b/internal/gatewayapi/testdata/envoyproxy-tracing-backend.out.yaml index 2c8753b2777..70e07bd18f2 100644 --- a/internal/gatewayapi/testdata/envoyproxy-tracing-backend.out.yaml +++ b/internal/gatewayapi/testdata/envoyproxy-tracing-backend.out.yaml @@ -150,7 +150,7 @@ xdsIR: endpoints: - host: 8.7.6.5 port: 4317 - protocol: TCP + protocol: GRPC provider: backendRefs: - name: otel-collector diff --git a/internal/gatewayapi/translator_test.go b/internal/gatewayapi/translator_test.go index 062808af580..357f6586bee 100644 --- a/internal/gatewayapi/translator_test.go +++ b/internal/gatewayapi/translator_test.go @@ -239,9 +239,16 @@ func TestTranslate(t *testing.T) { ClusterIP: "3.3.3.3", Ports: []corev1.ServicePort{ { - Name: "grpc", - Port: 4317, - TargetPort: intstr.IntOrString{IntVal: 4317}, + Name: "grpc", + Port: 4317, + TargetPort: intstr.IntOrString{IntVal: 4317}, + Protocol: corev1.ProtocolTCP, + AppProtocol: ptr.To("grpc"), + }, + { + Name: "zipkin", + Port: 9411, + TargetPort: intstr.IntOrString{IntVal: 9411}, Protocol: corev1.ProtocolTCP, }, }, @@ -264,6 +271,11 @@ func TestTranslate(t *testing.T) { Port: ptr.To[int32](4317), Protocol: ptr.To(corev1.ProtocolTCP), }, + { + Name: ptr.To("zipkin"), + Port: ptr.To[int32](9411), + Protocol: ptr.To(corev1.ProtocolTCP), + }, }, Endpoints: []discoveryv1.Endpoint{ {