You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by jo...@apache.org on 2023/10/23 09:22:58 UTC
[shenyu] branch master updated: [type:feat]:init ingress controller grpc (#5240)
This is an automated email from the ASF dual-hosted git repository.
jooks pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 16f6fb164a [type:feat]:init ingress controller grpc (#5240)
16f6fb164a is described below
commit 16f6fb164a99234161d81b8914b0ff2c8e1714d0
Author: Runqi Zhao <40...@users.noreply.github.com>
AuthorDate: Mon Oct 23 17:22:50 2023 +0800
[type:feat]:init ingress controller grpc (#5240)
* [type:feat]:init ingress controller grpc
* [type:feat]:init ingress controller grpc
---
.github/workflows/integrated-test-k8s-ingress.yml | 1 +
.../shenyu-examples-grpc/k8s/ingress.yml | 61 ++++
.../k8s/shenyu-examples-grpc.yml | 89 +++++
shenyu-integrated-test/pom.xml | 1 +
.../Dockerfile | 29 ++
.../deploy/deploy-shenyu.yaml | 103 ++++++
.../deploy/kind-config.yaml | 36 ++
.../pom.xml | 117 ++++++
.../script/build_k8s_cluster.sh | 23 ++
.../script/healthcheck.sh | 36 ++
.../script/services.list | 18 +
.../GrpcIngressControllerIntegratedBootstrap.java | 36 ++
.../src/main/resources/application-local.yml | 71 ++++
.../src/main/resources/application.yml | 18 +
.../test/k8s/ingress/grpc/GrpcPluginTest.java | 58 +++
.../apache/shenyu/k8s/common/IngressConstants.java | 28 ++
.../org/apache/shenyu/k8s/parser/GrpcParser.java | 398 +++++++++++++++++++++
.../apache/shenyu/k8s/parser/IngressParser.java | 4 +
.../shenyu/k8s/reconciler/IngressReconciler.java | 14 +
19 files changed, 1141 insertions(+)
diff --git a/.github/workflows/integrated-test-k8s-ingress.yml b/.github/workflows/integrated-test-k8s-ingress.yml
index 0d0439d66c..ec3c92bf57 100644
--- a/.github/workflows/integrated-test-k8s-ingress.yml
+++ b/.github/workflows/integrated-test-k8s-ingress.yml
@@ -33,6 +33,7 @@ jobs:
- shenyu-integrated-test-k8s-ingress-spring-cloud
- shenyu-integrated-test-k8s-ingress-websocket
- shenyu-integrated-test-k8s-ingress-brpc
+ - shenyu-integrated-test-k8s-ingress-grpc
runs-on: ubuntu-latest
if: github.repository == 'apache/shenyu'
steps:
diff --git a/shenyu-examples/shenyu-examples-grpc/k8s/ingress.yml b/shenyu-examples/shenyu-examples-grpc/k8s/ingress.yml
new file mode 100644
index 0000000000..fd7c20f94a
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-grpc/k8s/ingress.yml
@@ -0,0 +1,61 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+apiVersion: v1
+kind: Service
+metadata:
+ name: echo
+ namespace: shenyu-ingress
+ annotations:
+ kubernetes.io/ingress.class: shenyu
+ shenyu.apache.org/plugin-grpc-enabled: 'true'
+ shenyu.apache.org/plugin-grpc-app-name: 127.0.0.1:38080
+ shenyu.apache.org/plugin-context-path-path: /grpc
+ shenyu.apache.org/plugin-grpc-path: /grpc/echo
+ shenyu.apache.org/plugin-grpc-rpc-type: grpc
+ shenyu.apache.org/plugin-grpc-service-name: echo.EchoService
+ shenyu.apache.org/plugin-grpc-method-name: echo
+ shenyu.apache.org/plugin-grpc-params-type: echo.EchoRequest,io.grpc.stub.StreamObserver
+ shenyu.apache.org/plugin-grpc-rpc-expand: |
+ {"timeout":5000,"methodType":"UNARY"}
+spec:
+ selector:
+ app: shenyu-examples-grpc-service
+ ports:
+ - port: 38080
+
+---
+
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+ namespace: shenyu-ingress
+ labels:
+ shenyu.apache.org/metadata-labels-1: echo
+ annotations:
+ kubernetes.io/ingress.class: shenyu
+ shenyu.apache.org/plugin-grpc-enabled: 'true'
+ name: demo-ingress
+spec:
+ rules:
+ - http:
+ paths:
+ - backend:
+ service:
+ name: shenyu-examples-grpc-service
+ port:
+ number: 38080
+ path: /grpc/
+ pathType: Prefix
diff --git a/shenyu-examples/shenyu-examples-grpc/k8s/shenyu-examples-grpc.yml b/shenyu-examples/shenyu-examples-grpc/k8s/shenyu-examples-grpc.yml
new file mode 100644
index 0000000000..1458a50047
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-grpc/k8s/shenyu-examples-grpc.yml
@@ -0,0 +1,89 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+apiVersion: v1
+kind: Namespace
+metadata:
+ name: shenyu-ingress
+---
+
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: shenyu-examples-grpc-deployment
+ namespace: shenyu-ingress
+ labels:
+ app: shenyu-examples-grpc
+ all: shenyu-examples-grpc
+spec:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: shenyu-examples-grpc
+ all: shenyu-examples-grpc
+ strategy: {}
+ template:
+ metadata:
+ namespace: shenyu-ingress
+ labels:
+ app: shenyu-examples-grpc
+ all: shenyu-examples-grpc
+ spec:
+ containers:
+ - image: shenyu-examples-grpc
+ name: shenyu-examples-grpc
+ livenessProbe:
+ exec:
+ command:
+ - wget -q -O - http://localhost:55290/actuator/health | grep UP || exit 1
+ initialDelaySeconds: 10
+ failureThreshold: 3
+ timeoutSeconds: 2
+ env:
+ - name: shenyu.register.serverLists
+ value: http://shenyu-admin:9095
+ ports:
+ - containerPort: 55290
+ - containerPort: 8080
+ imagePullPolicy: IfNotPresent
+ restartPolicy: Always
+status: {}
+
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: shenyu-examples-grpc-service
+ namespace: shenyu-ingress
+ labels:
+ app: shenyu-examples-grpc
+ all: shenyu-examples-grpc
+spec:
+ selector:
+ app: shenyu-examples-grpc
+ all: shenyu-examples-grpc
+ type: NodePort
+ ports:
+ - name: "55290"
+ port: 55290
+ targetPort: 55290
+ nodePort: 31183
+ - name: "8080"
+ port: 8080
+ targetPort: 8080
+ nodePort: 31184
+status:
+ loadBalancer: {}
diff --git a/shenyu-integrated-test/pom.xml b/shenyu-integrated-test/pom.xml
index 18357dd95a..81b29a3c96 100644
--- a/shenyu-integrated-test/pom.xml
+++ b/shenyu-integrated-test/pom.xml
@@ -52,6 +52,7 @@
<module>shenyu-integrated-test-k8s-ingress-apache-dubbo</module>
<module>shenyu-integrated-test-k8s-ingress-websocket</module>
<module>shenyu-integrated-test-k8s-ingress-brpc</module>
+ <module>shenyu-integrated-test-k8s-ingress-grpc</module>
</modules>
<properties>
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/Dockerfile b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/Dockerfile
new file mode 100644
index 0000000000..94f2929b8b
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/Dockerfile
@@ -0,0 +1,29 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+FROM openjdk:8-jre-alpine
+
+ENV APP_NAME shenyu-integrated-test-k8s-ingress-grpc
+ENV LOCAL_PATH /opt/${APP_NAME}
+
+RUN mkdir -p ${LOCAL_PATH}
+
+ADD target/${APP_NAME}.jar ${LOCAL_PATH}
+
+WORKDIR ${LOCAL_PATH}
+EXPOSE 9195
+
+CMD java -jar ${APP_NAME}.jar
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/deploy-shenyu.yaml b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/deploy-shenyu.yaml
new file mode 100644
index 0000000000..a663789584
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/deploy-shenyu.yaml
@@ -0,0 +1,103 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+apiVersion: v1
+automountServiceAccountToken: true
+kind: ServiceAccount
+metadata:
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
+---
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
+ labels:
+ app: shenyu-ingress-controller
+ all: shenyu-ingress-controller
+spec:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: shenyu-ingress-controller
+ template:
+ metadata:
+ labels:
+ app: shenyu-ingress-controller
+ spec:
+ containers:
+ - name: shenyu-ingress-controller
+ image: apache/shenyu-integrated-test-k8s-ingress-grpc:latest
+ ports:
+ - containerPort: 9195
+ imagePullPolicy: IfNotPresent
+ serviceAccountName: shenyu-ingress-controller
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
+spec:
+ selector:
+ app: shenyu-ingress-controller
+ type: NodePort
+ ports:
+ - port: 9195
+ targetPort: 9195
+ nodePort: 30095
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
+rules:
+- apiGroups:
+ - ""
+ resources:
+ - namespaces
+ - services
+ - endpoints
+ - secrets
+ - pods
+ verbs:
+ - get
+ - list
+ - watch
+- apiGroups:
+ - networking.k8s.io
+ resources:
+ - ingresses
+ verbs:
+ - get
+ - list
+ - watch
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRoleBinding
+metadata:
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
+roleRef:
+ apiGroup: rbac.authorization.k8s.io
+ kind: ClusterRole
+ name: shenyu-ingress-controller
+subjects:
+- kind: ServiceAccount
+ name: shenyu-ingress-controller
+ namespace: shenyu-ingress
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/kind-config.yaml b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/kind-config.yaml
new file mode 100644
index 0000000000..18b7da990e
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/kind-config.yaml
@@ -0,0 +1,36 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+kind: Cluster
+apiVersion: kind.x-k8s.io/v1alpha4
+nodes:
+ - role: control-plane
+ kubeadmConfigPatches:
+ - |
+ kind: InitConfiguration
+ nodeRegistration:
+ kubeletExtraArgs:
+ node-labels: "ingress-ready=true"
+ extraPortMappings:
+ - containerPort: 80
+ hostPort: 80
+ protocol: TCP
+ - containerPort: 443
+ hostPort: 443
+ protocol: TCP
+ - containerPort: 30095
+ hostPort: 30095
+ protocol: TCP
\ No newline at end of file
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/pom.xml b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/pom.xml
new file mode 100644
index 0000000000..afba718e05
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/pom.xml
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>shenyu-integrated-test</artifactId>
+ <groupId>org.apache.shenyu</groupId>
+ <version>2.6.1-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>shenyu-integrated-test-k8s-ingress-grpc</artifactId>
+
+ <dependencies>
+ <!--shenyu grpc plugin start-->
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-spring-boot-starter-plugin-grpc</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <!--shenyu grpc plugin end-->
+
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-integrated-test-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <!-- shenyu kubernetes controller begin -->
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-spring-boot-starter-k8s</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <!-- shenyu kubernetes controller end -->
+ </dependencies>
+
+ <profiles>
+ <profile>
+ <id>it</id>
+ <properties>
+ <docker.buildArg.APP_NAME>shenyu-integrated-test-k8s-ingress-grpc</docker.buildArg.APP_NAME>
+ <docker.image.tag.repo>apache/shenyu-integrated-test-k8s-ingress-grpc</docker.image.tag.repo>
+ <docker.image.tag.tagName>latest</docker.image.tag.tagName>
+ </properties>
+ <build>
+ <finalName>shenyu-integrated-test-k8s-ingress-grpc</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <version>${spring-boot.version}</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>repackage</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <mainClass>org.apache.shenyu.integrated.test.k8s.ingress.grpc.GrpcIngressControllerIntegratedBootstrap
+ </mainClass>
+ <executable>true</executable>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>io.fabric8</groupId>
+ <artifactId>docker-maven-plugin</artifactId>
+ <version>${docker-maven-plugin.version}</version>
+ <configuration>
+ <images>
+ <image>
+ <name>apache/shenyu-integrated-test-k8s-ingress-grpc</name>
+ <build>
+ <contextDir>${project.basedir}</contextDir>
+ </build>
+ </image>
+ </images>
+ </configuration>
+ <executions>
+ <execution>
+ <id>start</id>
+ <goals>
+ <goal>build</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skipTests>false</skipTests>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+</project>
\ No newline at end of file
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/build_k8s_cluster.sh b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/build_k8s_cluster.sh
new file mode 100644
index 0000000000..dba18f5da6
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/build_k8s_cluster.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+kind load docker-image "shenyu-examples-grpc:latest"
+kind load docker-image "apache/shenyu-integrated-test-k8s-ingress-grpc:latest"
+kubectl apply -f ./shenyu-examples/shenyu-examples-grpc/k8s/shenyu-examples-grpc.yml
+kubectl apply -f ./shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/deploy/deploy-shenyu.yaml
+kubectl apply -f ./shenyu-examples/shenyu-examples-grpc/k8s/ingress.yml
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/healthcheck.sh b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/healthcheck.sh
new file mode 100644
index 0000000000..22a76034ab
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/healthcheck.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+PRGDIR=`dirname "$0"`
+for service in `grep -v -E "^$|^#" ${PRGDIR}/services.list`
+do
+ for loop in `seq 1 30`
+ do
+ status=`curl -o /dev/null -s -w %{http_code} $service`
+ echo -e "curl $service response $status"
+
+ if [ $status -eq 200 ]; then
+ break
+ fi
+
+ sleep 2
+ done
+done
+
+sleep 3
+echo -e "\n-------------------"
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/services.list b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/services.list
new file mode 100644
index 0000000000..ef0e742c04
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/script/services.list
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+http://localhost:30095/actuator/health
+http://localhost:31184/actuator/health
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcIngressControllerIntegratedBootstrap.java b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcIngressControllerIntegratedBootstrap.java
new file mode 100644
index 0000000000..d117c43e69
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcIngressControllerIntegratedBootstrap.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.integrated.test.k8s.ingress.grpc;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * The type grpc integrated bootstrap.
+ */
+@SpringBootApplication
+public class GrpcIngressControllerIntegratedBootstrap {
+ /**
+ * main mathod of app.
+ *
+ * @param args args
+ */
+ public static void main(final String[] args) {
+ SpringApplication.run(GrpcIngressControllerIntegratedBootstrap.class);
+ }
+}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application-local.yml b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application-local.yml
new file mode 100644
index 0000000000..6ae49e9e29
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application-local.yml
@@ -0,0 +1,71 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+server:
+ port: 9195
+ address: 0.0.0.0
+
+spring:
+ main:
+ allow-bean-definition-overriding: true
+ application:
+ name: shenyu-bootstrap
+
+management:
+ health:
+ defaults:
+ enabled: false
+
+shenyu:
+ httpclient:
+ maxInMemorySize: 64
+ netty:
+ tcp:
+ selectCount: 1
+ workerCount: 4
+ connectTimeoutMillis: 10000
+ writeBufferHighWaterMark: 65536
+ writeBufferLowWaterMark: 32768
+ writeSpinCount: 16
+ autoRead: true
+ tcpNodelay: true
+ soKeepalive: false
+ soReuseaddr: false
+ soLinger: -1
+ soBacklog: 128
+ switchConfig:
+ local: true
+ cross:
+ enabled: true
+ exclude:
+ enabled: true
+ paths:
+ - /favicon.ico
+ - /actuator/health
+ local:
+ enabled: true
+ sha512Key: "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413"
+ sharedPool:
+ enable: true
+
+logging:
+ level:
+ root: info
+ org.springframework.boot: info
+ org.apache.ibatis: info
+ org.apache.shenyu.bonuspoint: info
+ org.apache.shenyu.lottery: info
+ org.apache.shenyu: info
+
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application.yml b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application.yml
new file mode 100644
index 0000000000..393ad24df1
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/main/resources/application.yml
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+spring:
+ profiles:
+ active: local
diff --git a/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/test/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/test/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcPluginTest.java
new file mode 100644
index 0000000000..cde4314496
--- /dev/null
+++ b/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-grpc/src/test/java/org/apache/shenyu/integrated/test/k8s/ingress/grpc/GrpcPluginTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.integrated.test.k8s.ingress.grpc;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit;
+import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class GrpcPluginTest extends AbstractPluginDataInit {
+
+ private static final HttpHelper HTTP_HELPER = HttpHelper.INSTANCE;
+
+ @BeforeAll
+ public static void setup() {
+ HTTP_HELPER.setGatewayEndpoint("http://localhost:30095");
+ }
+
+ @Test
+ public void testHelloWorld() throws Exception {
+ JsonObject request = buildGrpcRequest();
+ JsonArray response = HttpHelper.INSTANCE.postGateway("/grpc/echo", request, JsonArray.class);
+ Map<String, Object> result = GsonUtils.getInstance().toObjectMap(response.get(0).toString(), Object.class);
+ assertEquals("ReceivedHELLO", result.get("message"));
+ }
+
+ private JsonObject buildGrpcRequest() {
+ JsonArray jsonArray = new JsonArray();
+ JsonObject child = new JsonObject();
+ child.addProperty("message", "hello rpc");
+ jsonArray.add(child);
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.add("data", jsonArray);
+ return jsonObject;
+ }
+}
diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/common/IngressConstants.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/common/IngressConstants.java
index 5e6374b035..e20cfa5143 100644
--- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/common/IngressConstants.java
+++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/common/IngressConstants.java
@@ -189,5 +189,33 @@ public class IngressConstants {
//The configuration key to specify the Brpc parameter type for the plugin, in string
public static final String PLUGIN_BRPC_PARAMETER_TYPE = "shenyu.apache.org/plugin-grpc-parameter-type";
+ //The configuration key to specify the Grpc application name for the plugin, in string
+ public static final String PLUGIN_GRPC_APP_NAME = "shenyu.apache.org/plugin-grpc-app-name";
+ //The configuration key to specify the Grpc service name for the plugin, in string
+ public static final String PLUGIN_GRPC_SERVICE_NAME = "shenyu.apache.org/plugin-grpc-service-name";
+
+ //The configuration key to specify the Grpc method name for the plugin, in string
+ public static final String PLUGIN_GRPC_METHOD_NAME = "shenyu.apache.org/plugin-grpc-method-name";
+
+ //The configuration key to specify the Grpc params type for the plugin, in string
+ public static final String PLUGIN_GRPC_PARAMS_TYPE = "shenyu.apache.org/plugin-grpc-params-type";
+
+ //The configuration key to specify the Grpc rpc type for the plugin, in string
+ public static final String PLUGIN_GRPC_RPC_TYPE = "shenyu.apache.org/plugin-grpc-rpc-type";
+
+ //The configuration key to specify the Grpc context path for the plugin, in string
+ public static final String PLUGIN_GRPC_CONTEXT_PATH = "shenyu.apache.org/plugin-grpc-context-path";
+
+ //The configuration key to specify the Grpc path for the plugin, in string
+ public static final String PLUGIN_GRPC_PATH = "shenyu.apache.org/plugin-grpc-path";
+
+ //The configuration key to specify the Grpc enabled for the plugin, in string
+ public static final String PLUGIN_GRPC_ENABLED = "shenyu.apache.org/plugin-grpc-enabled";
+
+ //The configuration key to specify the Grpc rpc expand for the plugin, in string
+ public static final String PLUGIN_GRPC_RPC_EXPAND = "shenyu.apache.org/plugin-grpc-rpc-expand";
+
+ //The configuration key to specify the Grpc parameter type for the plugin, in string
+ public static final String PLUGIN_GRPC_PARAMETER_TYPE = "shenyu.apache.org/plugin-grpc-parameter-type";
}
diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java
new file mode 100644
index 0000000000..964f784d29
--- /dev/null
+++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/GrpcParser.java
@@ -0,0 +1,398 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.k8s.parser;
+
+import io.kubernetes.client.informer.cache.Lister;
+import io.kubernetes.client.openapi.ApiException;
+import io.kubernetes.client.openapi.apis.CoreV1Api;
+import io.kubernetes.client.openapi.models.V1EndpointAddress;
+import io.kubernetes.client.openapi.models.V1EndpointSubset;
+import io.kubernetes.client.openapi.models.V1Endpoints;
+import io.kubernetes.client.openapi.models.V1HTTPIngressPath;
+import io.kubernetes.client.openapi.models.V1Ingress;
+import io.kubernetes.client.openapi.models.V1IngressBackend;
+import io.kubernetes.client.openapi.models.V1IngressRule;
+import io.kubernetes.client.openapi.models.V1IngressServiceBackend;
+import io.kubernetes.client.openapi.models.V1IngressTLS;
+import io.kubernetes.client.openapi.models.V1Secret;
+import io.kubernetes.client.openapi.models.V1Service;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.shenyu.common.config.ssl.SslCrtAndKeyStream;
+import org.apache.shenyu.common.dto.ConditionData;
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.impl.GrpcRuleHandle;
+import org.apache.shenyu.common.dto.convert.selector.GrpcUpstream;
+import org.apache.shenyu.common.enums.LoadBalanceEnum;
+import org.apache.shenyu.common.enums.MatchModeEnum;
+import org.apache.shenyu.common.enums.OperatorEnum;
+import org.apache.shenyu.common.enums.ParamTypeEnum;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.enums.SelectorTypeEnum;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.k8s.common.IngressConfiguration;
+import org.apache.shenyu.k8s.common.IngressConstants;
+import org.apache.shenyu.k8s.common.ShenyuMemoryConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GrpcParser implements K8sResourceParser<V1Ingress> {
+ private static final Logger LOG = LoggerFactory.getLogger(GrpcParser.class);
+
+ private final Lister<V1Service> serviceLister;
+
+ private final Lister<V1Endpoints> endpointsLister;
+
+ /**
+ * GrpcIngressParser Constructor.
+ *
+ * @param serviceInformer serviceInformer
+ * @param endpointsInformer endpointsInformer
+ */
+ public GrpcParser(final Lister<V1Service> serviceInformer, final Lister<V1Endpoints> endpointsInformer) {
+ this.serviceLister = serviceInformer;
+ this.endpointsLister = endpointsInformer;
+ }
+
+ /**
+ * Parse ingress to ShenyuMemoryConfig.
+ *
+ * @param ingress ingress resource
+ * @param coreV1Api coreV1Api
+ * @return ShenyuMemoryConfig
+ */
+ @Override
+ public ShenyuMemoryConfig parse(final V1Ingress ingress, final CoreV1Api coreV1Api) {
+ ShenyuMemoryConfig res = new ShenyuMemoryConfig();
+
+ if (Objects.nonNull(ingress.getSpec())) {
+ // parse the grpc backend
+ V1IngressBackend grpcBackend = ingress.getSpec().getDefaultBackend();
+ List<V1IngressRule> rules = ingress.getSpec().getRules();
+ List<V1IngressTLS> tlsList = ingress.getSpec().getTls();
+
+ String namespace = Objects.requireNonNull(ingress.getMetadata()).getNamespace();
+ List<GrpcUpstream> grpcDefaultUpstreamList = parseDefaultService(grpcBackend, namespace);
+
+ if (Objects.isNull(rules) || CollectionUtils.isEmpty(rules)) {
+ // if rules is null, grpcBackend become global default
+ if (Objects.nonNull(grpcBackend) && Objects.nonNull(grpcBackend.getService())) {
+ IngressConfiguration defaultRouteConfig = getRpcRouteConfig(grpcDefaultUpstreamList, ingress.getMetadata().getAnnotations());
+ res.setGlobalDefaultBackend(Pair.of(Pair.of(namespace + "/" + ingress.getMetadata().getName(), grpcBackend.getService().getName()),
+ defaultRouteConfig));
+ }
+ } else {
+ // if rules is not null, grpcBackend is default in this ingress
+ List<IngressConfiguration> routeList = new ArrayList<>(rules.size());
+ for (V1IngressRule ingressRule : rules) {
+ List<IngressConfiguration> routes = parseIngressRule(ingressRule,
+ Objects.requireNonNull(ingress.getMetadata()).getNamespace(),
+ ingress.getMetadata().getAnnotations(), ingress.getMetadata().getLabels());
+ routeList.addAll(routes);
+ }
+ res.setRouteConfigList(routeList);
+ }
+
+ // Parse tls
+ if (Objects.nonNull(tlsList) && CollectionUtils.isNotEmpty(tlsList)) {
+ List<SslCrtAndKeyStream> sslList = new ArrayList<>();
+ for (V1IngressTLS tls : tlsList) {
+ if (Objects.nonNull(tls.getSecretName()) && Objects.nonNull(tls.getHosts()) && CollectionUtils.isNotEmpty(tls.getHosts())) {
+ try {
+ V1Secret secret = coreV1Api.readNamespacedSecret(tls.getSecretName(), namespace, "ture");
+ if (secret.getData() != null) {
+ InputStream keyCertChainInputStream = new ByteArrayInputStream(secret.getData().get("tls.crt"));
+ InputStream keyInputStream = new ByteArrayInputStream(secret.getData().get("tls.key"));
+ tls.getHosts().forEach(host ->
+ sslList.add(new SslCrtAndKeyStream(host, keyCertChainInputStream, keyInputStream))
+ );
+ }
+ } catch (ApiException e) {
+ LOG.error("parse tls failed ", e);
+ }
+ }
+ }
+ res.setTlsConfigList(sslList);
+ }
+ }
+ return res;
+ }
+
+ private List<GrpcUpstream> parseDefaultService(final V1IngressBackend defaultBackend, final String namespace) {
+ List<GrpcUpstream> defaultUpstreamList = new ArrayList<>();
+ if (Objects.nonNull(defaultBackend) && Objects.nonNull(defaultBackend.getService())) {
+ String serviceName = defaultBackend.getService().getName();
+ // shenyu routes directly to the container
+ V1Endpoints v1Endpoints = endpointsLister.namespace(namespace).get(serviceName);
+ List<V1EndpointSubset> subsets = v1Endpoints.getSubsets();
+ if (Objects.isNull(subsets) || CollectionUtils.isEmpty(subsets)) {
+ LOG.info("Endpoints {} do not have subsets", serviceName);
+ } else {
+ for (V1EndpointSubset subset : subsets) {
+ List<V1EndpointAddress> addresses = subset.getAddresses();
+ if (Objects.isNull(addresses) || CollectionUtils.isEmpty(addresses)) {
+ continue;
+ }
+ for (V1EndpointAddress address : addresses) {
+ String upstreamIp = address.getIp();
+ String defaultPort = parsePort(defaultBackend.getService());
+ if (Objects.nonNull(defaultPort)) {
+ GrpcUpstream upstream = GrpcUpstream.builder()
+ .upstreamUrl(upstreamIp + ":" + defaultPort)
+ .weight(50).build();
+ defaultUpstreamList.add(upstream);
+ }
+ }
+ }
+ }
+ }
+ return defaultUpstreamList;
+ }
+
+ private List<GrpcUpstream> parseUpstream(final V1IngressBackend backend, final String namespace) {
+ List<GrpcUpstream> upstreamList = new ArrayList<>();
+ if (Objects.nonNull(backend) && Objects.nonNull(backend.getService()) && Objects.nonNull(backend.getService().getName())) {
+ String serviceName = backend.getService().getName();
+ // shenyu routes directly to the container
+ V1Endpoints v1Endpoints = endpointsLister.namespace(namespace).get(serviceName);
+ List<V1EndpointSubset> subsets = v1Endpoints.getSubsets();
+
+ if (Objects.isNull(subsets) || CollectionUtils.isEmpty(subsets)) {
+ LOG.info("Endpoints {} do not have subsets", serviceName);
+ } else {
+ for (V1EndpointSubset subset : subsets) {
+ List<V1EndpointAddress> addresses = subset.getAddresses();
+ if (Objects.isNull(addresses) || addresses.isEmpty()) {
+ continue;
+ }
+ for (V1EndpointAddress address : addresses) {
+ String upstreamIp = address.getIp();
+ String defaultPort = parsePort(backend.getService());
+ if (Objects.nonNull(defaultPort)) {
+ GrpcUpstream upstream = GrpcUpstream.builder()
+ .upstreamUrl(upstreamIp + ":" + defaultPort)
+ .weight(100).build();
+ upstreamList.add(upstream);
+ }
+ }
+ }
+ }
+ }
+ return upstreamList;
+ }
+
+ private String parsePort(final V1IngressServiceBackend service) {
+ if (Objects.nonNull(service.getPort())) {
+ if (service.getPort().getNumber() != null && service.getPort().getNumber() > 0) {
+ return String.valueOf(service.getPort().getNumber());
+ } else if (service.getPort().getName() != null && !"".equals(service.getPort().getName().trim())) {
+ return service.getPort().getName().trim();
+ }
+ }
+ return null;
+ }
+
+ private List<IngressConfiguration> parseIngressRule(final V1IngressRule ingressRule,
+ final String namespace,
+ final Map<String, String> annotations,
+ final Map<String, String> labels) {
+ List<IngressConfiguration> res = new ArrayList<>();
+ ConditionData hostCondition = Objects.nonNull(ingressRule.getHost()) ? createHostCondition(ingressRule.getHost()) : null;
+ if (Objects.nonNull(ingressRule.getHttp())) {
+ List<V1HTTPIngressPath> paths = ingressRule.getHttp().getPaths();
+ if (Objects.nonNull(paths)) {
+ for (V1HTTPIngressPath path : paths) {
+ if (path.getPath() == null) {
+ continue;
+ }
+ OperatorEnum operator = getOperator(path.getPathType());
+ ConditionData pathCondition = createPathCondition(path.getPath(), operator);
+ List<ConditionData> conditionList = new ArrayList<>(2);
+ if (Objects.nonNull(hostCondition)) {
+ conditionList.add(hostCondition);
+ }
+ conditionList.add(pathCondition);
+ List<GrpcUpstream> grpcUpstreamList = parseUpstream(path.getBackend(), namespace);
+
+ SelectorData selectorData = createSelectorData(path.getPath(), conditionList, grpcUpstreamList);
+ List<RuleData> ruleDataList = new ArrayList<>();
+ List<MetaData> metaDataList = new ArrayList<>();
+ for (String label : labels.keySet()) {
+ Map<String, String> metadataAnnotations = serviceLister.namespace(namespace).get(labels.get(label)).getMetadata().getAnnotations();
+ List<ConditionData> ruleConditionList = getRuleConditionList(metadataAnnotations);
+ RuleData ruleData = createRuleData(metadataAnnotations, ruleConditionList, annotations);
+ MetaData metaData = parseMetaData(metadataAnnotations);
+ ruleDataList.add(ruleData);
+ metaDataList.add(metaData);
+ }
+ res.add(new IngressConfiguration(selectorData, ruleDataList, metaDataList));
+ }
+ }
+ }
+ return res;
+ }
+
+ private List<ConditionData> getRuleConditionList(final Map<String, String> annotations) {
+ final List<ConditionData> ruleConditionList = new ArrayList<>();
+ ConditionData ruleCondition = new ConditionData();
+ ruleCondition.setOperator(OperatorEnum.EQ.getAlias());
+ ruleCondition.setParamType(ParamTypeEnum.URI.getName());
+ ruleCondition.setParamValue(annotations.get(IngressConstants.PLUGIN_GRPC_PATH));
+ ruleConditionList.add(ruleCondition);
+ return ruleConditionList;
+ }
+
+ private ConditionData createHostCondition(final String host) {
+ ConditionData hostCondition = new ConditionData();
+ hostCondition.setParamType(ParamTypeEnum.DOMAIN.getName());
+ hostCondition.setOperator(OperatorEnum.EQ.getAlias());
+ hostCondition.setParamValue(host);
+ return hostCondition;
+ }
+
+ private OperatorEnum getOperator(final String pathType) {
+ if ("ImplementationSpecific".equals(pathType)) {
+ return OperatorEnum.MATCH;
+ } else if ("Prefix".equals(pathType)) {
+ return OperatorEnum.STARTS_WITH;
+ } else if ("Exact".equals(pathType)) {
+ return OperatorEnum.EQ;
+ } else {
+ LOG.info("Invalid path type, set it with match operator");
+ return OperatorEnum.MATCH;
+ }
+ }
+
+ private ConditionData createPathCondition(final String path, final OperatorEnum operator) {
+ ConditionData pathCondition = new ConditionData();
+ pathCondition.setOperator(operator.getAlias());
+ pathCondition.setParamType(ParamTypeEnum.URI.getName());
+ pathCondition.setParamValue(path);
+ return pathCondition;
+ }
+
+ private SelectorData createSelectorData(final String path, final List<ConditionData> conditionList, final List<GrpcUpstream> grpcUpstreamList) {
+ return SelectorData.builder()
+ .pluginId(String.valueOf(PluginEnum.GRPC.getCode()))
+ .pluginName(PluginEnum.GRPC.getName())
+ .name(path)
+ .matchMode(MatchModeEnum.AND.getCode())
+ .type(SelectorTypeEnum.CUSTOM_FLOW.getCode())
+ .handle(GsonUtils.getInstance().toJson(grpcUpstreamList))
+ .enabled(true)
+ .logged(false)
+ .continued(true)
+ .conditionList(conditionList)
+ .build();
+ }
+
+ private RuleData createRuleData(final Map<String, String> metadataAnnotations, final List<ConditionData> ruleConditionList, final Map<String, String> annotations) {
+ GrpcRuleHandle grpcRuleHandle = new GrpcRuleHandle();
+ if (Objects.nonNull(annotations)) {
+ grpcRuleHandle.setLoadBalance(annotations.getOrDefault(IngressConstants.LOADBALANCER_ANNOTATION_KEY, "random"));
+ }
+ return RuleData.builder()
+ .name(metadataAnnotations.get(IngressConstants.PLUGIN_GRPC_PATH))
+ .pluginName(PluginEnum.GRPC.getName())
+ .matchMode(MatchModeEnum.AND.getCode())
+ .handle(GsonUtils.getInstance().toJson(grpcRuleHandle))
+ .conditionDataList(ruleConditionList)
+ .loged(true)
+ .enabled(true)
+ .build();
+ }
+
+ private MetaData parseMetaData(final Map<String, String> annotations) {
+ return MetaData.builder()
+ .appName(annotations.get(IngressConstants.PLUGIN_GRPC_APP_NAME))
+ .path(annotations.get(IngressConstants.PLUGIN_GRPC_PATH))
+ .contextPath(annotations.get(IngressConstants.PLUGIN_CONTEXT_PATH_PATH))
+ .rpcType(annotations.get(IngressConstants.PLUGIN_GRPC_RPC_TYPE))
+ .rpcExt(annotations.get(IngressConstants.PLUGIN_GRPC_RPC_EXPAND))
+ .serviceName(annotations.get(IngressConstants.PLUGIN_GRPC_SERVICE_NAME))
+ .methodName(annotations.get(IngressConstants.PLUGIN_GRPC_METHOD_NAME))
+ .parameterTypes(annotations.get(IngressConstants.PLUGIN_GRPC_PARAMS_TYPE))
+ .enabled(true)
+ .build();
+ }
+
+ private IngressConfiguration getRpcRouteConfig(final List<GrpcUpstream> grpcUpStream, final Map<String, String> annotations) {
+ final ConditionData conditionData = new ConditionData();
+ conditionData.setParamName("grpc");
+ conditionData.setParamType(ParamTypeEnum.URI.getName());
+ conditionData.setOperator(OperatorEnum.PATH_PATTERN.getAlias());
+ conditionData.setParamValue("/**");
+
+ final SelectorData selectorData = SelectorData.builder()
+ .name("grpc-selector")
+ .sort(Integer.MAX_VALUE)
+ .conditionList(Collections.singletonList(conditionData))
+ .handle(GsonUtils.getInstance().toJson(grpcUpStream))
+ .enabled(true)
+ .id(IngressConstants.ID)
+ .pluginName(PluginEnum.GRPC.getName())
+ .pluginId(String.valueOf(PluginEnum.GRPC.getCode()))
+ .logged(false)
+ .continued(true)
+ .matchMode(MatchModeEnum.AND.getCode())
+ .type(SelectorTypeEnum.FULL_FLOW.getCode()).build();
+
+ GrpcRuleHandle grpcRuleHandle = new GrpcRuleHandle();
+ if (Objects.nonNull(annotations)) {
+ grpcRuleHandle.setLoadBalance(annotations.getOrDefault(IngressConstants.LOADBALANCER_ANNOTATION_KEY, LoadBalanceEnum.RANDOM.getName()));
+ }
+
+ final RuleData ruleData = RuleData.builder()
+ .selectorId(IngressConstants.ID)
+ .pluginName(PluginEnum.GRPC.getName())
+ .name("grpc-rule")
+ .handle(GsonUtils.getInstance().toJson(grpcRuleHandle))
+ .matchMode(MatchModeEnum.AND.getCode())
+ .conditionDataList(Collections.singletonList(conditionData))
+ .loged(false)
+ .enabled(true)
+ .sort(Integer.MAX_VALUE).build();
+
+ MetaData metaData = new MetaData();
+ if (Objects.nonNull(annotations)) {
+ metaData.setAppName(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_APP_NAME, "grpc"));
+ metaData.setMethodName(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_METHOD_NAME, "hello"));
+ metaData.setPath(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_PATH, "/grpc/helloService/hello"));
+ metaData.setRpcType(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_RPC_TYPE, RpcTypeEnum.GRPC.getName()));
+ metaData.setServiceName(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_SERVICE_NAME, "hello.HelloService"));
+ metaData.setContextPath(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_CONTEXT_PATH, "/grpc"));
+ metaData.setRpcExt(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_RPC_EXPAND, "{\"timeout\":5000,\"methodType\":\"UNARY\"}"));
+ metaData.setServiceName(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_SERVICE_NAME, "hello.HelloService"));
+ metaData.setParameterTypes(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_PARAMETER_TYPE, "hello.HelloRequest,io.grpc.stub.StreamObserver"));
+ metaData.setEnabled(Boolean.parseBoolean(annotations.getOrDefault(IngressConstants.PLUGIN_GRPC_ENABLED, "true")));
+ }
+ return new IngressConfiguration(selectorData, Arrays.asList(ruleData), Arrays.asList(metaData));
+ }
+}
diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java
index 7232baa374..6d9f2b18ea 100644
--- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java
+++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/parser/IngressParser.java
@@ -70,6 +70,7 @@ public class IngressParser implements K8sResourceListParser<V1Ingress> {
boolean springCloudEnabled = getBooleanAnnotation(ingress, IngressConstants.PLUGIN_SPRING_CLOUD_ENABLED);
boolean webSocketEnabled = getBooleanAnnotation(ingress, IngressConstants.PLUGIN_WEB_SOCKET_ENABLED);
boolean brpcEnabled = getBooleanAnnotation(ingress, IngressConstants.PLUGIN_BRPC_ENABLED);
+ boolean grpcEnabled = getBooleanAnnotation(ingress, IngressConstants.PLUGIN_GRPC_ENABLED);
if (!dubboEnabled || !motanEnabled) {
contextPathParse(ingress, shenyuMemoryConfigList, coreV1Api);
@@ -89,6 +90,9 @@ public class IngressParser implements K8sResourceListParser<V1Ingress> {
} else if (brpcEnabled) {
BrpcParser brpcParser = new BrpcParser(serviceLister, endpointsLister);
shenyuMemoryConfigList.add(brpcParser.parse(ingress, coreV1Api));
+ } else if (grpcEnabled) {
+ GrpcParser grpcParser = new GrpcParser(serviceLister, endpointsLister);
+ shenyuMemoryConfigList.add(grpcParser.parse(ingress, coreV1Api));
} else {
DivideIngressParser divideIngressParser = new DivideIngressParser(serviceLister, endpointsLister);
shenyuMemoryConfigList.add(divideIngressParser.parse(ingress, coreV1Api));
diff --git a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java
index 2cc64cbab4..bf6beb8a03 100644
--- a/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java
+++ b/shenyu-kubernetes-controller/src/main/java/org/apache/shenyu/k8s/reconciler/IngressReconciler.java
@@ -141,6 +141,8 @@ public class IngressReconciler implements Reconciler {
enablePlugin(shenyuCacheRepository, PluginEnum.WEB_SOCKET, null);
} else if (Objects.equals(annotations.get(IngressConstants.PLUGIN_BRPC_ENABLED), "true")) {
enablePlugin(shenyuCacheRepository, PluginEnum.BRPC, null);
+ } else if (Objects.equals(annotations.get(IngressConstants.PLUGIN_GRPC_ENABLED), "true")) {
+ enablePlugin(shenyuCacheRepository, PluginEnum.GRPC, null);
}
if (Objects.isNull(v1Ingress)) {
if (Objects.nonNull(oldIngress)) {
@@ -215,6 +217,8 @@ public class IngressReconciler implements Reconciler {
selectorList = deleteSelectorByIngressName(request.getNamespace(), request.getName(), PluginEnum.WEB_SOCKET.getName(), "");
} else if (Objects.equals(oldIngress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_BRPC_ENABLED), "true")) {
selectorList = deleteSelectorByIngressName(request.getNamespace(), request.getName(), PluginEnum.BRPC.getName(), "");
+ } else if (Objects.equals(oldIngress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_GRPC_ENABLED), "true")) {
+ selectorList = deleteSelectorByIngressName(request.getNamespace(), request.getName(), PluginEnum.GRPC.getName(), "");
} else {
selectorList = deleteSelectorByIngressName(request.getNamespace(), request.getName(), PluginEnum.DIVIDE.getName(), "");
}
@@ -229,6 +233,8 @@ public class IngressReconciler implements Reconciler {
IngressSelectorCache.getInstance().remove(request.getNamespace(), request.getName(), PluginEnum.WEB_SOCKET.getName());
} else if (Objects.equals(oldIngress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_BRPC_ENABLED), "true")) {
IngressSelectorCache.getInstance().remove(request.getNamespace(), request.getName(), PluginEnum.BRPC.getName());
+ } else if (Objects.equals(oldIngress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_GRPC_ENABLED), "true")) {
+ IngressSelectorCache.getInstance().remove(request.getNamespace(), request.getName(), PluginEnum.GRPC.getName());
} else {
IngressSelectorCache.getInstance().remove(request.getNamespace(), request.getName(), PluginEnum.DIVIDE.getName());
}
@@ -277,6 +283,8 @@ public class IngressReconciler implements Reconciler {
return "{\"registerProtocol\":\"zk\",\"registerAddress\":\"" + zookeeperUrl + "\",\"corethreads\":0,\"threads\":2147483647,\"queues\":0,\"threadpool\":\"shared\"}";
case WEB_SOCKET:
return "{multiSelectorHandle: 1}";
+ case GRPC:
+ return "{\"multiSelectorHandle\":\"1\",\"multiRuleHandle\":\"0\",\"threadpool\":\"shared\"}";
default:
return null;
}
@@ -552,6 +560,8 @@ public class IngressReconciler implements Reconciler {
String pluginMotanEnabled = ingress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_MOTAN_ENABLED);
String pluginSpringCloudEnabled = ingress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_SPRING_CLOUD_ENABLED);
String pluginWebSocketEnabled = ingress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_WEB_SOCKET_ENABLED);
+ String pluginBrpcEnabled = ingress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_BRPC_ENABLED);
+ String pluginGrpcEnabled = ingress.getMetadata().getAnnotations().get(IngressConstants.PLUGIN_GRPC_ENABLED);
if ((Boolean.TRUE.toString()).equals(pluginDubboEnabled)) {
pluginName = PluginEnum.DUBBO.getName();
} else if ((Boolean.TRUE.toString()).equals(pluginMotanEnabled)) {
@@ -560,6 +570,10 @@ public class IngressReconciler implements Reconciler {
pluginName = PluginEnum.SPRING_CLOUD.getName();
} else if ((Boolean.TRUE.toString()).equals(pluginWebSocketEnabled)) {
pluginName = PluginEnum.WEB_SOCKET.getName();
+ } else if ((Boolean.TRUE.toString()).equals(pluginBrpcEnabled)) {
+ pluginName = PluginEnum.BRPC.getName();
+ } else if ((Boolean.TRUE.toString()).equals(pluginGrpcEnabled)) {
+ pluginName = PluginEnum.GRPC.getName();
} else {
pluginName = PluginEnum.DIVIDE.getName();
}