mirror of
https://github.com/alibaba/higress.git
synced 2026-03-15 22:30:47 +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 查询 | 不能为空 |
|
||||
| `endpoint` | graphql 查询端点 | `/graphql` |
|
||||
| `timeout` | 查询连接超时,单位毫秒 | `5000` |
|
||||
| `serviceSource` | 服务来源:k8s, nacos,dns, ip | 不能为空 |
|
||||
| `serviceName` | 服务名称 | 不能为空 |
|
||||
| `servicePort` | 服务端口 | 不能为空 |
|
||||
| `namespace` | 服务命名空间, 当服务来源是nacos需要配置 | |
|
||||
| `domain` | 服务域名,当服务来源是dns配置 | |
|
||||
|
||||
### 插件使用
|
||||
@@ -158,9 +154,6 @@ spec:
|
||||
config:
|
||||
timeout: 5000
|
||||
endpoint: /graphql
|
||||
serviceSource: dns
|
||||
serviceName: github
|
||||
servicePort: 443
|
||||
domain: api.github.com
|
||||
gql: |
|
||||
query ($owner:String! $name:String!){
|
||||
|
||||
@@ -16,7 +16,6 @@ package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -46,10 +45,10 @@ type Variable struct {
|
||||
}
|
||||
|
||||
type DeGraphQLConfig struct {
|
||||
client wrapper.HttpClient
|
||||
gql string
|
||||
endpoint string
|
||||
timeout uint32
|
||||
domain string
|
||||
variables []Variable
|
||||
}
|
||||
|
||||
@@ -63,6 +62,14 @@ func (d *DeGraphQLConfig) SetEndpoint(endpoint string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DeGraphQLConfig) GetDomain() string {
|
||||
return d.domain
|
||||
}
|
||||
|
||||
func (d *DeGraphQLConfig) SetDomain(domain string) {
|
||||
d.domain = domain
|
||||
}
|
||||
|
||||
func (d *DeGraphQLConfig) GetEndpoint() string {
|
||||
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 {
|
||||
if strings.TrimSpace(gql) == "" {
|
||||
return errors.New("gql can't be empty")
|
||||
|
||||
@@ -22,7 +22,7 @@ static_resources:
|
||||
- match:
|
||||
prefix: "/api"
|
||||
route:
|
||||
cluster: mock_service
|
||||
cluster: github
|
||||
http_filters:
|
||||
- name: envoy.filters.http.wasm
|
||||
typed_config:
|
||||
@@ -35,9 +35,6 @@ static_resources:
|
||||
value: |-
|
||||
{
|
||||
"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",
|
||||
"endpoint": "/graphql",
|
||||
"timeout": 2000
|
||||
@@ -96,7 +93,7 @@ static_resources:
|
||||
socket_address:
|
||||
address: 127.0.0.1
|
||||
port_value: 8099
|
||||
- name: outbound|443||github.dns
|
||||
- name: github
|
||||
connect_timeout: 0.5s
|
||||
type: STRICT_DNS
|
||||
lb_policy: ROUND_ROBIN
|
||||
@@ -107,7 +104,7 @@ static_resources:
|
||||
typed_config:
|
||||
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
|
||||
load_assignment:
|
||||
cluster_name: outbound|443||github.dns
|
||||
cluster_name: github
|
||||
endpoints:
|
||||
- lb_endpoints:
|
||||
- endpoint:
|
||||
|
||||
@@ -2,6 +2,8 @@ module de-graphql
|
||||
|
||||
go 1.19
|
||||
|
||||
replace github.com/alibaba/higress/plugins/wasm-go => ../..
|
||||
|
||||
require (
|
||||
github.com/alibaba/higress/plugins/wasm-go v0.0.0-20230410091208-df60dd43079c
|
||||
github.com/stretchr/testify v1.8.0
|
||||
|
||||
@@ -51,11 +51,9 @@ spec:
|
||||
description
|
||||
}
|
||||
}
|
||||
serviceName: github
|
||||
servicePort: 443
|
||||
serviceSource: dns
|
||||
timeout: 5000
|
||||
configDisable: false
|
||||
ingress:
|
||||
- github-api
|
||||
url: oci://docker.io/2456868764/de-graphql:1.0.0
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -42,7 +41,8 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log wrapper.
|
||||
gql := json.Get("gql").String()
|
||||
endpoint := json.Get("endpoint").String()
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -52,48 +52,7 @@ func parseConfig(json gjson.Result, config *config.DeGraphQLConfig, log wrapper.
|
||||
return err
|
||||
}
|
||||
config.SetTimeout(uint32(timeout))
|
||||
serviceSource := json.Get("serviceSource").String()
|
||||
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)
|
||||
}
|
||||
|
||||
config.SetDomain(domain)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -122,8 +81,9 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config config.DeGraphQLConfig
|
||||
}
|
||||
// Add header 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
|
||||
config.GetClient().Post(config.GetEndpoint(), headers, []byte(replaceBody),
|
||||
client.Post(config.GetEndpoint(), headers, []byte(replaceBody),
|
||||
func(statusCode int, responseHeaders http.Header, responseBody []byte) {
|
||||
// Pass response headers and body to client
|
||||
headers := make([][2]string, 0, len(responseHeaders)+3)
|
||||
|
||||
@@ -17,6 +17,8 @@ package wrapper
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
|
||||
)
|
||||
|
||||
type Cluster interface {
|
||||
@@ -24,6 +26,25 @@ type Cluster interface {
|
||||
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 {
|
||||
ServiceName string
|
||||
Namespace string
|
||||
|
||||
Reference in New Issue
Block a user