From 9454522033b75e54d30474086c615dacfa7c31d2 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Sun, 21 Sep 2014 19:28:04 -0700 Subject: [PATCH] fleet: refactor config - Explicitly specify all of the valid options for fleet - Seperate the config from Units() - Add YAML tags for the fields --- config/fleet.go | 14 +++++++++++++ initialize/config.go | 4 ++-- initialize/env.go | 13 ------------ initialize/fleet.go | 35 -------------------------------- initialize/fleet_test.go | 43 ---------------------------------------- system/fleet.go | 26 ++++++++++++++++++++++++ system/fleet_test.go | 41 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 93 deletions(-) create mode 100644 config/fleet.go delete mode 100644 initialize/fleet.go delete mode 100644 initialize/fleet_test.go create mode 100644 system/fleet.go create mode 100644 system/fleet_test.go diff --git a/config/fleet.go b/config/fleet.go new file mode 100644 index 0000000..41268b7 --- /dev/null +++ b/config/fleet.go @@ -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"` +} diff --git a/initialize/config.go b/initialize/config.go index 384b9ba..957abfc 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -32,7 +32,7 @@ type CloudConfig struct { SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` Coreos struct { Etcd config.Etcd - Fleet FleetEnvironment + Fleet config.Fleet OEM OEMRelease Update UpdateConfig 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()) if err != nil { return err diff --git a/initialize/env.go b/initialize/env.go index e34c268..69e528e 100644 --- a/initialize/env.go +++ b/initialize/env.go @@ -104,16 +104,3 @@ func (e *Environment) DefaultEnvironmentFile() *system.EnvFile { 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 -} diff --git a/initialize/fleet.go b/initialize/fleet.go deleted file mode 100644 index c03e1d1..0000000 --- a/initialize/fleet.go +++ /dev/null @@ -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 -} diff --git a/initialize/fleet_test.go b/initialize/fleet_test.go deleted file mode 100644 index 481a8de..0000000 --- a/initialize/fleet_test.go +++ /dev/null @@ -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!") - } -} diff --git a/system/fleet.go b/system/fleet.go new file mode 100644 index 0000000..50f288a --- /dev/null +++ b/system/fleet.go @@ -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 +} diff --git a/system/fleet_test.go b/system/fleet_test.go new file mode 100644 index 0000000..4618d5d --- /dev/null +++ b/system/fleet_test.go @@ -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) + } + } +}