From 4a0019c66966a57915d281d56cb8690cef81bf12 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 21 Nov 2014 15:43:48 -0800 Subject: [PATCH 1/4] config: add support for float64 --- config/validate/node.go | 2 +- config/validate/rules.go | 8 +++--- system/env.go | 4 +-- system/env_test.go | 55 ++++++++++++++++++++++++++++++++++++++++ system/etcd_test.go | 2 +- system/fleet_test.go | 2 +- 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 system/env_test.go diff --git a/config/validate/node.go b/config/validate/node.go index e010257..2df801b 100644 --- a/config/validate/node.go +++ b/config/validate/node.go @@ -125,7 +125,7 @@ func toNode(v interface{}, c context, n *node) { n.children = append(n.children, cn) c.Increment() } - case reflect.String, reflect.Int, reflect.Bool: + case reflect.String, reflect.Int, reflect.Bool, reflect.Float64: default: panic(fmt.Sprintf("toNode(): unhandled kind %s", vv.Kind())) } diff --git a/config/validate/rules.go b/config/validate/rules.go index 9c29dce..87f2d08 100644 --- a/config/validate/rules.go +++ b/config/validate/rules.go @@ -61,7 +61,7 @@ func checkNodeStructure(n, g node, r *Report) { toNode(reflect.New(c).Elem().Interface(), context{}, &cg) checkNodeStructure(cn, cg, r) } - case reflect.String, reflect.Int, reflect.Bool: + case reflect.String, reflect.Int, reflect.Float64, reflect.Bool: default: panic(fmt.Sprintf("checkNodeStructure(): unhandled kind %s", g.Kind())) } @@ -92,7 +92,7 @@ func checkNodeValidity(n, g node, r *Report) { toNode(reflect.New(c).Elem().Interface(), context{}, &cg) checkNodeValidity(cn, cg, r) } - case reflect.String, reflect.Int, reflect.Bool: + case reflect.String, reflect.Int, reflect.Float64, reflect.Bool: default: panic(fmt.Sprintf("checkNodeValidity(): unhandled kind %s", g.Kind())) } @@ -104,10 +104,10 @@ func checkNodeValidity(n, g node, r *Report) { func isCompatible(n, g reflect.Kind) bool { switch g { case reflect.String: - return n == reflect.String || n == reflect.Int || n == reflect.Bool + return n == reflect.String || n == reflect.Int || n == reflect.Float64 || n == reflect.Bool case reflect.Struct: return n == reflect.Struct || n == reflect.Map - case reflect.Bool, reflect.Slice: + case reflect.Bool, reflect.Slice, reflect.Int, reflect.Float64: return n == g default: panic(fmt.Sprintf("isCompatible(): unhandled kind %s", g)) diff --git a/system/env.go b/system/env.go index 42042d0..152b977 100644 --- a/system/env.go +++ b/system/env.go @@ -31,9 +31,9 @@ func dropinContents(e interface{}) string { var out string for i := 0; i < et.NumField(); i++ { - if val := ev.Field(i).String(); val != "" { + if val := ev.Field(i).Interface(); !config.IsZero(val) { key := et.Field(i).Tag.Get("env") - out += fmt.Sprintf("Environment=\"%s=%s\"\n", key, val) + out += fmt.Sprintf("Environment=\"%s=%v\"\n", key, val) } } diff --git a/system/env_test.go b/system/env_test.go new file mode 100644 index 0000000..0623186 --- /dev/null +++ b/system/env_test.go @@ -0,0 +1,55 @@ +package system + +import ( + "testing" +) + +func TestDropinContents(t *testing.T) { + tests := []struct { + Config interface{} + Contents string + }{ + { + struct{}{}, + "", + }, + { + struct { + A string `env:"A"` + B int `env:"B"` + C bool `env:"C"` + D float64 `env:"D"` + }{ + "hi", 1, true, 0.12345, + }, + `[Service] +Environment="A=hi" +Environment="B=1" +Environment="C=true" +Environment="D=0.12345" +`, + }, + { + struct { + A float64 `env:"A"` + B float64 `env:"B"` + C float64 `env:"C"` + D float64 `env:"D"` + }{ + 0.000001, 1, 0.9999999, 0.1, + }, + `[Service] +Environment="A=1e-06" +Environment="B=1" +Environment="C=0.9999999" +Environment="D=0.1" +`, + }, + } + + for _, tt := range tests { + if c := dropinContents(tt.Config); c != tt.Contents { + t.Errorf("bad contents (%+v): want %q, got %q", tt, tt.Contents, c) + } + } +} diff --git a/system/etcd_test.go b/system/etcd_test.go index 6115353..879eb8f 100644 --- a/system/etcd_test.go +++ b/system/etcd_test.go @@ -67,7 +67,7 @@ Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002" } { units := Etcd{tt.config}.Units() if !reflect.DeepEqual(tt.units, units) { - t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units) + t.Errorf("bad units (%+v): want %#v, got %#v", tt.config, tt.units, units) } } } diff --git a/system/fleet_test.go b/system/fleet_test.go index 5f6902c..bf3c77e 100644 --- a/system/fleet_test.go +++ b/system/fleet_test.go @@ -48,7 +48,7 @@ Environment="FLEET_PUBLIC_IP=12.34.56.78" } { units := Fleet{tt.config}.Units() if !reflect.DeepEqual(units, tt.units) { - t.Errorf("bad units (%q): want %#v, got %#v", tt.config, tt.units, units) + t.Errorf("bad units (%+v): want %#v, got %#v", tt.config, tt.units, units) } } } From 08131ffab129d98e3051b4d21c77230c0f1ee436 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 21 Nov 2014 15:00:42 -0800 Subject: [PATCH 2/4] config/etcd: fix configs This new table is pulled from the etcd codebase rather than the docs... Added: GraphiteHost PeerElectionTimeout PeerHeartbeatInterval PeerKeyFile RetryInterval SnapshotCount StrTrace VeryVeryVerbose Fixed types: ClusterActiveSize ClusterRemoveDelay ClusterSyncInterval HTTPReadTimeout HTTPWriteTimeout MaxResultBuffer MaxRetryAttempts Snapshot Verbose VeryVerbose Renamed: Cors Removed: MaxClusterSize CPUProfileFile --- config/etcd.go | 61 +++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/config/etcd.go b/config/etcd.go index 2adc4a2..dfc9559 100644 --- a/config/etcd.go +++ b/config/etcd.go @@ -17,32 +17,37 @@ package config type Etcd struct { - Addr string `yaml:"addr" env:"ETCD_ADDR"` - BindAddr string `yaml:"bind_addr" env:"ETCD_BIND_ADDR"` - CAFile string `yaml:"ca_file" env:"ETCD_CA_FILE"` - CertFile string `yaml:"cert_file" env:"ETCD_CERT_FILE"` - ClusterActiveSize string `yaml:"cluster_active_size" env:"ETCD_CLUSTER_ACTIVE_SIZE"` - ClusterRemoveDelay string `yaml:"cluster_remove_delay" env:"ETCD_CLUSTER_REMOVE_DELAY"` - ClusterSyncInterval string `yaml:"cluster_sync_interval" env:"ETCD_CLUSTER_SYNC_INTERVAL"` - Cors string `yaml:"cors" env:"ETCD_CORS"` - CPUProfileFile string `yaml:"cpu_profile_file" env:"ETCD_CPU_PROFILE_FILE"` - DataDir string `yaml:"data_dir" env:"ETCD_DATA_DIR"` - Discovery string `yaml:"discovery" env:"ETCD_DISCOVERY"` - HTTPReadTimeout string `yaml:"http_read_timeout" env:"ETCD_HTTP_READ_TIMEOUT"` - HTTPWriteTimeout string `yaml:"http_write_timeout" env:"ETCD_HTTP_WRITE_TIMEOUT"` - KeyFile string `yaml:"key_file" env:"ETCD_KEY_FILE"` - MaxClusterSize string `yaml:"max_cluster_size" env:"ETCD_MAX_CLUSTER_SIZE"` - MaxResultBuffer string `yaml:"max_result_buffer" env:"ETCD_MAX_RESULT_BUFFER"` - MaxRetryAttempts string `yaml:"max_retry_attempts" env:"ETCD_MAX_RETRY_ATTEMPTS"` - Name string `yaml:"name" env:"ETCD_NAME"` - PeerAddr string `yaml:"peer_addr" env:"ETCD_PEER_ADDR"` - PeerBindAddr string `yaml:"peer_bind_addr" env:"ETCD_PEER_BIND_ADDR"` - PeerCAFile string `yaml:"peer_ca_file" env:"ETCD_PEER_CA_FILE"` - PeerCertFile string `yaml:"peer_cert_file" env:"ETCD_PEER_CERT_FILE"` - PeerKeyFile string `yaml:"peer_key_file" env:"ETCD_PEER_KEY_FILE"` - Peers string `yaml:"peers" env:"ETCD_PEERS"` - PeersFile string `yaml:"peers_file" env:"ETCD_PEERS_FILE"` - Snapshot string `yaml:"snapshot" env:"ETCD_SNAPSHOT"` - Verbose string `yaml:"verbose" env:"ETCD_VERBOSE"` - VeryVerbose string `yaml:"very_verbose" env:"ETCD_VERY_VERBOSE"` + Addr string `yaml:"addr" env:"ETCD_ADDR"` + BindAddr string `yaml:"bind_addr" env:"ETCD_BIND_ADDR"` + CAFile string `yaml:"ca_file" env:"ETCD_CA_FILE"` + CertFile string `yaml:"cert_file" env:"ETCD_CERT_FILE"` + ClusterActiveSize int `yaml:"cluster_active_size" env:"ETCD_CLUSTER_ACTIVE_SIZE"` + ClusterRemoveDelay float64 `yaml:"cluster_remove_delay" env:"ETCD_CLUSTER_REMOVE_DELAY"` + ClusterSyncInterval float64 `yaml:"cluster_sync_interval" env:"ETCD_CLUSTER_SYNC_INTERVAL"` + CorsOrigins string `yaml:"cors" env:"ETCD_CORS"` + DataDir string `yaml:"data_dir" env:"ETCD_DATA_DIR"` + Discovery string `yaml:"discovery" env:"ETCD_DISCOVERY"` + GraphiteHost string `yaml:"graphite_host" env:"ETCD_GRAPHITE_HOST"` + HTTPReadTimeout float64 `yaml:"http_read_timeout" env:"ETCD_HTTP_READ_TIMEOUT"` + HTTPWriteTimeout float64 `yaml:"http_write_timeout" env:"ETCD_HTTP_WRITE_TIMEOUT"` + KeyFile string `yaml:"key_file" env:"ETCD_KEY_FILE"` + MaxResultBuffer int `yaml:"max_result_buffer" env:"ETCD_MAX_RESULT_BUFFER"` + MaxRetryAttempts int `yaml:"max_retry_attempts" env:"ETCD_MAX_RETRY_ATTEMPTS"` + Name string `yaml:"name" env:"ETCD_NAME"` + PeerAddr string `yaml:"peer_addr" env:"ETCD_PEER_ADDR"` + PeerBindAddr string `yaml:"peer_bind_addr" env:"ETCD_PEER_BIND_ADDR"` + PeerCAFile string `yaml:"peer_ca_file" env:"ETCD_PEER_CA_FILE"` + PeerCertFile string `yaml:"peer_cert_file" env:"ETCD_PEER_CERT_FILE"` + PeerElectionTimeout int `yaml:"peer_election_timeout" env:"ETCD_PEER_ELECTION_TIMEOUT"` + PeerHeartbeatInterval int `yaml:"peer_heartbeat_interval" env:"ETCD_PEER_HEARTBEAT_INTERVAL"` + PeerKeyFile string `yaml:"peer_key_file" env:"ETCD_PEER_KEY_FILE"` + Peers string `yaml:"peers" env:"ETCD_PEERS"` + PeersFile string `yaml:"peers_file" env:"ETCD_PEERS_FILE"` + RetryInterval float64 `yaml:"retry_interval" env:"ETCD_RETRY_INTERVAL"` + Snapshot bool `yaml:"snapshot" env:"ETCD_SNAPSHOT"` + SnapshotCount int `yaml:"snapshot_count" env:"ETCD_SNAPSHOTCOUNT"` + StrTrace string `yaml:"trace" env:"ETCD_TRACE"` + Verbose bool `yaml:"verbose" env:"ETCD_VERBOSE"` + VeryVerbose bool `yaml:"very_verbose" env:"ETCD_VERY_VERBOSE"` + VeryVeryVerbose bool `yaml:"very_very_verbose" env:"ETCD_VERY_VERY_VERBOSE"` } From d2a19cc86d0d5e6f5686963075306746b69bc2d5 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 21 Nov 2014 15:01:23 -0800 Subject: [PATCH 3/4] config/flannel: correct - vs _ --- config/flannel.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/flannel.go b/config/flannel.go index 6111514..d7f4d0d 100644 --- a/config/flannel.go +++ b/config/flannel.go @@ -1,9 +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"` + 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"` } From 62248ea33d99fcf3b9e518504e6fed527bad8cfe Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 21 Nov 2014 15:14:53 -0800 Subject: [PATCH 4/4] config/fleet: fix configs Added EtcdKeyPrefix and fixed the types of EngineReconcileInterval and EtcdRequestTimeout. --- config/fleet.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/config/fleet.go b/config/fleet.go index 63fbb87..c1c6749 100644 --- a/config/fleet.go +++ b/config/fleet.go @@ -17,14 +17,15 @@ 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"` + AgentTTL string `yaml:"agent_ttl" env:"FLEET_AGENT_TTL"` + EngineReconcileInterval float64 `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"` + EtcdKeyPrefix string `yaml:"etcd_key_prefix" env:"FLEET_ETCD_KEY_PREFIX"` + EtcdRequestTimeout float64 `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 int `yaml:"verbosity" env:"FLEET_VERBOSITY"` }