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 5f03759
Showing 1 changed file with 19 additions and 14 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

0 comments on commit 5f03759

Please sign in to comment.