-
Notifications
You must be signed in to change notification settings - Fork 208
/
client.go
1103 lines (914 loc) · 41.5 KB
/
client.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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package k8s
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/url"
"regexp"
"strings"
"time"
"github.com/blang/semver/v4"
"github.com/distribution/distribution/reference"
"helm.sh/helm/v3/pkg/chartutil"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Register all auth providers (azure, gcp, oidc, openstack, ..).
"k8s.io/client-go/rest"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"github.com/cilium/cilium/api/v1/models"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
ciliumv2alpha1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
ciliumClientset "github.com/cilium/cilium/pkg/k8s/client/clientset/versioned"
"github.com/cilium/cilium/pkg/versioncheck"
tetragonv1alpha1 "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
tetragonClientset "github.com/cilium/tetragon/pkg/k8s/client/clientset/versioned"
"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/internal/helm"
"github.com/cilium/cilium-cli/internal/utils"
)
type Client struct {
Clientset kubernetes.Interface
ExtensionClientset apiextensionsclientset.Interface // k8s api extension needed to retrieve CRDs
DynamicClientset dynamic.Interface
CiliumClientset ciliumClientset.Interface
TetragonClientset tetragonClientset.Interface
Config *rest.Config
RawConfig clientcmdapi.Config
RESTClientGetter genericclioptions.RESTClientGetter
contextName string
}
func NewClient(contextName, kubeconfig string) (*Client, error) {
// Register the Cilium types in the default scheme.
_ = ciliumv2.AddToScheme(scheme.Scheme)
_ = ciliumv2alpha1.AddToScheme(scheme.Scheme)
_ = tetragonv1alpha1.AddToScheme(scheme.Scheme)
restClientGetter := genericclioptions.ConfigFlags{
Context: &contextName,
KubeConfig: &kubeconfig,
}
rawKubeConfigLoader := restClientGetter.ToRawKubeConfigLoader()
config, err := rawKubeConfigLoader.ClientConfig()
if err != nil {
return nil, err
}
rawConfig, err := rawKubeConfigLoader.RawConfig()
if err != nil {
return nil, err
}
ciliumClientset, err := ciliumClientset.NewForConfig(config)
if err != nil {
return nil, err
}
tetragonClientset, err := tetragonClientset.NewForConfig(config)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
extensionClientset, err := apiextensionsclientset.NewForConfig(config)
if err != nil {
return nil, err
}
dynamicClientset, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
if contextName == "" {
contextName = rawConfig.CurrentContext
}
return &Client{
CiliumClientset: ciliumClientset,
TetragonClientset: tetragonClientset,
Clientset: clientset,
ExtensionClientset: extensionClientset,
Config: config,
DynamicClientset: dynamicClientset,
RawConfig: rawConfig,
RESTClientGetter: &restClientGetter,
contextName: contextName,
}, nil
}
// ContextName returns the name of the context the client is connected to
func (c *Client) ContextName() (name string) {
return c.contextName
}
// ClusterName returns the name of the cluster the client is connected to
func (c *Client) ClusterName() (name string) {
if context, ok := c.RawConfig.Contexts[c.ContextName()]; ok {
name = context.Cluster
}
return
}
func (c *Client) GetAPIServerHostAndPort() (string, string) {
if context, ok := c.RawConfig.Contexts[c.ContextName()]; ok {
addr := c.RawConfig.Clusters[context.Cluster].Server
if addr != "" {
url, err := url.Parse(addr)
if err == nil {
host, port, _ := net.SplitHostPort(url.Host)
return host, port
}
}
}
return "", ""
}
func (c *Client) CreateSecret(ctx context.Context, namespace string, secret *corev1.Secret, opts metav1.CreateOptions) (*corev1.Secret, error) {
return c.Clientset.CoreV1().Secrets(namespace).Create(ctx, secret, opts)
}
func (c *Client) UpdateSecret(ctx context.Context, namespace string, secret *corev1.Secret, opts metav1.UpdateOptions) (*corev1.Secret, error) {
return c.Clientset.CoreV1().Secrets(namespace).Update(ctx, secret, opts)
}
func (c *Client) PatchSecret(ctx context.Context, namespace, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*corev1.Secret, error) {
return c.Clientset.CoreV1().Secrets(namespace).Patch(ctx, name, pt, data, opts)
}
func (c *Client) DeleteSecret(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().Secrets(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetSecret(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.Secret, error) {
return c.Clientset.CoreV1().Secrets(namespace).Get(ctx, name, opts)
}
func (c *Client) CreateServiceAccount(ctx context.Context, namespace string, account *corev1.ServiceAccount, opts metav1.CreateOptions) (*corev1.ServiceAccount, error) {
return c.Clientset.CoreV1().ServiceAccounts(namespace).Create(ctx, account, opts)
}
func (c *Client) DeleteServiceAccount(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().ServiceAccounts(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetClusterRole(ctx context.Context, name string, opts metav1.GetOptions) (*rbacv1.ClusterRole, error) {
return c.Clientset.RbacV1().ClusterRoles().Get(ctx, name, opts)
}
func (c *Client) CreateClusterRole(ctx context.Context, role *rbacv1.ClusterRole, opts metav1.CreateOptions) (*rbacv1.ClusterRole, error) {
return c.Clientset.RbacV1().ClusterRoles().Create(ctx, role, opts)
}
func (c *Client) DeleteClusterRole(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.Clientset.RbacV1().ClusterRoles().Delete(ctx, name, opts)
}
func (c *Client) CreateClusterRoleBinding(ctx context.Context, role *rbacv1.ClusterRoleBinding, opts metav1.CreateOptions) (*rbacv1.ClusterRoleBinding, error) {
return c.Clientset.RbacV1().ClusterRoleBindings().Create(ctx, role, opts)
}
func (c *Client) DeleteClusterRoleBinding(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.Clientset.RbacV1().ClusterRoleBindings().Delete(ctx, name, opts)
}
func (c *Client) CreateRole(ctx context.Context, namespace string, role *rbacv1.Role, opts metav1.CreateOptions) (*rbacv1.Role, error) {
return c.Clientset.RbacV1().Roles(namespace).Create(ctx, role, opts)
}
func (c *Client) UpdateRole(ctx context.Context, namespace string, role *rbacv1.Role, opts metav1.UpdateOptions) (*rbacv1.Role, error) {
return c.Clientset.RbacV1().Roles(namespace).Update(ctx, role, opts)
}
func (c *Client) DeleteRole(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
return c.Clientset.RbacV1().Roles(namespace).Delete(ctx, name, opts)
}
func (c *Client) CreateRoleBinding(ctx context.Context, namespace string, roleBinding *rbacv1.RoleBinding, opts metav1.CreateOptions) (*rbacv1.RoleBinding, error) {
return c.Clientset.RbacV1().RoleBindings(namespace).Create(ctx, roleBinding, opts)
}
func (c *Client) UpdateRoleBinding(ctx context.Context, namespace string, roleBinding *rbacv1.RoleBinding, opts metav1.UpdateOptions) (*rbacv1.RoleBinding, error) {
return c.Clientset.RbacV1().RoleBindings(namespace).Update(ctx, roleBinding, opts)
}
func (c *Client) DeleteRoleBinding(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.RbacV1().RoleBindings(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetConfigMap(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.ConfigMap, error) {
return c.Clientset.CoreV1().ConfigMaps(namespace).Get(ctx, name, opts)
}
func (c *Client) PatchConfigMap(ctx context.Context, namespace, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*corev1.ConfigMap, error) {
return c.Clientset.CoreV1().ConfigMaps(namespace).Patch(ctx, name, pt, data, opts)
}
func (c *Client) UpdateConfigMap(ctx context.Context, configMap *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) {
return c.Clientset.CoreV1().ConfigMaps(configMap.Namespace).Update(ctx, configMap, opts)
}
func (c *Client) CreateService(ctx context.Context, namespace string, service *corev1.Service, opts metav1.CreateOptions) (*corev1.Service, error) {
return c.Clientset.CoreV1().Services(namespace).Create(ctx, service, opts)
}
func (c *Client) DeleteService(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().Services(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetService(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.Service, error) {
return c.Clientset.CoreV1().Services(namespace).Get(ctx, name, opts)
}
func (c *Client) CreateEndpoints(ctx context.Context, namespace string, ep *corev1.Endpoints, opts metav1.CreateOptions) (*corev1.Endpoints, error) {
return c.Clientset.CoreV1().Endpoints(namespace).Create(ctx, ep, opts)
}
func (c *Client) GetEndpoints(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.Endpoints, error) {
return c.Clientset.CoreV1().Endpoints(namespace).Get(ctx, name, opts)
}
func (c *Client) DeleteEndpoints(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().Endpoints(namespace).Delete(ctx, name, opts)
}
func (c *Client) CreateDeployment(ctx context.Context, namespace string, deployment *appsv1.Deployment, opts metav1.CreateOptions) (*appsv1.Deployment, error) {
return c.Clientset.AppsV1().Deployments(namespace).Create(ctx, deployment, opts)
}
func (c *Client) GetDeployment(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*appsv1.Deployment, error) {
return c.Clientset.AppsV1().Deployments(namespace).Get(ctx, name, opts)
}
func (c *Client) DeleteDeployment(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.AppsV1().Deployments(namespace).Delete(ctx, name, opts)
}
func (c *Client) PatchDeployment(ctx context.Context, namespace, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*appsv1.Deployment, error) {
return c.Clientset.AppsV1().Deployments(namespace).Patch(ctx, name, pt, data, opts)
}
func (c *Client) CheckDeploymentStatus(ctx context.Context, namespace, deployment string) error {
d, err := c.GetDeployment(ctx, namespace, deployment, metav1.GetOptions{})
if err != nil {
return err
}
if d == nil {
return fmt.Errorf("deployment is not available")
}
if d.Status.ObservedGeneration != d.Generation {
return fmt.Errorf("observed generation (%d) is older than generation of the desired state (%d)",
d.Status.ObservedGeneration, d.Generation)
}
if d.Status.Replicas == 0 {
return fmt.Errorf("replicas count is zero")
}
if d.Status.AvailableReplicas != d.Status.Replicas {
return fmt.Errorf("only %d of %d replicas are available", d.Status.AvailableReplicas, d.Status.Replicas)
}
if d.Status.ReadyReplicas != d.Status.Replicas {
return fmt.Errorf("only %d of %d replicas are ready", d.Status.ReadyReplicas, d.Status.Replicas)
}
if d.Status.UpdatedReplicas != d.Status.Replicas {
return fmt.Errorf("only %d of %d replicas are up-to-date", d.Status.UpdatedReplicas, d.Status.Replicas)
}
return nil
}
func (c *Client) CreateNamespace(ctx context.Context, namespace string, opts metav1.CreateOptions) (*corev1.Namespace, error) {
return c.Clientset.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, opts)
}
func (c *Client) GetNamespace(ctx context.Context, namespace string, options metav1.GetOptions) (*corev1.Namespace, error) {
return c.Clientset.CoreV1().Namespaces().Get(ctx, namespace, options)
}
func (c *Client) DeleteNamespace(ctx context.Context, namespace string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().Namespaces().Delete(ctx, namespace, opts)
}
func (c *Client) GetPod(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*corev1.Pod, error) {
return c.Clientset.CoreV1().Pods(namespace).Get(ctx, name, opts)
}
func (c *Client) CreatePod(ctx context.Context, namespace string, pod *corev1.Pod, opts metav1.CreateOptions) (*corev1.Pod, error) {
return c.Clientset.CoreV1().Pods(namespace).Create(ctx, pod, opts)
}
func (c *Client) DeletePod(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().Pods(namespace).Delete(ctx, name, opts)
}
func (c *Client) DeletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
return c.Clientset.CoreV1().Pods(namespace).DeleteCollection(ctx, opts, listOpts)
}
func (c *Client) ListPods(ctx context.Context, namespace string, options metav1.ListOptions) (*corev1.PodList, error) {
return c.Clientset.CoreV1().Pods(namespace).List(ctx, options)
}
func (c *Client) PodLogs(namespace, name string, opts *corev1.PodLogOptions) *rest.Request {
return c.Clientset.CoreV1().Pods(namespace).GetLogs(name, opts)
}
// separator for locating the start of the next log message. Sometimes
// logs may span multiple lines, locate the timestamp, log level and
// msg that always start a new log message
var logSplitter = regexp.MustCompile(`\r?\n[^ ]+ level=[[:alpha:]]+ msg=`)
func (c *Client) CiliumLogs(ctx context.Context, namespace, pod string, since time.Time, filter *regexp.Regexp) (string, error) {
opts := &corev1.PodLogOptions{
Container: defaults.AgentContainerName,
Timestamps: true,
SinceTime: &metav1.Time{Time: since},
}
req := c.PodLogs(namespace, pod, opts)
podLogs, err := req.Stream(ctx)
if err != nil {
return "", fmt.Errorf("error getting cilium-agent logs for %s/%s: %w", namespace, pod, err)
}
defer podLogs.Close()
buf := new(bytes.Buffer)
scanner := bufio.NewScanner(podLogs)
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
// find the full log line separator
loc := logSplitter.FindIndex(data)
if loc != nil {
// Locate '\n', advance just past it
nl := loc[0] + bytes.IndexByte(data[loc[0]:loc[1]], '\n') + 1
return nl, data[:nl], nil
} else if atEOF {
// EOF, return all we have
return len(data), data, nil
} else {
// Nothing to return
return 0, nil, nil
}
})
for scanner.Scan() {
if filter != nil && !filter.Match(scanner.Bytes()) {
continue
}
buf.Write(scanner.Bytes())
}
err = scanner.Err()
if err != nil {
err = fmt.Errorf("error reading cilium-agent logs for %s/%s: %w", namespace, pod, err)
}
return buf.String(), err
}
func (c *Client) ListServices(ctx context.Context, namespace string, options metav1.ListOptions) (*corev1.ServiceList, error) {
return c.Clientset.CoreV1().Services(namespace).List(ctx, options)
}
func (c *Client) ExecInPodWithStderr(ctx context.Context, namespace, pod, container string, command []string) (bytes.Buffer, bytes.Buffer, error) {
result, err := c.execInPod(ctx, ExecParameters{
Namespace: namespace,
Pod: pod,
Container: container,
Command: command,
})
return result.Stdout, result.Stderr, err
}
func (c *Client) ExecInPod(ctx context.Context, namespace, pod, container string, command []string) (bytes.Buffer, error) {
result, err := c.execInPod(ctx, ExecParameters{
Namespace: namespace,
Pod: pod,
Container: container,
Command: command,
})
if err != nil {
return bytes.Buffer{}, err
}
if errString := result.Stderr.String(); errString != "" {
return bytes.Buffer{}, fmt.Errorf("command failed: %s", errString)
}
return result.Stdout, nil
}
func (c *Client) ExecInPodWithWriters(connCtx, killCmdCtx context.Context, namespace, pod, container string, command []string, stdout, stderr io.Writer) error {
execParams := ExecParameters{
Namespace: namespace,
Pod: pod,
Container: container,
Command: command,
}
if killCmdCtx != nil {
execParams.TTY = true
}
err := c.execInPodWithWriters(connCtx, killCmdCtx, execParams, stdout, stderr)
if err != nil {
return err
}
return nil
}
func (c *Client) CiliumStatus(ctx context.Context, namespace, pod string) (*models.StatusResponse, error) {
stdout, err := c.ExecInPod(ctx, namespace, pod, defaults.AgentContainerName, []string{"cilium", "status", "-o", "json"})
if err != nil {
return nil, err
}
statusResponse := models.StatusResponse{}
if err := json.Unmarshal(stdout.Bytes(), &statusResponse); err != nil {
return nil, fmt.Errorf("unable to unmarshal response of cilium status: %w", err)
}
return &statusResponse, nil
}
func (c *Client) CreateConfigMap(ctx context.Context, namespace string, config *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) {
return c.Clientset.CoreV1().ConfigMaps(namespace).Create(ctx, config, opts)
}
func (c *Client) DeleteConfigMap(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().ConfigMaps(namespace).Delete(ctx, name, opts)
}
func (c *Client) CreateDaemonSet(ctx context.Context, namespace string, ds *appsv1.DaemonSet, opts metav1.CreateOptions) (*appsv1.DaemonSet, error) {
return c.Clientset.AppsV1().DaemonSets(namespace).Create(ctx, ds, opts)
}
func (c *Client) PatchDaemonSet(ctx context.Context, namespace, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*appsv1.DaemonSet, error) {
return c.Clientset.AppsV1().DaemonSets(namespace).Patch(ctx, name, pt, data, opts)
}
func (c *Client) GetDaemonSet(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*appsv1.DaemonSet, error) {
return c.Clientset.AppsV1().DaemonSets(namespace).Get(ctx, name, opts)
}
func (c *Client) ListDaemonSet(ctx context.Context, namespace string, o metav1.ListOptions) (*appsv1.DaemonSetList, error) {
return c.Clientset.AppsV1().DaemonSets(namespace).List(ctx, o)
}
func (c *Client) DeleteDaemonSet(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.AppsV1().DaemonSets(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetCRD(ctx context.Context, name string, opts metav1.GetOptions) (*apiextensions.CustomResourceDefinition, error) {
return c.ExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, name, opts)
}
// Kubernetes Network Policies specific commands
func (c *Client) ListKubernetesNetworkPolicies(ctx context.Context, namespace string, opts metav1.ListOptions) (*networkingv1.NetworkPolicyList, error) {
return c.Clientset.NetworkingV1().NetworkPolicies(namespace).List(ctx, opts)
}
func (c *Client) GetKubernetesNetworkPolicy(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*networkingv1.NetworkPolicy, error) {
return c.Clientset.NetworkingV1().NetworkPolicies(namespace).Get(ctx, name, opts)
}
func (c *Client) CreateKubernetesNetworkPolicy(ctx context.Context, policy *networkingv1.NetworkPolicy, opts metav1.CreateOptions) (*networkingv1.NetworkPolicy, error) {
return c.Clientset.NetworkingV1().NetworkPolicies(policy.Namespace).Create(ctx, policy, opts)
}
func (c *Client) UpdateKubernetesNetworkPolicy(ctx context.Context, policy *networkingv1.NetworkPolicy, opts metav1.UpdateOptions) (*networkingv1.NetworkPolicy, error) {
return c.Clientset.NetworkingV1().NetworkPolicies(policy.Namespace).Update(ctx, policy, opts)
}
func (c *Client) DeleteKubernetesNetworkPolicy(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.NetworkingV1().NetworkPolicies(namespace).Delete(ctx, name, opts)
}
type Kind int
const (
KindUnknown Kind = iota
KindMinikube
KindKind
KindEKS
KindGKE
KindAKS
KindMicrok8s
KindRancherDesktop
KindK3s
)
func (k Kind) String() string {
switch k {
case KindUnknown:
return "unknown"
case KindMinikube:
return "minikube"
case KindKind:
return "kind"
case KindEKS:
return "EKS"
case KindGKE:
return "GKE"
case KindAKS:
return "AKS"
case KindMicrok8s:
return "microk8s"
case KindRancherDesktop:
return "rancher-desktop"
case KindK3s:
return "K3s"
default:
return "invalid"
}
}
type Flavor struct {
ClusterName string
Kind Kind
}
type Platform struct {
OS string
Arch string
}
func (c *Client) AutodetectFlavor(ctx context.Context) Flavor {
f := Flavor{
ClusterName: c.ClusterName(),
}
if c.ClusterName() == "minikube" || c.ContextName() == "minikube" {
f.Kind = KindMinikube
return f
}
if strings.HasPrefix(c.ClusterName(), "microk8s-") || c.ContextName() == "microk8s" {
f.Kind = KindMicrok8s
}
if c.ClusterName() == "rancher-desktop" || c.ContextName() == "rancher-desktop" {
f.Kind = KindRancherDesktop
return f
}
// When creating a cluster with kind create cluster --name foo,
// the context and cluster name are kind-foo.
if strings.HasPrefix(c.ClusterName(), "kind-") || strings.HasPrefix(c.ContextName(), "kind-") {
f.Kind = KindKind
return f
}
if strings.HasPrefix(c.ClusterName(), "gke_") {
f.Kind = KindGKE
return f
}
// When creating a cluster with eksctl create cluster --name foo,
// the cluster name is foo.<region>.eksctl.io
if strings.HasSuffix(c.ClusterName(), ".eksctl.io") {
f.ClusterName = strings.ReplaceAll(c.ClusterName(), ".", "-")
f.Kind = KindEKS
return f
}
if context, ok := c.RawConfig.Contexts[c.ContextName()]; ok {
if cluster, ok := c.RawConfig.Clusters[context.Cluster]; ok {
if strings.HasSuffix(cluster.Server, "eks.amazonaws.com") {
f.Kind = KindEKS
return f
} else if strings.HasSuffix(cluster.Server, "azmk8s.io:443") {
f.Kind = KindAKS
return f
}
}
}
nodeList, err := c.ListNodes(ctx, metav1.ListOptions{})
if err != nil {
return f
}
// Assume k3s if the k8s master node runs k3s
for _, node := range nodeList.Items {
isMaster := node.Labels["node-role.kubernetes.io/master"]
if isMaster != "true" {
continue
}
instanceType, ok := node.Labels[corev1.LabelInstanceTypeStable]
if !ok {
instanceType = node.Labels[corev1.LabelInstanceType]
}
if instanceType == "k3s" {
f.Kind = KindK3s
return f
}
}
return f
}
func (c *Client) CreateResourceQuota(ctx context.Context, namespace string, rq *corev1.ResourceQuota, opts metav1.CreateOptions) (*corev1.ResourceQuota, error) {
return c.Clientset.CoreV1().ResourceQuotas(namespace).Create(ctx, rq, opts)
}
func (c *Client) DeleteResourceQuota(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.Clientset.CoreV1().ResourceQuotas(namespace).Delete(ctx, name, opts)
}
func (c *Client) GetCiliumEndpoint(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*ciliumv2.CiliumEndpoint, error) {
return c.CiliumClientset.CiliumV2().CiliumEndpoints(namespace).Get(ctx, name, opts)
}
func (c *Client) ListCiliumEndpoints(ctx context.Context, namespace string, options metav1.ListOptions) (*ciliumv2.CiliumEndpointList, error) {
return c.CiliumClientset.CiliumV2().CiliumEndpoints(namespace).List(ctx, options)
}
func (c *Client) ListCiliumEndpointSlices(ctx context.Context, options metav1.ListOptions) (*ciliumv2alpha1.CiliumEndpointSliceList, error) {
return c.CiliumClientset.CiliumV2alpha1().CiliumEndpointSlices().List(ctx, options)
}
func (c *Client) ListCiliumEnvoyConfigs(ctx context.Context, namespace string, options metav1.ListOptions) (*ciliumv2.CiliumEnvoyConfigList, error) {
return c.CiliumClientset.CiliumV2().CiliumEnvoyConfigs(namespace).List(ctx, options)
}
func (c *Client) GetNode(ctx context.Context, name string, opts metav1.GetOptions) (*corev1.Node, error) {
return c.Clientset.CoreV1().Nodes().Get(ctx, name, opts)
}
func (c *Client) ListNodes(ctx context.Context, options metav1.ListOptions) (*corev1.NodeList, error) {
return c.Clientset.CoreV1().Nodes().List(ctx, options)
}
func (c *Client) PatchNode(ctx context.Context, nodeName string, pt types.PatchType, data []byte) (*corev1.Node, error) {
return c.Clientset.CoreV1().Nodes().Patch(ctx, nodeName, pt, data, metav1.PatchOptions{})
}
func (c *Client) ListCiliumExternalWorkloads(ctx context.Context, opts metav1.ListOptions) (*ciliumv2.CiliumExternalWorkloadList, error) {
return c.CiliumClientset.CiliumV2().CiliumExternalWorkloads().List(ctx, opts)
}
func (c *Client) GetCiliumExternalWorkload(ctx context.Context, name string, opts metav1.GetOptions) (*ciliumv2.CiliumExternalWorkload, error) {
return c.CiliumClientset.CiliumV2().CiliumExternalWorkloads().Get(ctx, name, opts)
}
func (c *Client) CreateCiliumExternalWorkload(ctx context.Context, cew *ciliumv2.CiliumExternalWorkload, opts metav1.CreateOptions) (*ciliumv2.CiliumExternalWorkload, error) {
return c.CiliumClientset.CiliumV2().CiliumExternalWorkloads().Create(ctx, cew, opts)
}
func (c *Client) DeleteCiliumExternalWorkload(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.CiliumClientset.CiliumV2().CiliumExternalWorkloads().Delete(ctx, name, opts)
}
func (c *Client) ListCiliumNetworkPolicies(ctx context.Context, namespace string, opts metav1.ListOptions) (*ciliumv2.CiliumNetworkPolicyList, error) {
return c.CiliumClientset.CiliumV2().CiliumNetworkPolicies(namespace).List(ctx, opts)
}
func (c *Client) GetCiliumNetworkPolicy(ctx context.Context, namespace, name string, opts metav1.GetOptions) (*ciliumv2.CiliumNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumNetworkPolicies(namespace).Get(ctx, name, opts)
}
func (c *Client) CreateCiliumNetworkPolicy(ctx context.Context, cnp *ciliumv2.CiliumNetworkPolicy, opts metav1.CreateOptions) (*ciliumv2.CiliumNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumNetworkPolicies(cnp.Namespace).Create(ctx, cnp, opts)
}
func (c *Client) UpdateCiliumNetworkPolicy(ctx context.Context, cnp *ciliumv2.CiliumNetworkPolicy, opts metav1.UpdateOptions) (*ciliumv2.CiliumNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumNetworkPolicies(cnp.Namespace).Update(ctx, cnp, opts)
}
func (c *Client) DeleteCiliumNetworkPolicy(ctx context.Context, namespace, name string, opts metav1.DeleteOptions) error {
return c.CiliumClientset.CiliumV2().CiliumNetworkPolicies(namespace).Delete(ctx, name, opts)
}
func (c *Client) ListCiliumEgressGatewayPolicies(ctx context.Context, opts metav1.ListOptions) (*ciliumv2.CiliumEgressGatewayPolicyList, error) {
return c.CiliumClientset.CiliumV2().CiliumEgressGatewayPolicies().List(ctx, opts)
}
func (c *Client) GetCiliumEgressGatewayPolicy(ctx context.Context, name string, opts metav1.GetOptions) (*ciliumv2.CiliumEgressGatewayPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumEgressGatewayPolicies().Get(ctx, name, opts)
}
func (c *Client) CreateCiliumEgressGatewayPolicy(ctx context.Context, cegp *ciliumv2.CiliumEgressGatewayPolicy, opts metav1.CreateOptions) (*ciliumv2.CiliumEgressGatewayPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumEgressGatewayPolicies().Create(ctx, cegp, opts)
}
func (c *Client) UpdateCiliumEgressGatewayPolicy(ctx context.Context, cegp *ciliumv2.CiliumEgressGatewayPolicy, opts metav1.UpdateOptions) (*ciliumv2.CiliumEgressGatewayPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumEgressGatewayPolicies().Update(ctx, cegp, opts)
}
func (c *Client) DeleteCiliumEgressGatewayPolicy(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.CiliumClientset.CiliumV2().CiliumEgressGatewayPolicies().Delete(ctx, name, opts)
}
func (c *Client) ListCiliumBGPPeeringPolicies(ctx context.Context, opts metav1.ListOptions) (*ciliumv2alpha1.CiliumBGPPeeringPolicyList, error) {
return c.CiliumClientset.CiliumV2alpha1().CiliumBGPPeeringPolicies().List(ctx, opts)
}
func (c *Client) ListCiliumCIDRGroups(ctx context.Context, opts metav1.ListOptions) (*ciliumv2alpha1.CiliumCIDRGroupList, error) {
return c.CiliumClientset.CiliumV2alpha1().CiliumCIDRGroups().List(ctx, opts)
}
func (c *Client) ListCiliumClusterwideNetworkPolicies(ctx context.Context, opts metav1.ListOptions) (*ciliumv2.CiliumClusterwideNetworkPolicyList, error) {
return c.CiliumClientset.CiliumV2().CiliumClusterwideNetworkPolicies().List(ctx, opts)
}
func (c *Client) ListCiliumClusterwideEnvoyConfigs(ctx context.Context, opts metav1.ListOptions) (*ciliumv2.CiliumClusterwideEnvoyConfigList, error) {
return c.CiliumClientset.CiliumV2().CiliumClusterwideEnvoyConfigs().List(ctx, opts)
}
func (c *Client) GetCiliumClusterwideNetworkPolicy(ctx context.Context, name string, opts metav1.GetOptions) (*ciliumv2.CiliumClusterwideNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumClusterwideNetworkPolicies().Get(ctx, name, opts)
}
func (c *Client) CreateCiliumClusterwideNetworkPolicy(ctx context.Context, ccnp *ciliumv2.CiliumClusterwideNetworkPolicy, opts metav1.CreateOptions) (*ciliumv2.CiliumClusterwideNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumClusterwideNetworkPolicies().Create(ctx, ccnp, opts)
}
func (c *Client) UpdateCiliumClusterwideNetworkPolicy(ctx context.Context, ccnp *ciliumv2.CiliumClusterwideNetworkPolicy, opts metav1.UpdateOptions) (*ciliumv2.CiliumClusterwideNetworkPolicy, error) {
return c.CiliumClientset.CiliumV2().CiliumClusterwideNetworkPolicies().Update(ctx, ccnp, opts)
}
func (c *Client) DeleteCiliumClusterwideNetworkPolicy(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.CiliumClientset.CiliumV2().CiliumClusterwideNetworkPolicies().Delete(ctx, name, opts)
}
func (c *Client) GetVersion(_ context.Context) (string, error) {
v, err := c.Clientset.Discovery().ServerVersion()
if err != nil {
return "", fmt.Errorf("failed to get Kubernetes version: %w", err)
}
return fmt.Sprintf("%#v", *v), nil
}
func (c *Client) ListEvents(ctx context.Context, o metav1.ListOptions) (*corev1.EventList, error) {
return c.Clientset.CoreV1().Events(corev1.NamespaceAll).List(ctx, o)
}
func (c *Client) ListNamespaces(ctx context.Context, o metav1.ListOptions) (*corev1.NamespaceList, error) {
return c.Clientset.CoreV1().Namespaces().List(ctx, o)
}
func (c *Client) GetPodsTable(_ context.Context) (*metav1.Table, error) {
r := resource.NewBuilder(c.RESTClientGetter).
Unstructured().
AllNamespaces(true).
ResourceTypes("pods").
SingleResourceType().
SelectAllParam(true).
RequestChunksOf(500).
ContinueOnError().
Latest().
Flatten().
TransformRequests(func(r *rest.Request) {
r.SetHeader(
"Accept", fmt.Sprintf("application/json;as=Table;v=%s;g=%s", metav1.SchemeGroupVersion.Version, metav1.GroupName),
)
}).
Do()
if r.Err() != nil {
return nil, r.Err()
}
i, err := r.Infos()
if err != nil {
return nil, err
}
if len(i) != 1 {
return nil, fmt.Errorf("expected a single kind of resource (got %d)", len(i))
}
return unstructuredToTable(i[0].Object)
}
func (c *Client) ListUnstructured(ctx context.Context, gvr schema.GroupVersionResource, namespace *string, o metav1.ListOptions) (*unstructured.UnstructuredList, error) {
if namespace == nil {
return c.DynamicClientset.Resource(gvr).List(ctx, o)
}
return c.DynamicClientset.Resource(gvr).Namespace(*namespace).List(ctx, o)
}
func (c *Client) ListEndpoints(ctx context.Context, o metav1.ListOptions) (*corev1.EndpointsList, error) {
return c.Clientset.CoreV1().Endpoints(corev1.NamespaceAll).List(ctx, o)
}
func (c *Client) ListIngressClasses(ctx context.Context, o metav1.ListOptions) (*networkingv1.IngressClassList, error) {
return c.Clientset.NetworkingV1().IngressClasses().List(ctx, o)
}
func (c *Client) ListIngresses(ctx context.Context, o metav1.ListOptions) (*networkingv1.IngressList, error) {
return c.Clientset.NetworkingV1().Ingresses(corev1.NamespaceAll).List(ctx, o)
}
func (c *Client) ListNetworkPolicies(ctx context.Context, o metav1.ListOptions) (*networkingv1.NetworkPolicyList, error) {
return c.Clientset.NetworkingV1().NetworkPolicies(corev1.NamespaceAll).List(ctx, o)
}
func (c *Client) ListCiliumIdentities(ctx context.Context) (*ciliumv2.CiliumIdentityList, error) {
return c.CiliumClientset.CiliumV2().CiliumIdentities().List(ctx, metav1.ListOptions{})
}
func (c *Client) ListCiliumNodes(ctx context.Context) (*ciliumv2.CiliumNodeList, error) {
return c.CiliumClientset.CiliumV2().CiliumNodes().List(ctx, metav1.ListOptions{})
}
func (c *Client) ListCiliumNodeConfigs(ctx context.Context, namespace string, opts metav1.ListOptions) (*ciliumv2alpha1.CiliumNodeConfigList, error) {
return c.CiliumClientset.CiliumV2alpha1().CiliumNodeConfigs(namespace).List(ctx, opts)
}
func (c *Client) GetLogs(ctx context.Context, namespace, name, container string, sinceTime time.Time, limitBytes int64, previous bool) (string, error) {
t := metav1.NewTime(sinceTime)
o := corev1.PodLogOptions{
Container: container,
Follow: false,
LimitBytes: &limitBytes,
Previous: previous,
SinceTime: &t,
Timestamps: true,
}
r := c.Clientset.CoreV1().Pods(namespace).GetLogs(name, &o)
s, err := r.Stream(ctx)
if err != nil {
return "", err
}
defer s.Close()
var b bytes.Buffer
if _, err = io.Copy(&b, s); err != nil {
return "", err
}
return b.String(), nil
}
func getCiliumVersionFromImage(image string) (string, error) {
// default to "latest" as k8s may not include it explicitly
version := "latest"
ref, err := reference.Parse(image)
if err != nil {
// Image has an invalid name, skip it
return "", err
}
tagged, isTagged := ref.(reference.Tagged)
if isTagged {
version = tagged.Tag()
}
named, isNamed := ref.(reference.Named)
if isNamed {
path := reference.Path(named)
strs := strings.Split(path, "/")
// Take the last element as an image name
imageName := strs[len(strs)-1]
if !strings.HasPrefix(imageName, "cilium") {
// Not likely to be Cilium
return "", fmt.Errorf("image name %s is not prefixed with cilium", imageName)
}
// Add any part in the pod image separated by a '-` to the version,
// e.g., "quay.io/cilium/cilium-ci:1234" -> "-ci:1234"
dash := strings.Index(imageName, "-")
if dash >= 0 {
version = imageName[dash:] + ":" + version
}
} else {
// Image somehow doesn't contain name, skip it.
return "", fmt.Errorf("image does't contain name")
}
return version, nil
}
// GetCiliumVersion returns a semver.Version representing the version of cilium
// running in the cilium-agent pod
func (c *Client) GetCiliumVersion(ctx context.Context, p *corev1.Pod) (*semver.Version, error) {
o, _, err := c.ExecInPodWithStderr(
ctx,
p.Namespace,
p.Name,
defaults.AgentContainerName,
[]string{"cilium", "version", "-o", "jsonpath={$.Daemon.Version}"},
)
if err != nil {
return nil, fmt.Errorf("unable to fetch cilium version on pod %q: %w", p.Name, err)
}
v, _, _ := strings.Cut(strings.TrimSpace(o.String()), "-") // strips proprietary -releaseX suffix
podVersion, err := semver.Parse(v)
if err != nil {
return nil, fmt.Errorf("unable to parse cilium version on pod %q: %w", p.Name, err)
}
return &podVersion, nil
}
func (c *Client) GetRunningCiliumVersion(ctx context.Context, namespace string) (string, error) {
pods, err := c.ListPods(ctx, namespace, metav1.ListOptions{LabelSelector: defaults.AgentPodSelector})
if err != nil {
return "", fmt.Errorf("unable to list cilium pods: %w", err)
}
if len(pods.Items) > 0 && len(pods.Items[0].Spec.Containers) > 0 {
for _, container := range pods.Items[0].Spec.Containers {
version, err := getCiliumVersionFromImage(container.Image)
if err != nil {
continue
}
return version, nil
}
return "", errors.New("unable to obtain cilium version: no cilium container found")
}
return "", errors.New("unable to obtain cilium version: no cilium pods found")
}
func (c *Client) ListCiliumLoadBalancerIPPools(ctx context.Context, opts metav1.ListOptions) (*ciliumv2alpha1.CiliumLoadBalancerIPPoolList, error) {
return c.CiliumClientset.CiliumV2alpha1().CiliumLoadBalancerIPPools().List(ctx, opts)
}
func (c *Client) ListCiliumLocalRedirectPolicies(ctx context.Context, namespace string, opts metav1.ListOptions) (*ciliumv2.CiliumLocalRedirectPolicyList, error) {
return c.CiliumClientset.CiliumV2().CiliumLocalRedirectPolicies(namespace).List(ctx, opts)
}
func (c *Client) GetPlatform(_ context.Context) (*Platform, error) {
v, err := c.Clientset.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("failed to get Kubernetes version: %w", err)
}
fileds := strings.Split(v.Platform, "/")
if len(fileds) != 2 {
return nil, fmt.Errorf("unknown platform type")
}
return &Platform{
OS: fileds[0],
Arch: fileds[1],
}, err
}
func (c *Client) GetServerVersion() (*semver.Version, error) {
sv, err := c.Clientset.Discovery().ServerVersion()
if err != nil {
return nil, err
}
var ver semver.Version
// Try GitVersion first. In case of error fallback to MajorMinor
if sv.GitVersion != "" {
// This is a string like "v1.9.0"
ver, err = versioncheck.Version(sv.GitVersion)
if err == nil {
return &ver, nil
}
}
if sv.Major != "" && sv.Minor != "" {
ver, err = versioncheck.Version(fmt.Sprintf("%s.%s", sv.Major, sv.Minor))
if err == nil {
return &ver, nil