/
controller.go
247 lines (213 loc) · 7 KB
/
controller.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package consul
import (
"time"
"istio.io/istio/pkg/spiffe"
"github.com/hashicorp/consul/api"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
)
// Controller communicates with Consul and monitors for changes
type Controller struct {
client *api.Client
monitor Monitor
}
// NewController creates a new Consul controller
func NewController(addr string, interval time.Duration) (*Controller, error) {
conf := api.DefaultConfig()
conf.Address = addr
client, err := api.NewClient(conf)
return &Controller{
monitor: NewConsulMonitor(client, interval),
client: client,
}, err
}
// Services list declarations of all services in the system
func (c *Controller) Services() ([]*model.Service, error) {
data, err := c.getServices()
if err != nil {
return nil, err
}
services := make([]*model.Service, 0, len(data))
for name := range data {
endpoints, err := c.getCatalogService(name, nil)
if err != nil {
return nil, err
}
services = append(services, convertService(endpoints))
}
return services, nil
}
// GetService retrieves a service by host name if it exists
func (c *Controller) GetService(hostname model.Hostname) (*model.Service, error) {
// Get actual service by name
name, err := parseHostname(hostname)
if err != nil {
log.Infof("parseHostname(%s) => error %v", hostname, err)
return nil, err
}
endpoints, err := c.getCatalogService(name, nil)
if len(endpoints) == 0 || err != nil {
return nil, err
}
return convertService(endpoints), nil
}
func (c *Controller) getServices() (map[string][]string, error) {
data, _, err := c.client.Catalog().Services(nil)
if err != nil {
log.Warnf("Could not retrieve services from consul: %v", err)
return nil, err
}
return data, nil
}
// nolint: unparam
func (c *Controller) getCatalogService(name string, q *api.QueryOptions) ([]*api.CatalogService, error) {
endpoints, _, err := c.client.Catalog().Service(name, "", q)
if err != nil {
log.Warnf("Could not retrieve service catalogue from consul: %v", err)
return nil, err
}
return endpoints, nil
}
// ManagementPorts retrieves set of health check ports by instance IP.
// This does not apply to Consul service registry, as Consul does not
// manage the service instances. In future, when we integrate Nomad, we
// might revisit this function.
func (c *Controller) ManagementPorts(addr string) model.PortList {
return nil
}
// WorkloadHealthCheckInfo retrieves set of health check info by instance IP.
// This does not apply to Consul service registry, as Consul does not
// manage the service instances. In future, when we integrate Nomad, we
// might revisit this function.
func (c *Controller) WorkloadHealthCheckInfo(addr string) model.ProbeList {
return nil
}
// InstancesByPort retrieves instances for a service that match
// any of the supplied labels. All instances match an empty tag list.
func (c *Controller) InstancesByPort(hostname model.Hostname, port int,
labels model.LabelsCollection) ([]*model.ServiceInstance, error) {
// Get actual service by name
name, err := parseHostname(hostname)
if err != nil {
log.Infof("parseHostname(%s) => error %v", hostname, err)
return nil, err
}
endpoints, err := c.getCatalogService(name, nil)
if err != nil {
return nil, err
}
instances := []*model.ServiceInstance{}
for _, endpoint := range endpoints {
instance := convertInstance(endpoint)
if labels.HasSubsetOf(instance.Labels) && portMatch(instance, port) {
instances = append(instances, instance)
}
}
return instances, nil
}
// returns true if an instance's port matches with any in the provided list
func portMatch(instance *model.ServiceInstance, port int) bool {
return port == 0 || port == instance.Endpoint.ServicePort.Port
}
// GetProxyServiceInstances lists service instances co-located with a given proxy
func (c *Controller) GetProxyServiceInstances(node *model.Proxy) ([]*model.ServiceInstance, error) {
data, err := c.getServices()
if err != nil {
return nil, err
}
out := make([]*model.ServiceInstance, 0)
for svcName := range data {
endpoints, err := c.getCatalogService(svcName, nil)
if err != nil {
return nil, err
}
for _, endpoint := range endpoints {
addr := endpoint.ServiceAddress
if addr == "" {
addr = endpoint.Address
}
if len(node.IPAddresses) > 0 {
for _, ipAddress := range node.IPAddresses {
if ipAddress == addr {
out = append(out, convertInstance(endpoint))
break
}
}
}
}
}
return out, nil
}
func (c *Controller) GetProxyWorkloadLabels(proxy *model.Proxy) (model.LabelsCollection, error) {
data, err := c.getServices()
if err != nil {
return nil, err
}
out := make(model.LabelsCollection, 0)
for svcName := range data {
endpoints, err := c.getCatalogService(svcName, nil)
if err != nil {
return nil, err
}
for _, endpoint := range endpoints {
addr := endpoint.ServiceAddress
if addr == "" {
addr = endpoint.Address
}
if len(proxy.IPAddresses) > 0 {
for _, ipAddress := range proxy.IPAddresses {
if ipAddress == addr {
labels := convertLabels(endpoint.ServiceTags)
out = append(out, labels)
break
}
}
}
}
}
return out, nil
}
// Run all controllers until a signal is received
func (c *Controller) Run(stop <-chan struct{}) {
c.monitor.Start(stop)
}
// AppendServiceHandler implements a service catalog operation
func (c *Controller) AppendServiceHandler(f func(*model.Service, model.Event)) error {
c.monitor.AppendServiceHandler(func(instances []*api.CatalogService, event model.Event) error {
f(convertService(instances), event)
return nil
})
return nil
}
// AppendInstanceHandler implements a service catalog operation
func (c *Controller) AppendInstanceHandler(f func(*model.ServiceInstance, model.Event)) error {
c.monitor.AppendInstanceHandler(func(instance *api.CatalogService, event model.Event) error {
f(convertInstance(instance), event)
return nil
})
return nil
}
// GetIstioServiceAccounts implements model.ServiceAccounts operation TODO
func (c *Controller) GetIstioServiceAccounts(hostname model.Hostname, ports []int) []string {
// Need to get service account of service registered with consul
// Currently Consul does not have service account or equivalent concept
// As a step-1, to enabling istio security in Consul, We assume all the services run in default service account
// This will allow all the consul services to do mTLS
// Follow - https://goo.gl/Dt11Ct
return []string{
spiffe.MustGenSpiffeURI("default", "default"),
}
}