Skip to content

Commit

Permalink
fix: nodes convert failed (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan authored Aug 17, 2022
1 parent 356b220 commit 35c9f6b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
45 changes: 45 additions & 0 deletions pkg/apisix/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,51 @@ func TestItemConvertRoute(t *testing.T) {
assert.Equal(t, r.Name, "unknown")
}

func TestItemConvertUpstream(t *testing.T) {
ite := &item{
Key: "/apisix/upstreams/419655639963271872",
Value: json.RawMessage(`{ "nodes":{"httpbin.org:80":1, "foo.com:8080": 2}}`),
}
ups, err := ite.upstream()
assert.Nil(t, err)
assert.Len(t, ups.Nodes, 2)
assert.Equal(t, ups.Nodes[0], v1.UpstreamNode{Host: "httpbin.org", Port: 80, Weight: 1})
assert.Equal(t, ups.Nodes[1], v1.UpstreamNode{Host: "foo.com", Port: 8080, Weight: 2})

ite = &item{
Key: "/apisix/upstreams/419655639963271872",
Value: json.RawMessage(`
{
"id": "419655639963271872",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 1
},
{
"host": "httpbin.com",
"port": 8080,
"weight": 1
}
]
}`),
}
ups, err = ite.upstream()
assert.Nil(t, err)
assert.Len(t, ups.Nodes, 2)
assert.Equal(t, ups.Nodes[0], v1.UpstreamNode{Host: "httpbin.org", Port: 80, Weight: 1})
assert.Equal(t, ups.Nodes[1], v1.UpstreamNode{Host: "httpbin.com", Port: 8080, Weight: 1})

ite = &item{
Key: "/apisix/upstreams/419655639963271872",
Value: json.RawMessage(`{ "id":"419655639963271872" }`),
}
ups, err = ite.upstream()
assert.Nil(t, err)
assert.Len(t, ups.Nodes, 0)
}

func TestRouteVarsUnmarshalJSONCompatibility(t *testing.T) {
var route v1.Route
data := `{"vars":{}}`
Expand Down
43 changes: 39 additions & 4 deletions pkg/types/apisix/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -216,21 +217,55 @@ type UpstreamNodes []UpstreamNode
// and by default empty array will be encoded as '{}'.
// We have to maintain the compatibility.
func (n *UpstreamNodes) UnmarshalJSON(p []byte) error {
var data []UpstreamNode
if p[0] == '{' {
if len(p) != 2 {
return errors.New("unexpected non-empty object")
value := map[string]float64{}
if err := json.Unmarshal(p, &value); err != nil {
return err
}
for k, v := range value {
node, err := mapKV2Node(k, v)
if err != nil {
return err
}
data = append(data, *node)
}
*n = UpstreamNodes{}
*n = data
return nil
}
var data []UpstreamNode
if err := json.Unmarshal(p, &data); err != nil {
return err
}
*n = data
return nil
}

func mapKV2Node(key string, val float64) (*UpstreamNode, error) {
hp := strings.Split(key, ":")
host := hp[0]
// according to APISIX upstream nodes policy, port is required
port := "80"

if len(hp) > 2 {
return nil, errors.New("invalid upstream node")
} else if len(hp) == 2 {
port = hp[1]
}

portInt, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("parse port to int fail: %s", err.Error())
}

node := &UpstreamNode{
Host: host,
Port: portInt,
Weight: int(val),
}

return node, nil
}

// UpstreamNode is the node in upstream
// +k8s:deepcopy-gen=true
type UpstreamNode struct {
Expand Down

0 comments on commit 35c9f6b

Please sign in to comment.