Skip to content

Commit be22cd2

Browse files
committed
MAJOR: service discovery: Add support for Nomad
1 parent eccb1e8 commit be22cd2

35 files changed

+4338
-81
lines changed

configuration/configuration.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ type NotifyConfiguration struct {
129129
type ServiceDiscovery struct {
130130
consulMu sync.Mutex
131131
Consuls []*models.Consul `yaml:"consuls" group:"service_discovery" save:"true"`
132+
nomadMu sync.Mutex
133+
Nomads []*models.Nomad `yaml:"nomads" group:"service_discovery" save:"true"`
132134
awsMu sync.Mutex
133135
AWSRegions []*models.AwsRegion `yaml:"aws-regions" group:"service_discovery" save:"true"`
134136
}
@@ -273,11 +275,15 @@ func (c *Configuration) Save() error {
273275
cfg := c.storage.Get()
274276
cfg.ServiceDiscovery.Consuls = nil
275277
}
278+
if len(c.ServiceDiscovery.Nomads) == 0 {
279+
cfg := c.storage.Get()
280+
cfg.ServiceDiscovery.Nomads = nil
281+
}
276282
if len(c.ServiceDiscovery.AWSRegions) == 0 {
277283
cfg := c.storage.Get()
278284
cfg.ServiceDiscovery.AWSRegions = nil
279285
}
280-
if cfg.ServiceDiscovery.Consuls == nil && cfg.ServiceDiscovery.AWSRegions == nil {
286+
if cfg.ServiceDiscovery.Consuls == nil && cfg.ServiceDiscovery.AWSRegions == nil && cfg.ServiceDiscovery.Nomads == nil {
281287
cfg := c.storage.Get()
282288
cfg.ServiceDiscovery = nil
283289
}
@@ -317,6 +323,13 @@ func (c *Configuration) SaveConsuls(consuls []*models.Consul) error {
317323
return c.Save()
318324
}
319325

326+
func (c *Configuration) SaveNomads(nomads []*models.Nomad) error {
327+
c.ServiceDiscovery.nomadMu.Lock()
328+
c.ServiceDiscovery.Nomads = nomads
329+
c.ServiceDiscovery.nomadMu.Unlock()
330+
return c.Save()
331+
}
332+
320333
func (c *Configuration) SaveAWS(aws []*models.AwsRegion) error {
321334
c.ServiceDiscovery.awsMu.Lock()
322335
defer c.ServiceDiscovery.awsMu.Unlock()

configuration/configuration_storage.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type configTypeReload struct {
8484
ReloadDelay *int `yaml:"reload_delay,omitempty" hcl:"reload_delay,omitempty"`
8585
ReloadCmd *string `yaml:"reload_cmd,omitempty" hcl:"reload_cmd,omitempty"`
8686
RestartCmd *string `yaml:"restart_cmd,omitempty" hcl:"restart_cmd,omitempty"`
87-
StatusCmd *string `yaml:"status_cmd,omitempty" hcl:"status_cmd,omitempty"`
87+
StatusCmd *string `yaml:"status_cmd,omitempty" hcl:"status_cmd,omitempty"`
8888
ReloadRetention *int `yaml:"reload_retention,omitempty" hcl:"reload_retention,omitempty"`
8989
ValidateCmd *string `yaml:"validate_cmd,omitempty" hcl:"validate_cmd,omitempty"`
9090
}
@@ -133,6 +133,7 @@ type configTypeAdvertised struct {
133133

134134
type configTypeServiceDiscovery struct {
135135
Consuls *[]*models.Consul `yaml:"consuls,omitempty" hcl:"consuls,omitempty"`
136+
Nomads *[]*models.Nomad `yaml:"nomads,omitempty" hcl:"nomads,omitempty"`
136137
AWSRegions *[]*models.AwsRegion `yaml:"aws_regions,omitempty" hcl:"aws_regions,omitempty"`
137138
}
138139

@@ -343,6 +344,9 @@ func copyToConfiguration(cfg *Configuration) {
343344
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.Consuls != nil && !misc.HasOSArg("", "", "") {
344345
cfg.ServiceDiscovery.Consuls = *cfgStorage.ServiceDiscovery.Consuls
345346
}
347+
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.Nomads != nil && !misc.HasOSArg("", "", "") {
348+
cfg.ServiceDiscovery.Nomads = *cfgStorage.ServiceDiscovery.Nomads
349+
}
346350
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.AWSRegions != nil && !misc.HasOSArg("", "", "") {
347351
cfg.ServiceDiscovery.AWSRegions = *cfgStorage.ServiceDiscovery.AWSRegions
348352
}
@@ -521,6 +525,11 @@ func copyConfigurationToStorage(cfg *Configuration) {
521525
}
522526
cfgStorage.ServiceDiscovery.AWSRegions = &cfg.ServiceDiscovery.AWSRegions
523527

528+
if cfgStorage.ServiceDiscovery == nil {
529+
cfgStorage.ServiceDiscovery = &configTypeServiceDiscovery{}
530+
}
531+
cfgStorage.ServiceDiscovery.Nomads = &cfg.ServiceDiscovery.Nomads
532+
524533
if cfgStorage.LogTargets == nil {
525534
cfgStorage.LogTargets = &dpapilog.Targets{}
526535
}
+74-74
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
config_version = 2
2-
name = "famous_condor"
3-
mode = "single"
4-
status = "null"
2+
name = "famous_condor"
3+
mode = "single"
4+
status = "null"
55

66
dataplaneapi {
7-
scheme = ["http"]
8-
cleanup_timeout = "10s"
7+
scheme = ["http"]
8+
cleanup_timeout = "10s"
99
graceful_timeout = "15s"
10-
max_header_size = "1MiB"
11-
socket_path = "/var/run/data-plane.sock"
12-
host = "localhost"
13-
port = 80
14-
listen_limit = 1024
15-
keep_alive = "3m"
16-
read_timeout = "30s"
17-
write_timeout = "60s"
10+
max_header_size = "1MiB"
11+
socket_path = "/var/run/data-plane.sock"
12+
host = "localhost"
13+
port = 80
14+
listen_limit = 1024
15+
keep_alive = "3m"
16+
read_timeout = "30s"
17+
write_timeout = "60s"
1818
show_system_info = false
19-
disable_inotify = false
20-
pid_file = "/tmp/dataplane.pid"
21-
uid = 1000
22-
gid = 1000
19+
disable_inotify = false
20+
pid_file = "/tmp/dataplane.pid"
21+
uid = 1000
22+
gid = 1000
2323

2424
tls {
25-
tls_host = "null"
26-
tls_port = 6443
27-
tls_certificate = "null"
28-
tls_key = "null"
29-
tls_ca = "null"
30-
tls_listen_limit = 10
31-
tls_keep_alive = "1m"
32-
tls_read_timeout = "10s"
25+
tls_host = "null"
26+
tls_port = 6443
27+
tls_certificate = "null"
28+
tls_key = "null"
29+
tls_ca = "null"
30+
tls_listen_limit = 10
31+
tls_keep_alive = "1m"
32+
tls_read_timeout = "10s"
3333
tls_write_timeout = "10s"
3434
}
3535

@@ -39,88 +39,88 @@ dataplaneapi {
3939
}
4040

4141
userlist {
42-
userlist = "controller"
42+
userlist = "controller"
4343
userlist_file = "null"
4444
}
4545

4646
transaction {
47-
transaction_dir = "/tmp/haproxy"
48-
backups_number = 0
49-
backups_dir = "/tmp/backups"
47+
transaction_dir = "/tmp/haproxy"
48+
backups_number = 0
49+
backups_dir = "/tmp/backups"
5050
max_open_transactions = 20
5151
}
5252

5353
resources {
54-
maps_dir = "/etc/haproxy/maps"
55-
ssl_certs_dir = "/etc/haproxy/ssl"
56-
update_map_files = false
54+
maps_dir = "/etc/haproxy/maps"
55+
ssl_certs_dir = "/etc/haproxy/ssl"
56+
update_map_files = false
5757
update_map_files_period = 10
58-
spoe_dir = "/etc/haproxy/spoe"
59-
spoe_transaction_dir = "/tmp/spoe-haproxy"
58+
spoe_dir = "/etc/haproxy/spoe"
59+
spoe_transaction_dir = "/tmp/spoe-haproxy"
6060
}
6161

6262
advertised {
6363
api_address = "10.2.3.4"
64-
api_port = 80
64+
api_port = 80
6565
}
6666
}
6767

6868
haproxy {
69-
config_file = "/etc/haproxy/haproxy.cfg"
70-
haproxy_bin = "haproxy"
71-
master_runtime = "null"
72-
fid = "null"
69+
config_file = "/etc/haproxy/haproxy.cfg"
70+
haproxy_bin = "haproxy"
71+
master_runtime = "null"
72+
fid = "null"
7373
master_worker_mode = false
7474

7575
reload {
76-
reload_delay = 5
77-
reload_cmd = "systemctl reload haproxy"
78-
restart_cmd = "systemctl restart haproxy"
79-
status_cmd = "systemctl status haproxy"
76+
reload_delay = 5
77+
reload_cmd = "systemctl reload haproxy"
78+
restart_cmd = "systemctl restart haproxy"
79+
status_cmd = "systemctl status haproxy"
8080
reload_retention = 1
81-
validate_cmd = "null"
81+
validate_cmd = "null"
8282
}
8383
}
8484

8585
cluster {
86-
cluster_tls_dir = "null"
87-
id = "null"
88-
bootstrap_key = "null"
86+
cluster_tls_dir = "null"
87+
id = "null"
88+
bootstrap_key = "null"
8989
active_bootstrap_key = "null"
90-
token = "null"
91-
url = "null"
92-
port = 80
93-
api_base_path = "null"
94-
api_nodes_path = "null"
95-
api_register_path = "null"
96-
storage_dir = "null"
97-
cert_path = "null"
98-
cert_fetched = false
99-
name = "null"
100-
description = "null"
90+
token = "null"
91+
url = "null"
92+
port = 80
93+
api_base_path = "null"
94+
api_nodes_path = "null"
95+
api_register_path = "null"
96+
storage_dir = "null"
97+
cert_path = "null"
98+
cert_fetched = false
99+
name = "null"
100+
description = "null"
101101
}
102102

103103
service_discovery {
104-
consuls = []
104+
consuls = []
105105
aws_regions = []
106106
}
107107

108108
log_targets = [
109109
{
110-
log_to = "stdout"
111-
log_level = "debug"
112-
log_format = "json"
110+
log_to = "stdout"
111+
log_level = "debug"
112+
log_format = "json"
113113
log_types = [
114114
"access",
115115
"app",
116116
]
117117
},
118118
{
119-
log_to = "file"
120-
log_file = "/var/log/dataplanepi.log"
121-
log_level = "info"
122-
log_format = "text"
123-
log_types = ["app"]
119+
log_to = "file"
120+
log_file = "/var/log/dataplanepi.log"
121+
log_level = "info"
122+
log_format = "text"
123+
log_types = ["app"]
124124
},
125125
{
126126
log_to = "syslog"
@@ -136,17 +136,17 @@ log_targets = [
136136

137137
# Deprecated: use log_targets instead
138138
log {
139-
log_to = "stdout"
140-
log_file = "/var/log/dataplaneapi/dataplaneapi.log"
141-
log_level = "warning"
142-
log_format = "text"
139+
log_to = "stdout"
140+
log_file = "/var/log/dataplaneapi/dataplaneapi.log"
141+
log_level = "warning"
142+
log_format = "text"
143143
apache_common_log_format = "%h"
144144

145145
syslog {
146-
syslog_address = "null"
146+
syslog_address = "null"
147147
syslog_protocol = "tcp"
148-
syslog_tag = "dataplaneapi"
149-
syslog_level = "debug"
148+
syslog_tag = "dataplaneapi"
149+
syslog_level = "debug"
150150
syslog_facility = "local0"
151151
}
152152
}

configure_data_plane.go

+24
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
648648
ReloadAgent: ra,
649649
Context: ctx,
650650
})
651+
// Consul Handlers.
651652
api.ServiceDiscoveryCreateConsulHandler = &handlers.CreateConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}
652653
api.ServiceDiscoveryDeleteConsulHandler = &handlers.DeleteConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}
653654
api.ServiceDiscoveryGetConsulHandler = &handlers.GetConsulHandlerImpl{Discovery: discovery}
654655
api.ServiceDiscoveryGetConsulsHandler = &handlers.GetConsulsHandlerImpl{Discovery: discovery}
655656
api.ServiceDiscoveryReplaceConsulHandler = &handlers.ReplaceConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}
656657

658+
// Nomad Handlers.
659+
api.ServiceDiscoveryCreateNomadHandler = &handlers.CreateNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}
660+
api.ServiceDiscoveryDeleteNomadHandler = &handlers.DeleteNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}
661+
api.ServiceDiscoveryGetNomadHandler = &handlers.GetNomadHandlerImpl{Discovery: discovery}
662+
api.ServiceDiscoveryGetNomadsHandler = &handlers.GetNomadsHandlerImpl{Discovery: discovery}
663+
api.ServiceDiscoveryReplaceNomadHandler = &handlers.ReplaceNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}
664+
665+
// AWS Handlers.
657666
api.ServiceDiscoveryCreateAWSRegionHandler = &handlers.CreateAWSHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveAWS}
658667
api.ServiceDiscoveryGetAWSRegionHandler = &handlers.GetAWSRegionHandlerImpl{Discovery: discovery}
659668
api.ServiceDiscoveryGetAWSRegionsHandler = &handlers.GetAWSRegionsHandlerImpl{Discovery: discovery}
@@ -675,6 +684,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
675684
}
676685
_ = cfg.SaveConsuls(cfg.ServiceDiscovery.Consuls)
677686

687+
// Create stored nomad instances.
688+
for _, data := range cfg.ServiceDiscovery.Nomads {
689+
var errSD error
690+
if data.ID == nil || len(*data.ID) == 0 {
691+
data.ID = service_discovery.NewServiceDiscoveryUUID()
692+
}
693+
if errSD = service_discovery.ValidateNomadData(data, true); errSD != nil {
694+
log.Fatalf("Error validating Nomad instance: " + errSD.Error())
695+
}
696+
if errSD = discovery.AddNode("nomad", *data.ID, data); errSD != nil {
697+
log.Warning("Error creating nomad instance: " + errSD.Error())
698+
}
699+
}
700+
_ = cfg.SaveNomads(cfg.ServiceDiscovery.Nomads)
701+
678702
// create stored AWS instances
679703
for _, data := range cfg.ServiceDiscovery.AWSRegions {
680704
var errSD error

0 commit comments

Comments
 (0)