Merge pull request #281 from thommay/flannel_env_file
Create an environment file for flannel
This commit is contained in:
commit
edced59fa6
@ -113,6 +113,7 @@ func Apply(cfg config.CloudConfig, env *Environment) error {
|
||||
system.OEM{OEM: cfg.Coreos.OEM},
|
||||
system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig},
|
||||
system.EtcHosts{EtcHosts: cfg.ManageEtcHosts},
|
||||
system.Flannel{Flannel: cfg.Coreos.Flannel},
|
||||
} {
|
||||
f, err := ccf.File()
|
||||
if err != nil {
|
||||
@ -132,7 +133,6 @@ func Apply(cfg config.CloudConfig, env *Environment) error {
|
||||
system.Etcd{Etcd: cfg.Coreos.Etcd},
|
||||
system.Fleet{Fleet: cfg.Coreos.Fleet},
|
||||
system.Locksmith{Locksmith: cfg.Coreos.Locksmith},
|
||||
system.Flannel{Flannel: cfg.Coreos.Flannel},
|
||||
system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig},
|
||||
} {
|
||||
units = append(units, ccu.Units()...)
|
||||
|
@ -23,22 +23,32 @@ import (
|
||||
"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.
|
||||
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)
|
||||
ev := reflect.ValueOf(e)
|
||||
|
||||
var out string
|
||||
vars := []string{}
|
||||
for i := 0; i < et.NumField(); i++ {
|
||||
if val := ev.Field(i).Interface(); !config.IsZero(val) {
|
||||
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 ""
|
||||
}
|
||||
return "[Service]\n" + out
|
||||
return vars
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
@ -10,15 +13,18 @@ type Flannel struct {
|
||||
config.Flannel
|
||||
}
|
||||
|
||||
// Units generates a Unit file drop-in for flannel, if any flannel options were
|
||||
// configured in cloud-config
|
||||
func (fl Flannel) Units() []Unit {
|
||||
return []Unit{{config.Unit{
|
||||
Name: "flanneld.service",
|
||||
Runtime: true,
|
||||
DropIns: []config.UnitDropIn{{
|
||||
Name: "20-cloudinit.conf",
|
||||
Content: serviceContents(fl.Flannel),
|
||||
}},
|
||||
}}}
|
||||
func (fl Flannel) envVars() string {
|
||||
return strings.Join(getEnvVars(fl.Flannel), "\n")
|
||||
}
|
||||
|
||||
func (fl Flannel) File() (*File, error) {
|
||||
vars := fl.envVars()
|
||||
if vars == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return &File{config.File{
|
||||
Path: path.Join("run", "flannel", "options.env"),
|
||||
RawFilePermissions: "0644",
|
||||
Content: vars,
|
||||
}}, nil
|
||||
}
|
||||
|
@ -7,40 +7,56 @@ import (
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
func TestFlannelUnits(t *testing.T) {
|
||||
func TestFlannelEnvVars(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
config config.Flannel
|
||||
units []Unit
|
||||
contents string
|
||||
}{
|
||||
{
|
||||
config.Flannel{},
|
||||
[]Unit{{config.Unit{
|
||||
Name: "flanneld.service",
|
||||
Runtime: true,
|
||||
DropIns: []config.UnitDropIn{{Name: "20-cloudinit.conf"}},
|
||||
}}},
|
||||
"",
|
||||
},
|
||||
{
|
||||
config.Flannel{
|
||||
EtcdEndpoint: "http://12.34.56.78:4001",
|
||||
EtcdPrefix: "/coreos.com/network/tenant1",
|
||||
},
|
||||
[]Unit{{config.Unit{
|
||||
Name: "flanneld.service",
|
||||
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"
|
||||
`,
|
||||
}},
|
||||
}}},
|
||||
`FLANNELD_ETCD_ENDPOINT=http://12.34.56.78:4001
|
||||
FLANNELD_ETCD_PREFIX=/coreos.com/network/tenant1`,
|
||||
},
|
||||
} {
|
||||
units := Flannel{tt.config}.Units()
|
||||
if !reflect.DeepEqual(units, tt.units) {
|
||||
t.Errorf("bad units (%q): want %v, got %v", tt.config, tt.units, units)
|
||||
out := Flannel{tt.config}.envVars()
|
||||
if out != tt.contents {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user