-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathdb.go
386 lines (331 loc) · 9.7 KB
/
db.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// 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 api
import (
"fmt"
"sort"
"strings"
"github.com/golang/protobuf/proto"
"github.com/contiv/vpp/dbresources"
)
// KeyValuePairs is a set of key-value pairs.
type KeyValuePairs map[string]proto.Message
// KubeStateData contains Kubernetes state data organized as key-value pairs sorted
// by the resource type.
type KubeStateData map[string]KeyValuePairs // resource name -> {(key, value)}
/******************************** DB Resync ***********************************/
// DBResync is a Resync Event that carries snapshot of the database for all watched
// Kubernetes resources and the external configuration (for vpp-agent).
type DBResync struct {
Local bool // against local DB?
KubeState KubeStateData
ExternalConfig KeyValuePairs
}
// NewDBResync is a constructor for DBResync
func NewDBResync() *DBResync {
return &DBResync{
KubeState: NewKubeStateData(),
ExternalConfig: make(KeyValuePairs),
}
}
// NewKubeStateData is a constructor for KubeStateData.
func NewKubeStateData() KubeStateData {
kubeStateData := make(KubeStateData)
for _, resource := range dbresources.GetDBResources() {
kubeStateData[resource.Keyword] = make(KeyValuePairs)
}
return kubeStateData
}
// withName is implemented by Kubernetes resources that have a name.
type withName interface {
// GetName is implemented by resources with Name.
GetName() string
}
// withNamespace is implemented by Kubernetes resources that are in a namespace.
type withNamespace interface {
// GetNamespace is implemented by resources with Namespace.
GetNamespace() string
}
// GetName returns name of the DBResync event.
func (ev *DBResync) GetName() string {
return "Database Resync"
}
// String describes DBResync event.
func (ev *DBResync) String() string {
str := ev.GetName()
if ev.Local {
str += " (Local DB)"
} else {
str += " (Remote DB)"
}
// order resources alphabetically
var resources []string
for resource := range ev.KubeState {
resources = append(resources, resource)
}
sort.Strings(resources)
// describe Kubernetes state
empty := true
for _, resource := range resources {
var (
withColon string
resourceItems []string
)
data := ev.KubeState[resource]
for key, value := range data {
var valueStr string
valWithName, hasName := value.(withName)
valWithNamespace, hasNamespace := value.(withNamespace)
if !hasName {
valueStr = key
}
if hasName && !hasNamespace {
valueStr = valWithName.GetName()
}
if hasName && hasNamespace {
valueStr = valWithNamespace.GetNamespace() + "/" + valWithName.GetName()
}
resourceItems = append(resourceItems, valueStr)
}
if len(resourceItems) == 0 {
continue
}
empty = false
sort.Strings(resourceItems)
str += fmt.Sprintf("\n* %dx %s%s",
len(data), resource, withColon)
for _, resourceItem := range resourceItems {
str += "\n - " + resourceItem
}
}
// describe external config if there is any
var externalKeys []string
for key := range ev.ExternalConfig {
externalKeys = append(externalKeys, key)
}
sort.Strings(externalKeys)
if len(externalKeys) > 0 {
empty = false
str += fmt.Sprintf("\n* %dx external config items: %s",
len(externalKeys), strings.Join(externalKeys, ", "))
}
// handle empty DB
if empty {
str += " - empty dataset"
}
return str
}
// Method is FullResync.
func (ev *DBResync) Method() EventMethodType {
return FullResync
}
// IsBlocking returns false.
func (ev *DBResync) IsBlocking() bool {
return false
}
// Done is NOOP.
func (ev *DBResync) Done(error) {
return
}
/***************************** Kube State Change ******************************/
// KubeStateChange is an Update event that represents change for one key from
// Kubernetes state data.
type KubeStateChange struct {
Key string
Resource string
PrevValue proto.Message // nil if newly added
NewValue proto.Message // nil if deleted
}
// GetName returns name of the KubeStateChange event.
func (ev *KubeStateChange) GetName() string {
return "Kubernetes State Change"
}
// String describes KubeStateChange event.
func (ev *KubeStateChange) String() string {
return fmt.Sprintf("%s\n"+
"* resource: %s\n"+
"* key: %s\n"+
"* prev-value: %s\n"+
"* new-value: %s", ev.GetName(), ev.Resource, ev.Key,
protoToString(ev.PrevValue), protoToString(ev.NewValue))
}
// Method is Update.
func (ev *KubeStateChange) Method() EventMethodType {
return Update
}
// TransactionType is BestEffort.
func (ev *KubeStateChange) TransactionType() UpdateTransactionType {
return BestEffort
}
// Direction is forward.
func (ev *KubeStateChange) Direction() UpdateDirectionType {
if ev.NewValue == nil {
// the item is being removed - undo changes in the reverse direction
return Reverse
}
return Forward
}
// IsBlocking returns false.
func (ev *KubeStateChange) IsBlocking() bool {
return false
}
// Done is NOOP.
func (ev *KubeStateChange) Done(error) {
return
}
// protoToString converts proto message to string
func protoToString(msg proto.Message) string {
if msg == nil {
return "<nil>"
}
return msg.String()
}
/*************************** External Config Change ***************************/
// ExternalConfigChange is an Update event that represents change for one or more
// keys from the external configuration (for vpp-agent).
type ExternalConfigChange struct {
result chan error
blocking bool
Source string
UpdatedKVs KeyValuePairs
}
// NewExternalConfigChange is a constructor for ExternalConfigChange.
func NewExternalConfigChange(source string, blocking bool) *ExternalConfigChange {
return &ExternalConfigChange{
result: make(chan error, 1),
blocking: blocking,
Source: source,
UpdatedKVs: make(KeyValuePairs),
}
}
// GetName returns name of the ExternalConfigChange event.
func (ev *ExternalConfigChange) GetName() string {
return "External Config Change"
}
// String describes ExternalConfigChange event.
func (ev *ExternalConfigChange) String() string {
const (
PutOpName = "PUT"
DelOpName = "DEL"
)
var hasPut, hasDelete bool
flags := []string{strings.ToUpper(ev.Source)}
for _, value := range ev.UpdatedKVs {
if value == nil {
hasDelete = true
} else {
hasPut = true
}
}
if hasPut != hasDelete {
if hasPut {
flags = append(flags, PutOpName)
} else {
flags = append(flags, DelOpName)
}
}
str := ev.GetName() + " " + flagsToString(flags, 0)
for key, value := range ev.UpdatedKVs {
var flags []string
if hasPut == hasDelete {
if value != nil {
flags = append(flags, PutOpName)
} else {
flags = append(flags, DelOpName)
}
}
str += fmt.Sprintf("\n* %skey: %s", flagsToString(flags, 1), key)
str += fmt.Sprintf("\n new-value: %s", value)
}
return str
}
func flagsToString(flags []string, trailingSpace int) string {
if len(flags) == 0 {
return ""
}
return "[" + strings.Join(flags, ", ") + "]" + strings.Repeat(" ", trailingSpace)
}
// Method is Update.
func (ev *ExternalConfigChange) Method() EventMethodType {
return Update
}
// TransactionType is BestEffort.
func (ev *ExternalConfigChange) TransactionType() UpdateTransactionType {
return BestEffort
}
// Direction is Forward.
func (ev *ExternalConfigChange) Direction() UpdateDirectionType {
return Forward
}
// IsBlocking returns what is configured in the constructor.
func (ev *ExternalConfigChange) IsBlocking() bool {
return ev.blocking
}
// Done propagates error to the event producer.
func (ev *ExternalConfigChange) Done(err error) {
ev.result <- err
return
}
// Wait waits for the result of the ExternalConfigChange event.
func (ev *ExternalConfigChange) Wait() error {
return <-ev.result
}
/*************************** External Config Resync ***************************/
// ExternalConfigResync is a Resync event triggered by external config source.
// Note: External config from Remote DB uses DBResync instead.
type ExternalConfigResync struct {
result chan error
blocking bool
Source string
ExternalConfig KeyValuePairs
}
// NewExternalConfigResync is a constructor for ExternalConfigResync.
func NewExternalConfigResync(source string, blocking bool) *ExternalConfigResync {
return &ExternalConfigResync{
result: make(chan error, 1),
blocking: blocking,
Source: source,
ExternalConfig: make(KeyValuePairs),
}
}
// GetName returns name of the ExternalConfigResync event.
func (ev *ExternalConfigResync) GetName() string {
return "External Config Resync"
}
// String describes ExternalConfigResync event.
func (ev *ExternalConfigResync) String() string {
flags := []string{strings.ToUpper(ev.Source)}
str := ev.GetName() + " " + flagsToString(flags, 0)
for key := range ev.ExternalConfig {
str += fmt.Sprintf("\n* key: %s", key)
}
return str
}
// Method is Update.
func (ev *ExternalConfigResync) Method() EventMethodType {
return UpstreamResync
}
// IsBlocking returns what is configured in the constructor.
func (ev *ExternalConfigResync) IsBlocking() bool {
return ev.blocking
}
// Done propagates error to the event producer.
func (ev *ExternalConfigResync) Done(err error) {
ev.result <- err
return
}
// Wait waits for the result of the ExternalConfigResync event.
func (ev *ExternalConfigResync) Wait() error {
return <-ev.result
}