/
sysdump.go
2927 lines (2811 loc) · 104 KB
/
sysdump.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 sysdump
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/cilium/cilium/pkg/versioncheck"
"github.com/cilium/workerpool"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/k8s"
"github.com/cilium/cilium-cli/utils/features"
)
const sysdumpLogFile = "cilium-sysdump.log"
const helmReleaseName = "cilium"
// Options groups together the set of options required to collect a sysdump.
type Options struct {
// The labels used to target Cilium pods.
CiliumLabelSelector string
// The namespace Cilium is running in.
CiliumNamespace string
// The namespace Cilium operator is running in.
CiliumOperatorNamespace string
// The namespace Cilium SPIRE installation is running in.
CiliumSPIRENamespace string
// The labels used to target Cilium daemon set. Usually, this label is same as CiliumLabelSelector.
CiliumDaemonSetSelector string
// The labels used to target Cilium Envoy pods.
CiliumEnvoyLabelSelector string
// The release name of Cilium Helm chart.
CiliumHelmReleaseName string
// The labels used to target Cilium Node Init daemon set. Usually, this label is same as CiliumNodeInitLabelSelector.
CiliumNodeInitDaemonSetSelector string
// The labels used to target Cilium Node Init pods.
CiliumNodeInitLabelSelector string
// The labels used to target Cilium operator pods.
CiliumOperatorLabelSelector string
// The labels used to target 'clustermesh-apiserver' pods.
ClustermeshApiserverLabelSelector string
// The labels used to target Cilium SPIRE server pods.
CiliumSPIREServerLabelSelector string
// The labels used to target Cilium SPIRE agent pods.
CiliumSPIREAgentLabelSelector string
// Whether to enable debug logging.
Debug bool
// Whether to enable scraping profiling data.
Profiling bool
// Whether to enable scraping tracing data.
Tracing bool
// The labels used to target additional pods
ExtraLabelSelectors []string
// The labels used to target Hubble pods.
HubbleLabelSelector string
// Number of Hubble flows to collect.
HubbleFlowsCount int64
// Timeout for collecting Hubble flows.
HubbleFlowsTimeout time.Duration
// The labels used to target Hubble Relay pods.
HubbleRelayLabelSelector string
// The labels used to target Hubble UI pods.
HubbleUILabelSelector string
// The labels used to target Hubble generate certs pods.
HubbleGenerateCertsLabelSelector string
// The amount of time to wait for the user to cancel the sysdump on a large cluster.
LargeSysdumpAbortTimeout time.Duration
// The threshold on the number of nodes present in the cluster that triggers a warning message.
LargeSysdumpThreshold int
// The limit on the number of bytes to retrieve when collecting logs
LogsLimitBytes int64
// How far back in time to go when collecting logs.
LogsSinceTime time.Duration
// Comma-separated list of node IPs or names to filter pods for which to collect gops and logs.
NodeList string
// The name of the resulting file (without extension)\n'<ts>' can be used as the placeholder for the timestamp.
OutputFileName string
// Whether to enable quick mode (i.e. skip collection of 'cilium-bugtool' output and logs).
Quick bool
// A 'RESTClientGetter' that can be used to create REST clients for the Kubernetes API.
// Required at least for getting the proper output of 'kubectl get pod -o wide' without actually using 'kubectl'.
RESTClientGetter genericclioptions.RESTClientGetter
// The number of workers to use.
WorkerCount int
// The writer used for logging.
Writer io.Writer
// Flags to pass to cilium-bugtool command
CiliumBugtoolFlags []string
// Whether to automatically detect the gops agent PID
DetectGopsPID bool
// Directory where CNI configs are located
CNIConfigDirectory string
// The name of the CNI config map
CNIConfigMapName string
// The labels used to target Tetragon pods.
TetragonLabelSelector string
// The labels used to target Tetragon oeprator pods.
TetragonOperatorLabelSelector string
// The namespace Namespace is running in.
TetragonNamespace string
// Retry limit for copying files from pods
CopyRetryLimit int
}
// Task defines a task for the sysdump collector to execute.
type Task struct {
// MUST be set to true if the task submits additional tasks to the worker pool.
CreatesSubtasks bool
// The description of the task.
Description string
// Whether this task runs when running in quick mode.
Quick bool
// The task itself.
Task func(context.Context) error
}
// Collector knows how to collect information required to troubleshoot issues with Cilium and Hubble.
type Collector struct {
Client KubernetesClient
Options Options
Pool *workerpool.WorkerPool
// logFile is the log file for the sydump log messages.
logFile *os.File
// logWriter is the io.Writer used for logging.
logWriter io.Writer
// subtasksWg is used to wait for subtasks to be submitted to the pool before calling 'Drain'.
// It is required since we don't know beforehand how many sub-tasks will be created, as they depend on the number of Cilium/Hubble/... pods found by "main" tasks.
subtasksWg sync.WaitGroup
// startTime keeps track of the time this sysdump collector got initialized. This timestamp
// is used to substitute '<ts>' in filenames.
startTime time.Time
// Directory to collect sysdump in.
sysdumpDir string
// allNodes is a list of all the node names in the cluster.
allNodes *corev1.NodeList
// NodeList is a list of nodes to collect sysdump information from.
NodeList []string
// CiliumPods is a list of Cilium agent pods running on nodes in NodeList.
CiliumPods []*corev1.Pod
// CiliumConfigMap is a pointer to cilium-config ConfigMap.
CiliumConfigMap *corev1.ConfigMap
// additionalTasks keeps track of additional tasks added via AddTasks.
additionalTasks []Task
// FeatureSet is a map of enabled / disabled features based on the contents of cilium-config ConfigMap.
FeatureSet features.Set
}
// NewCollector returns a new sysdump collector.
func NewCollector(k KubernetesClient, o Options, startTime time.Time, cliVersion string) (*Collector, error) {
c := Collector{
Client: k,
Options: o,
startTime: startTime,
FeatureSet: features.Set{},
}
tmp, err := os.MkdirTemp("", "*")
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}
c.sysdumpDir = filepath.Join(tmp, c.replaceTimestamp(c.Options.OutputFileName))
if err = os.MkdirAll(c.sysdumpDir, dirMode); err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}
if err = c.setupLogging(o.Writer); err != nil {
return nil, err
}
c.logDebug("Using %v as a temporary directory", c.sysdumpDir)
c.logTask("Collecting sysdump with cilium-cli version: %s, args: %s", cliVersion, os.Args[1:])
if c.Options.CiliumNamespace == "" {
ns, err := detectCiliumNamespace(k)
if err == nil {
c.log("🔮 Detected Cilium installation in namespace: %q", ns)
c.Options.CiliumNamespace = ns
} else {
c.log("ℹ️ Failed to detect Cilium installation")
}
} else {
c.log("ℹ️ Cilium namespace: %s", c.Options.CiliumNamespace)
}
if c.Options.CiliumOperatorNamespace == "" {
ns, err := detectCiliumOperatorNamespace(k)
if err == nil {
c.log("🔮 Detected Cilium operator in namespace: %q", ns)
c.Options.CiliumOperatorNamespace = ns
} else {
c.log("ℹ️ Failed to detect Cilium operator")
}
} else {
c.log("ℹ️ Cilium operator namespace: %s", c.Options.CiliumOperatorNamespace)
}
if c.Options.CiliumHelmReleaseName == "" {
c.log("ℹ️ Using default Cilium Helm release name: %q", helmReleaseName)
c.Options.CiliumHelmReleaseName = helmReleaseName
} else {
c.log("ℹ️ Cilium Helm release name: %q", c.Options.CiliumHelmReleaseName)
}
if c.Options.CiliumSPIRENamespace == "" {
if ns, err := detectCiliumSPIRENamespace(k); err != nil {
c.logDebug("Failed to detect Cilium SPIRE installation: %v", err)
if c.Options.CiliumOperatorNamespace != "" {
c.log("ℹ️ Failed to detect Cilium SPIRE installation - using Cilium namespace as Cilium SPIRE namespace: %q", c.Options.CiliumOperatorNamespace)
c.Options.CiliumSPIRENamespace = c.Options.CiliumOperatorNamespace
}
} else {
c.log("🔮 Detected Cilium SPIRE installation in namespace: %q", ns)
c.Options.CiliumSPIRENamespace = ns
}
} else {
c.log("ℹ️ Cilium SPIRE namespace: %q", c.Options.CiliumSPIRENamespace)
}
// Grab the Kubernetes nodes for the target cluster.
c.logTask("Collecting Kubernetes nodes")
c.allNodes, err = c.Client.ListNodes(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to collect Kubernetes nodes: %w", err)
}
c.logDebug("Finished collecting Kubernetes nodes")
// Exit if there are no nodes, as there's nothing to do.
if len(c.allNodes.Items) == 0 {
return nil, fmt.Errorf("no nodes found in the current cluster")
}
// If there are many nodes and no filters are specified, issue a warning and wait for a while before proceeding so the user can cancel the process.
if len(c.allNodes.Items) > c.Options.LargeSysdumpThreshold && (c.Options.NodeList == DefaultNodeList && c.Options.LogsLimitBytes == DefaultLogsLimitBytes && c.Options.LogsSinceTime == DefaultLogsSinceTime) {
c.logWarn("Detected a large cluster (%d nodes, threshold is %d)", len(c.allNodes.Items), c.Options.LargeSysdumpThreshold)
c.logWarn("Consider using a node filter (--node-list option, default=\"\"),")
c.logWarn("a custom log size limit (--logs-limit-bytes option, default=1GiB)")
c.logWarn("and/or a custom log time range (--logs-since-time option, default=1y)")
c.logWarn("to decrease the size of the sysdump")
c.logWarn("Waiting for %s before continuing, press control+c to abort and adjust your options", c.Options.LargeSysdumpAbortTimeout)
t := time.NewTicker(c.Options.LargeSysdumpAbortTimeout)
defer t.Stop()
<-t.C
}
// Build the list of node names in which the user is interested.
c.NodeList, err = buildNodeNameList(c.allNodes, c.Options.NodeList)
if err != nil {
return nil, fmt.Errorf("failed to build node list: %w", err)
}
c.logDebug("Restricting bugtool and logs collection to pods in %v", c.NodeList)
if c.Options.CiliumNamespace != "" {
ciliumPods, err := c.Client.ListPods(context.Background(), c.Options.CiliumNamespace, metav1.ListOptions{
LabelSelector: c.Options.CiliumLabelSelector,
})
if err != nil {
return nil, fmt.Errorf("failed to get Cilium pods: %w", err)
}
c.CiliumPods = FilterPods(ciliumPods, c.NodeList)
c.CiliumConfigMap, err = c.Client.GetConfigMap(context.Background(), c.Options.CiliumNamespace, ciliumConfigMapName, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
return nil, fmt.Errorf("failed to get %s ConfigMap: %w", ciliumConfigMapName, err)
}
c.log("ℹ️ %s ConfigMap not found in %s namespace", ciliumConfigMapName, c.Options.CiliumNamespace)
}
if c.CiliumConfigMap != nil {
c.FeatureSet.ExtractFromConfigMap(c.CiliumConfigMap)
c.log("🔮 Detected Cilium features: %v", c.FeatureSet)
}
}
return &c, nil
}
// GatherResourceUnstructured queries resources with the given GroupVersionResource, storing them in the file specified by fname.
// If keep is non-empty; then it will filter the items returned, keeping only those with names listed in keep.
// If keep is empty, it will not filter the resources returned.
func (c *Collector) GatherResourceUnstructured(ctx context.Context, r schema.GroupVersionResource, fname string, keep ...string) error {
n := corev1.NamespaceAll
v, err := c.Client.ListUnstructured(ctx, r, &n, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect %s (%s): %w", r.Resource, r.Version, err)
}
filtered := &unstructured.UnstructuredList{
Object: v.Object,
}
// keep everything if keep is empty
if len(keep) == 0 {
filtered.Items = v.Items
} else {
// only save the resources which are specified by keep
for _, elem := range v.Items {
for _, name := range keep {
if name == elem.GetName() {
filtered.Items = append(filtered.Items, elem)
}
}
}
}
if err := c.WriteYAML(fname, filtered); err != nil {
return fmt.Errorf("failed to write %s YAML: %w", r.Resource, err)
}
return nil
}
// setupLogging sets up sysdump collector loggging.
func (c *Collector) setupLogging(w io.Writer) error {
var err error
c.logFile, err = os.Create(filepath.Join(c.sysdumpDir, sysdumpLogFile))
if err != nil {
return fmt.Errorf("failed to create sysdump log file: %w", err)
}
// Log to stdout and to a file in the sysdump itself
c.logWriter = io.MultiWriter(w, c.logFile)
return nil
}
// teardownLogging flushes writes to the sysdump collector log file and closes it.
func (c *Collector) teardownLogging() {
// flush writes to log file
c.logFile.Sync()
// ...and close the log file
c.logFile.Close()
// rewire logger for the remaining log messages which won't make it into to log file
c.logWriter = os.Stdout
}
// replaceTimestamp can be used to replace the special timestamp placeholder in file and directory names.
func (c *Collector) replaceTimestamp(f string) string {
return strings.Replace(f, timestampPlaceholderFileName, c.startTime.Format(timeFormat), -1)
}
// AbsoluteTempPath returns the absolute path where to store the specified filename temporarily.
func (c *Collector) AbsoluteTempPath(f string) string {
return path.Join(c.sysdumpDir, c.replaceTimestamp(f))
}
// WriteYAML writes a kubernetes object to a file as YAML.
func (c *Collector) WriteYAML(filename string, o runtime.Object) error {
return writeYaml(c.AbsoluteTempPath(filename), o)
}
// WriteString writes a string to a file.
func (c *Collector) WriteString(filename string, value string) error {
return writeString(c.AbsoluteTempPath(filename), value)
}
// WriteBytes writes a byte array to a file.
func (c *Collector) WriteBytes(filename string, value []byte) error {
return writeBytes(c.AbsoluteTempPath(filename), value)
}
// WriteTable writes a kubernetes table to a file.
func (c *Collector) WriteTable(filename string, value *metav1.Table) error {
return writeTable(c.AbsoluteTempPath(filename), value)
}
// Run performs the actual sysdump collection.
func (c *Collector) Run() error {
// tasks is the list of base tasks to be run.
tasks := []Task{
{
Description: "Collect Kubernetes nodes",
Quick: true,
Task: func(_ context.Context) error {
if err := c.WriteYAML(kubernetesNodesFileName, c.allNodes); err != nil {
return fmt.Errorf("failed to collect Kubernetes nodes: %w", err)
}
return nil
},
},
{
Description: "Collect Kubernetes version",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetVersion(ctx)
if err != nil {
return fmt.Errorf("failed to collect Kubernetes version: %w", err)
}
if err := c.WriteString(kubernetesVersionInfoFileName, v); err != nil {
return fmt.Errorf("failed to dump Kubernetes version: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes events",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListEvents(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes events: %w", err)
}
if err := c.WriteYAML(kubernetesEventsFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes events: %w", err)
}
if err := c.WriteBytes(kubernetesEventsTableFileName, makeEventTable(v.Items)); err != nil {
return fmt.Errorf("failed to write event tble: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes namespaces",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListNamespaces(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes namespaces: %w", err)
}
if err := c.WriteYAML(kubernetesNamespacesFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes namespaces: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes pods",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListPods(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes pods: %w", err)
}
if err := c.WriteYAML(kubernetesPodsFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes pods: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes pods summary",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetPodsTable(ctx)
if err != nil {
return fmt.Errorf("failed to collect Kubernetes pods summary: %w", err)
}
if err := c.WriteTable(kubernetesPodsSummaryFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes pods summary: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes services",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListServices(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes services: %w", err)
}
if err := c.WriteYAML(kubernetesServicesFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes services: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes network policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListNetworkPolicies(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes network policies: %w", err)
}
if err := c.WriteYAML(kubernetesNetworkPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes network policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes endpoints",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListEndpoints(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes endpoints: %w", err)
}
if err := c.WriteYAML(kubernetesEndpointsFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes endpoints: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes leases",
Quick: true,
Task: func(ctx context.Context) error {
n := corev1.NamespaceAll
v, err := c.Client.ListUnstructured(ctx, k8sLeases, &n, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Kubernetes leases: %w", err)
}
if err := c.WriteYAML(kubernetesLeasesFileName, v); err != nil {
return fmt.Errorf("failed to collect Kubernetes leases: %w", err)
}
return nil
},
},
{
Description: "Collecting Kubernetes metrics",
Quick: true,
Task: func(ctx context.Context) error {
result, err := c.Client.GetRaw(ctx, "/metrics")
if err != nil {
return fmt.Errorf("failed to collect Kubernetes metrics: %w", err)
}
if err := c.WriteString(kubernetesMetricsFileName, result); err != nil {
return fmt.Errorf("failed to collect Kubernetes metrics: %w", err)
}
return nil
},
},
}
// task that needs to be executed "serially" (i.e: not concurrently with other tasks).
var serialTasks []Task
ciliumTasks := []Task{
{
Description: "Collecting Cilium network policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumNetworkPolicies(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium network policies: %w", err)
}
if err := c.WriteYAML(ciliumNetworkPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium network policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium cluster-wide network policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumClusterwideNetworkPolicies(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium cluster-wide network policies: %w", err)
}
if err := c.WriteYAML(ciliumClusterWideNetworkPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium cluster-wide network policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium egress NAT policies",
Quick: true,
Task: func(ctx context.Context) error {
gvr := schema.GroupVersionResource{
Group: "cilium.io",
Version: "v2alpha1",
Resource: "ciliumegressnatpolicies",
}
allNamespace := corev1.NamespaceAll
v, err := c.Client.ListUnstructured(ctx, gvr, &allNamespace, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium egress NAT policies: %w", err)
}
if err := c.WriteYAML(ciliumEgressNATPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium egress NAT policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium Egress Gateway policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumEgressGatewayPolicies(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium Egress Gateway policies: %w", err)
}
if err := c.WriteYAML(ciliumEgressGatewayPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium Egress Gateway policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium CIDR Groups",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumCIDRGroups(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium CIDR Groups: %w", err)
}
if err := c.WriteYAML(ciliumCIDRGroupsFileName, v); err != nil {
return fmt.Errorf("failed to write Cilium CIDR Groups to file: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium local redirect policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumLocalRedirectPolicies(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium local redirect policies: %w", err)
}
if err := c.WriteYAML(ciliumLocalRedirectPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium local redirect policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium endpoints",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumEndpoints(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium endpoints: %w", err)
}
if err := c.WriteYAML(ciliumEndpointsFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium endpoints: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium endpoint slices",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumEndpointSlices(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium endpoint slices: %w", err)
}
if err := c.WriteYAML(ciliumEndpointSlicesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium endpoint slices: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium identities",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumIdentities(ctx)
if err != nil {
return fmt.Errorf("failed to collect Cilium identities: %w", err)
}
if err := c.WriteYAML(ciliumIdentitiesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium identities: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium nodes",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumNodes(ctx)
if err != nil {
return fmt.Errorf("failed to collect Cilium nodes: %w", err)
}
if err := c.WriteYAML(ciliumNodesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium nodes: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium Node Configs",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumNodeConfigs(ctx, corev1.NamespaceAll, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium Node configs: %w", err)
}
if err := c.WriteYAML(ciliumNodeConfigsFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium Node configs: %w", err)
}
return nil
},
},
{
Description: "Collecting Ingresses",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListIngresses(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Ingresses: %w", err)
}
if err := c.WriteYAML(ciliumIngressesFileName, v); err != nil {
return fmt.Errorf("failed to collect Ingresses: %w", err)
}
return nil
},
},
{
Description: "Collecting IngressClasses",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListIngressClasses(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect IngressClasses: %w", err)
}
if err := c.WriteYAML(ingressClassesFileName, v); err != nil {
return fmt.Errorf("failed to collect IngressClasses: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium BGP Peering Policies",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumBGPPeeringPolicies(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium BGP Peering policies: %w", err)
}
if err := c.WriteYAML(ciliumBPGPeeringPoliciesFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium BGP Peering policies: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium LoadBalancer IP Pools",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumLoadBalancerIPPools(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium LoadBalancer IP Pools: %w", err)
}
if err := c.WriteYAML(ciliumLoadBalancerIPPoolsFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium LoadBalancer IP Pools: %w", err)
}
return nil
},
},
{
Description: "Collecting Cilium Pod IP Pools",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListCiliumPodIPPools(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("failed to collect Cilium Pod IP pools: %w", err)
}
if err := c.WriteYAML(ciliumPodIPPoolsFileName, v); err != nil {
return fmt.Errorf("failed to collect Cilium Pod IP pools: %w", err)
}
return nil
},
},
{
Description: fmt.Sprintf("Checking if %s exists in %s namespace", ciliumEtcdSecretsSecretName, c.Options.CiliumNamespace),
Quick: true,
Task: func(ctx context.Context) error {
_, err := c.Client.GetSecret(ctx, c.Options.CiliumNamespace, ciliumEtcdSecretsSecretName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.log("Secret %q not found in namespace %q - this is expected when using the CRD KVStore", ciliumEtcdSecretsSecretName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect Cilium etcd secret: %w", err)
}
c.log("Secret %q found in namespace %q", ciliumEtcdSecretsSecretName, c.Options.CiliumNamespace)
return nil
},
},
{
Description: "Collecting the Cilium configuration",
Quick: true,
Task: func(_ context.Context) error {
if c.CiliumConfigMap == nil {
return nil
}
if err := c.WriteYAML(ciliumConfigMapFileName, c.CiliumConfigMap); err != nil {
return fmt.Errorf("failed to collect the Cilium configuration: %w", err)
}
return nil
},
},
{
Description: "Collecting the Cilium daemonset(s)",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.ListDaemonSet(ctx, c.Options.CiliumNamespace, metav1.ListOptions{
LabelSelector: c.Options.CiliumDaemonSetSelector,
})
if err != nil {
return fmt.Errorf("failed to list Cilium daemonsets: %w", err)
}
if len(v.Items) == 0 {
return fmt.Errorf("failed to find Cilium daemonsets with label %q in namespace %q", c.Options.CiliumDaemonSetSelector, c.Options.CiliumNamespace)
}
if err = c.WriteYAML(ciliumDaemonSetFileName, v); err != nil {
return fmt.Errorf("failed to collect the Cilium daemonsets: %w", err)
}
return nil
},
},
{
Description: "Collecting the Cilium Node Init daemonset",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDaemonSet(ctx, c.Options.CiliumNamespace, ciliumNodeInitDaemonSetName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logWarn("Daemonset %q not found in namespace %q - this is expected if Node Init DaemonSet is not enabled", ciliumNodeInitDaemonSetName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Cilium Node Init daemonset: %w", err)
}
if err := c.WriteYAML(ciliumNodeInitDaemonsetFileName, v); err != nil {
return fmt.Errorf("could not write Cilium Node Init daemonset YAML to file: %w", err)
}
return nil
},
},
{
Description: "Collecting the Cilium Envoy configuration",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetConfigMap(ctx, c.Options.CiliumNamespace, ciliumEnvoyConfigMapName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to collect the Cilium Envoy configuration: %w", err)
}
if err := c.WriteYAML(ciliumEnvoyConfigMapFileName, v); err != nil {
return fmt.Errorf("failed to collect the Cilium Envoy configuration: %w", err)
}
return nil
},
},
{
Description: "Collecting the Cilium Envoy daemonset",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDaemonSet(ctx, c.Options.CiliumNamespace, ciliumEnvoyDaemonSetName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logWarn("Daemonset %q not found in namespace %q - this is expected if Envoy DaemonSet is not enabled", ciliumEnvoyDaemonSetName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Cilium Envoy daemonset: %w", err)
}
if err := c.WriteYAML(ciliumEnvoyDaemonsetFileName, v); err != nil {
return fmt.Errorf("failed to collect the Cilium Envoy daemonset: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble daemonset",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDaemonSet(ctx, c.Options.CiliumNamespace, hubbleDaemonSetName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logDebug("Daemonset %q not found in namespace %q - this is expected in recent versions of Cilium", hubbleDaemonSetName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Hubble daemonset: %w", err)
}
if err := c.WriteYAML(hubbleDaemonsetFileName, v); err != nil {
return fmt.Errorf("failed to collect the Hubble daemonset: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble Relay configuration",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetConfigMap(ctx, c.Options.CiliumNamespace, hubbleRelayConfigMapName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to collect the Hubble Relay configuration: %w", err)
}
if err := c.WriteYAML(hubbleRelayConfigMapFileName, v); err != nil {
return fmt.Errorf("failed to collect the Hubble Relay configuration: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble Relay deployment",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDeployment(ctx, c.Options.CiliumNamespace, hubbleRelayDeploymentName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logWarn("Deployment %q not found in namespace %q - this is expected if Hubble is not enabled", hubbleRelayDeploymentName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Hubble Relay deployment: %w", err)
}
if err := c.WriteYAML(hubbleRelayDeploymentFileName, v); err != nil {
return fmt.Errorf("failed to collect the Hubble Relay deployment: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble UI deployment",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDeployment(ctx, c.Options.CiliumNamespace, hubbleUIDeploymentName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logWarn("Deployment %q not found in namespace %q - this is expected if Hubble UI is not enabled", hubbleUIDeploymentName, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Hubble UI deployment: %w", err)
}
if err := c.WriteYAML(hubbleUIDeploymentFileName, v); err != nil {
return fmt.Errorf("failed to collect the Hubble UI deployment: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble generate certs cronjob",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetCronJob(ctx, c.Options.CiliumNamespace, hubbleGenerateCertsCronJob, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.logWarn("cronjob %q not found in namespace %q - this is expected if auto TLS is not enabled or if not using hubble.auto.tls.method=cronjob", hubbleGenerateCertsCronJob, c.Options.CiliumNamespace)
return nil
}
return fmt.Errorf("failed to collect the Hubble generate certs cronjob: %w", err)
}
if err := c.WriteYAML(hubbleGenerateCertsCronJobFileName, v); err != nil {
return fmt.Errorf("failed to collect the Hubble generate certs cronjob: %w", err)
}
return nil
},
},
{
Description: "Collecting the Hubble generate certs pod logs",
Quick: false,
Task: func(ctx context.Context) error {
p, err := c.Client.ListPods(ctx, c.Options.CiliumNamespace, metav1.ListOptions{
LabelSelector: c.Options.HubbleGenerateCertsLabelSelector,
})
if err != nil {
return fmt.Errorf("failed to get logs from Hubble certgen pods")
}
if err := c.SubmitLogsTasks(FilterPods(p, c.NodeList), c.Options.LogsSinceTime, c.Options.LogsLimitBytes); err != nil {
return fmt.Errorf("failed to collect logs from Hubble certgen pods")
}
return nil
},
},
{
Description: "Collecting the Hubble cert-manager certificates",
Quick: true,
Task: func(ctx context.Context) error {
return c.GatherResourceUnstructured(
ctx,
certificate,
hubbleCertificatesFileName,
"hubble-relay-client-certs",
"hubble-relay-server-certs",
"hubble-server-certs",
)
},
},
{
Description: "Collecting the Cilium operator deployment",
Quick: true,
Task: func(ctx context.Context) error {
v, err := c.Client.GetDeployment(ctx, c.Options.CiliumOperatorNamespace, ciliumOperatorDeploymentName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to collect the Cilium operator deployment: %w", err)
}
if err := c.WriteYAML(ciliumOperatorDeploymentFileName, v); err != nil {
return fmt.Errorf("failed to collect the Cilium operator deployment: %w", err)
}