Merge pull request #281 from thommay/flannel_env_file

Create an environment file for flannel
This commit is contained in:
Michael Marineau 2014-12-29 15:07:08 -08:00
commit edced59fa6
4 changed files with 73 additions and 41 deletions

View File

@ -113,6 +113,7 @@ func Apply(cfg config.CloudConfig, env *Environment) error {
system.OEM{OEM: cfg.Coreos.OEM}, system.OEM{OEM: cfg.Coreos.OEM},
system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig}, system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig},
system.EtcHosts{EtcHosts: cfg.ManageEtcHosts}, system.EtcHosts{EtcHosts: cfg.ManageEtcHosts},
system.Flannel{Flannel: cfg.Coreos.Flannel},
} { } {
f, err := ccf.File() f, err := ccf.File()
if err != nil { if err != nil {
@ -132,7 +133,6 @@ func Apply(cfg config.CloudConfig, env *Environment) error {
system.Etcd{Etcd: cfg.Coreos.Etcd}, system.Etcd{Etcd: cfg.Coreos.Etcd},
system.Fleet{Fleet: cfg.Coreos.Fleet}, system.Fleet{Fleet: cfg.Coreos.Fleet},
system.Locksmith{Locksmith: cfg.Coreos.Locksmith}, system.Locksmith{Locksmith: cfg.Coreos.Locksmith},
system.Flannel{Flannel: cfg.Coreos.Flannel},
system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig}, system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig},
} { } {
units = append(units, ccu.Units()...) units = append(units, ccu.Units()...)

View File

@ -23,22 +23,32 @@ import (
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
) )
// dropinContents generates the contents for a drop-in unit given the config. // serviceContents generates the contents for a drop-in unit given the config.
// The argument must be a struct from the 'config' package. // The argument must be a struct from the 'config' package.
func serviceContents(e interface{}) string { func serviceContents(e interface{}) string {
vars := getEnvVars(e)
if len(vars) == 0 {
return ""
}
out := "[Service]\n"
for _, v := range vars {
out += fmt.Sprintf("Environment=\"%s\"\n", v)
}
return out
}
func getEnvVars(e interface{}) []string {
et := reflect.TypeOf(e) et := reflect.TypeOf(e)
ev := reflect.ValueOf(e) ev := reflect.ValueOf(e)
var out string vars := []string{}
for i := 0; i < et.NumField(); i++ { for i := 0; i < et.NumField(); i++ {
if val := ev.Field(i).Interface(); !config.IsZero(val) { if val := ev.Field(i).Interface(); !config.IsZero(val) {
key := et.Field(i).Tag.Get("env") key := et.Field(i).Tag.Get("env")
out += fmt.Sprintf("Environment=\"%s=%v\"\n", key, val) vars = append(vars, fmt.Sprintf("%s=%v", key, val))
} }
} }
if out == "" { return vars
return ""
}
return "[Service]\n" + out
} }

View File

@ -1,6 +1,9 @@
package system package system
import ( import (
"path"
"strings"
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
) )
@ -10,15 +13,18 @@ type Flannel struct {
config.Flannel config.Flannel
} }
// Units generates a Unit file drop-in for flannel, if any flannel options were func (fl Flannel) envVars() string {
// configured in cloud-config return strings.Join(getEnvVars(fl.Flannel), "\n")
func (fl Flannel) Units() []Unit { }
return []Unit{{config.Unit{
Name: "flanneld.service", func (fl Flannel) File() (*File, error) {
Runtime: true, vars := fl.envVars()
DropIns: []config.UnitDropIn{{ if vars == "" {
Name: "20-cloudinit.conf", return nil, nil
Content: serviceContents(fl.Flannel), }
}}, return &File{config.File{
}}} Path: path.Join("run", "flannel", "options.env"),
RawFilePermissions: "0644",
Content: vars,
}}, nil
} }

View File

@ -7,40 +7,56 @@ import (
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
) )
func TestFlannelUnits(t *testing.T) { func TestFlannelEnvVars(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
config config.Flannel config config.Flannel
units []Unit contents string
}{ }{
{ {
config.Flannel{}, config.Flannel{},
[]Unit{{config.Unit{ "",
Name: "flanneld.service",
Runtime: true,
DropIns: []config.UnitDropIn{{Name: "20-cloudinit.conf"}},
}}},
}, },
{ {
config.Flannel{ config.Flannel{
EtcdEndpoint: "http://12.34.56.78:4001", EtcdEndpoint: "http://12.34.56.78:4001",
EtcdPrefix: "/coreos.com/network/tenant1", EtcdPrefix: "/coreos.com/network/tenant1",
}, },
[]Unit{{config.Unit{ `FLANNELD_ETCD_ENDPOINT=http://12.34.56.78:4001
Name: "flanneld.service", FLANNELD_ETCD_PREFIX=/coreos.com/network/tenant1`,
Runtime: true,
DropIns: []config.UnitDropIn{{
Name: "20-cloudinit.conf",
Content: `[Service]
Environment="FLANNELD_ETCD_ENDPOINT=http://12.34.56.78:4001"
Environment="FLANNELD_ETCD_PREFIX=/coreos.com/network/tenant1"
`,
}},
}}},
}, },
} { } {
units := Flannel{tt.config}.Units() out := Flannel{tt.config}.envVars()
if !reflect.DeepEqual(units, tt.units) { if out != tt.contents {
t.Errorf("bad units (%q): want %v, got %v", tt.config, tt.units, units) t.Errorf("bad contents (%+v): want %q, got %q", tt, tt.contents, out)
}
}
}
func TestFlannelFile(t *testing.T) {
for _, tt := range []struct {
config config.Flannel
file *File
}{
{
config.Flannel{},
nil,
},
{
config.Flannel{
EtcdEndpoint: "http://12.34.56.78:4001",
EtcdPrefix: "/coreos.com/network/tenant1",
},
&File{config.File{
Path: "run/flannel/options.env",
RawFilePermissions: "0644",
Content: `FLANNELD_ETCD_ENDPOINT=http://12.34.56.78:4001
FLANNELD_ETCD_PREFIX=/coreos.com/network/tenant1`,
}},
},
} {
file, _ := Flannel{tt.config}.File()
if !reflect.DeepEqual(tt.file, file) {
t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.file, file)
} }
} }
} }