fleet: refactor config
- Explicitly specify all of the valid options for fleet - Seperate the config from Units() - Add YAML tags for the fields
This commit is contained in:
parent
c255739a93
commit
9454522033
14
config/fleet.go
Normal file
14
config/fleet.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type Fleet struct {
|
||||||
|
AgentTTL string `yaml:"agent-ttl" env:"FLEET_AGENT_TTL"`
|
||||||
|
EngineReconcileInterval string `yaml:"engine-reconcile-interval" env:"FLEET_ENGINE_RECONCILE_INTERVAL"`
|
||||||
|
EtcdCAFile string `yaml:"etcd-cafile" env:"FLEET_ETCD_CAFILE"`
|
||||||
|
EtcdCertFile string `yaml:"etcd-certfile" env:"FLEET_ETCD_CERTFILE"`
|
||||||
|
EtcdKeyFile string `yaml:"etcd-keyfile" env:"FLEET_ETCD_KEYFILE"`
|
||||||
|
EtcdRequestTimeout string `yaml:"etcd-request-timeout" env:"FLEET_ETCD_REQUEST_TIMEOUT"`
|
||||||
|
EtcdServers string `yaml:"etcd-servers" env:"FLEET_ETCD_SERVERS"`
|
||||||
|
Metadata string `yaml:"metadata" env:"FLEET_METADATA"`
|
||||||
|
PublicIP string `yaml:"public-ip" env:"FLEET_PUBLIC_IP"`
|
||||||
|
Verbosity string `yaml:"verbosity" env:"FLEET_VERBOSITY"`
|
||||||
|
}
|
@ -32,7 +32,7 @@ type CloudConfig struct {
|
|||||||
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
|
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
|
||||||
Coreos struct {
|
Coreos struct {
|
||||||
Etcd config.Etcd
|
Etcd config.Etcd
|
||||||
Fleet FleetEnvironment
|
Fleet config.Fleet
|
||||||
OEM OEMRelease
|
OEM OEMRelease
|
||||||
Update UpdateConfig
|
Update UpdateConfig
|
||||||
Units []system.Unit
|
Units []system.Unit
|
||||||
@ -227,7 +227,7 @@ func Apply(cfg CloudConfig, env *Environment) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ccu := range []CloudConfigUnit{system.Etcd{cfg.Coreos.Etcd}, cfg.Coreos.Fleet, cfg.Coreos.Update} {
|
for _, ccu := range []CloudConfigUnit{system.Etcd{cfg.Coreos.Etcd}, system.Fleet{cfg.Coreos.Fleet}, cfg.Coreos.Update} {
|
||||||
u, err := ccu.Units(env.Root())
|
u, err := ccu.Units(env.Root())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -104,16 +104,3 @@ func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
|
|||||||
return &ef
|
return &ef
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// normalizeSvcEnv standardizes the keys of the map (environment variables for a service)
|
|
||||||
// by replacing any dashes with underscores and ensuring they are entirely upper case.
|
|
||||||
// For example, "some-env" --> "SOME_ENV"
|
|
||||||
func normalizeSvcEnv(m map[string]string) map[string]string {
|
|
||||||
out := make(map[string]string, len(m))
|
|
||||||
for key, val := range m {
|
|
||||||
key = strings.ToUpper(key)
|
|
||||||
key = strings.Replace(key, "-", "_", -1)
|
|
||||||
out[key] = val
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
package initialize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/coreos/coreos-cloudinit/system"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FleetEnvironment map[string]string
|
|
||||||
|
|
||||||
func (fe FleetEnvironment) String() (out string) {
|
|
||||||
norm := normalizeSvcEnv(fe)
|
|
||||||
out += "[Service]\n"
|
|
||||||
|
|
||||||
for key, val := range norm {
|
|
||||||
out += fmt.Sprintf("Environment=\"FLEET_%s=%s\"\n", key, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Units generates a Unit file drop-in for fleet, if any fleet options were
|
|
||||||
// configured in cloud-config
|
|
||||||
func (fe FleetEnvironment) Units(root string) ([]system.Unit, error) {
|
|
||||||
if len(fe) < 1 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
fleet := system.Unit{
|
|
||||||
Name: "fleet.service",
|
|
||||||
Runtime: true,
|
|
||||||
DropIn: true,
|
|
||||||
Content: fe.String(),
|
|
||||||
}
|
|
||||||
return []system.Unit{fleet}, nil
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package initialize
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestFleetEnvironment(t *testing.T) {
|
|
||||||
cfg := make(FleetEnvironment, 0)
|
|
||||||
cfg["public-ip"] = "12.34.56.78"
|
|
||||||
|
|
||||||
env := cfg.String()
|
|
||||||
|
|
||||||
expect := `[Service]
|
|
||||||
Environment="FLEET_PUBLIC_IP=12.34.56.78"
|
|
||||||
`
|
|
||||||
|
|
||||||
if env != expect {
|
|
||||||
t.Errorf("Generated environment:\n%s\nExpected environment:\n%s", env, expect)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFleetUnit(t *testing.T) {
|
|
||||||
cfg := make(FleetEnvironment, 0)
|
|
||||||
uu, err := cfg.Units("/")
|
|
||||||
if len(uu) != 0 {
|
|
||||||
t.Errorf("unexpectedly generated unit with empty FleetEnvironment")
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg["public-ip"] = "12.34.56.78"
|
|
||||||
|
|
||||||
uu, err = cfg.Units("/")
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("error generating fleet unit: %v", err)
|
|
||||||
}
|
|
||||||
if len(uu) != 1 {
|
|
||||||
t.Fatalf("expected 1 unit generated, got %d", len(uu))
|
|
||||||
}
|
|
||||||
u := uu[0]
|
|
||||||
if !u.Runtime {
|
|
||||||
t.Errorf("bad Runtime for generated fleet unit!")
|
|
||||||
}
|
|
||||||
if !u.DropIn {
|
|
||||||
t.Errorf("bad DropIn for generated fleet unit!")
|
|
||||||
}
|
|
||||||
}
|
|
26
system/fleet.go
Normal file
26
system/fleet.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/coreos/coreos-cloudinit/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Fleet is a top-level structure which embeds its underlying configuration,
|
||||||
|
// config.Fleet, and provides the system-specific Unit().
|
||||||
|
type Fleet struct {
|
||||||
|
config.Fleet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Units generates a Unit file drop-in for fleet, if any fleet options were
|
||||||
|
// configured in cloud-config
|
||||||
|
func (fe Fleet) Units(_ string) ([]Unit, error) {
|
||||||
|
content := dropinContents(fe.Fleet)
|
||||||
|
if content == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return []Unit{{
|
||||||
|
Name: "fleet.service",
|
||||||
|
Runtime: true,
|
||||||
|
DropIn: true,
|
||||||
|
Content: content,
|
||||||
|
}}, nil
|
||||||
|
}
|
41
system/fleet_test.go
Normal file
41
system/fleet_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/coreos/coreos-cloudinit/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFleetUnits(t *testing.T) {
|
||||||
|
for _, tt := range []struct {
|
||||||
|
config config.Fleet
|
||||||
|
units []Unit
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
config.Fleet{},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
config.Fleet{
|
||||||
|
PublicIP: "12.34.56.78",
|
||||||
|
},
|
||||||
|
[]Unit{{
|
||||||
|
Name: "fleet.service",
|
||||||
|
Content: `[Service]
|
||||||
|
Environment="FLEET_PUBLIC_IP=12.34.56.78"
|
||||||
|
`,
|
||||||
|
Runtime: true,
|
||||||
|
DropIn: true,
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
units, err := Fleet{tt.config}.Units("")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("bad error (%q): want %q, got %q", tt.config, nil, err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(units, tt.units) {
|
||||||
|
t.Errorf("bad units (%q): want %q, got %q", tt.config, tt.units, units)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user