diff --git a/sd/nginx/nginx.go b/sd/nginx/nginx.go index 4729d2298..e7d86d250 100644 --- a/sd/nginx/nginx.go +++ b/sd/nginx/nginx.go @@ -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 ( @@ -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 @@ -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 @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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 diff --git a/sd/nginx/nginx_test.go b/sd/nginx/nginx_test.go index 076c4736e..b1ffcd7f2 100644 --- a/sd/nginx/nginx_test.go +++ b/sd/nginx/nginx_test.go @@ -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("", "") @@ -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, ) @@ -74,7 +74,7 @@ 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, ) @@ -82,7 +82,7 @@ func TestNginx(t *testing.T) { 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, ) @@ -93,7 +93,7 @@ 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, ) @@ -101,7 +101,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, ) @@ -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, ) @@ -127,8 +127,8 @@ 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, ) @@ -136,9 +136,9 @@ func TestNginx(t *testing.T) { 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, ) @@ -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, ) @@ -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("", "") @@ -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, ) @@ -214,9 +214,9 @@ 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, ) @@ -224,9 +224,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":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, ) @@ -237,9 +237,9 @@ 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, ) @@ -247,9 +247,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, ) @@ -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, ) @@ -277,12 +277,12 @@ 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, ) @@ -290,15 +290,15 @@ func TestNginxDC(t *testing.T) { 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, ) @@ -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, )