-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
171 lines (150 loc) · 4.72 KB
/
monitor.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
// 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 (
"reflect"
"sort"
"time"
"github.com/golang/glog"
"github.com/hashicorp/consul/api"
"istio.io/istio/pilot/model"
)
type consulServices map[string][]string
type consulServiceInstances []*api.CatalogService
// Monitor handles service and instance changes
type Monitor interface {
Start(<-chan struct{})
AppendServiceHandler(ServiceHandler)
AppendInstanceHandler(InstanceHandler)
}
// InstanceHandler processes service instance change events
type InstanceHandler func(instance *api.CatalogService, event model.Event) error
// ServiceHandler processes service change events
type ServiceHandler func(instances []*api.CatalogService, event model.Event) error
type consulMonitor struct {
discovery *api.Client
instanceCachedRecord consulServiceInstances
serviceCachedRecord consulServices
instanceHandlers []InstanceHandler
serviceHandlers []ServiceHandler
period time.Duration
}
// NewConsulMonitor polls for changes in Consul Services and CatalogServices
func NewConsulMonitor(client *api.Client, period time.Duration) Monitor {
return &consulMonitor{
discovery: client,
period: period,
instanceCachedRecord: make(consulServiceInstances, 0),
serviceCachedRecord: make(consulServices),
instanceHandlers: make([]InstanceHandler, 0),
serviceHandlers: make([]ServiceHandler, 0),
}
}
func (m *consulMonitor) Start(stop <-chan struct{}) {
m.run(stop)
}
func (m *consulMonitor) run(stop <-chan struct{}) {
ticker := time.NewTicker(m.period)
for {
select {
case <-stop:
ticker.Stop()
return
case <-ticker.C:
m.updateServiceRecord()
m.updateInstanceRecord()
}
}
}
func (m *consulMonitor) updateServiceRecord() {
svcs, _, err := m.discovery.Catalog().Services(nil)
if err != nil {
glog.Warningf("Could not fetch services: %v", err)
return
}
newRecord := consulServices(svcs)
if !reflect.DeepEqual(newRecord, m.serviceCachedRecord) {
// This is only a work-around solution currently
// Since Handler functions generally act as a refresher
// regardless of the input, thus passing in meaningless
// input should make functionalities work
//TODO
obj := []*api.CatalogService{}
var event model.Event
for _, f := range m.serviceHandlers {
go func(handler ServiceHandler) {
if err := handler(obj, event); err != nil {
glog.Warningf("Error executing service handler function: %v", err)
}
}(f)
}
m.serviceCachedRecord = newRecord
}
}
func (m *consulMonitor) updateInstanceRecord() {
svcs, _, err := m.discovery.Catalog().Services(nil)
if err != nil {
glog.Warningf("Could not fetch instances: %v", err)
return
}
for _, tags := range svcs {
sort.Strings(tags)
}
instances := make([]*api.CatalogService, 0)
for name := range svcs {
endpoints, _, err := m.discovery.Catalog().Service(name, "", nil)
if err != nil {
glog.Warningf("Could not retrieve service catalogue from consul: %v", err)
continue
}
instances = append(instances, endpoints...)
}
newRecord := consulServiceInstances(instances)
sort.Sort(newRecord)
if !reflect.DeepEqual(newRecord, m.instanceCachedRecord) {
// This is only a work-around solution currently
// Since Handler functions generally act as a refresher
// regardless of the input, thus passing in meaningless
// input should make functionalities work
// TODO
obj := &api.CatalogService{}
var event model.Event
for _, f := range m.instanceHandlers {
go func(handler InstanceHandler) {
if err := handler(obj, event); err != nil {
glog.Warningf("Error executing instance handler function: %v", err)
}
}(f)
}
m.instanceCachedRecord = newRecord
}
}
func (m *consulMonitor) AppendServiceHandler(h ServiceHandler) {
m.serviceHandlers = append(m.serviceHandlers, h)
}
func (m *consulMonitor) AppendInstanceHandler(h InstanceHandler) {
m.instanceHandlers = append(m.instanceHandlers, h)
}
// Len of the array
func (a consulServiceInstances) Len() int {
return len(a)
}
// Swap i and j
func (a consulServiceInstances) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
// Less i and j
func (a consulServiceInstances) Less(i, j int) bool {
return a[i].ID < a[j].ID
}