Skip to content

Commit

Permalink
get upstream serviceSource from RouteCluster information and update d…
Browse files Browse the repository at this point in the history
…ocs (#337)
  • Loading branch information
2456868764 authored May 19, 2023
1 parent e4a47df commit 625c06e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 71 deletions.
7 changes: 0 additions & 7 deletions plugins/wasm-go/extensions/de-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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配置 | |

### 插件使用
Expand Down Expand Up @@ -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!){
Expand Down
19 changes: 9 additions & 10 deletions plugins/wasm-go/extensions/de-graphql/config/degraphql_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package config

import (
"errors"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -46,10 +45,10 @@ type Variable struct {
}

type DeGraphQLConfig struct {
client wrapper.HttpClient
gql string
endpoint string
timeout uint32
domain string
variables []Variable
}

Expand All @@ -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
}
Expand All @@ -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")
Expand Down
9 changes: 3 additions & 6 deletions plugins/wasm-go/extensions/de-graphql/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static_resources:
- match:
prefix: "/api"
route:
cluster: mock_service
cluster: github
http_filters:
- name: envoy.filters.http.wasm
typed_config:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions plugins/wasm-go/extensions/de-graphql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions plugins/wasm-go/extensions/de-graphql/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

50 changes: 5 additions & 45 deletions plugins/wasm-go/extensions/de-graphql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"errors"
"fmt"
"net/http"

Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions plugins/wasm-go/pkg/wrapper/cluster_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,34 @@ package wrapper
import (
"fmt"
"strings"

"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
)

type Cluster interface {
ClusterName() 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 {
ServiceName string
Namespace string
Expand Down

0 comments on commit 625c06e

Please sign in to comment.