@@ -2,48 +2,33 @@ package actions
2
2
3
3
import (
4
4
"../proxy"
5
- "../registry"
6
5
"encoding/json"
7
6
"fmt"
8
- "io/ioutil"
9
7
"net/http"
10
- "strings"
11
8
)
12
9
13
10
// Fetchable defines interface that fetches information from other sources
14
11
type Fetchable interface {
15
- // TODO: It's deprecated (Consul). Remove it.
16
- ReloadServicesFromRegistry (addresses []string , instanceName , mode string ) error
17
12
// Sends request to swarm-listener to request reconfiguration of all proxy instances in Swarm.
18
13
ReloadClusterConfig (listenerAddr string ) error
19
14
// Reconfigures this instance of proxy based on configuration taken from swarm-listener.
20
15
// This is synchronous.
21
16
// If listenerAddr is nil, unreachable or any other problem error is returned.
22
- ReloadConfig (baseData BaseReconfigure , mode string , listenerAddr string ) error
17
+ ReloadConfig (baseData BaseReconfigure , listenerAddr string ) error
23
18
}
24
19
type fetch struct {
25
20
BaseReconfigure
26
- Mode string `short:"m" long:"mode" env:"MODE" description:"If set to 'swarm', proxy will operate assuming that Docker service from v1.12+ is used."`
27
21
}
28
22
29
23
// NewFetch returns instance of the Fetchable object
30
- var NewFetch = func (baseData BaseReconfigure , mode string ) Fetchable {
24
+ var NewFetch = func (baseData BaseReconfigure ) Fetchable {
31
25
return & fetch {
32
26
BaseReconfigure : baseData ,
33
- Mode : mode ,
34
27
}
35
28
}
36
29
37
- // TODO: It's deprecated (Consul). Remove it.
38
- func (m * fetch ) ReloadServicesFromRegistry (addresses []string , instanceName , mode string ) error {
39
- if len (addresses ) > 0 {
40
- return m .reloadFromRegistry (addresses , instanceName , mode )
41
- }
42
- return nil
43
- }
44
-
45
30
// ReloadConfig recreates proxy configuration with data fetches from Swarm Listener
46
- func (m * fetch ) ReloadConfig (baseData BaseReconfigure , mode string , listenerAddr string ) error {
31
+ func (m * fetch ) ReloadConfig (baseData BaseReconfigure , listenerAddr string ) error {
47
32
if len (listenerAddr ) == 0 {
48
33
return fmt .Errorf ("Swarm Listener address is missing %s" , listenerAddr )
49
34
}
@@ -64,7 +49,7 @@ func (m *fetch) ReloadConfig(baseData BaseReconfigure, mode string, listenerAddr
64
49
for _ , s := range services {
65
50
proxyService := proxy .GetServiceFromMap (& s )
66
51
if statusCode , _ := proxy .IsValidReconf (proxyService ); statusCode == http .StatusOK {
67
- reconfigure := NewReconfigure (baseData , * proxyService , mode )
52
+ reconfigure := NewReconfigure (baseData , * proxyService )
68
53
reconfigure .Execute (false )
69
54
needsReload = true
70
55
}
@@ -91,88 +76,10 @@ func (m *fetch) ReloadClusterConfig(listenerAddr string) error {
91
76
}
92
77
93
78
func (m * fetch ) getReconfigure (service * proxy.Service ) Reconfigurable {
94
- return NewReconfigure (m .BaseReconfigure , * service , m . Mode )
79
+ return NewReconfigure (m .BaseReconfigure , * service )
95
80
}
96
81
97
82
func (m * fetch ) getReload () Reloader {
98
83
return NewReload ()
99
84
}
100
85
101
- // TODO: It's deprecated (Consul). Remove it.
102
- func (m * fetch ) reloadFromRegistry (addresses []string , instanceName , mode string ) error {
103
- var resp * http.Response
104
- var err error
105
- logPrintf ("Configuring existing services" )
106
- found := false
107
- for _ , address := range addresses {
108
- address = strings .ToLower (address )
109
- if ! strings .HasPrefix (address , "http" ) {
110
- address = fmt .Sprintf ("http://%s" , address )
111
- }
112
- servicesUrl := fmt .Sprintf ("%s/v1/catalog/services" , address )
113
- resp , err = http .Get (servicesUrl )
114
- if err == nil {
115
- found = true
116
- break
117
- }
118
- }
119
- if ! found {
120
- return fmt .Errorf ("Could not retrieve the list of services from Consul" )
121
- }
122
- defer resp .Body .Close ()
123
- body , _ := ioutil .ReadAll (resp .Body )
124
- c := make (chan proxy.Service )
125
- count := 0
126
- var data map [string ]interface {}
127
- json .Unmarshal (body , & data )
128
- count = len (data )
129
- for key := range data {
130
- go m .getService (addresses , key , instanceName , c )
131
- }
132
- logPrintf ("\t Found %d services" , count )
133
- for i := 0 ; i < count ; i ++ {
134
- s := <- c
135
- if len (s .ServiceDest ) > 0 && len (s .ServiceDest [0 ].ServicePath ) > 0 {
136
- reconfigure := m .getReconfigure (& s )
137
- reconfigure .Execute (false )
138
- }
139
- }
140
- reload := m .getReload ()
141
- return reload .Execute (true )
142
- }
143
-
144
- // TODO: It's deprecated (Consul). Remove it.
145
- func (m * fetch ) getService (addresses []string , serviceName , instanceName string , c chan proxy.Service ) {
146
- sr := proxy.Service {ServiceName : serviceName }
147
-
148
- path , err := registryInstance .GetServiceAttribute (addresses , serviceName , registry .PATH_KEY , instanceName )
149
- port , _ := m .getServiceAttribute (addresses , serviceName , registry .PORT , instanceName )
150
- sd := proxy.ServiceDest {
151
- ServicePath : strings .Split (path , "," ),
152
- Port : port ,
153
- }
154
- if err == nil {
155
- sr .ServiceDest = []proxy.ServiceDest {sd }
156
- sr .ServiceColor , _ = m .getServiceAttribute (addresses , serviceName , registry .COLOR_KEY , instanceName )
157
- sr .ServiceCert , _ = m .getServiceAttribute (addresses , serviceName , registry .CERT_KEY , instanceName )
158
- sr .OutboundHostname , _ = m .getServiceAttribute (addresses , serviceName , registry .HOSTNAME_KEY , instanceName )
159
- sr .PathType , _ = m .getServiceAttribute (addresses , serviceName , registry .PATH_TYPE_KEY , instanceName )
160
- sr .ConsulTemplateFePath , _ = m .getServiceAttribute (addresses , serviceName , registry .CONSUL_TEMPLATE_FE_PATH_KEY , instanceName )
161
- sr .ConsulTemplateBePath , _ = m .getServiceAttribute (addresses , serviceName , registry .CONSUL_TEMPLATE_BE_PATH_KEY , instanceName )
162
- }
163
- c <- sr
164
- }
165
-
166
- // TODO: It's deprecated (Consul). Remove it.
167
- func (m * fetch ) getServiceAttribute (addresses []string , serviceName , key , instanceName string ) (string , bool ) {
168
- for _ , address := range addresses {
169
- url := fmt .Sprintf ("%s/v1/kv/%s/%s/%s?raw" , address , instanceName , serviceName , key )
170
- resp , err := http .Get (url )
171
- if err == nil && resp .StatusCode == http .StatusOK {
172
- defer resp .Body .Close ()
173
- body , _ := ioutil .ReadAll (resp .Body )
174
- return string (body ), true
175
- }
176
- }
177
- return "" , false
178
- }
0 commit comments