@ -227,7 +227,7 @@ func (endpoints EndpointList) UpdateIsLocal() error {
keepAliveTicker := time . NewTicker ( retryInterval * time . Second )
keepAliveTicker := time . NewTicker ( retryInterval * time . Second )
defer keepAliveTicker . Stop ( )
defer keepAliveTicker . Stop ( )
for {
for {
// Break if the local endpoint is found already. Or all the endpoints are resolved.
// Break if the local endpoint is found already Or all the endpoints are resolved.
if foundLocal || ( epsResolved == len ( endpoints ) ) {
if foundLocal || ( epsResolved == len ( endpoints ) ) {
break
break
}
}
@ -240,13 +240,13 @@ func (endpoints EndpointList) UpdateIsLocal() error {
default :
default :
for i , resolved := range resolvedList {
for i , resolved := range resolvedList {
if resolved {
if resolved {
// Continue if host is already resolved.
continue
continue
}
}
// return err if not Docker or Kubernetes
// return err if not Docker or Kubernetes
// We use IsDocker() method to check for Docker Swarm environment
// We use IsDocker() to check for Docker environment
// as there is no reliable way to clearly identify Swarm from
// We use IsKubernetes() to check for Kubernetes environment
// Docker environment.
isLocal , err := isLocalHost ( endpoints [ i ] . HostName )
isLocal , err := isLocalHost ( endpoints [ i ] . HostName )
if err != nil {
if err != nil {
if ! IsDocker ( ) && ! IsKubernetes ( ) {
if ! IsDocker ( ) && ! IsKubernetes ( ) {
@ -256,8 +256,7 @@ func (endpoints EndpointList) UpdateIsLocal() error {
timeElapsed := time . Since ( startTime )
timeElapsed := time . Since ( startTime )
// log error only if more than 1s elapsed
// log error only if more than 1s elapsed
if timeElapsed > time . Second {
if timeElapsed > time . Second {
// log the message to console about the host not being
// Log the message to console about the host not being resolveable.
// resolveable.
reqInfo := ( & logger . ReqInfo { } ) . AppendTags ( "host" , endpoints [ i ] . HostName )
reqInfo := ( & logger . ReqInfo { } ) . AppendTags ( "host" , endpoints [ i ] . HostName )
reqInfo . AppendTags ( "elapsedTime" , humanize . RelTime ( startTime , startTime . Add ( timeElapsed ) , "elapsed" , "" ) )
reqInfo . AppendTags ( "elapsedTime" , humanize . RelTime ( startTime , startTime . Add ( timeElapsed ) , "elapsed" , "" ) )
ctx := logger . SetReqInfo ( context . Background ( ) , reqInfo )
ctx := logger . SetReqInfo ( context . Background ( ) , reqInfo )
@ -274,15 +273,33 @@ func (endpoints EndpointList) UpdateIsLocal() error {
}
}
// Wait for the tick, if the there exist a local endpoint in discovery.
// Wait for the tick, if the there exist a local endpoint in discovery.
// Non docker/kubernetes environment does not need to wait.
// Non docker/kubernetes environment we do not need to wait.
if ! foundLocal && ( IsDocker ( ) && IsKubernetes ( ) ) {
if ! foundLocal && ( IsDocker ( ) || IsKubernetes ( ) ) {
<- keepAliveTicker . C
<- keepAliveTicker . C
}
}
}
}
}
}
// On Kubernetes/Docker setups DNS resolves inappropriately sometimes
// where there are situations same endpoints with multiple disks
// come online indicating either one of them is local and some
// of them are not local. This situation can never happen and
// its only a possibility in orchestrated deployments with dynamic
// DNS. Following code ensures that we treat if one of the endpoint
// says its local for a given host - it is true for all endpoints
// for the same host. Following code ensures that this assumption
// is true and it works in all scenarios and it is safe to assume
// for a given host.
endpointLocalMap := make ( map [ string ] bool )
for _ , ep := range endpoints {
if ep . IsLocal {
endpointLocalMap [ ep . Host ] = ep . IsLocal
}
}
for i := range endpoints {
endpoints [ i ] . IsLocal = endpointLocalMap [ endpoints [ i ] . Host ]
}
return nil
return nil
}
}
// NewEndpointList - returns new endpoint list based on input args.
// NewEndpointList - returns new endpoint list based on input args.