etcd: refactor config
- Explicitly specify all of the valid options for etcd - Remove the default name generation (ETCD_NAME is set by its unit file now) - Seperate the etcd config from Units() - Remove support for DISCOVERY_URL - Add YAML tags for the fields
This commit is contained in:
26
system/env.go
Normal file
26
system/env.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// dropinContents generates the contents for a drop-in unit given the config.
|
||||
// The argument must be a struct from the 'config' package.
|
||||
func dropinContents(e interface{}) string {
|
||||
et := reflect.TypeOf(e)
|
||||
ev := reflect.ValueOf(e)
|
||||
|
||||
var out string
|
||||
for i := 0; i < et.NumField(); i++ {
|
||||
if val := ev.Field(i).String(); val != "" {
|
||||
key := et.Field(i).Tag.Get("env")
|
||||
out += fmt.Sprintf("Environment=\"%s=%s\"\n", key, val)
|
||||
}
|
||||
}
|
||||
|
||||
if out == "" {
|
||||
return ""
|
||||
}
|
||||
return "[Service]\n" + out
|
||||
}
|
25
system/etcd.go
Normal file
25
system/etcd.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
// Etcd is a top-level structure which embeds its underlying configuration,
|
||||
// config.Etcd, and provides the system-specific Unit().
|
||||
type Etcd struct {
|
||||
config.Etcd
|
||||
}
|
||||
|
||||
// Units creates a Unit file drop-in for etcd, using any configured options.
|
||||
func (ee Etcd) Units(_ string) ([]Unit, error) {
|
||||
content := dropinContents(ee.Etcd)
|
||||
if content == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return []Unit{{
|
||||
Name: "etcd.service",
|
||||
Runtime: true,
|
||||
DropIn: true,
|
||||
Content: content,
|
||||
}}, nil
|
||||
}
|
60
system/etcd_test.go
Normal file
60
system/etcd_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
func TestEtcdUnits(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
config config.Etcd
|
||||
units []Unit
|
||||
}{
|
||||
{
|
||||
config.Etcd{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
config.Etcd{
|
||||
Discovery: "http://disco.example.com/foobar",
|
||||
PeerBindAddr: "127.0.0.1:7002",
|
||||
},
|
||||
[]Unit{{
|
||||
Name: "etcd.service",
|
||||
Runtime: true,
|
||||
DropIn: true,
|
||||
Content: `[Service]
|
||||
Environment="ETCD_DISCOVERY=http://disco.example.com/foobar"
|
||||
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
||||
`,
|
||||
}},
|
||||
},
|
||||
{
|
||||
config.Etcd{
|
||||
Name: "node001",
|
||||
Discovery: "http://disco.example.com/foobar",
|
||||
PeerBindAddr: "127.0.0.1:7002",
|
||||
},
|
||||
[]Unit{{
|
||||
Name: "etcd.service",
|
||||
Runtime: true,
|
||||
DropIn: true,
|
||||
Content: `[Service]
|
||||
Environment="ETCD_DISCOVERY=http://disco.example.com/foobar"
|
||||
Environment="ETCD_NAME=node001"
|
||||
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
||||
`,
|
||||
}},
|
||||
},
|
||||
} {
|
||||
units, err := Etcd{tt.config}.Units("")
|
||||
if err != nil {
|
||||
t.Errorf("bad error (%q): want %q, got %q", tt.config, nil, err)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.units, units) {
|
||||
t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user