@@ -10,20 +10,21 @@ import (
10
10
log "github.com/Sirupsen/logrus"
11
11
"github.com/allegro/marathon-consul/metrics"
12
12
"github.com/allegro/marathon-consul/utils"
13
- consulapi "github.com/hashicorp/consul/api"
14
13
)
15
14
16
15
type Agents interface {
17
- GetAgent (agentAddress string ) (agent * consulapi.Client , err error )
16
+ GetAgent (agentAddress string ) (agent * Agent , err error )
17
+ GetLocalAgent () (agent * Agent , err error )
18
18
GetAnyAgent () (agent * Agent , err error )
19
19
RemoveAgent (agentAddress string )
20
20
}
21
21
22
22
type ConcurrentAgents struct {
23
- agents map [string ]* Agent
24
- config * Config
25
- lock sync.Mutex
26
- client * http.Client
23
+ localAgent * Agent
24
+ agents map [string ]* Agent
25
+ config * Config
26
+ lock sync.Mutex
27
+ client * http.Client
27
28
}
28
29
29
30
func NewAgents (config * Config ) * ConcurrentAgents {
@@ -48,21 +49,7 @@ func NewAgents(config *Config) *ConcurrentAgents {
48
49
log .WithError (err ).WithField ("agent" , config .LocalAgentHost ).Fatal (
49
50
"Cannot connect with consul agent. Check if configuration is valid." )
50
51
}
51
-
52
- // Get all agents from current DC and store them in cache
53
- nodes , _ , err := agent .Catalog ().Nodes (nil )
54
- if err != nil {
55
- log .WithError (err ).WithField ("agent" , config .LocalAgentHost ).Warn (
56
- "Cannot obtain agents from local consul agent." )
57
- return agents
58
- }
59
- for _ , node := range nodes {
60
- _ , err := agents .GetAgent (node .Address )
61
- if err != nil {
62
- log .WithError (err ).WithField ("agent" , node .Address ).Warn (
63
- "Cannot connect with consul agent. Check if configuration is valid." )
64
- }
65
- }
52
+ agents .localAgent = agent
66
53
}
67
54
return agents
68
55
}
@@ -78,6 +65,13 @@ func (a *ConcurrentAgents) GetAnyAgent() (*Agent, error) {
78
65
return nil , errors .New ("No Consul client available in agents cache" )
79
66
}
80
67
68
+ func (a * ConcurrentAgents ) GetLocalAgent () (* Agent , error ) {
69
+ if a .localAgent == nil {
70
+ return nil , errors .New ("No local consul agent defined" )
71
+ }
72
+ return a .localAgent , nil
73
+ }
74
+
81
75
func (a * ConcurrentAgents ) getRandomAgentIPAddress () string {
82
76
ipAddresses := []string {}
83
77
for ipAddress := range a .agents {
@@ -101,7 +95,7 @@ func (a *ConcurrentAgents) RemoveAgent(agentAddress string) {
101
95
}
102
96
}
103
97
104
- func (a * ConcurrentAgents ) GetAgent (agentAddress string ) (* consulapi. Client , error ) {
98
+ func (a * ConcurrentAgents ) GetAgent (agentAddress string ) (* Agent , error ) {
105
99
a .lock .Lock ()
106
100
defer a .lock .Unlock ()
107
101
@@ -112,7 +106,7 @@ func (a *ConcurrentAgents) GetAgent(agentAddress string) (*consulapi.Client, err
112
106
ipAddress := IP .String ()
113
107
114
108
if agent , ok := a .agents [ipAddress ]; ok {
115
- return agent . Client , nil
109
+ return agent , nil
116
110
}
117
111
118
112
newAgent , err := a .createAgent (ipAddress )
@@ -121,7 +115,7 @@ func (a *ConcurrentAgents) GetAgent(agentAddress string) (*consulapi.Client, err
121
115
}
122
116
a .addAgent (ipAddress , newAgent )
123
117
124
- return newAgent . Client , nil
118
+ return newAgent , nil
125
119
}
126
120
127
121
func (a * ConcurrentAgents ) addAgent (agentHost string , agent * Agent ) {
0 commit comments