Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support search route by label #1061

Merged
merged 5 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ type GetInput struct {
// description: uri of route
// required: false
// type: string
// - name: label
// in: query
// description: label of route
// required: false
// type: string
// responses:
// '0':
// description: list response
Expand Down Expand Up @@ -141,8 +146,9 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
}

type ListInput struct {
Name string `auto_read:"name,query"`
URI string `auto_read:"uri,query"`
Name string `auto_read:"name,query"`
URI string `auto_read:"uri,query"`
Label string `auto_read:"label,query"`
store.Pagination
}

Expand All @@ -161,21 +167,26 @@ func uriContains(obj *entity.Route, uri string) bool {

func (h *Handler) List(c droplet.Context) (interface{}, error) {
input := c.Input().(*ListInput)
labelMap, err := utils.GenLabelMap(input.Label)
if err != nil {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("%s: \"%s\"", err.Error(), input.Label)
}

ret, err := h.routeStore.List(store.ListInput{
Predicate: func(obj interface{}) bool {
if input.Name != "" && input.URI != "" {
if strings.Contains(obj.(*entity.Route).Name, input.Name) {
return uriContains(obj.(*entity.Route), input.URI)
}
if input.Name != "" && !strings.Contains(obj.(*entity.Route).Name, input.Name) {
return false
}
if input.Name != "" {
return strings.Contains(obj.(*entity.Route).Name, input.Name)

if input.URI != "" && !uriContains(obj.(*entity.Route), input.URI) {
return false
}
if input.URI != "" {
return uriContains(obj.(*entity.Route), input.URI)

if input.Label != "" && !utils.LabelContains(obj.(*entity.Route).Labels, labelMap) {
return false
}

return true
},
Format: func(obj interface{}) interface{} {
Expand Down
117 changes: 99 additions & 18 deletions api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ func TestRoute(t *testing.T) {
"hosts": ["foo.com", "*.bar.com"],
"remote_addrs": ["127.0.0.0/8"],
"methods": ["PUT", "GET"],
"labels": {
"l1": "v1",
"l2": "v2"
},
"upstream": {
"type": "roundrobin",
"nodes": [{
Expand Down Expand Up @@ -765,38 +769,115 @@ func TestRoute(t *testing.T) {
assert.Equal(t, len(dataPage.Rows), 1)

//list search match
listInput2 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "uri": "index"}`
err = json.Unmarshal([]byte(reqBody), listInput2)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput2)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search name not match
listInput3 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "not-exists", "uri": "index"}`
err = json.Unmarshal([]byte(reqBody), listInput3)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput3)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//list search uri not match
listInput4 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "uri": "not-exists"}`
err = json.Unmarshal([]byte(reqBody), listInput4)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//list search label not match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "label":"l3"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//list search label match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "label":"l1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search label match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "label":"l1:v1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search and label not match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "label":"l1:v2"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput4)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//list search with name and label
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "label":"l1:v1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search with uri and label
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "uri": "index", "label":"l1:v1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search with uri,name and label
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "uri": "index", "label":"l1:v1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//create route using uris
route3 := &entity.Route{}
reqBody = `{
Expand All @@ -821,11 +902,11 @@ func TestRoute(t *testing.T) {
time.Sleep(time.Duration(100) * time.Millisecond)

//list search match uris
listInput5 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "bbb", "uri": "bb"}`
err = json.Unmarshal([]byte(reqBody), listInput5)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput5)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
Expand Down Expand Up @@ -959,20 +1040,20 @@ func TestRoute(t *testing.T) {
assert.Equal(t, "11", stored.ID)

//list
listInput11 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 10, "page": 1}`
err = json.Unmarshal([]byte(reqBody), listInput11)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput11)
ctx.SetInput(listInput)
_, err = handler.List(ctx)
assert.Nil(t, err)

//list search match
listInput12 := &ListInput{}
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "uri": "r11"}`
err = json.Unmarshal([]byte(reqBody), listInput12)
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput12)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
Expand Down
48 changes: 48 additions & 0 deletions api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package utils

import (
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"

"github.com/sony/sonyflake"
)
Expand Down Expand Up @@ -94,3 +96,49 @@ func InterfaceToString(val interface{}) string {
str := fmt.Sprintf("%v", val)
return str
}

func GenLabelMap(label string) (map[string]string, error) {
var err = errors.New("malformed label")
mp := make(map[string]string)

if label == "" {
return mp, nil
}

labels := strings.Split(label, ",")
for _, l := range labels {
kv := strings.Split(l, ":")
if len(kv) == 2 {
if kv[0] == "" || kv[1] == "" {
return nil, err
}

mp[kv[0]] = kv[1]
} else if len(kv) == 1 {
if kv[0] == "" {
return nil, err
}

mp[kv[0]] = ""
} else {
return nil, err
}
tokers marked this conversation as resolved.
Show resolved Hide resolved
}

return mp, nil
}

func LabelContains(labels, reqLabels map[string]string) bool {
if len(reqLabels) == 0 {
return true
}

for k, v := range labels {
l, exist := reqLabels[k]
if exist && ((l == "") || v == l) {
return true
}
}

return false
}
40 changes: 40 additions & 0 deletions api/internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package utils

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -42,3 +43,42 @@ func TestSumIPs_with_nil(t *testing.T) {
total := sumIPs(nil)
assert.Equal(t, uint16(0), total)
}

func TestGenLabelMap(t *testing.T) {
expectedErr := errors.New("malformed label")
mp, err := GenLabelMap("l1")
assert.Nil(t, err)
assert.Equal(t, mp["l1"], "")

mp, err = GenLabelMap("l1,l2:v2")
assert.Nil(t, err)
assert.Equal(t, mp["l1"], "")
assert.Equal(t, mp["l2"], "v2")

mp, err = GenLabelMap(",")
assert.Equal(t, expectedErr, err)
assert.Nil(t, mp)

mp, err = GenLabelMap(",l2:,")
assert.Equal(t, expectedErr, err)
assert.Nil(t, mp)
}

func TestLabelContains(t *testing.T) {
mp1, _ := GenLabelMap("l1,l2:v2")
mp2 := map[string]string{
"l1": "v1",
}
assert.True(t, LabelContains(mp2, mp1))

mp3 := map[string]string{
"l1": "v1",
"l2": "v3",
}
assert.True(t, LabelContains(mp3, mp1))

mp4 := map[string]string{
"l2": "v3",
}
assert.False(t, LabelContains(mp4, mp1))
}
Loading