This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
config.go
138 lines (118 loc) · 3.66 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package config
import (
"encoding/json"
"path/filepath"
)
type Config struct {
Node ConfigNode
Confab ConfigConfab
Consul ConfigConsul
Path ConfigPath
}
type ConfigConfab struct {
TimeoutInSeconds int `json:"timeout_in_seconds"`
}
type ConfigConsul struct {
Agent ConfigConsulAgent
EncryptKeys []string `json:"encrypt_keys"`
}
type ConfigPath struct {
AgentPath string `json:"agent_path"`
ConsulConfigDir string `json:"consul_config_dir"`
PIDFile string `json:"pid_file"`
KeyringFile string `json:"keyring_file"`
DataDir string `json:"data_dir"`
}
type ConfigNode struct {
Name string `json:"name"`
Index int `json:"index"`
ExternalIP string `json:"external_ip"`
Zone string `json:"zone"`
}
type ConfigConsulAgent struct {
Servers ConfigConsulAgentServers `json:"servers"`
Services map[string]ServiceDefinition `json:"services"`
Mode string `json:"mode"`
Domain string `json:"domain"`
Datacenter string `json:"datacenter"`
LogLevel string `json:"log_level"`
ProtocolVersion int `json:"protocol_version"`
DnsConfig ConfigConsulAgentDnsConfig `json:"dns_config"`
Telemetry ConfigConsulTelemetry `json:"telemetry"`
Bootstrap bool `json:"bootstrap"`
NodeName string `json:"node_name"`
RequireSSL bool `json:"require_ssl"`
Ports ConfigConsulAgentPorts `json:"ports"`
}
type ConfigConsulAgentPorts struct {
DNS int `json:"dns"`
}
type ConfigConsulAgentDnsConfig struct {
AllowStale bool `json:"allow_stale"`
MaxStale string `json:"max_stale"`
RecursorTimeout string `json:"recursor_timeout"`
ServiceTTL string `json:"service_ttl"`
}
type ConfigConsulTelemetry struct {
StatsdAddress string `json:"statsd_address"`
}
type ConfigConsulAgentServers struct {
LAN []string `json:"lan"`
WAN []string `json:"wan"`
}
func defaultConfig() Config {
return Config{
Path: ConfigPath{
AgentPath: "/var/vcap/packages/consul/bin/consul",
ConsulConfigDir: "/var/vcap/jobs/consul_agent/config",
PIDFile: "/var/vcap/sys/run/consul_agent/consul_agent.pid",
},
Consul: ConfigConsul{
Agent: ConfigConsulAgent{
DnsConfig: ConfigConsulAgentDnsConfig{
AllowStale: true,
MaxStale: "30s",
RecursorTimeout: "5s",
ServiceTTL: "0s",
},
Servers: ConfigConsulAgentServers{
LAN: []string{},
WAN: []string{},
},
},
},
Confab: ConfigConfab{
TimeoutInSeconds: 55,
},
}
}
func ConfigFromJSON(configData, configConsulLinkData []byte) (Config, error) {
config := defaultConfig()
if err := json.Unmarshal(configData, &config); err != nil {
return Config{}, err
}
configConsul, err := mergedConsulConfig(config.Consul, configConsulLinkData)
if err != nil {
return Config{}, err
}
config.Consul = configConsul
if config.Path.DataDir == "" {
if config.Consul.Agent.Mode == "server" {
config.Path.DataDir = "/var/vcap/store/consul_agent"
} else {
config.Path.DataDir = "/var/vcap/data/consul_agent"
}
}
if config.Path.KeyringFile == "" {
config.Path.KeyringFile = filepath.Join(config.Path.DataDir, "serf", "local.keyring")
}
return config, nil
}
func mergedConsulConfig(configConsul ConfigConsul, configConsulLinkData []byte) (ConfigConsul, error) {
if len(configConsulLinkData) > 0 {
if err := json.Unmarshal(configConsulLinkData, &configConsul); err != nil {
return ConfigConsul{}, err
}
}
return configConsul, nil
}