Skip to content

Commit

Permalink
Move backend selection to backends package
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Jun 12, 2014
1 parent f0ee4b5 commit 30d6d8a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
31 changes: 31 additions & 0 deletions backends/store_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
package backends

import (
"errors"
"strings"

"github.com/kelseyhightower/confd/backends/consul"
"github.com/kelseyhightower/confd/backends/env"
"github.com/kelseyhightower/confd/backends/etcd/etcdutil"
"github.com/kelseyhightower/confd/config"
"github.com/kelseyhightower/confd/log"
)

// The StoreClient interface is implemented by objects that can retrieve
// key/value pairs from a backend store.
type StoreClient interface {
GetValues(keys []string) (map[string]interface{}, error)
}

// New is used to create a storage client based on our configuration.
func New(backend string) (StoreClient, error) {
if backend == "" {
backend = "etcd"
}
switch backend {
case "consul":
log.Notice("Consul address set to " + config.ConsulAddr())
return consul.NewConsulClient(config.ConsulAddr())
case "etcd":
// Create the etcd client upfront and use it for the life of the process.
// The etcdClient is an http.Client and designed to be reused.
log.Notice("etcd nodes set to " + strings.Join(config.EtcdNodes(), ", "))
return etcdutil.NewEtcdClient(config.EtcdNodes(), config.ClientCert(), config.ClientKey(), config.ClientCaKeys())
case "env":
return env.NewEnvClient()
}
return nil, errors.New("Invalid backend")
}
30 changes: 2 additions & 28 deletions confd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"strings"
"time"

"github.com/kelseyhightower/confd/backends"
"github.com/kelseyhightower/confd/backends/consul"
"github.com/kelseyhightower/confd/backends/env"
"github.com/kelseyhightower/confd/backends/etcd/etcdutil"
"github.com/kelseyhightower/confd/config"
"github.com/kelseyhightower/confd/log"
"github.com/kelseyhightower/confd/resource/template"
Expand Down Expand Up @@ -60,7 +55,8 @@ func main() {
log.Notice("Starting confd")

// Create the storage client
store, err := createStoreClient(config.Backend())
log.Notice("Backend set to " + config.Backend())
store, err := backends.New(config.Backend())
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -79,28 +75,6 @@ func main() {
}
}

// createStoreClient is used to create a storage client based
// on our configuration. Either an etcd or Consul client.
func createStoreClient(backend string) (backends.StoreClient, error) {
log.Notice("Backend set to " + backend)
if backend == "" {
backend = "etcd"
}
switch backend {
case "consul":
log.Notice("Consul address set to " + config.ConsulAddr())
return consul.NewConsulClient(config.ConsulAddr())
case "etcd":
// Create the etcd client upfront and use it for the life of the process.
// The etcdClient is an http.Client and designed to be reused.
log.Notice("etcd nodes set to " + strings.Join(config.EtcdNodes(), ", "))
return etcdutil.NewEtcdClient(config.EtcdNodes(), config.ClientCert(), config.ClientKey(), config.ClientCaKeys())
case "env":
return env.NewEnvClient()
}
return nil, errors.New("Invalid backend")
}

// IsFileExist reports whether path exits.
func IsFileExist(fpath string) bool {
if _, err := os.Stat(fpath); os.IsNotExist(err) {
Expand Down
2 changes: 1 addition & 1 deletion integration/etcd/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ curl -L -X PUT http://${etcd_host}:${etcd_port}/v2/keys/upstream/app1 -d value=1
curl -L -X PUT http://${etcd_host}:${etcd_port}/v2/keys/upstream/app2 -d value=10.0.1.11:8080

# Run confd
./confd -debug -onetime -confdir ./integration/confdir -backend etcd -node "http://${etcd_host}:${etcd_port}"
./confd -onetime -confdir ./integration/confdir -backend etcd -node "http://${etcd_host}:${etcd_port}"
2 changes: 1 addition & 1 deletion resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"text/template"

"github.com/BurntSushi/toml"
"github.com/kelseyhightower/confd/backends"
"github.com/kelseyhightower/confd/config"
"github.com/kelseyhightower/confd/log"
"github.com/kelseyhightower/confd/node"
"github.com/kelseyhightower/confd/backends"
)

// TemplateResourceConfig holds the parsed template resource.
Expand Down

0 comments on commit 30d6d8a

Please sign in to comment.