Skip to content

Commit

Permalink
fix(sd): default namespace is graphite
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf1980 committed Nov 2, 2023
1 parent c47e8c8 commit 8837434
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
33 changes: 19 additions & 14 deletions sd/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

type ErrInvalidKey struct {
key string
val string
}

func (e ErrInvalidKey) Error() string {
if e.val == "" {
return "list key is invalid"
return "list key '" + e.key + "' is invalid"
}
return "list key is invalid: '" + e.val + "'"
return "list key '" + e.key + "' is invalid: '" + e.val + "'"
}

var (
Expand Down Expand Up @@ -50,6 +51,7 @@ func splitNode(node string) (dc, host, listen string, ok bool) {
type Nginx struct {
weight int64
hostname string
namespace string
body []byte
backupBody []byte
url stringutils.Builder
Expand All @@ -60,21 +62,24 @@ type Nginx struct {
}

func New(url, namespace, hostname string, logger *zap.Logger) *Nginx {
if namespace == "" {
namespace = "graphite"
}
sd := &Nginx{
logger: logger,
body: make([]byte, 128),
backupBody: []byte(`{"backup":1,"max_fails":0}`),
nsEnd: "upstreams/" + namespace + "/",
hostname: hostname,
namespace: namespace,
nsEnd: "upstreams/" + namespace + "/",
}
sd.setWeight(0)

sd.url.WriteString(url)
sd.url.WriteByte('/')
if namespace != "" {
sd.url.WriteString(namespace)
sd.url.WriteByte('/')
}

sd.url.WriteString(namespace)
sd.url.WriteByte('/')
sd.pos = sd.url.Len()

return sd
Expand Down Expand Up @@ -109,13 +114,13 @@ func (sd *Nginx) List() (nodes []string, err error) {
if i, ok := jNode["Key"]; ok {
if s, ok := i.(string); ok {
if strings.HasPrefix(s, sd.nsEnd) {
s = s[len(sd.nsEnd):]
s = s[len(sd.namespace)+2:]
_, host, _, ok := splitNode(s)
if ok && host == sd.hostname {
nodes = append(nodes, s)
}
} else {
return nil, ErrInvalidKey{s}
return nil, ErrInvalidKey{key: sd.nsEnd, val: s}
}
} else {
return nil, ErrNoKey
Expand Down Expand Up @@ -147,8 +152,8 @@ func (sd *Nginx) ListMap() (nodes map[string]string, err error) {
if jNode, ok := i.(map[string]interface{}); ok {
if i, ok := jNode["Key"]; ok {
if s, ok := i.(string); ok {
if strings.HasPrefix(s, sd.nsEnd) {
s = s[len(sd.nsEnd):]
if strings.HasPrefix(s, sd.namespace) {
s = s[len(sd.namespace)+2:]
_, host, _, ok := splitNode(s)
if ok && host == sd.hostname {
if i, ok := jNode["Value"]; ok {
Expand All @@ -166,7 +171,7 @@ func (sd *Nginx) ListMap() (nodes map[string]string, err error) {
}
}
} else {
return nil, ErrInvalidKey{s}
return nil, ErrInvalidKey{key: sd.nsEnd, val: s}
}
} else {
return nil, ErrNoKey
Expand Down Expand Up @@ -199,7 +204,7 @@ func (sd *Nginx) Nodes() (nodes []utils.KV, err error) {
if i, ok := jNode["Key"]; ok {
if s, ok := i.(string); ok {
if strings.HasPrefix(s, sd.nsEnd) {
s = s[len(sd.nsEnd):]
s = s[len(sd.namespace)+2:]
kv := utils.KV{Key: s}
if i, ok := jNode["Value"]; ok {
if v, ok := i.(string); ok {
Expand All @@ -222,7 +227,7 @@ func (sd *Nginx) Nodes() (nodes []utils.KV, err error) {
}
nodes = append(nodes, kv)
} else {
return nil, ErrInvalidKey{s}
return nil, ErrInvalidKey{key: sd.nsEnd, val: s}
}
} else {
return nil, ErrNoKey
Expand Down
108 changes: 54 additions & 54 deletions sd/nginx/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNginx(t *testing.T) {

logger := zapwriter.Default()

sd1 := New("http://127.0.0.1:8500/v1/kv/upstreams", "graphite", hostname1, logger)
sd1 := New("http://127.0.0.1:8500/v1/kv/upstreams", "", hostname1, logger)
sd2 := New("http://127.0.0.1:8500/v1/kv/upstreams", "graphite", hostname2, logger)

err := sd1.Clear("", "")
Expand All @@ -56,14 +56,14 @@ func TestNginx(t *testing.T) {
sort.Strings(nodes)
assert.Equal(
t, []string{
"_/test_host1/192.168.0.1:9090",
"graphite/test_host1/192.168.0.1:9090",
}, nodes,
)
nodesMap, err := sd1.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"graphite/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -74,15 +74,15 @@ func TestNginx(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, []string{
"_/test_host2/192.168.1.25:9090",
"graphite/test_host2/192.168.1.25:9090",
},
nodes,
)
nodesMap, err = sd2.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host2/192.168.1.25:9090": `{"weight":21,"max_fails":0}`,
"graphite/test_host2/192.168.1.25:9090": `{"weight":21,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -93,15 +93,15 @@ func TestNginx(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, []string{
"_/test_host2/192.168.1.25:9090",
"graphite/test_host2/192.168.1.25:9090",
},
nodes,
)
nodesMap, err = sd2.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -116,7 +116,7 @@ func TestNginx(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"graphite/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -127,18 +127,18 @@ func TestNginx(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"_/test_host2/192.168.0.1:9090": `{"weight":25,"max_fails":0}`,
"graphite/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/test_host2/192.168.0.1:9090": `{"weight":25,"max_fails":0}`,
}, nodesMap,
)

nodesV, err := sd2.Nodes()
require.NoError(t, err)
assert.Equal(
t, []utils.KV{
{Key: "_/test_host1/192.168.0.1:9090", Value: `{"weight":10,"max_fails":0}`, Flags: 1682408721},
{Key: "_/test_host2/192.168.0.1:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "_/test_host2/192.168.1.25:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/test_host1/192.168.0.1:9090", Value: `{"weight":10,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/test_host2/192.168.0.1:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/test_host2/192.168.1.25:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
}, nodesV,
)

Expand All @@ -147,7 +147,7 @@ func TestNginx(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"_/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -170,7 +170,7 @@ func TestNginxDC(t *testing.T) {

logger := zapwriter.Default()

sd1 := New("http://127.0.0.1:8500/v1/kv/upstreams", "graphite", hostname1, logger)
sd1 := New("http://127.0.0.1:8500/v1/kv/upstreams", "", hostname1, logger)
sd2 := New("http://127.0.0.1:8500/v1/kv/upstreams", "graphite", hostname2, logger)

err := sd1.Clear("", "")
Expand All @@ -192,18 +192,18 @@ func TestNginxDC(t *testing.T) {
sort.Strings(nodes)
assert.Equal(
t, []string{
"dc1/test_host1/192.168.0.1:9090",
"dc2/test_host1/192.168.0.1:9090",
"dc3/test_host1/192.168.0.1:9090",
"graphite/dc1/test_host1/192.168.0.1:9090",
"graphite/dc2/test_host1/192.168.0.1:9090",
"graphite/dc3/test_host1/192.168.0.1:9090",
}, nodes,
)
nodesMap, err := sd1.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc1/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"dc2/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc1/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"graphite/dc2/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -214,19 +214,19 @@ func TestNginxDC(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, []string{
"dc1/test_host2/192.168.1.25:9090",
"dc2/test_host2/192.168.1.25:9090",
"dc3/test_host2/192.168.1.25:9090",
"graphite/dc1/test_host2/192.168.1.25:9090",
"graphite/dc2/test_host2/192.168.1.25:9090",
"graphite/dc3/test_host2/192.168.1.25:9090",
},
nodes,
)
nodesMap, err = sd2.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc2/test_host2/192.168.1.25:9090": `{"weight":21,"max_fails":0}`,
"dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc2/test_host2/192.168.1.25:9090": `{"weight":21,"max_fails":0}`,
"graphite/dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -237,19 +237,19 @@ func TestNginxDC(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, []string{
"dc1/test_host2/192.168.1.25:9090",
"dc2/test_host2/192.168.1.25:9090",
"dc3/test_host2/192.168.1.25:9090",
"graphite/dc1/test_host2/192.168.1.25:9090",
"graphite/dc2/test_host2/192.168.1.25:9090",
"graphite/dc3/test_host2/192.168.1.25:9090",
},
nodes,
)
nodesMap, err = sd2.ListMap()
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -264,9 +264,9 @@ func TestNginxDC(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc1/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"dc2/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc1/test_host1/192.168.0.1:9090": `{"weight":10,"max_fails":0}`,
"graphite/dc2/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host1/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

Expand All @@ -277,28 +277,28 @@ func TestNginxDC(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"dc2/test_host2/192.168.0.1:9090": `{"weight":25,"max_fails":0}`,
"dc1/test_host2/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host2/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc2/test_host2/192.168.0.1:9090": `{"weight":25,"max_fails":0}`,
"graphite/dc1/test_host2/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host2/192.168.0.1:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

nodesV, err := sd2.Nodes()
require.NoError(t, err)
assert.Equal(
t, []utils.KV{
{Key: "dc1/test_host1/192.168.0.1:9090", Value: `{"weight":10,"max_fails":0}`, Flags: 1682408721},
{Key: "dc1/test_host2/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "dc1/test_host2/192.168.1.25:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "dc2/test_host1/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "dc2/test_host2/192.168.0.1:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "dc2/test_host2/192.168.1.25:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "dc3/test_host1/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "dc3/test_host2/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "dc3/test_host2/192.168.1.25:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc1/test_host1/192.168.0.1:9090", Value: `{"weight":10,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc1/test_host2/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc1/test_host2/192.168.1.25:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc2/test_host1/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc2/test_host2/192.168.0.1:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc2/test_host2/192.168.1.25:9090", Value: `{"weight":25,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc3/test_host1/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc3/test_host2/192.168.0.1:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
{Key: "graphite/dc3/test_host2/192.168.1.25:9090", Value: `{"backup":1,"max_fails":0}`, Flags: 1682408721},
}, nodesV,
)

Expand All @@ -307,9 +307,9 @@ func TestNginxDC(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t, map[string]string{
"dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc2/test_host2/192.168.1.25:9090": `{"weight":25,"max_fails":0}`,
"graphite/dc1/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
"graphite/dc3/test_host2/192.168.1.25:9090": `{"backup":1,"max_fails":0}`,
}, nodesMap,
)

Expand Down

0 comments on commit 8837434

Please sign in to comment.