Skip to content

MAJOR: service discovery: Add support for Nomad #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ type NotifyConfiguration struct {
type ServiceDiscovery struct {
consulMu sync.Mutex
Consuls []*models.Consul `yaml:"consuls" group:"service_discovery" save:"true"`
nomadMu sync.Mutex
Nomads []*models.Nomad `yaml:"nomads" group:"service_discovery" save:"true"`
awsMu sync.Mutex
AWSRegions []*models.AwsRegion `yaml:"aws-regions" group:"service_discovery" save:"true"`
}
Expand Down Expand Up @@ -273,11 +275,15 @@ func (c *Configuration) Save() error {
cfg := c.storage.Get()
cfg.ServiceDiscovery.Consuls = nil
}
if len(c.ServiceDiscovery.Nomads) == 0 {
cfg := c.storage.Get()
cfg.ServiceDiscovery.Nomads = nil
}
if len(c.ServiceDiscovery.AWSRegions) == 0 {
cfg := c.storage.Get()
cfg.ServiceDiscovery.AWSRegions = nil
}
if cfg.ServiceDiscovery.Consuls == nil && cfg.ServiceDiscovery.AWSRegions == nil {
if cfg.ServiceDiscovery.Consuls == nil && cfg.ServiceDiscovery.AWSRegions == nil && cfg.ServiceDiscovery.Nomads == nil {
cfg := c.storage.Get()
cfg.ServiceDiscovery = nil
}
Expand Down Expand Up @@ -317,6 +323,13 @@ func (c *Configuration) SaveConsuls(consuls []*models.Consul) error {
return c.Save()
}

func (c *Configuration) SaveNomads(nomads []*models.Nomad) error {
c.ServiceDiscovery.nomadMu.Lock()
c.ServiceDiscovery.Nomads = nomads
c.ServiceDiscovery.nomadMu.Unlock()
return c.Save()
}

func (c *Configuration) SaveAWS(aws []*models.AwsRegion) error {
c.ServiceDiscovery.awsMu.Lock()
defer c.ServiceDiscovery.awsMu.Unlock()
Expand Down
12 changes: 10 additions & 2 deletions configuration/configuration_storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Code generated by go generate; DO NOT EDIT.
// Copyright 2021 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -85,7 +84,7 @@ type configTypeReload struct {
ReloadDelay *int `yaml:"reload_delay,omitempty" hcl:"reload_delay,omitempty"`
ReloadCmd *string `yaml:"reload_cmd,omitempty" hcl:"reload_cmd,omitempty"`
RestartCmd *string `yaml:"restart_cmd,omitempty" hcl:"restart_cmd,omitempty"`
StatusCmd *string `yaml:"status_cmd,omitempty" hcl:"status_cmd,omitempty"`
StatusCmd *string `yaml:"status_cmd,omitempty" hcl:"status_cmd,omitempty"`
ReloadRetention *int `yaml:"reload_retention,omitempty" hcl:"reload_retention,omitempty"`
ValidateCmd *string `yaml:"validate_cmd,omitempty" hcl:"validate_cmd,omitempty"`
}
Expand Down Expand Up @@ -134,6 +133,7 @@ type configTypeAdvertised struct {

type configTypeServiceDiscovery struct {
Consuls *[]*models.Consul `yaml:"consuls,omitempty" hcl:"consuls,omitempty"`
Nomads *[]*models.Nomad `yaml:"nomads,omitempty" hcl:"nomads,omitempty"`
AWSRegions *[]*models.AwsRegion `yaml:"aws_regions,omitempty" hcl:"aws_regions,omitempty"`
}

Expand Down Expand Up @@ -344,6 +344,9 @@ func copyToConfiguration(cfg *Configuration) {
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.Consuls != nil && !misc.HasOSArg("", "", "") {
cfg.ServiceDiscovery.Consuls = *cfgStorage.ServiceDiscovery.Consuls
}
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.Nomads != nil && !misc.HasOSArg("", "", "") {
cfg.ServiceDiscovery.Nomads = *cfgStorage.ServiceDiscovery.Nomads
}
if cfgStorage.ServiceDiscovery != nil && cfgStorage.ServiceDiscovery.AWSRegions != nil && !misc.HasOSArg("", "", "") {
cfg.ServiceDiscovery.AWSRegions = *cfgStorage.ServiceDiscovery.AWSRegions
}
Expand Down Expand Up @@ -522,6 +525,11 @@ func copyConfigurationToStorage(cfg *Configuration) {
}
cfgStorage.ServiceDiscovery.AWSRegions = &cfg.ServiceDiscovery.AWSRegions

if cfgStorage.ServiceDiscovery == nil {
cfgStorage.ServiceDiscovery = &configTypeServiceDiscovery{}
}
cfgStorage.ServiceDiscovery.Nomads = &cfg.ServiceDiscovery.Nomads

if cfgStorage.LogTargets == nil {
cfgStorage.LogTargets = &dpapilog.Targets{}
}
Expand Down
148 changes: 74 additions & 74 deletions configuration/examples/example-full.hcl
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
config_version = 2
name = "famous_condor"
mode = "single"
status = "null"
name = "famous_condor"
mode = "single"
status = "null"

dataplaneapi {
scheme = ["http"]
cleanup_timeout = "10s"
scheme = ["http"]
cleanup_timeout = "10s"
graceful_timeout = "15s"
max_header_size = "1MiB"
socket_path = "/var/run/data-plane.sock"
host = "localhost"
port = 80
listen_limit = 1024
keep_alive = "3m"
read_timeout = "30s"
write_timeout = "60s"
max_header_size = "1MiB"
socket_path = "/var/run/data-plane.sock"
host = "localhost"
port = 80
listen_limit = 1024
keep_alive = "3m"
read_timeout = "30s"
write_timeout = "60s"
show_system_info = false
disable_inotify = false
pid_file = "/tmp/dataplane.pid"
uid = 1000
gid = 1000
disable_inotify = false
pid_file = "/tmp/dataplane.pid"
uid = 1000
gid = 1000

tls {
tls_host = "null"
tls_port = 6443
tls_certificate = "null"
tls_key = "null"
tls_ca = "null"
tls_listen_limit = 10
tls_keep_alive = "1m"
tls_read_timeout = "10s"
tls_host = "null"
tls_port = 6443
tls_certificate = "null"
tls_key = "null"
tls_ca = "null"
tls_listen_limit = 10
tls_keep_alive = "1m"
tls_read_timeout = "10s"
tls_write_timeout = "10s"
}

Expand All @@ -39,88 +39,88 @@ dataplaneapi {
}

userlist {
userlist = "controller"
userlist = "controller"
userlist_file = "null"
}

transaction {
transaction_dir = "/tmp/haproxy"
backups_number = 0
backups_dir = "/tmp/backups"
transaction_dir = "/tmp/haproxy"
backups_number = 0
backups_dir = "/tmp/backups"
max_open_transactions = 20
}

resources {
maps_dir = "/etc/haproxy/maps"
ssl_certs_dir = "/etc/haproxy/ssl"
update_map_files = false
maps_dir = "/etc/haproxy/maps"
ssl_certs_dir = "/etc/haproxy/ssl"
update_map_files = false
update_map_files_period = 10
spoe_dir = "/etc/haproxy/spoe"
spoe_transaction_dir = "/tmp/spoe-haproxy"
spoe_dir = "/etc/haproxy/spoe"
spoe_transaction_dir = "/tmp/spoe-haproxy"
}

advertised {
api_address = "10.2.3.4"
api_port = 80
api_port = 80
}
}

haproxy {
config_file = "/etc/haproxy/haproxy.cfg"
haproxy_bin = "haproxy"
master_runtime = "null"
fid = "null"
config_file = "/etc/haproxy/haproxy.cfg"
haproxy_bin = "haproxy"
master_runtime = "null"
fid = "null"
master_worker_mode = false

reload {
reload_delay = 5
reload_cmd = "systemctl reload haproxy"
restart_cmd = "systemctl restart haproxy"
status_cmd = "systemctl status haproxy"
reload_delay = 5
reload_cmd = "systemctl reload haproxy"
restart_cmd = "systemctl restart haproxy"
status_cmd = "systemctl status haproxy"
reload_retention = 1
validate_cmd = "null"
validate_cmd = "null"
}
}

cluster {
cluster_tls_dir = "null"
id = "null"
bootstrap_key = "null"
cluster_tls_dir = "null"
id = "null"
bootstrap_key = "null"
active_bootstrap_key = "null"
token = "null"
url = "null"
port = 80
api_base_path = "null"
api_nodes_path = "null"
api_register_path = "null"
storage_dir = "null"
cert_path = "null"
cert_fetched = false
name = "null"
description = "null"
token = "null"
url = "null"
port = 80
api_base_path = "null"
api_nodes_path = "null"
api_register_path = "null"
storage_dir = "null"
cert_path = "null"
cert_fetched = false
name = "null"
description = "null"
}

service_discovery {
consuls = []
consuls = []
aws_regions = []
}

log_targets = [
{
log_to = "stdout"
log_level = "debug"
log_format = "json"
log_to = "stdout"
log_level = "debug"
log_format = "json"
log_types = [
"access",
"app",
]
},
{
log_to = "file"
log_file = "/var/log/dataplanepi.log"
log_level = "info"
log_format = "text"
log_types = ["app"]
log_to = "file"
log_file = "/var/log/dataplanepi.log"
log_level = "info"
log_format = "text"
log_types = ["app"]
},
{
log_to = "syslog"
Expand All @@ -136,17 +136,17 @@ log_targets = [

# Deprecated: use log_targets instead
log {
log_to = "stdout"
log_file = "/var/log/dataplaneapi/dataplaneapi.log"
log_level = "warning"
log_format = "text"
log_to = "stdout"
log_file = "/var/log/dataplaneapi/dataplaneapi.log"
log_level = "warning"
log_format = "text"
apache_common_log_format = "%h"

syslog {
syslog_address = "null"
syslog_address = "null"
syslog_protocol = "tcp"
syslog_tag = "dataplaneapi"
syslog_level = "debug"
syslog_tag = "dataplaneapi"
syslog_level = "debug"
syslog_facility = "local0"
}
}
24 changes: 24 additions & 0 deletions configure_data_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
ReloadAgent: ra,
Context: ctx,
})
// Consul Handlers.
api.ServiceDiscoveryCreateConsulHandler = &handlers.CreateConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}
api.ServiceDiscoveryDeleteConsulHandler = &handlers.DeleteConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}
api.ServiceDiscoveryGetConsulHandler = &handlers.GetConsulHandlerImpl{Discovery: discovery}
api.ServiceDiscoveryGetConsulsHandler = &handlers.GetConsulsHandlerImpl{Discovery: discovery}
api.ServiceDiscoveryReplaceConsulHandler = &handlers.ReplaceConsulHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveConsuls}

// Nomad Handlers.
api.ServiceDiscoveryCreateNomadHandler = &handlers.CreateNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}
api.ServiceDiscoveryDeleteNomadHandler = &handlers.DeleteNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}
api.ServiceDiscoveryGetNomadHandler = &handlers.GetNomadHandlerImpl{Discovery: discovery}
api.ServiceDiscoveryGetNomadsHandler = &handlers.GetNomadsHandlerImpl{Discovery: discovery}
api.ServiceDiscoveryReplaceNomadHandler = &handlers.ReplaceNomadHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveNomads}

// AWS Handlers.
api.ServiceDiscoveryCreateAWSRegionHandler = &handlers.CreateAWSHandlerImpl{Discovery: discovery, PersistCallback: cfg.SaveAWS}
api.ServiceDiscoveryGetAWSRegionHandler = &handlers.GetAWSRegionHandlerImpl{Discovery: discovery}
api.ServiceDiscoveryGetAWSRegionsHandler = &handlers.GetAWSRegionsHandlerImpl{Discovery: discovery}
Expand All @@ -675,6 +684,21 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
}
_ = cfg.SaveConsuls(cfg.ServiceDiscovery.Consuls)

// Create stored nomad instances.
for _, data := range cfg.ServiceDiscovery.Nomads {
var errSD error
if data.ID == nil || len(*data.ID) == 0 {
data.ID = service_discovery.NewServiceDiscoveryUUID()
}
if errSD = service_discovery.ValidateNomadData(data, true); errSD != nil {
log.Fatalf("Error validating Nomad instance: " + errSD.Error())
}
if errSD = discovery.AddNode("nomad", *data.ID, data); errSD != nil {
log.Warning("Error creating nomad instance: " + errSD.Error())
}
}
_ = cfg.SaveNomads(cfg.ServiceDiscovery.Nomads)

// create stored AWS instances
for _, data := range cfg.ServiceDiscovery.AWSRegions {
var errSD error
Expand Down
Loading