From a20addd05e32e7edc1b7d1ed67e111014fc6f06f Mon Sep 17 00:00:00 2001 From: Eugene Yakubovich Date: Mon, 6 Oct 2014 12:06:33 -0700 Subject: [PATCH] flannel: added flannel support and helper to make dropins fleet, flannel, and etcd all generate dropins from config. To reduce code duplication, factor out a helper to do that. --- Documentation/cloud-config.md | 23 ++++++++++++++++++++ config/config.go | 11 +++++----- config/flannel.go | 9 ++++++++ initialize/config.go | 1 + system/env.go | 15 +++++++++++++ system/etcd.go | 11 +--------- system/flannel.go | 17 +++++++++++++++ system/flannel_test.go | 40 +++++++++++++++++++++++++++++++++++ system/fleet.go | 11 +--------- 9 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 config/flannel.go create mode 100644 system/flannel.go create mode 100644 system/flannel_test.go diff --git a/Documentation/cloud-config.md b/Documentation/cloud-config.md index 8af0cef..fa2d969 100644 --- a/Documentation/cloud-config.md +++ b/Documentation/cloud-config.md @@ -97,6 +97,29 @@ For more information on fleet configuration, see the [fleet documentation][fleet [fleet-config]: https://github.com/coreos/fleet/blob/master/Documentation/deployment-and-configuration.md#configuration +#### flannel + +The `coreos.flannel.*` parameters also work very similarly to `coreos.etcd.*` and `coreos.fleet.*`. They can be used to set enviornment variables for flanneld. Given the following cloud-config... + +```yaml +#cloud-config + +coreos: + flannel: + etcd-prefix: /coreos.com/network2 +``` + +...will generate systemd unit drop-in like so: + +``` +[Service] +Environment="FLANNELD_ETCD_PREFIX=/coreos.com/network2" +``` + +For complete list of flannel configuraion parameters, see the [flannel documentation][flannel-readme]. + +[flannel-readme]: https://github.com/coreos/flannel/blob/master/README.md + #### update The `coreos.update.*` parameters manipulate settings related to how CoreOS instances are updated. diff --git a/config/config.go b/config/config.go index 9ac6d69..76a8823 100644 --- a/config/config.go +++ b/config/config.go @@ -30,11 +30,12 @@ import ( type CloudConfig struct { SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` Coreos struct { - Etcd Etcd `yaml:"etcd"` - Fleet Fleet `yaml:"fleet"` - OEM OEM `yaml:"oem"` - Update Update `yaml:"update"` - Units []Unit `yaml:"units"` + Etcd Etcd `yaml:"etcd"` + Flannel Flannel `yaml:"flannel"` + Fleet Fleet `yaml:"fleet"` + OEM OEM `yaml:"oem"` + Update Update `yaml:"update"` + Units []Unit `yaml:"units"` } `yaml:"coreos"` WriteFiles []File `yaml:"write_files"` Hostname string `yaml:"hostname"` diff --git a/config/flannel.go b/config/flannel.go new file mode 100644 index 0000000..6111514 --- /dev/null +++ b/config/flannel.go @@ -0,0 +1,9 @@ +package config + +type Flannel struct { + EtcdEndpoint string `yaml:"etcd-endpoint" env:"FLANNELD_ETCD_ENDPOINT"` + EtcdPrefix string `yaml:"etcd-prefix" env:"FLANNELD_ETCD_PREFIX"` + IPMasq string `yaml:"ip-masq" env:"FLANNELD_IP_MASQ"` + SubnetFile string `yaml:"subnet-file" env:"FLANNELD_SUBNET_FILE"` + Iface string `yaml:"interface" env:"FLANNELD_IFACE"` +} diff --git a/initialize/config.go b/initialize/config.go index 94d1d82..a2024ce 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -131,6 +131,7 @@ func Apply(cfg config.CloudConfig, env *Environment) error { for _, ccu := range []CloudConfigUnit{ system.Etcd{Etcd: cfg.Coreos.Etcd}, system.Fleet{Fleet: cfg.Coreos.Fleet}, + system.Flannel{Flannel: cfg.Coreos.Flannel}, system.Update{Update: cfg.Coreos.Update, ReadConfig: system.DefaultReadConfig}, } { units = append(units, ccu.Units()...) diff --git a/system/env.go b/system/env.go index 5779d13..42042d0 100644 --- a/system/env.go +++ b/system/env.go @@ -19,6 +19,8 @@ package system import ( "fmt" "reflect" + + "github.com/coreos/coreos-cloudinit/config" ) // dropinContents generates the contents for a drop-in unit given the config. @@ -40,3 +42,16 @@ func dropinContents(e interface{}) string { } return "[Service]\n" + out } + +func dropinFromConfig(cfg interface{}, name string) []Unit { + content := dropinContents(cfg) + if content == "" { + return nil + } + return []Unit{{config.Unit{ + Name: name, + Runtime: true, + DropIn: true, + Content: content, + }}} +} diff --git a/system/etcd.go b/system/etcd.go index 578be51..a80b091 100644 --- a/system/etcd.go +++ b/system/etcd.go @@ -28,14 +28,5 @@ type Etcd struct { // Units creates a Unit file drop-in for etcd, using any configured options. func (ee Etcd) Units() []Unit { - content := dropinContents(ee.Etcd) - if content == "" { - return nil - } - return []Unit{{config.Unit{ - Name: "etcd.service", - Runtime: true, - DropIn: true, - Content: content, - }}} + return dropinFromConfig(ee.Etcd, "etcd.service") } diff --git a/system/flannel.go b/system/flannel.go new file mode 100644 index 0000000..dc75a62 --- /dev/null +++ b/system/flannel.go @@ -0,0 +1,17 @@ +package system + +import ( + "github.com/coreos/coreos-cloudinit/config" +) + +// flannel is a top-level structure which embeds its underlying configuration, +// config.Flannel, and provides the system-specific Unit(). +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 dropinFromConfig(fl.Flannel, "flannel.service") +} diff --git a/system/flannel_test.go b/system/flannel_test.go new file mode 100644 index 0000000..7800a95 --- /dev/null +++ b/system/flannel_test.go @@ -0,0 +1,40 @@ +package system + +import ( + "reflect" + "testing" + + "github.com/coreos/coreos-cloudinit/config" +) + +func TestFlannelUnits(t *testing.T) { + for _, tt := range []struct { + config config.Flannel + units []Unit + }{ + { + config.Flannel{}, + nil, + }, + { + config.Flannel{ + EtcdEndpoint: "http://12.34.56.78:4001", + EtcdPrefix: "/coreos.com/network/tenant1", + }, + []Unit{{config.Unit{ + Name: "flannel.service", + Content: `[Service] +Environment="FLANNELD_ETCD_ENDPOINT=http://12.34.56.78:4001" +Environment="FLANNELD_ETCD_PREFIX=/coreos.com/network/tenant1" +`, + Runtime: true, + DropIn: true, + }}}, + }, + } { + 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) + } + } +} diff --git a/system/fleet.go b/system/fleet.go index 2a2b694..45def39 100644 --- a/system/fleet.go +++ b/system/fleet.go @@ -29,14 +29,5 @@ type Fleet struct { // Units generates a Unit file drop-in for fleet, if any fleet options were // configured in cloud-config func (fe Fleet) Units() []Unit { - content := dropinContents(fe.Fleet) - if content == "" { - return nil - } - return []Unit{{config.Unit{ - Name: "fleet.service", - Runtime: true, - DropIn: true, - Content: content, - }}} + return dropinFromConfig(fe.Fleet, "fleet.service") }