forked from projectcalico/calico
/
k8s.go
782 lines (711 loc) · 25.2 KB
/
k8s.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// Copyright (c) 2016-2021 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 k8s
import (
"context"
"fmt"
"path/filepath"
"reflect"
"strings"
"sync"
log "github.com/sirupsen/logrus"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Import all auth providers.
apiv3 "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
"github.com/dtest11/calico/libcalico-go/lib/apiconfig"
libapiv3 "github.com/dtest11/calico/libcalico-go/lib/apis/v3"
"github.com/dtest11/calico/libcalico-go/lib/backend/api"
"github.com/dtest11/calico/libcalico-go/lib/backend/k8s/conversion"
"github.com/dtest11/calico/libcalico-go/lib/backend/k8s/resources"
"github.com/dtest11/calico/libcalico-go/lib/backend/model"
cerrors "github.com/dtest11/calico/libcalico-go/lib/errors"
"github.com/dtest11/calico/libcalico-go/lib/net"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var (
resourceKeyType = reflect.TypeOf(model.ResourceKey{})
resourceListType = reflect.TypeOf(model.ResourceListOptions{})
)
type KubeClient struct {
// Main Kubernetes clients.
ClientSet *kubernetes.Clientset
// Client for interacting with CustomResourceDefinition.
crdClientV1 *rest.RESTClient
disableNodePoll bool
// Contains methods for converting Kubernetes resources to
// Calico resources.
converter conversion.Converter
// Resource clients keyed off Kind.
clientsByResourceKind map[string]resources.K8sResourceClient
// Non v3 resource clients keyed off Key Type.
clientsByKeyType map[reflect.Type]resources.K8sResourceClient
// Non v3 resource clients keyed off List Type.
clientsByListType map[reflect.Type]resources.K8sResourceClient
}
func NewKubeClient(ca *apiconfig.CalicoAPIConfigSpec) (api.Client, error) {
config, cs, err := CreateKubernetesClientset(ca)
if err != nil {
return nil, err
}
crdClientV1, err := buildCRDClientV1(*config)
if err != nil {
return nil, fmt.Errorf("Failed to build V1 CRD client: %v", err)
}
kubeClient := &KubeClient{
ClientSet: cs,
crdClientV1: crdClientV1,
disableNodePoll: ca.K8sDisableNodePoll,
clientsByResourceKind: make(map[string]resources.K8sResourceClient),
clientsByKeyType: make(map[reflect.Type]resources.K8sResourceClient),
clientsByListType: make(map[reflect.Type]resources.K8sResourceClient),
}
// Create the Calico sub-clients and register them.
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindIPPool,
resources.NewIPPoolClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindIPReservation,
resources.NewIPReservationClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindGlobalNetworkPolicy,
resources.NewGlobalNetworkPolicyClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindGlobalNetworkSet,
resources.NewGlobalNetworkSetClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindNetworkPolicy,
resources.NewNetworkPolicyClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
model.KindKubernetesNetworkPolicy,
resources.NewKubernetesNetworkPolicyClient(cs),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
model.KindKubernetesEndpointSlice,
resources.NewKubernetesEndpointSliceClient(cs),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindNetworkSet,
resources.NewNetworkSetClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindBGPPeer,
resources.NewBGPPeerClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindBGPConfiguration,
resources.NewBGPConfigClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindFelixConfiguration,
resources.NewFelixConfigClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindClusterInformation,
resources.NewClusterInfoClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
libapiv3.KindNode,
resources.NewNodeClient(cs, ca.K8sUsePodCIDR),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindProfile,
resources.NewProfileClient(cs),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindHostEndpoint,
resources.NewHostEndpointClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
libapiv3.KindWorkloadEndpoint,
resources.NewWorkloadEndpointClient(cs),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindKubeControllersConfiguration,
resources.NewKubeControllersConfigClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindCalicoNodeStatus,
resources.NewCalicoNodeStatusClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
model.KindKubernetesService,
resources.NewServiceClient(cs),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
libapiv3.KindIPAMConfig,
resources.NewIPAMConfigClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
libapiv3.KindBlockAffinity,
resources.NewBlockAffinityClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.ResourceKey{}),
reflect.TypeOf(model.ResourceListOptions{}),
apiv3.KindBGPFilter,
resources.NewBGPFilterClient(cs, crdClientV1),
)
if !ca.K8sUsePodCIDR {
// Using Calico IPAM - use CRDs to back IPAM resources.
log.Debug("Calico is configured to use calico-ipam")
kubeClient.registerResourceClient(
reflect.TypeOf(model.BlockAffinityKey{}),
reflect.TypeOf(model.BlockAffinityListOptions{}),
libapiv3.KindBlockAffinity,
resources.NewBlockAffinityClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.BlockKey{}),
reflect.TypeOf(model.BlockListOptions{}),
libapiv3.KindIPAMBlock,
resources.NewIPAMBlockClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.IPAMHandleKey{}),
reflect.TypeOf(model.IPAMHandleListOptions{}),
libapiv3.KindIPAMHandle,
resources.NewIPAMHandleClient(cs, crdClientV1),
)
kubeClient.registerResourceClient(
reflect.TypeOf(model.IPAMConfigKey{}),
nil,
libapiv3.KindIPAMConfig,
resources.NewIPAMConfigClient(cs, crdClientV1),
)
}
return kubeClient, nil
}
// deduplicate removes any duplicated values and returns a new slice, keeping the order unchanged
//
// based on deduplicate([]string) []string found in k8s.io/client-go/tools/clientcmd/loader.go#634
// Copyright 2014 The Kubernetes Authors.
func deduplicate(s []string) []string {
encountered := map[string]struct{}{}
ret := make([]string, 0)
for i := range s {
if _, ok := encountered[s[i]]; ok {
continue
}
encountered[s[i]] = struct{}{}
ret = append(ret, s[i])
}
return ret
}
// fill out loading rules based on filename(s) encountered in specified kubeconfig
func fillLoadingRulesFromKubeConfigSpec(loadingRules *clientcmd.ClientConfigLoadingRules, kubeConfig string) {
fileList := filepath.SplitList(kubeConfig)
if len(fileList) > 1 {
loadingRules.Precedence = deduplicate(fileList)
loadingRules.WarnIfAllMissing = true
return
}
loadingRules.ExplicitPath = kubeConfig
}
func CreateKubernetesClientset(ca *apiconfig.CalicoAPIConfigSpec) (*rest.Config, *kubernetes.Clientset, error) {
// Use the kubernetes client code to load the kubeconfig file and combine it with the overrides.
configOverrides := &clientcmd.ConfigOverrides{}
var overridesMap = []struct {
variable *string
value string
}{
{&configOverrides.CurrentContext, ca.K8sCurrentContext},
{&configOverrides.ClusterInfo.Server, ca.K8sAPIEndpoint},
{&configOverrides.AuthInfo.ClientCertificate, ca.K8sCertFile},
{&configOverrides.AuthInfo.ClientKey, ca.K8sKeyFile},
{&configOverrides.ClusterInfo.CertificateAuthority, ca.K8sCAFile},
{&configOverrides.AuthInfo.Token, ca.K8sAPIToken},
}
// Set an explicit path to the kubeconfig if one
// was provided.
loadingRules := clientcmd.ClientConfigLoadingRules{}
if ca.Kubeconfig != "" {
fillLoadingRulesFromKubeConfigSpec(&loadingRules, ca.Kubeconfig)
}
// Using the override map above, populate any non-empty values.
for _, override := range overridesMap {
if override.value != "" {
*override.variable = override.value
}
}
if ca.K8sInsecureSkipTLSVerify {
configOverrides.ClusterInfo.InsecureSkipTLSVerify = true
}
// A kubeconfig file was provided. Use it to load a config, passing through
// any overrides.
var config *rest.Config
var err error
if ca.KubeconfigInline != "" {
var clientConfig clientcmd.ClientConfig
clientConfig, err = clientcmd.NewClientConfigFromBytes([]byte(ca.KubeconfigInline))
if err != nil {
return nil, nil, resources.K8sErrorToCalico(err, nil)
}
config, err = clientConfig.ClientConfig()
} else {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&loadingRules, configOverrides).ClientConfig()
}
if err != nil {
return nil, nil, resources.K8sErrorToCalico(err, nil)
}
config.AcceptContentTypes = strings.Join([]string{runtime.ContentTypeProtobuf, runtime.ContentTypeJSON}, ",")
config.ContentType = runtime.ContentTypeProtobuf
// Overwrite the QPS if provided. Default QPS is 5.
if ca.K8sClientQPS != float32(0) {
config.QPS = ca.K8sClientQPS
}
// Create the clientset. We increase the burst so that the IPAM code performs
// efficiently. The IPAM code can create bursts of requests to the API, so
// in order to keep pod creation times sensible we allow a higher request rate.
config.Burst = 100
cs, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, resources.K8sErrorToCalico(err, nil)
}
return config, cs, nil
}
// registerResourceClient registers a specific resource client with the associated
// key and list types (and for v3 resources with the resource kind - since these share
// a common key and list type).
func (c *KubeClient) registerResourceClient(keyType, listType reflect.Type, resourceKind string, client resources.K8sResourceClient) {
if keyType == resourceKeyType {
c.clientsByResourceKind[resourceKind] = client
} else {
c.clientsByKeyType[keyType] = client
c.clientsByListType[listType] = client
}
}
// getResourceClientFromKey returns the appropriate resource client for the v3 resource kind.
func (c *KubeClient) GetResourceClientFromResourceKind(kind string) resources.K8sResourceClient {
return c.clientsByResourceKind[kind]
}
// getResourceClientFromKey returns the appropriate resource client for the key.
func (c *KubeClient) getResourceClientFromKey(key model.Key) resources.K8sResourceClient {
kt := reflect.TypeOf(key)
if kt == resourceKeyType {
return c.clientsByResourceKind[key.(model.ResourceKey).Kind]
} else {
return c.clientsByKeyType[kt]
}
}
// getResourceClientFromList returns the appropriate resource client for the list.
func (c *KubeClient) getResourceClientFromList(list model.ListInterface) resources.K8sResourceClient {
lt := reflect.TypeOf(list)
if lt == resourceListType {
return c.clientsByResourceKind[list.(model.ResourceListOptions).Kind]
} else {
return c.clientsByListType[lt]
}
}
// EnsureInitialized checks that the necessary custom resource definitions
// exist in the backend. This usually passes when using etcd
// as a backend but can often fail when using KDD as it relies
// on various custom resources existing.
// To ensure the datastore is initialized, this function checks that a
// known custom resource is defined: GlobalFelixConfig. It accomplishes this
// by trying to set the ClusterType (an instance of GlobalFelixConfig).
func (c *KubeClient) EnsureInitialized() error {
return nil
}
// Remove Calico-creatable data from the datastore. This is purely used for the
// test framework.
func (c *KubeClient) Clean() error {
log.Warning("Cleaning KDD of all Calico-creatable data")
kinds := []string{
apiv3.KindBGPConfiguration,
apiv3.KindBGPPeer,
apiv3.KindClusterInformation,
apiv3.KindCalicoNodeStatus,
apiv3.KindFelixConfiguration,
apiv3.KindGlobalNetworkPolicy,
apiv3.KindNetworkPolicy,
apiv3.KindGlobalNetworkSet,
apiv3.KindNetworkSet,
apiv3.KindIPPool,
apiv3.KindIPReservation,
apiv3.KindHostEndpoint,
apiv3.KindKubeControllersConfiguration,
libapiv3.KindIPAMConfig,
libapiv3.KindBlockAffinity,
apiv3.KindBGPFilter,
}
ctx := context.Background()
for _, k := range kinds {
lo := model.ResourceListOptions{Kind: k}
if rs, err := c.List(ctx, lo, ""); err != nil {
log.WithError(err).WithField("Kind", k).Warning("Failed to list resources")
} else {
for _, r := range rs.KVPairs {
if _, err = c.Delete(ctx, r.Key, r.Revision); err != nil {
log.WithField("Key", r.Key).Warning("Failed to delete entry from KDD")
}
}
}
}
// Cleanup IPAM resources that have slightly different backend semantics.
for _, li := range []model.ListInterface{
model.BlockListOptions{},
model.BlockAffinityListOptions{},
model.BlockAffinityListOptions{},
model.IPAMHandleListOptions{},
} {
if rs, err := c.List(ctx, li, ""); err != nil {
log.WithError(err).WithField("Kind", li).Warning("Failed to list resources")
} else {
for _, r := range rs.KVPairs {
if _, err = c.DeleteKVP(ctx, r); err != nil {
log.WithError(err).WithField("Key", r.Key).Warning("Failed to delete entry from KDD")
}
}
}
}
// Get a list of Nodes and remove all BGP configuration from the nodes.
if nodes, err := c.List(ctx, model.ResourceListOptions{Kind: libapiv3.KindNode}, ""); err != nil {
log.Warning("Failed to list Nodes")
} else {
for _, nodeKvp := range nodes.KVPairs {
node := nodeKvp.Value.(*libapiv3.Node)
node.Spec.BGP = nil
if _, err := c.Update(ctx, nodeKvp); err != nil {
log.WithField("Node", node.Name).Warning("Failed to remove Calico config from node")
}
}
}
// Delete global IPAM config
if _, err := c.Delete(ctx, model.IPAMConfigKey{}, ""); err != nil {
log.WithError(err).WithField("key", model.IPAMConfigGlobalName).Warning("Failed to delete global IPAM Config from KDD")
}
return nil
}
// Close the underlying client
func (c *KubeClient) Close() error {
log.Debugf("Closing client - NOOP")
return nil
}
var addToSchemeOnce sync.Once
// buildCRDClientV1 builds a RESTClient configured to interact with Calico CustomResourceDefinitions
func buildCRDClientV1(cfg rest.Config) (*rest.RESTClient, error) {
// Generate config using the base config.
cfg.GroupVersion = &schema.GroupVersion{
Group: "crd.projectcalico.org",
Version: "v1",
}
cfg.APIPath = "/apis"
cfg.ContentType = runtime.ContentTypeJSON
cfg.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
cli, err := rest.RESTClientFor(&cfg)
if err != nil {
return nil, err
}
// We're operating on the pkg level scheme.Scheme, so make sure that multiple
// calls to this function don't do this simultaneously, which can cause crashes
// due to concurrent access to underlying maps. For good measure, use a once
// since this really only needs to happen one time.
addToSchemeOnce.Do(func() {
// We also need to register resources.
schemeBuilder := runtime.NewSchemeBuilder(
func(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(
*cfg.GroupVersion,
&apiv3.FelixConfiguration{},
&apiv3.FelixConfigurationList{},
&apiv3.IPPool{},
&apiv3.IPPoolList{},
&apiv3.IPReservation{},
&apiv3.IPReservationList{},
&apiv3.BGPPeer{},
&apiv3.BGPPeerList{},
&apiv3.BGPConfiguration{},
&apiv3.BGPConfigurationList{},
&apiv3.ClusterInformation{},
&apiv3.ClusterInformationList{},
&apiv3.GlobalNetworkSet{},
&apiv3.GlobalNetworkSetList{},
&apiv3.GlobalNetworkPolicy{},
&apiv3.GlobalNetworkPolicyList{},
&apiv3.NetworkPolicy{},
&apiv3.NetworkPolicyList{},
&apiv3.NetworkSet{},
&apiv3.NetworkSetList{},
&apiv3.HostEndpoint{},
&apiv3.HostEndpointList{},
&libapiv3.BlockAffinity{},
&libapiv3.BlockAffinityList{},
&libapiv3.IPAMBlock{},
&libapiv3.IPAMBlockList{},
&libapiv3.IPAMHandle{},
&libapiv3.IPAMHandleList{},
&libapiv3.IPAMConfig{},
&libapiv3.IPAMConfigList{},
&apiv3.KubeControllersConfiguration{},
&apiv3.KubeControllersConfigurationList{},
&apiv3.CalicoNodeStatus{},
&apiv3.CalicoNodeStatusList{},
&apiv3.BGPFilter{},
&apiv3.BGPFilterList{},
)
return nil
})
err := schemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.WithError(err).Fatal("failed to add calico resources to scheme")
}
metav1.AddToGroupVersion(scheme.Scheme, schema.GroupVersion{Group: "crd.projectcalico.org", Version: "v1"})
})
return cli, nil
}
// Create an entry in the datastore. This errors if the entry already exists.
func (c *KubeClient) Create(ctx context.Context, d *model.KVPair) (*model.KVPair, error) {
log.Debugf("Performing 'Create' for %+v", d)
client := c.getResourceClientFromKey(d.Key)
if client == nil {
log.Debug("Attempt to 'Create' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: d.Key,
Operation: "Create",
}
}
return client.Create(ctx, d)
}
// Update an existing entry in the datastore. This errors if the entry does
// not exist.
func (c *KubeClient) Update(ctx context.Context, d *model.KVPair) (*model.KVPair, error) {
log.Debugf("Performing 'Update' for %+v", d)
client := c.getResourceClientFromKey(d.Key)
if client == nil {
log.Debug("Attempt to 'Update' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: d.Key,
Operation: "Update",
}
}
return client.Update(ctx, d)
}
// Set an existing entry in the datastore. This ignores whether an entry already
// exists. This is not exposed in the main client - but we keep here for the backend
// API.
func (c *KubeClient) Apply(ctx context.Context, kvp *model.KVPair) (*model.KVPair, error) {
logContext := log.WithFields(log.Fields{
"Key": kvp.Key,
"Value": kvp.Value,
})
logContext.Debug("Apply Kubernetes resource")
// Attempt to Create and do an Update if the resource already exists.
// We only log debug here since the Create and Update will also log.
// Can't set Revision while creating a resource.
updated, err := c.Create(ctx, &model.KVPair{
Key: kvp.Key,
Value: kvp.Value,
})
if err != nil {
if _, ok := err.(cerrors.ErrorResourceAlreadyExists); !ok {
logContext.Debug("Error applying resource (using Create)")
return nil, err
}
// Try to Update if the resource already exists.
updated, err = c.Update(ctx, kvp)
if err != nil {
logContext.Debug("Error applying resource (using Update)")
return nil, err
}
}
return updated, nil
}
// Delete an entry in the datastore.
func (c *KubeClient) DeleteKVP(ctx context.Context, kvp *model.KVPair) (*model.KVPair, error) {
log.Debugf("Performing 'DeleteKVP' for %+v", kvp.Key)
client := c.getResourceClientFromKey(kvp.Key)
if client == nil {
log.Debug("Attempt to 'DeleteKVP' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: kvp.Key,
Operation: "Delete",
}
}
return client.DeleteKVP(ctx, kvp)
}
// Delete an entry in the datastore by key.
func (c *KubeClient) Delete(ctx context.Context, k model.Key, revision string) (*model.KVPair, error) {
log.Debugf("Performing 'Delete' for %+v", k)
client := c.getResourceClientFromKey(k)
if client == nil {
log.Debug("Attempt to 'Delete' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: k,
Operation: "Delete",
}
}
return client.Delete(ctx, k, revision, nil)
}
// Get an entry from the datastore. This errors if the entry does not exist.
func (c *KubeClient) Get(ctx context.Context, k model.Key, revision string) (*model.KVPair, error) {
log.Debugf("Performing 'Get' for %+v %v", k, revision)
client := c.getResourceClientFromKey(k)
if client == nil {
log.Debug("Attempt to 'Get' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: k,
Operation: "Get",
}
}
return client.Get(ctx, k, revision)
}
// List entries in the datastore. This may return an empty list if there are
// no entries matching the request in the ListInterface.
func (c *KubeClient) List(ctx context.Context, l model.ListInterface, revision string) (*model.KVPairList, error) {
log.Debugf("Performing 'List' for %+v %v", l, reflect.TypeOf(l))
client := c.getResourceClientFromList(l)
if client == nil {
log.Info("Attempt to 'List' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: l,
Operation: "List",
}
}
return client.List(ctx, l, revision)
}
// List entries in the datastore. This may return an empty list if there are
// no entries matching the request in the ListInterface.
func (c *KubeClient) Watch(ctx context.Context, l model.ListInterface, revision string) (api.WatchInterface, error) {
log.Debugf("Performing 'Watch' for %+v %v", l, reflect.TypeOf(l))
client := c.getResourceClientFromList(l)
if client == nil {
log.Debug("Attempt to 'Watch' using kubernetes backend is not supported.")
return nil, cerrors.ErrorOperationNotSupported{
Identifier: l,
Operation: "Watch",
}
}
return client.Watch(ctx, l, revision)
}
func (c *KubeClient) getReadyStatus(ctx context.Context, k model.ReadyFlagKey, revision string) (*model.KVPair, error) {
return &model.KVPair{Key: k, Value: true}, nil
}
func (c *KubeClient) listHostConfig(ctx context.Context, l model.HostConfigListOptions, revision string) (*model.KVPairList, error) {
var kvps = []*model.KVPair{}
// Short circuit if they aren't asking for information we can provide.
if l.Name != "" && l.Name != "IpInIpTunnelAddr" {
return &model.KVPairList{
KVPairs: kvps,
Revision: revision,
}, nil
}
// First see if we were handed a specific host, if not list all Nodes
if l.Hostname == "" {
nodes, err := c.ClientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, resources.K8sErrorToCalico(err, l)
}
for _, node := range nodes.Items {
kvp, err := getTunIp(&node)
if err != nil || kvp == nil {
continue
}
kvps = append(kvps, kvp)
}
} else {
node, err := c.ClientSet.CoreV1().Nodes().Get(ctx, l.Hostname, metav1.GetOptions{})
if err != nil {
return nil, resources.K8sErrorToCalico(err, l)
}
kvp, err := getTunIp(node)
if err != nil || kvp == nil {
return &model.KVPairList{
KVPairs: []*model.KVPair{},
Revision: revision,
}, nil
}
kvps = append(kvps, kvp)
}
return &model.KVPairList{
KVPairs: kvps,
Revision: revision,
}, nil
}
func getTunIp(n *v1.Node) (*model.KVPair, error) {
if n.Spec.PodCIDR == "" {
log.Warnf("Node %s does not have podCIDR for HostConfig", n.Name)
return nil, nil
}
ip, _, err := net.ParseCIDR(n.Spec.PodCIDR)
if err != nil {
log.Warnf("Invalid podCIDR for HostConfig: %s, %s", n.Name, n.Spec.PodCIDR)
return nil, err
}
// We need to get the IP for the podCIDR and increment it to the
// first IP in the CIDR.
tunIp := ip.To4()
tunIp[3]++
kvp := &model.KVPair{
Key: model.HostConfigKey{
Hostname: n.Name,
Name: "IpInIpTunnelAddr",
},
Value: tunIp.String(),
}
return kvp, nil
}