optimize plugin sdk (#1930)

This commit is contained in:
澄潭
2025-03-22 22:46:37 +08:00
committed by GitHub
parent 1812a6b0a9
commit 45fbc8b084
117 changed files with 1036 additions and 766 deletions

View File

@@ -17,7 +17,7 @@ package main
import (
"testing"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)
@@ -25,7 +25,7 @@ import (
func TestConfig(t *testing.T) {
json := gjson.Result{Type: gjson.JSON, Raw: `{"serviceSource": "k8s","serviceName": "opa","servicePort": 8181,"namespace": "example1","policy": "example1","timeout": "5s"}`}
config := &OpaConfig{}
assert.NoError(t, parseConfig(json, config, wrapper.Log{}))
assert.NoError(t, parseConfig(json, config, log.Log{}))
assert.Equal(t, config.policy, "example1")
assert.Equal(t, config.timeout, uint32(5000))
assert.NotNil(t, config.client)
@@ -45,6 +45,6 @@ func TestConfig(t *testing.T) {
for _, test := range tests {
json = gjson.Result{Type: gjson.JSON, Raw: test.raw}
assert.Equal(t, parseConfig(json, config, wrapper.Log{}) == nil, test.result)
assert.Equal(t, parseConfig(json, config, log.Log{}) == nil, test.result)
}
}

View File

@@ -23,6 +23,7 @@ import (
"strings"
"time"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
@@ -42,7 +43,7 @@ type Metadata struct {
Input map[string]interface{} `json:"input"`
}
func parseConfig(json gjson.Result, config *OpaConfig, log wrapper.Log) error {
func parseConfig(json gjson.Result, config *OpaConfig, log log.Log) error {
policy := json.Get("policy").String()
if strings.TrimSpace(policy) == "" {
return errors.New("policy not allow empty")
@@ -76,15 +77,15 @@ func parseConfig(json gjson.Result, config *OpaConfig, log wrapper.Log) error {
return nil
}
func onHttpRequestHeaders(ctx wrapper.HttpContext, config OpaConfig, log wrapper.Log) types.Action {
func onHttpRequestHeaders(ctx wrapper.HttpContext, config OpaConfig, log log.Log) types.Action {
return opaCall(ctx, config, nil, log)
}
func onHttpRequestBody(ctx wrapper.HttpContext, config OpaConfig, body []byte, log wrapper.Log) types.Action {
func onHttpRequestBody(ctx wrapper.HttpContext, config OpaConfig, body []byte, log log.Log) types.Action {
return opaCall(ctx, config, body, log)
}
func opaCall(ctx wrapper.HttpContext, config OpaConfig, body []byte, log wrapper.Log) types.Action {
func opaCall(ctx wrapper.HttpContext, config OpaConfig, body []byte, log log.Log) types.Action {
request := make(map[string]interface{}, 6)
headers, _ := proxywasm.GetHttpRequestHeaders()