Moving the logic to find port and host inside consul client
This commit is contained in:
parent
404810043a
commit
d6da6372cd
|
@ -29,11 +29,15 @@ func NewConsulClient() (*ConsulClient, error) {
|
|||
return &consulClient, nil
|
||||
}
|
||||
|
||||
func (c *ConsulClient) Register(task *structs.Task, allocID string, port int, host string) error {
|
||||
func (c *ConsulClient) Register(task *structs.Task, allocID string) error {
|
||||
var mErr multierror.Error
|
||||
serviceDefns := make([]*api.AgentServiceRegistration, len(task.Services))
|
||||
var serviceDefns []*api.AgentServiceRegistration
|
||||
for idx, service := range task.Services {
|
||||
service.Id = fmt.Sprintf("%s-%s", allocID, task.Name)
|
||||
host, port := c.findPortAndHostForLabel(service.PortLabel, task)
|
||||
if host == "" || port == 0 {
|
||||
continue
|
||||
}
|
||||
asr := &api.AgentServiceRegistration{
|
||||
ID: service.Id,
|
||||
Name: service.Name,
|
||||
|
@ -62,3 +66,17 @@ func (c *ConsulClient) DeRegister(task *structs.Task) error {
|
|||
}
|
||||
return mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (c *ConsulClient) findPortAndHostForLabel(portLabel string, task *structs.Task) (string, int) {
|
||||
var host string
|
||||
var port int
|
||||
for _, network := range task.Resources.Networks {
|
||||
if p, ok := network.MapLabelToValues()[portLabel]; ok {
|
||||
host = network.IP
|
||||
port = p
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return host, port
|
||||
}
|
||||
|
|
|
@ -234,20 +234,7 @@ func (r *TaskRunner) run() {
|
|||
destroyed := false
|
||||
|
||||
// Register the services defined by the task with Consil
|
||||
for _, service := range r.task.Services {
|
||||
portLabel := service.PortLabel
|
||||
var port int
|
||||
var host string
|
||||
for _, network := range r.task.Resources.Networks {
|
||||
if p, ok := network.MapLabelToValues()[portLabel]; ok {
|
||||
port = p
|
||||
host = network.IP
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
r.consulClient.Register(r.task, r.allocID, port, host)
|
||||
}
|
||||
r.consulClient.Register(r.task, r.allocID)
|
||||
|
||||
OUTER:
|
||||
// Wait for updates
|
||||
|
|
Loading…
Reference in a new issue