-
Notifications
You must be signed in to change notification settings - Fork 161
/
compat.go
578 lines (520 loc) · 16 KB
/
compat.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
//
// 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 compat
import (
goerrors "errors"
log "github.com/Sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
)
type ModelAdaptor struct {
client api.Client
}
var _ api.Client = (*ModelAdaptor)(nil)
func NewAdaptor(c api.Client) *ModelAdaptor {
return &ModelAdaptor{client: c}
}
func (c *ModelAdaptor) EnsureInitialized() error {
return c.client.EnsureInitialized()
}
// Create an entry in the datastore. This errors if the entry already exists.
func (c *ModelAdaptor) Create(d *model.KVPair) (*model.KVPair, error) {
var err error
switch d.Key.(type) {
case model.ProfileKey:
t, l, r := ToTagsLabelsRules(d)
if t, err = c.client.Create(t); err != nil {
return nil, err
} else if _, err := c.client.Create(l); err != nil {
return nil, err
} else if _, err := c.client.Create(r); err != nil {
return nil, err
} else {
d.Revision = t.Revision
return d, nil
}
case model.NodeKey:
p, o := toNodeComponents(d)
if p, err = c.client.Create(p); err != nil {
return nil, err
}
if err = c.applyOrDeleteSubcomponents(o); err != nil {
return nil, err
}
d.Revision = p.Revision
return d, nil
case model.BlockKey:
if err = validateBlockValue(d); err != nil {
return nil, err
}
b, err := c.client.Create(d)
if err != nil {
return nil, err
}
d.Revision = b.Revision
return d, nil
default:
return c.client.Create(d)
}
}
// Update an existing entry in the datastore. This errors if the entry does
// not exist.
func (c *ModelAdaptor) Update(d *model.KVPair) (*model.KVPair, error) {
var err error
switch d.Key.(type) {
case model.ProfileKey:
t, l, r := ToTagsLabelsRules(d)
if t, err = c.client.Update(t); err != nil {
return nil, err
} else if _, err := c.client.Apply(l); err != nil {
return nil, err
} else if _, err := c.client.Apply(r); err != nil {
return nil, err
} else {
d.Revision = t.Revision
return d, nil
}
case model.NodeKey:
p, o := toNodeComponents(d)
if p, err = c.client.Update(p); err != nil {
return nil, err
}
if err = c.applyOrDeleteSubcomponents(o); err != nil {
return nil, err
}
d.Revision = p.Revision
return d, nil
case model.BlockKey:
if err = validateBlockValue(d); err != nil {
return nil, err
}
b, err := c.client.Update(d)
if err != nil {
return nil, err
}
d.Revision = b.Revision
return d, nil
default:
return c.client.Update(d)
}
}
// Set an existing entry in the datastore. This ignores whether an entry already
// exists.
func (c *ModelAdaptor) Apply(d *model.KVPair) (*model.KVPair, error) {
var err error
switch d.Key.(type) {
case model.ProfileKey:
t, l, r := ToTagsLabelsRules(d)
if t, err = c.client.Apply(t); err != nil {
return nil, err
} else if _, err := c.client.Apply(l); err != nil {
return nil, err
} else if _, err := c.client.Apply(r); err != nil {
return nil, err
} else {
d.Revision = t.Revision
return d, nil
}
case model.NodeKey:
p, o := toNodeComponents(d)
if p, err = c.client.Apply(p); err != nil {
return nil, err
}
if err = c.applyOrDeleteSubcomponents(o); err != nil {
return nil, err
}
d.Revision = p.Revision
return d, nil
case model.BlockKey:
if err = validateBlockValue(d); err != nil {
return nil, err
}
b, err := c.client.Apply(d)
if err != nil {
return nil, err
}
d.Revision = b.Revision
return d, nil
default:
return c.client.Apply(d)
}
}
// Delete an entry in the datastore. This errors if the entry does not exists.
func (c *ModelAdaptor) Delete(d *model.KVPair) error {
var err error
switch d.Key.(type) {
case model.NodeKey:
p, o := toNodeDeleteComponents(d)
if err = c.applyOrDeleteSubcomponents(o); err != nil {
return err
}
if err = c.client.Delete(p); err != nil {
return err
}
return nil
default:
return c.client.Delete(d)
}
}
// Get an entry from the datastore. This errors if the entry does not exist.
func (c *ModelAdaptor) Get(k model.Key) (*model.KVPair, error) {
switch kt := k.(type) {
case model.ProfileKey:
return c.getProfile(k)
case model.NodeKey:
return c.getNode(kt)
case model.BlockKey:
return c.getBlock(k)
default:
return c.client.Get(k)
}
}
// List entries in the datastore. This may return an empty list of there are
// no entries matching the request in the ListInterface.
func (c *ModelAdaptor) List(l model.ListInterface) ([]*model.KVPair, error) {
switch lt := l.(type) {
case model.NodeListOptions:
return c.listNodes(lt)
case model.BlockListOptions:
return c.listBlock(lt)
default:
return c.client.List(l)
}
}
func (c *ModelAdaptor) Syncer(callbacks api.SyncerCallbacks) api.Syncer {
return c.client.Syncer(callbacks)
}
// getProfile gets the composite profile by getting the individual components
// and joining the results together.
func (c *ModelAdaptor) getProfile(k model.Key) (*model.KVPair, error) {
var t, l, r *model.KVPair
var err error
pk := k.(model.ProfileKey)
if t, err = c.client.Get(model.ProfileTagsKey{pk}); err != nil {
return nil, err
}
d := model.KVPair{
Key: k,
Value: &model.Profile{
Tags: t.Value.([]string),
},
Revision: t.Revision,
}
p := d.Value.(*model.Profile)
if l, err = c.client.Get(model.ProfileLabelsKey{pk}); err == nil {
p.Labels = l.Value.(map[string]string)
}
if r, err = c.client.Get(model.ProfileRulesKey{pk}); err == nil {
p.Rules = *r.Value.(*model.ProfileRules)
}
return &d, nil
}
// getBlock gets KVPair for Block. It gets the block value first,
// then checks for `Affinity` field first, then `HostAffinity` as a backup.
// For more details see: https://github.com/projectcalico/libcalico-go/issues/226
func (c *ModelAdaptor) getBlock(k model.Key) (*model.KVPair, error) {
bk := k.(model.BlockKey)
v, err := c.client.Get(model.BlockKey{CIDR: bk.CIDR})
if err != nil {
return nil, err
}
// Make sure Affinity field has a proper value,
// and map the value to Affinity if the deprecated HostAffinity field is used
// by calling ensureBlockAffinity, and update the KVPair to return.
return ensureBlockAffinity(v), nil
}
// getNode gets the composite node by getting the individual components
// and joining the results together.
func (c *ModelAdaptor) getNode(nk model.NodeKey) (*model.KVPair, error) {
var err error
// Fill in the Metadata specific part of the node configuration. At the
// moment, there is nothing to fill in.
if _, err = c.client.Get(model.HostMetadataKey{nk.Hostname}); err != nil {
return nil, err
}
nv := model.Node{}
err = c.getNodeSubcomponents(nk, &nv)
if err != nil {
return nil, err
}
return &model.KVPair{Key: nk, Value: &nv}, nil
}
// validateBlockValue validates the AllocationBlock fields (specifically Affinity) to
// make sure the deprecated HostAffinity field is not used.
func validateBlockValue(kvp *model.KVPair) error {
if kvp.Value.(*model.AllocationBlock).HostAffinity != nil {
return goerrors.New("AllocationBlock.HostAffinity is deprecated, please use Affinity instead.")
}
return nil
}
// listNodes lists the composite node resources by listing the primary node
// object and then getting the remaining components through additional queries.
// Note that enumeration of the primary component is horribly inefficient
// because of the way we do our list queries - we'll enumerate all endpoints on
// host as well!
func (c *ModelAdaptor) listNodes(l model.NodeListOptions) ([]*model.KVPair, error) {
hml := model.HostMetadataListOptions{Hostname: l.Hostname}
hmr, err := c.client.List(hml)
if err != nil {
return nil, err
}
results := make([]*model.KVPair, len(hmr))
for idx, hmkv := range hmr {
hmk := hmkv.Key.(model.HostMetadataKey)
// Fill in the metadata part of the node - at the moment there is
// nothing to fill in.
nk := model.NodeKey{Hostname: hmk.Hostname}
nv := model.Node{}
err = c.getNodeSubcomponents(nk, &nv)
if err != nil {
return nil, err
}
results[idx] = &model.KVPair{Key: nk, Value: &nv}
}
return results, nil
}
// listBlock returns list of KVPairs for Block, includes making sure
// backwards compatiblity. See getBlock for more details.
func (c *ModelAdaptor) listBlock(l model.BlockListOptions) ([]*model.KVPair, error) {
// Get a list of block KVPairs.
blockList, err := c.client.List(l)
if err != nil {
return nil, err
}
// Create an empty slice of KVPair.
results := make([]*model.KVPair, len(blockList))
// Go through the list to make sure Affinity field has a proper value,
// and maps the value to Affinity if the deprecated HostAffinity field is used
// by calling ensureBlockAffinity, and populate the KVPair slice to return.
for i, bkv := range blockList {
results[i] = ensureBlockAffinity(bkv)
}
return results, nil
}
// ensureBlockAffinity ensures Affinity field has a proper value,
// and maps the value to Affinity if the deprecated HostAffinity field is used.
func ensureBlockAffinity(kvp *model.KVPair) *model.KVPair {
val := kvp.Value.(*model.AllocationBlock)
// Check for `Affinity` field first (this is to make sure we're
// compatible with Python version etcd data-model).
if val.Affinity == nil && val.HostAffinity != nil {
// Convert HostAffinity=hostname into Affinity=host:hostname format.
hostAffinityStr := "host:" + *val.HostAffinity
val.Affinity = &hostAffinityStr
// Set AllocationBlock.HostAffinity to nil so it's never non-nil for the clients.
val.HostAffinity = nil
}
return &model.KVPair{Key: kvp.Key, Value: val, Revision: kvp.Revision, TTL: kvp.TTL}
}
// Get the node sub components and fill in the details in the supplied node
// struct.
func (c *ModelAdaptor) getNodeSubcomponents(nk model.NodeKey, nv *model.Node) error {
var component *model.KVPair
var err error
var strval string
// Fill in the Metadata specific part of the node configuration.
if component, err = c.client.Get(model.HostBGPConfigKey{Hostname: nk.Hostname, Name: "ip_addr_v4"}); err == nil {
strval = component.Value.(string)
if strval != "" {
nv.BGPIPv4 = &net.IP{}
err = nv.BGPIPv4.UnmarshalText([]byte(strval))
if err != nil {
log.WithError(err).Warning("Error unmarshalling IPv4")
nv.BGPIPv4 = nil
}
}
} else if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
return err
}
if component, err = c.client.Get(model.HostBGPConfigKey{Hostname: nk.Hostname, Name: "ip_addr_v6"}); err == nil {
strval = component.Value.(string)
if strval != "" {
nv.BGPIPv6 = &net.IP{}
err = nv.BGPIPv6.UnmarshalText([]byte(strval))
if err != nil {
log.WithError(err).Warning("Error unmarshalling IPv6")
nv.BGPIPv6 = nil
}
}
} else if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
return err
}
if component, err = c.client.Get(model.HostBGPConfigKey{Hostname: nk.Hostname, Name: "as_num"}); err == nil {
strval = component.Value.(string)
if strval != "" {
asn, err := numorstring.ASNumberFromString(strval)
if err != nil {
log.WithError(err).Warning("Error unmarshalling AS Number")
} else {
nv.BGPASNumber = &asn
}
}
} else if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
return err
}
return nil
}
// applyOrDeleteSubcomponents applies the configuration if the value is non-nil
// or deletes the entry if the value is nil.
func (c *ModelAdaptor) applyOrDeleteSubcomponents(components []*model.KVPair) error {
for _, component := range components {
// If there is a value, apply it to either create or update. Otherwise
// delete the entry, ignoring error indicating the entry does not exist.
if component.Value != nil {
if _, err := c.client.Apply(component); err != nil {
return err
}
} else if err := c.client.Delete(component); err != nil {
if _, ok := err.(errors.ErrorResourceDoesNotExist); !ok {
return err
}
}
}
return nil
}
// ToTagsLabelsRules converts a Profile KVPair to separate KVPair types for Keys,
// Labels and Rules. These separate KVPairs are used to write three separate objects
// that make up a single profile.
func ToTagsLabelsRules(d *model.KVPair) (t, l, r *model.KVPair) {
p := d.Value.(*model.Profile)
pk := d.Key.(model.ProfileKey)
t = &model.KVPair{
Key: model.ProfileTagsKey{pk},
Value: p.Tags,
}
l = &model.KVPair{
Key: model.ProfileLabelsKey{pk},
Value: p.Labels,
}
r = &model.KVPair{
Key: model.ProfileRulesKey{pk},
Value: &p.Rules,
}
return t, l, r
}
// toNodeComponents converts a Node KVPair to separate KVPair types that make up the
// Node. This includes:
// - The host metadata (the primary component)
// - The host IPv4 address (used by Felix to filter IPIP traffic)
// - The BGP AS Number
// - The BGP IPv4 address
// - The BGP IPv6 address
//
// A nil value is used to indicate that the entry should be deleted rather than
// configured.
func toNodeComponents(d *model.KVPair) (primary *model.KVPair, optional []*model.KVPair) {
n := d.Value.(*model.Node)
nk := d.Key.(model.NodeKey)
primary = &model.KVPair{
Key: model.HostMetadataKey{nk.Hostname},
Value: &model.HostMetadata{},
Revision: d.Revision,
}
// The calico/node image always expects the BGP IP keys to be present
// when running BGP even if they are not specified (the value in that
// case should be a blank string). Felix on the other hand deals
// with values not existing.
ipv4Str := ""
if n.BGPIPv4 != nil {
ipv4Str = n.BGPIPv4.String()
}
ipv6Str := ""
if n.BGPIPv6 != nil {
ipv6Str = n.BGPIPv6.String()
}
// Add the BGP IPv4 and IPv6 values - these are always present.
optional = []*model.KVPair{
&model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "ip_addr_v4",
},
Value: ipv4Str,
},
&model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "ip_addr_v6",
},
Value: ipv6Str,
},
}
// Now add the entries that may or may not exist: the Felix ipv4
// address and the host ASN. If either config is not specified, set
// the value to be nil to indicate to our default processing to delete
// the entry rather than set it.
if n.BGPIPv4 != nil {
optional = append(optional, &model.KVPair{
Key: model.HostIPKey{nk.Hostname},
Value: n.BGPIPv4,
})
} else {
optional = append(optional, &model.KVPair{
Key: model.HostIPKey{nk.Hostname},
})
}
if n.BGPASNumber != nil {
optional = append(optional, &model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "as_num",
},
Value: n.BGPASNumber.String(),
})
} else {
optional = append(optional, &model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "as_num",
},
})
}
return primary, optional
}
// toNodeDeleteComponents is similar to function toNodeComponents, but returns nil
// interface values which the applyOrDeleteSubcomponents method will treat as a delete.
func toNodeDeleteComponents(d *model.KVPair) (primary *model.KVPair, optional []*model.KVPair) {
nk := d.Key.(model.NodeKey)
primary = &model.KVPair{
Key: model.HostMetadataKey{nk.Hostname},
Revision: d.Revision,
}
optional = []*model.KVPair{
&model.KVPair{
Key: model.HostIPKey{nk.Hostname},
},
&model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "ip_addr_v4",
},
},
&model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "ip_addr_v6",
},
},
&model.KVPair{
Key: model.HostBGPConfigKey{
Hostname: nk.Hostname,
Name: "as_num",
},
},
}
return primary, optional
}