-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
events.go
239 lines (216 loc) · 6.53 KB
/
events.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
// Copyright 2017 Google Inc.
package speaker
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"go.universe.tf/metallb/pkg/k8s/types"
metallbspr "go.universe.tf/metallb/pkg/speaker"
"github.com/cilium/cilium/pkg/bgp/fence"
"github.com/cilium/cilium/pkg/k8s"
)
// Op enumerates the operation an event
// demonstrates.
type Op int
const (
Undefined Op = iota
Add
Update
Delete
)
func (o Op) String() string {
switch o {
case Undefined:
return "Undefined"
case Add:
return "Add"
case Update:
return "Update"
case Delete:
return "Delete"
default:
return fmt.Sprintf("Unknown(%d)", o)
}
}
// svcEvent holds the extracted fields from a K8s service event
// which are of interest to the BGP package.
type svcEvent struct {
fence.Meta
op Op
id k8s.ServiceID
svc *metallbspr.Service
eps *metallbspr.Endpoints
}
// epEvent is similar to svcEvent but signifies the service was
// discovered via a K8s Endpoint event.
type epEvent svcEvent
// nodeEvent holds the extracted fields from a K8s node event which are
// of interested to the BGP package.
//
// nodeEvents trigger a call to speaker.SetNodeLabels which ultimately
// syncs Cilium's BGP peer connections.
//
// this package assumes when Cilium Agent starts an initial node event
// is emitted and thus the BGP connections are setup.
type nodeEvent struct {
fence.Meta
op Op
// The following fields must be a pointers because they are not hashable
// (read comparable) in Go.
// labels is a pointer to a copy of the incoming node event's
// labels.
labels *map[string]string
// podCIDRs is the extracted pod cidr ranges associated
// with this node event.
podCIDRs *[]string
// withDraw will be set when a Delete node event occurs.
// the reduction of this event will elicit a withdrawal
// of all bgp routes.
withDraw bool
}
// run runs the reconciliation loop, fetching events off of the queue to
// process. The events supported are svcEvent, epEvent, and nodeEvent. This
// loop is only stopped (implicitly) when the Agent is shutting down.
//
// Adapted from go.universe.tf/metallb/pkg/k8s/k8s.go.
func (s *MetalLBSpeaker) run(ctx context.Context) {
l := log.WithFields(
logrus.Fields{
"component": "MetalLBSpeaker.run",
},
)
for {
// only check ctx here, we'll allow any in-flight
// events to be processed completely.
if ctx.Err() != nil {
return
}
// previous to this iteration, we processed an event
// which indicates the speaker should yield. shut
// it down.
if s.shutdown > 0 { // atomic load not necessary, we are the only writer.
l.Info("speaker shutting down.")
return
}
key, quit := s.queue.Get()
if quit {
return
}
l.Info("processing new event.")
st := s.do(key)
switch st {
case types.SyncStateError:
s.queue.Add(key)
// done must be called to requeue event after add.
case types.SyncStateSuccess, types.SyncStateReprocessAll:
// SyncStateReprocessAll is returned in MetalLB when the
// configuration changes. However, we are not watching for
// configuration changes because our configuration is static and
// loaded once at Cilium start time.
}
// if queue.Add(key) is called previous to this invocation the event
// is requeued, else it is discarded from the queue.
s.queue.Done(key)
}
}
// do performs the appropriate action depending on the event type. For example,
// if it is a service event (svcEvent), then it will call into MetalLB's
// SetService() to perform BGP announcements.
func (s *MetalLBSpeaker) do(key interface{}) types.SyncState {
l := log.WithFields(
logrus.Fields{
"component": "MetalLBSpeaker.do",
},
)
switch k := key.(type) {
case svcEvent:
if s.Fence(k.Meta) {
l.WithFields(logrus.Fields{
"uuid": k.Meta.UUID,
"type": "service",
"revision": k.Meta.Rev,
}).Debug("Encountered stale event, will not process")
return types.SyncStateSuccess
}
l.WithField("service-id", k.id.String()).Debug("announcing load balancer from service")
st := s.speaker.SetService(k.id.String(), k.svc, k.eps)
if st == types.SyncStateSuccess && k.op == Delete {
// this is a delete operation and we have succcessfully
// processed it, delete it from our fence.
s.Clear(k.UUID)
}
return st
case epEvent:
if s.Fence(k.Meta) {
l.WithFields(logrus.Fields{
"uuid": k.Meta.UUID,
"type": "endpoint",
"revision": k.Meta.Rev,
}).Debug("Encountered stale event, will not process")
return types.SyncStateSuccess
}
l.WithField("endpoint-id", k.id.String()).Debug("announcing load balancer from endpoint")
st := s.speaker.SetService(k.id.String(), k.svc, k.eps)
if st == types.SyncStateSuccess && k.op == Delete {
// this is a delete operation and we have succcessfully
// processed it, delete it from our fence.
s.Clear(k.UUID)
}
return st
case nodeEvent:
if s.Fence(k.Meta) {
l.WithFields(logrus.Fields{
"uuid": k.Meta.UUID,
"type": "node",
"revision": k.Meta.Rev,
}).Debug("Encountered stale event, will not process")
return types.SyncStateSuccess
}
st := s.handleNodeEvent(k)
return st
default:
l.Debugf("Encountered an unknown key type %T in BGP speaker", k)
return types.SyncStateSuccess
}
}
func (s *MetalLBSpeaker) handleNodeEvent(k nodeEvent) types.SyncState {
var (
l = log.WithFields(logrus.Fields{
"component": "MetalLBSpeaker.handleNodeEvent",
"labels": k.labels,
"cidrs": k.podCIDRs,
})
)
if k.withDraw {
// this is a best effort method call, so we don't
// care about errors. If the node is shutting down
// all routes will be closed once the speaker's TCP conn
// is closed anyway per the rfc.
// see: https://datatracker.ietf.org/doc/html/rfc4271#section-6
s.withdraw()
l.Info("node is leaving the cluster, speaker will shutdown.")
return types.SyncStateSuccess
}
l.Debug("syncing bgp sessions")
if r := s.speaker.SetNodeLabels(*k.labels); r != types.SyncStateSuccess {
switch r {
case types.SyncStateReprocessAll:
l.Errorf("MetalLB resync required, requeing event: MetalLB Sync State: %v.", r)
case types.SyncStateError:
l.Errorf("Speaker resync required, requeing event: MetalLB Sync State: %v.", r)
default:
l.Errorf("Unknown sync state returned from Speaker: %v", r)
}
return r
}
if s.announcePodCIDR {
l.Debug("announcing pod CIDR(s)")
if err := s.announcePodCIDRs(*k.podCIDRs); err != nil {
l.WithError(err).WithField("CIDRs", k.podCIDRs).Error("Failed to announce pod CIDRs")
return types.SyncStateError
}
}
return types.SyncStateSuccess
}