Skip to content

Commit

Permalink
added the ability to discover backend nodes via srv records
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Dec 7, 2016
1 parent eff0086 commit a1e90d3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin/
remco
18 changes: 15 additions & 3 deletions backends/consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package consul
import (
"github.com/HeavyHorst/easyKV/consul"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/backends/srvRecord"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
Expand All @@ -20,9 +21,10 @@ import (
type Config struct {
Nodes []string
Scheme string
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientCaKeys string `toml:"client_ca_keys"`
SRVRecord srvRecord.Record `toml:"srv_record"`
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientCaKeys string `toml:"client_ca_keys"`
template.Backend
}

Expand All @@ -32,6 +34,16 @@ func (c *Config) Connect() (template.Backend, error) {
return template.Backend{}, berr.ErrNilConfig
}
c.Backend.Name = "consul"

// No nodes are set but a SRVRecord is provided
if len(c.Nodes) == 0 && c.SRVRecord != "" {
var err error
c.Nodes, err = c.SRVRecord.GetNodesFromSRV("")
if err != nil {
return c.Backend, err
}
}

log.WithFields(logrus.Fields{
"backend": c.Backend.Name,
"nodes": c.Nodes,
Expand Down
27 changes: 24 additions & 3 deletions backends/etcd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package etcd
import (
"github.com/HeavyHorst/easyKV/etcd"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/backends/srvRecord"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
Expand All @@ -19,9 +20,11 @@ import (
// Config represents the config for the etcd backend.
type Config struct {
Nodes []string
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientCaKeys string `toml:"client_ca_keys"`
Scheme string //only needed for etcdv2 with SRVRecord
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientCaKeys string `toml:"client_ca_keys"`
SRVRecord srvRecord.Record `toml:"srv_record"`
Username string
Password string
Version int
Expand All @@ -45,6 +48,24 @@ func (c *Config) Connect() (template.Backend, error) {
c.Backend.Name = "etcd"
}

// No nodes are set but a SRVRecord is provided
if len(c.Nodes) == 0 && c.SRVRecord != "" {
var err error
if c.Version != 2 {
// no scheme required for etcdv3
c.Scheme = ""
} else if c.Scheme == "" {
// etcd version is 2 and no scheme is provided
// use http as default value
c.Scheme = "http"
}
c.Nodes, err = c.SRVRecord.GetNodesFromSRV(c.Scheme)

if err != nil {
return c.Backend, err
}
}

log.WithFields(logrus.Fields{
"backend": c.Backend.Name,
"nodes": c.Nodes,
Expand Down
18 changes: 15 additions & 3 deletions backends/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ package redis
import (
"github.com/HeavyHorst/easyKV/redis"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/backends/srvRecord"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
)

// Config represents the config for the redis backend.
type Config struct {
Nodes []string
Password string
Database int
Nodes []string
SRVRecord srvRecord.Record `toml:"srv_record"`
Password string
Database int
template.Backend
}

Expand All @@ -31,6 +33,16 @@ func (c *Config) Connect() (template.Backend, error) {
}

c.Backend.Name = "redis"

// No nodes are set but a SRVRecord is provided
if len(c.Nodes) == 0 && c.SRVRecord != "" {
var err error
c.Nodes, err = c.SRVRecord.GetNodesFromSRV("")
if err != nil {
return c.Backend, err
}
}

log.WithFields(logrus.Fields{
"backend": c.Backend.Name,
"nodes": c.Nodes,
Expand Down
40 changes: 40 additions & 0 deletions backends/srvRecord/srvRecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of remco.
* © 2016 The Remco Authors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

package srvRecord

import (
"fmt"
"net"
"strings"
)

// Record is a SRV-Record string
// for example _etcd-client._tcp.example.com.
type Record string

// GetNodesFromSRV returns the nodes stored in the record
func (r Record) GetNodesFromSRV(scheme string) ([]string, error) {
var nodes []string
_, addrs, err := net.LookupSRV("", "", string(r))
if err != nil {
return nodes, err
}

for _, srv := range addrs {
port := fmt.Sprintf("%d", srv.Port)
host := strings.TrimRight(srv.Target, ".")
host = net.JoinHostPort(host, port)
if scheme != "" {
host = fmt.Sprintf("%s://%s", scheme, host)
}

nodes = append(nodes, host)
}
return nodes, nil
}
14 changes: 13 additions & 1 deletion backends/zookeeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ package zookeeper
import (
"github.com/HeavyHorst/easyKV/zookeeper"
berr "github.com/HeavyHorst/remco/backends/error"
"github.com/HeavyHorst/remco/backends/srvRecord"
"github.com/HeavyHorst/remco/log"
"github.com/HeavyHorst/remco/template"
"github.com/Sirupsen/logrus"
)

// Config represents the config for the consul backend.
type Config struct {
Nodes []string
Nodes []string
SRVRecord srvRecord.Record `toml:"srv_record"`
template.Backend
}

Expand All @@ -29,6 +31,16 @@ func (c *Config) Connect() (template.Backend, error) {
}

c.Backend.Name = "zookeeper"

// No nodes are set but a SRVRecord is provided
if len(c.Nodes) == 0 && c.SRVRecord != "" {
var err error
c.Nodes, err = c.SRVRecord.GetNodesFromSRV("")
if err != nil {
return c.Backend, err
}
}

log.WithFields(logrus.Fields{
"backend": c.Backend.Name,
"nodes": c.Nodes,
Expand Down
24 changes: 17 additions & 7 deletions docs/content/config/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,27 @@ weight: 10
<details>
<summary> **valid in every backend** </summary>

- **keys([]string):**
- The backend keys that the template requires to be rendered correctly. The child keys are also loaded.
- **watch(bool, optional):**
- Enable watch support. Default is false.
- **prefix(string):**
- **prefix(string, optional):**
- Key path prefix. Default is "".
- **interval(int):**
- **interval(int, optional):**
- The backend polling interval. Can be used as a reconcilation loop for watch or standalone.
- **onetime(bool, optional):**
- Render the config file and quit. Default is false.
- **keys([]string):**
- The backend keys that the template requires to be rendered correctly. The child keys are also loaded.
</details>

<details>
<summary> **etcd** </summary>

- **nodes([]string):**
- List of backend nodes.
- **srv_record(string, optional):**
- A DNS server record to discover the etcd nodes.
- **scheme(string, optional):**
- The backend URI scheme (http or https). This is only used when the nodes are discovered via DNS srv records and the api level is 2. Default is http.
- **client_cert(string, optional):**
- The client cert file.
- **client_key(string, optional):**
Expand All @@ -93,9 +97,11 @@ weight: 10
<summary> **consul** </summary>

- **nodes([]string):**
- List of backend nodes.
- List of backend nodes.
- **srv_record(string, optional):**
- A DNS server record to discover the consul nodes.
- **scheme(string):**
- the backend URI scheme (http or https).
- The backend URI scheme (http or https).
- **client_cert(string, optional):**
- The client cert file.
- **client_key(string, optional):**
Expand All @@ -116,6 +122,8 @@ weight: 10

- **nodes([]string):**
- List of backend nodes.
- **srv_record(string), optional:**
- A DNS server record to discover the redis nodes.
- **password(string, optional):**
- The redis password.
- **database(int, optional):**
Expand All @@ -126,7 +134,7 @@ weight: 10
<summary> **vault** </summary>

- **node(string):**
- The backend node.
- The backend node.
- **auth_type(string):**
- The vault authentication type. (token, approle, app-id, userpass, github)
- **auth_token(string):**
Expand Down Expand Up @@ -161,4 +169,6 @@ weight: 10

- **nodes([]string):**
- List of backend nodes.
- **srv_record(string, optional):**
- A DNS server record to discover the zookeeper nodes.
</details>

0 comments on commit a1e90d3

Please sign in to comment.