mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 20:57:32 +08:00
get upstream serviceSource from RouteCluster information and update docs (#337)
This commit is contained in:
@@ -100,10 +100,6 @@ curl 'https://api.github.com/graphql' -X POST \
|
|||||||
| `gql` | graphql 查询 | 不能为空 |
|
| `gql` | graphql 查询 | 不能为空 |
|
||||||
| `endpoint` | graphql 查询端点 | `/graphql` |
|
| `endpoint` | graphql 查询端点 | `/graphql` |
|
||||||
| `timeout` | 查询连接超时,单位毫秒 | `5000` |
|
| `timeout` | 查询连接超时,单位毫秒 | `5000` |
|
||||||
| `serviceSource` | 服务来源:k8s, nacos,dns, ip | 不能为空 |
|
|
||||||
| `serviceName` | 服务名称 | 不能为空 |
|
|
||||||
| `servicePort` | 服务端口 | 不能为空 |
|
|
||||||
| `namespace` | 服务命名空间, 当服务来源是nacos需要配置 | |
|
|
||||||
| `domain` | 服务域名,当服务来源是dns配置 | |
|
| `domain` | 服务域名,当服务来源是dns配置 | |
|
||||||
|
|
||||||
### 插件使用
|
### 插件使用
|
||||||
@@ -158,9 +154,6 @@ spec:
|
|||||||
config:
|
config:
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
endpoint: /graphql
|
endpoint: /graphql
|
||||||
serviceSource: dns
|
|
||||||
serviceName: github
|
|
||||||
servicePort: 443
|
|
||||||
domain: api.github.com
|
domain: api.github.com
|
||||||
gql: |
|
gql: |
|
||||||
query ($owner:String! $name:String!){
|
query ($owner:String! $name:String!){
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -46,10 +45,10 @@ type Variable struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DeGraphQLConfig struct {
|
type DeGraphQLConfig struct {
|
||||||
client wrapper.HttpClient
|
|
||||||
gql string
|
gql string
|
||||||
endpoint string
|
endpoint string
|
||||||
timeout uint32
|
timeout uint32
|
||||||
|
domain string
|
||||||
variables []Variable
|
variables []Variable
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +62,14 @@ func (d *DeGraphQLConfig) SetEndpoint(endpoint string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DeGraphQLConfig) GetDomain() string {
|
||||||
|
return d.domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeGraphQLConfig) SetDomain(domain string) {
|
||||||
|
d.domain = domain
|
||||||
|
}
|
||||||
|
|
||||||
func (d *DeGraphQLConfig) GetEndpoint() string {
|
func (d *DeGraphQLConfig) GetEndpoint() string {
|
||||||
return d.endpoint
|
return d.endpoint
|
||||||
}
|
}
|
||||||
@@ -79,14 +86,6 @@ func (d *DeGraphQLConfig) SetTimeout(timeout uint32) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DeGraphQLConfig) SetClient(client wrapper.HttpClient) {
|
|
||||||
d.client = client
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DeGraphQLConfig) GetClient() wrapper.HttpClient {
|
|
||||||
return d.client
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DeGraphQLConfig) SetGql(gql string) error {
|
func (d *DeGraphQLConfig) SetGql(gql string) error {
|
||||||
if strings.TrimSpace(gql) == "" {
|
if strings.TrimSpace(gql) == "" {
|
||||||
return errors.New("gql can't be empty")
|
return errors.New("gql can't be empty")
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ static_resources:
|
|||||||
- match:
|
- match:
|
||||||
prefix: "/api"
|
prefix: "/api"
|
||||||
route:
|
route:
|
||||||
cluster: mock_service
|
cluster: github
|
||||||
http_filters:
|
http_filters:
|
||||||
- name: envoy.filters.http.wasm
|
- name: envoy.filters.http.wasm
|
||||||
typed_config:
|
typed_config:
|
||||||
@@ -35,9 +35,6 @@ static_resources:
|
|||||||
value: |-
|
value: |-
|
||||||
{
|
{
|
||||||
"gql": "query ($owner:String! $name:String!){\n repository(owner:$owner, name:$name) {\n name\n forkCount\n description\n}\n}",
|
"gql": "query ($owner:String! $name:String!){\n repository(owner:$owner, name:$name) {\n name\n forkCount\n description\n}\n}",
|
||||||
"serviceSource": "dns",
|
|
||||||
"serviceName": "github",
|
|
||||||
"servicePort": 443,
|
|
||||||
"domain": "api.github.com",
|
"domain": "api.github.com",
|
||||||
"endpoint": "/graphql",
|
"endpoint": "/graphql",
|
||||||
"timeout": 2000
|
"timeout": 2000
|
||||||
@@ -96,7 +93,7 @@ static_resources:
|
|||||||
socket_address:
|
socket_address:
|
||||||
address: 127.0.0.1
|
address: 127.0.0.1
|
||||||
port_value: 8099
|
port_value: 8099
|
||||||
- name: outbound|443||github.dns
|
- name: github
|
||||||
connect_timeout: 0.5s
|
connect_timeout: 0.5s
|
||||||
type: STRICT_DNS
|
type: STRICT_DNS
|
||||||
lb_policy: ROUND_ROBIN
|
lb_policy: ROUND_ROBIN
|
||||||
@@ -107,7 +104,7 @@ static_resources:
|
|||||||
typed_config:
|
typed_config:
|
||||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
|
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
|
||||||
load_assignment:
|
load_assignment:
|
||||||
cluster_name: outbound|443||github.dns
|
cluster_name: github
|
||||||
endpoints:
|
endpoints:
|
||||||
- lb_endpoints:
|
- lb_endpoints:
|
||||||
- endpoint:
|
- endpoint:
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ module de-graphql
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
replace github.com/alibaba/higress/plugins/wasm-go => ../..
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230410091208-df60dd43079c
|
github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230410091208-df60dd43079c
|
||||||
github.com/stretchr/testify v1.8.0
|
github.com/stretchr/testify v1.8.0
|
||||||
|
|||||||
@@ -51,11 +51,9 @@ spec:
|
|||||||
description
|
description
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serviceName: github
|
|
||||||
servicePort: 443
|
|
||||||
serviceSource: dns
|
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
configDisable: false
|
configDisable: false
|
||||||
ingress:
|
ingress:
|
||||||
- github-api
|
- github-api
|
||||||
url: oci://docker.io/2456868764/de-graphql:1.0.0
|
url: oci://docker.io/2456868764/de-graphql:1.0.0
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -42,7 +41,8 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log wrapper.
|
|||||||
gql := json.Get("gql").String()
|
gql := json.Get("gql").String()
|
||||||
endpoint := json.Get("endpoint").String()
|
endpoint := json.Get("endpoint").String()
|
||||||
timeout := json.Get("timeout").Int()
|
timeout := json.Get("timeout").Int()
|
||||||
log.Debugf("gql:%s endpoint:%s timeout:%d", gql, endpoint, timeout)
|
domain := json.Get("domain").String()
|
||||||
|
log.Debugf("gql:%s endpoint:%s timeout:%d domain:%s", gql, endpoint, timeout, domain)
|
||||||
err := config.SetGql(gql)
|
err := config.SetGql(gql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -52,48 +52,7 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log wrapper.
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config.SetTimeout(uint32(timeout))
|
config.SetTimeout(uint32(timeout))
|
||||||
serviceSource := json.Get("serviceSource").String()
|
config.SetDomain(domain)
|
||||||
serviceName := json.Get("serviceName").String()
|
|
||||||
servicePort := json.Get("servicePort").Int()
|
|
||||||
log.Debugf("serviceSource:%s serviceName:%s servicePort:%d", serviceSource, serviceName, servicePort)
|
|
||||||
if serviceName == "" || servicePort == 0 {
|
|
||||||
return errors.New("invalid service config")
|
|
||||||
}
|
|
||||||
switch serviceSource {
|
|
||||||
case "k8s":
|
|
||||||
namespace := json.Get("namespace").String()
|
|
||||||
config.SetClient(wrapper.NewClusterClient(wrapper.K8sCluster{
|
|
||||||
ServiceName: serviceName,
|
|
||||||
Namespace: namespace,
|
|
||||||
Port: servicePort,
|
|
||||||
}))
|
|
||||||
return nil
|
|
||||||
case "nacos":
|
|
||||||
namespace := json.Get("namespace").String()
|
|
||||||
config.SetClient(wrapper.NewClusterClient(wrapper.NacosCluster{
|
|
||||||
ServiceName: serviceName,
|
|
||||||
NamespaceID: namespace,
|
|
||||||
Port: servicePort,
|
|
||||||
}))
|
|
||||||
return nil
|
|
||||||
case "ip":
|
|
||||||
config.SetClient(wrapper.NewClusterClient(wrapper.StaticIpCluster{
|
|
||||||
ServiceName: serviceName,
|
|
||||||
Port: servicePort,
|
|
||||||
}))
|
|
||||||
return nil
|
|
||||||
case "dns":
|
|
||||||
domain := json.Get("domain").String()
|
|
||||||
config.SetClient(wrapper.NewClusterClient(wrapper.DnsCluster{
|
|
||||||
ServiceName: serviceName,
|
|
||||||
Port: servicePort,
|
|
||||||
Domain: domain,
|
|
||||||
}))
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return errors.New("unknown service source: " + serviceSource)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,8 +81,9 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig
|
|||||||
}
|
}
|
||||||
// Add header Content-Type: application/json
|
// Add header Content-Type: application/json
|
||||||
headers = append(headers, [2]string{"Content-Type", "application/json"})
|
headers = append(headers, [2]string{"Content-Type", "application/json"})
|
||||||
|
client := wrapper.NewClusterClient(wrapper.RouteCluster{Host: config.GetDomain()})
|
||||||
// Call upstream graphql endpoint
|
// Call upstream graphql endpoint
|
||||||
config.GetClient().Post(config.GetEndpoint(), headers, []byte(replaceBody),
|
client.Post(config.GetEndpoint(), headers, []byte(replaceBody),
|
||||||
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
||||||
// Pass response headers and body to client
|
// Pass response headers and body to client
|
||||||
headers := make([][2]string, 0, len(responseHeaders)+3)
|
headers := make([][2]string, 0, len(responseHeaders)+3)
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ package wrapper
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cluster interface {
|
type Cluster interface {
|
||||||
@@ -24,6 +26,25 @@ type Cluster interface {
|
|||||||
HostName() string
|
HostName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RouteCluster struct {
|
||||||
|
Host string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c RouteCluster) ClusterName() string {
|
||||||
|
routeName, err := proxywasm.GetProperty([]string{"cluster_name"})
|
||||||
|
if err != nil {
|
||||||
|
proxywasm.LogErrorf("get route cluster failed, err:%v", err)
|
||||||
|
}
|
||||||
|
return string(routeName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c RouteCluster) HostName() string {
|
||||||
|
if c.Host != "" {
|
||||||
|
return c.Host
|
||||||
|
}
|
||||||
|
return GetRequestHost()
|
||||||
|
}
|
||||||
|
|
||||||
type K8sCluster struct {
|
type K8sCluster struct {
|
||||||
ServiceName string
|
ServiceName string
|
||||||
Namespace string
|
Namespace string
|
||||||
|
|||||||
Reference in New Issue
Block a user