This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
consul.go
162 lines (135 loc) · 3.53 KB
/
consul.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package consul
import (
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/CiscoCloud/mesos-consul/registry"
consulapi "github.com/hashicorp/consul/api"
log "github.com/sirupsen/logrus"
)
type Consul struct {
agents map[string]*consulapi.Client
config consulConfig
}
//
func New() *Consul {
return &Consul{
agents: make(map[string]*consulapi.Client),
config: config,
}
}
// client()
// Return a consul client at the specified address
func (c *Consul) client(address string) *consulapi.Client {
if address == "" {
log.Warn("No address to Consul.Agent")
return nil
}
if _, ok := c.agents[address]; !ok {
// Agent connection not saved. Connect.
c.agents[address] = c.newAgent(address)
}
return c.agents[address]
}
// newAgent()
// Connect to a new agent specified by address
//
func (c *Consul) newAgent(address string) *consulapi.Client {
if address == "" {
log.Warnf("No address to Consul.NewAgent")
return nil
}
config := consulapi.DefaultConfig()
config.Address = fmt.Sprintf("%s:%s", address, c.config.port)
log.Debugf("consul address: %s", config.Address)
config.HttpClient.Timeout = time.Duration(c.config.timeout) * time.Second
log.Debugf("consul timeout: %d", config.HttpClient.Timeout)
if c.config.token != "" {
log.Debugf("setting token to %s", c.config.token)
config.Token = c.config.token
}
if c.config.sslEnabled {
log.Debugf("enabling SSL")
config.Scheme = "https"
}
if !c.config.sslVerify {
log.Debugf("disabled SSL verification")
config.HttpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
if c.config.auth.Enabled {
log.Debugf("setting basic auth")
config.HttpAuth = &consulapi.HttpBasicAuth{
Username: c.config.auth.Username,
Password: c.config.auth.Password,
}
}
client, err := consulapi.NewClient(config)
if err != nil {
log.Fatal("consul: ", address)
}
return client
}
func (c *Consul) Register(service *registry.Service) {
if _, ok := serviceCache[service.ID]; ok {
log.Debugf("Service found. Not registering: %s", service.ID)
c.CacheMark(service.ID)
return
}
if _, ok := c.agents[service.Agent]; !ok {
// Agent connection not saved. Connect.
c.agents[service.Agent] = c.newAgent(service.Agent)
}
log.Info("Registering ", service.ID)
s := &consulapi.AgentServiceRegistration{
ID: service.ID,
Name: service.Name,
Port: service.Port,
Address: service.Address,
Check: &consulapi.AgentServiceCheck{
TTL: service.Check.TTL,
Script: service.Check.Script,
HTTP: service.Check.HTTP,
Interval: service.Check.Interval,
},
}
if len(service.Tags) > 0 {
s.Tags = service.Tags
}
err := c.agents[service.Agent].Agent().ServiceRegister(s)
if err != nil {
log.Warnf("Unable to register %s: %s", s.ID, err.Error())
return
}
serviceCache[s.ID] = newCacheEntry(s, service.Agent)
c.CacheMark(s.ID)
}
// Deregister()
// Deregister services that no longer exist
//
func (c *Consul) Deregister() {
for s, b := range serviceCache {
if c.CacheIsValid(s) {
c.CacheProcessDeregister(s)
} else {
log.Infof("Deregistering %s", s)
err := c.deregister(b.agent, b.service)
if err != nil {
log.Info("Deregistration error ", err)
} else {
delete(serviceCache, s)
}
}
}
}
func (c *Consul) deregister(agent string, service *consulapi.AgentServiceRegistration) error {
if _, ok := c.agents[agent]; !ok {
// Agent connection not saved. Connect.
c.agents[agent] = c.newAgent(agent)
}
return c.agents[agent].Agent().ServiceDeregister(service.ID)
}