Skip to content

Commit

Permalink
Return healthy tunnel service instance (#48)
Browse files Browse the repository at this point in the history
* Service discovery should return a healthy tunnel service instance

* Always return at least one
  • Loading branch information
andrewpage authored Jan 8, 2024
1 parent 2d83a36 commit 89698f5
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tunnel/discovery/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,31 @@ func (d Discovery) GetTunnel(id uuid.UUID) (discovery.TunnelDetails, error) {
return discovery.TunnelDetails{}, errors.Wrap(err, "could not get tunnel details")
}

if len(services) != 1 {
return discovery.TunnelDetails{}, fmt.Errorf("expected 1 service, got %d", len(services))
if len(services) == 0 {
return discovery.TunnelDetails{}, fmt.Errorf("tunnel %s not found", id.String())
}
service := services[0]

// Search for a healthy tunnel service instance and return it
for _, service := range services {
if service.Checks.AggregatedStatus() != consul.HealthPassing {
continue
}
return formatTunnelDetails(id, service), nil
}

// If there are no healthy tunnel service instances, return the first
return formatTunnelDetails(id, services[0]), nil
}

func formatTunnelDetails(tunnelId uuid.UUID, service *consul.ServiceEntry) discovery.TunnelDetails {
response := discovery.TunnelDetails{
Host: service.Service.Address,
Port: service.Service.Port,
Checks: make([]discovery.HealthcheckDetails, 0),
}

// All relevant custom checks must start with this prefix
checkPrefix := getTunnelServiceId(id)
checkPrefix := getTunnelServiceId(tunnelId)
for _, c := range service.Checks {
// Only consider checks that have this prefix (this means that Passage manages them)
if strings.HasPrefix(c.CheckID, checkPrefix) {
Expand All @@ -115,7 +127,7 @@ func (d Discovery) GetTunnel(id uuid.UUID) (discovery.TunnelDetails, error) {
}
}

return response, nil
return response
}

func (d Discovery) RegisterHealthcheck(tunnelId uuid.UUID, options discovery.HealthcheckOptions) error {
Expand Down

0 comments on commit 89698f5

Please sign in to comment.