This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
data.go
890 lines (761 loc) · 30.6 KB
/
data.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
// Package support contains the methods for building a support package.
package support
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"strings"
"text/template"
access "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha2"
specs "github.com/servicemeshinterface/smi-controller-sdk/apis/specs/v1alpha3"
split "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha3"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/kubernetes"
"k8s.io/kubectl/pkg/describe"
"sigs.k8s.io/yaml"
"github.com/nginxinc/nginx-service-mesh/pkg/apis/mesh"
nsmspecsv1alpha1 "github.com/nginxinc/nginx-service-mesh/pkg/apis/specs/v1alpha1"
nsmspecsv1alpha2 "github.com/nginxinc/nginx-service-mesh/pkg/apis/specs/v1alpha2"
"github.com/nginxinc/nginx-service-mesh/pkg/helm"
"github.com/nginxinc/nginx-service-mesh/pkg/k8s"
)
const (
meshLabelSelectorKey = "app.kubernetes.io/part-of"
meshLabelSelectorValue = "nginx-service-mesh"
podDescFile = "desc.txt"
podYamlFile = "pod.yaml"
podListFile = "pods.txt"
deploymentsFile = "deployments.yaml"
statefulSetsFile = "statefulsets.yaml"
daemonSetsFile = "daemonsets.yaml"
servicesFile = "services.yaml"
configMapsFile = "configmaps.yaml"
serviceAccountsFile = "serviceaccounts.yaml"
secretsFile = "secrets.yaml"
validatingWebhookConfigurationsFile = "validatingwebhookconfigurations.yaml"
mutatingWebhookConfigurationsFile = "mutatingwebhookconfigurations.yaml"
clusterRolesFile = "clusterroles.yaml"
clusterRoleBindingsFile = "clusterrolebindings.yaml"
eventListFile = "events.txt"
crdsFile = "crds.yaml"
apiServicesFile = "apiservices.yaml"
trafficSplitsFile = "trafficsplits.yaml"
trafficTargetsFile = "traffictargets.yaml"
httpRouteGroupsFile = "httproutegroups.yaml"
tcpRoutesFile = "tcproutes.yaml"
rateLimitsFile = "ratelimits.yaml"
circuitBreakersFile = "circuitbreakers.yaml"
)
// DataFetcher gets all data for the support package and writes it to corresponding files.
type DataFetcher struct {
writer FileWriter
k8sClient k8s.Client
kubeconfig string
directory string
meshNamespace string
controlPlaneDir string
collectSidecarLogs bool
}
// NewDataFetcher returns a new DataFetcher.
func NewDataFetcher(
k8sClient k8s.Client,
writer FileWriter,
kubeconfig,
directory string,
collectSidecarLogs bool,
) *DataFetcher {
return &DataFetcher{
writer: writer,
k8sClient: k8sClient,
kubeconfig: kubeconfig,
directory: directory,
meshNamespace: k8sClient.Namespace(),
collectSidecarLogs: collectSidecarLogs,
}
}
// GatherAndWriteData pulls together all support package data and writes it to corresponding files.
func (df *DataFetcher) GatherAndWriteData() {
df.writeControlPlaneInformation()
df.writeMeshConfig()
df.writeDeployConfig()
if df.collectSidecarLogs {
df.writeSidecarInformation()
}
df.writeTrafficPolicies()
df.writeReadme()
}
// writeControlPlaneInformation retrieves and writes information about the control plane resources.
func (df *DataFetcher) writeControlPlaneInformation() {
if err := df.createControlPlaneDirectory(); err != nil {
log.Printf("- could not create proper directory structure: %v", err)
return
}
df.writeControlPlanePods()
df.writeControlPlaneDeployments()
df.writeControlPlaneStatefulSets()
df.writeControlPlaneDaemonSets()
df.writeControlPlaneServices()
df.writeControlPlaneConfigMaps()
df.writeControlPlaneServiceAccounts()
df.writeControlPlaneSecrets()
df.writeControlPlaneEvents()
df.writeControlPlaneValidatingWebhookConfigurations()
df.writeControlPlaneMutatingWebhookConfigurations()
df.writeControlPlaneClusterRoles()
df.writeControlPlaneClusterRoleBindings()
df.writeControlPlaneCRDs()
df.writeControlPlaneAPIServices()
df.writeK8sMetrics()
}
// createControlPlaneDirectory creates the sub-directory for the nginx-mesh namespace.
func (df *DataFetcher) createControlPlaneDirectory() error {
df.controlPlaneDir = filepath.Join(df.directory, df.meshNamespace)
return df.writer.MkdirAll(df.controlPlaneDir)
}
// writeControlPlanePods retrieves and writes information about control plane pods.
func (df *DataFetcher) writeControlPlanePods() {
log.Println("Getting control plane Pods.")
pods, err := df.k8sClient.ClientSet().CoreV1().Pods(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Pods: %v\n", err)
return
}
podDescriber := describe.PodDescriber{Interface: df.k8sClient.ClientSet()}
df.writeControlPlanePodList(pods)
for iter, pod := range pods.Items {
// create directory for Pod
if err := df.writer.Mkdir(filepath.Join(df.controlPlaneDir, pod.Name)); err != nil {
log.Printf("- could not create control plane Pod directory: %v\n", err)
continue
}
writeFile := func(filename, contents string) {
if contents != "" {
if err := df.writer.Write(filename, contents); err != nil {
log.Printf("- could not write %s for Pod '%s/%s': %v",
filename, df.meshNamespace, pod.Name, err)
}
}
}
// write yaml
podYaml, err := yaml.Marshal(pod)
if err != nil {
log.Printf("- could not create yaml for Pod '%s/%s': %v\n", df.meshNamespace, pod.Name, err)
} else {
writeFile(filepath.Join(df.controlPlaneDir, pod.Name, podYamlFile), string(podYaml))
}
// write description
description, err := podDescriber.Describe(df.meshNamespace, pod.Name, describe.DescriberSettings{ShowEvents: true})
if err != nil {
log.Printf("- could not get description for Pod '%s/%s': %v\n", df.meshNamespace, pod.Name, err)
} else {
writeFile(filepath.Join(df.controlPlaneDir, pod.Name, podDescFile), description)
}
// write logs
df.writeContainerLogs(&pods.Items[iter], df.controlPlaneDir, nil)
}
}
// writeControlPlanePods is the equivalent of `kubectl get pods -o wide`.
func (df *DataFetcher) writeControlPlanePodList(podList *v1.PodList) {
podTable := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name"},
{Name: "Ready", Type: "string", Format: ""},
{Name: "Status", Type: "string", Format: ""},
{Name: "Restarts", Type: "integer", Format: ""},
{Name: "Age", Type: "string", Format: ""},
{Name: "IP", Type: "string", Format: "", Priority: 1},
{Name: "Node", Type: "string", Format: "", Priority: 1},
{Name: "Nominated Node", Type: "string", Format: "", Priority: 1},
{Name: "Readiness Gates", Type: "string", Format: "", Priority: 1},
},
}
rows, err := printPodList(podList)
if err != nil {
log.Printf("- could not create Pod Table: %v\n", err)
return
}
podTable.Rows = rows
printer := printers.NewTablePrinter(printers.PrintOptions{
Wide: true,
})
out := bytes.NewBuffer([]byte{})
err = printer.PrintObj(podTable, out)
if err != nil {
log.Printf("- could not print Pod Table: %v\n", err)
return
}
if err := df.writer.Write(filepath.Join(df.controlPlaneDir, podListFile), out.String()); err != nil {
log.Printf("- could not write PodList: %v", err)
}
}
// writeControlPlaneDeployments retrieves and writes information about control plane deployments.
func (df *DataFetcher) writeControlPlaneDeployments() {
log.Println("Getting control plane Deployments.")
deployments, err := df.k8sClient.ClientSet().AppsV1().Deployments(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Deployments: %v\n", err)
return
}
if err = df.createYamlFromList(deployments, filepath.Join(df.controlPlaneDir, deploymentsFile)); err != nil {
log.Printf("- could not write Deployments to file: %v", err)
}
}
// writeControlPlaneStatefulSets retrieves and writes information about the control plane statefulsets.
func (df *DataFetcher) writeControlPlaneStatefulSets() {
log.Println("Getting control plane StatefulSets.")
statefulSets, err := df.k8sClient.ClientSet().AppsV1().StatefulSets(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list StatefulSets: %v\n", err)
return
}
if err = df.createYamlFromList(statefulSets, filepath.Join(df.controlPlaneDir, statefulSetsFile)); err != nil {
log.Printf("- could not write StatefulSets to file: %v", err)
}
}
// writeControlPlaneDaemonSets retrieves and writes information about control plane daemonsets.
func (df *DataFetcher) writeControlPlaneDaemonSets() {
log.Println("Getting control plane DaemonSets.")
daemonSets, err := df.k8sClient.ClientSet().AppsV1().DaemonSets(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list DaemonSets: %v\n", err)
return
}
if err = df.createYamlFromList(daemonSets, filepath.Join(df.controlPlaneDir, daemonSetsFile)); err != nil {
log.Printf("- could not write DaemonSets to file: %v", err)
}
}
// writeControlPlaneServices retrieves and writes information about control plane services.
func (df *DataFetcher) writeControlPlaneServices() {
log.Println("Getting control plane Services.")
services, err := df.k8sClient.ClientSet().CoreV1().Services(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Services: %v\n", err)
return
}
if err = df.createYamlFromList(services, filepath.Join(df.controlPlaneDir, servicesFile)); err != nil {
log.Printf("- could not write Services to file: %v", err)
}
}
// writeControlPlaneConfigMaps retrieves and writes information about control plane config maps.
func (df *DataFetcher) writeControlPlaneConfigMaps() {
log.Println("Getting control plane ConfigMaps.")
configMaps, err := df.k8sClient.ClientSet().CoreV1().ConfigMaps(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list ConfigMaps: %v\n", err)
return
}
if err = df.createYamlFromList(configMaps, filepath.Join(df.controlPlaneDir, configMapsFile)); err != nil {
log.Printf("- could not write ConfigMaps to file: %v", err)
}
}
// writeControlPlaneServiceAccounts retrieves and writes information about control plane service accounts.
func (df *DataFetcher) writeControlPlaneServiceAccounts() {
log.Println("Getting control plane Service Accounts.")
serviceAccounts, err := df.k8sClient.ClientSet().CoreV1().ServiceAccounts(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list ServiceAccounts: %v\n", err)
return
}
if err = df.createYamlFromList(serviceAccounts, filepath.Join(df.controlPlaneDir, serviceAccountsFile)); err != nil {
log.Printf("- could not write ServiceAccounts to file: %v", err)
}
}
// writeControlPlaneSecrets retrieves and writes information about control plane secrets.
func (df *DataFetcher) writeControlPlaneSecrets() {
log.Println("Getting control plane Secrets.")
secrets, err := df.k8sClient.ClientSet().CoreV1().Secrets(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Secrets: %v\n", err)
return
}
err = df.createYamlFromList(secrets, filepath.Join(df.controlPlaneDir, secretsFile))
if err != nil {
log.Printf("- could not write Secrets to file: %v", err)
}
}
// writeControlPlaneEvents is the equivalent of `kubectl get events -o wide`.
func (df *DataFetcher) writeControlPlaneEvents() {
log.Println("Getting control plane Events.")
events, err := df.k8sClient.ClientSet().CoreV1().Events(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Events: %v\n", err)
}
eventTable := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Last Seen", Type: "string"},
{Name: "Type", Type: "string"},
{Name: "Reason", Type: "string"},
{Name: "Object", Type: "string"},
{Name: "Subobject", Type: "string", Priority: 1},
{Name: "Source", Type: "string", Priority: 1},
{Name: "Message", Type: "string"},
{Name: "First Seen", Type: "string", Priority: 1},
{Name: "Count", Type: "string", Priority: 1},
{Name: "Name", Type: "string", Priority: 1},
},
}
rows, err := printEventList(events)
if err != nil {
log.Printf("- could not create Event Table: %v\n", err)
return
}
eventTable.Rows = rows
printer := printers.NewTablePrinter(printers.PrintOptions{
Wide: true,
})
out := bytes.NewBuffer([]byte{})
err = printer.PrintObj(eventTable, out)
if err != nil {
log.Printf("- could not print Event Table: %v\n", err)
return
}
if err := df.writer.Write(filepath.Join(df.controlPlaneDir, eventListFile), out.String()); err != nil {
log.Printf("- could not write EventList: %v", err)
}
}
// writeControlPlaneValidatingWebhookConfigurations retrieves and writes information about validating webhook configurations.
func (df *DataFetcher) writeControlPlaneValidatingWebhookConfigurations() {
log.Println("Getting ValidatingWebhookConfigurations.")
validatingWebhookConfigurations, err := df.k8sClient.ClientSet().AdmissionregistrationV1().ValidatingWebhookConfigurations().List(
context.TODO(),
metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
},
)
if err != nil {
log.Printf("- could not list ValidatingWebhookConfigurations: %v\n", err)
return
}
err = df.createYamlFromList(validatingWebhookConfigurations, filepath.Join(df.directory, validatingWebhookConfigurationsFile))
if err != nil {
log.Printf("- could not write ValidatingWebhookConfigurations to file: %v", err)
}
}
// writeControlPlaneMutatingWebhookConfigurations retrieves and writes information about mutating webhook configurations.
func (df *DataFetcher) writeControlPlaneMutatingWebhookConfigurations() {
log.Println("Getting MutatingWebhookConfigurations.")
mutatingWebhookConfigurations, err := df.k8sClient.ClientSet().AdmissionregistrationV1().MutatingWebhookConfigurations().List(
context.TODO(),
metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
},
)
if err != nil {
log.Printf("- could not list MutatingWebhookConfigurations: %v\n", err)
return
}
err = df.createYamlFromList(mutatingWebhookConfigurations, filepath.Join(df.directory, mutatingWebhookConfigurationsFile))
if err != nil {
log.Printf("- could not write MutatingWebhookConfigurations to file: %v", err)
}
}
// writeControlPlaneClusterRoles retrieves and writes information about cluster roles.
func (df *DataFetcher) writeControlPlaneClusterRoles() {
log.Println("Getting ClusterRoles.")
clusterRoles, err := df.k8sClient.ClientSet().RbacV1().ClusterRoles().List(context.TODO(), metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
})
if err != nil {
log.Printf("- could not list ClusterRoles: %v\n", err)
return
}
err = df.createYamlFromList(clusterRoles, filepath.Join(df.directory, clusterRolesFile))
if err != nil {
log.Printf("- could not write ClusterRoles to file: %v", err)
}
}
// writeControlPlaneClusterRoleBindings retrieves and writes information about cluster role bindings.
func (df *DataFetcher) writeControlPlaneClusterRoleBindings() {
log.Println("Getting ClusterRoleBindings.")
clusterRoleBindings, err := df.k8sClient.ClientSet().RbacV1().ClusterRoleBindings().List(context.TODO(), metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
})
if err != nil {
log.Printf("- could not list ClusterRoleBindings: %v\n", err)
return
}
err = df.createYamlFromList(clusterRoleBindings, filepath.Join(df.directory, clusterRoleBindingsFile))
if err != nil {
log.Printf("- could not write ClusterRoleBindings to file: %v", err)
}
}
// writeControlPlaneCRDs retrieves and writes information about control plane custom resource definitions (CRDs).
func (df *DataFetcher) writeControlPlaneCRDs() {
log.Println("Getting control plane CRDs.")
crds, err := df.k8sClient.APIExtensionClientSet().ApiextensionsV1().CustomResourceDefinitions().List(context.TODO(), metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
})
if err != nil {
log.Printf("- could not list CRDs: %v\n", err)
return
}
err = df.createYamlFromList(crds, filepath.Join(df.directory, crdsFile))
if err != nil {
log.Printf("- could not write CRDs to file: %v", err)
}
}
// writeControlPlaneAPIServices retrieves and writes information about control plane api services.
func (df *DataFetcher) writeControlPlaneAPIServices() {
log.Println("Getting control plane APIServices.")
apiServices, err := df.k8sClient.APIRegistrationClientSet().ApiregistrationV1().APIServices().List(context.TODO(), metav1.ListOptions{
LabelSelector: meshLabelSelectorKey + "=" + meshLabelSelectorValue,
})
if err != nil {
log.Printf("- could not list APIServices: %v\n", err)
return
}
err = df.createYamlFromList(apiServices, filepath.Join(df.directory, apiServicesFile))
if err != nil {
log.Printf("- could not write APIServices to file: %v", err)
}
}
// writeMeshConfig retrieves and writes the mesh config.
func (df *DataFetcher) writeMeshConfig() {
log.Println("Getting mesh configuration.")
meshCfg, err := df.k8sClient.ClientSet().CoreV1().
ConfigMaps(df.meshNamespace).Get(context.TODO(), mesh.MeshConfigMap, metav1.GetOptions{})
if err != nil {
log.Printf("- could not get ConfigMap '%s/%s': %v", df.meshNamespace, mesh.MeshConfigMap, err)
return
}
var buf bytes.Buffer
if err := json.Indent(&buf, meshCfg.BinaryData[mesh.MeshConfigFileName], "", "\t"); err != nil {
log.Printf("- could not format mesh configuration: %v", err)
return
}
if err := df.writer.Write(filepath.Join(df.directory, mesh.MeshConfigFileName), buf.String()); err != nil {
log.Printf("- could not write mesh configuration to file: %v", err)
}
}
// writeDeployConfig retrieves and writes the deployment config.
func (df *DataFetcher) writeDeployConfig() {
log.Println("Getting deployment configuration.")
values, _, err := helm.GetDeployValues(df.k8sClient, "nginx-service-mesh")
if err != nil {
log.Printf("- could not get deployment configuration: %v", err)
return
}
// redact registry credentials if set
redacted := "REDACTED"
if values.Registry.Key != "" {
values.Registry.Key = redacted
}
if values.Registry.Username != "" {
values.Registry.Username = redacted
}
if values.Registry.Password != "" {
values.Registry.Password = redacted
}
cfg, err := json.MarshalIndent(values, "", "\t")
if err != nil {
log.Printf("- could not format deployment configuration: %v", err)
return
}
if err := df.writer.Write(filepath.Join(df.directory, "deploy-config.json"), string(cfg)); err != nil {
log.Printf("- could not write deployment configuration to file: %v", err)
}
}
// writeK8sMetrics writes the CPU and memory usage info for the control plane components.
func (df *DataFetcher) writeK8sMetrics() {
log.Println("Getting control plane CPU and memory.")
pods, err := df.k8sClient.MetricsClientSet().MetricsV1beta1().PodMetricses(df.meshNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not get control plane Pod metrics: %v", err)
return
}
var str string
for _, pod := range pods.Items {
str += fmt.Sprintf("Pod: %s\n", pod.Name)
for _, container := range pod.Containers {
cpu := container.Usage[v1.ResourceCPU]
memory := container.Usage[v1.ResourceMemory]
scaledMem := memory.ScaledValue(resource.Mega)
str += fmt.Sprintf("- Container: %s; CPU: %v; Memory: %vM\n", container.Name, cpu.AsDec(), scaledMem)
}
}
if err := df.writer.Write(filepath.Join(df.controlPlaneDir, "metrics.txt"), str); err != nil {
log.Printf("- could not write metrics to file: %v", err)
}
}
// writeSidecarInformation retrieves and writes information about the sidecar resources.
func (df *DataFetcher) writeSidecarInformation() {
namespaces, err := df.k8sClient.ClientSet().CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Namespaces: %v\n", err)
return
}
for _, namespace := range namespaces.Items {
// skip ignored namespaces
if ok := mesh.IgnoredNamespaces[namespace.Name]; ok || namespace.Name == df.meshNamespace {
continue
}
namespaceDir := filepath.Join(df.directory, namespace.Name)
if err := df.writer.MkdirAll(namespaceDir); err != nil {
log.Printf("- could not create proper directory structure: %v", err)
continue
}
pods, err := df.k8sClient.ClientSet().CoreV1().Pods(namespace.Name).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list Pods: %v\n", err)
continue
}
for iter, pod := range pods.Items {
// only process this pod if it contains the sidecar
if !isInjected(&pods.Items[iter]) {
continue
}
// create directory for Pod
podDir := filepath.Join(namespaceDir, pod.Name)
if err := df.writer.Mkdir(podDir); err != nil {
log.Printf("- could not create Pod directory: %v\n", err)
continue
}
// logs
containers := map[string]struct{}{
mesh.MeshSidecar: {},
mesh.MeshSidecarInit: {},
}
df.writeContainerLogs(&pods.Items[iter], namespaceDir, containers)
// pod yaml
podYaml, err := yaml.Marshal(pod)
if err != nil {
log.Printf("- could not marshal yaml for Pod '%s/%s': %v\n", namespace.Name, pod.Name, err)
continue
}
if err := df.writer.Write(filepath.Join(podDir, podYamlFile), string(podYaml)); err != nil {
log.Printf("- could not write yaml for Pod '%s/%s': %v\n", namespace.Name, pod.Name, err)
}
}
}
}
// writeContainerLogs retrieves and writes the current and previous logs for all containers in a pod.
// If containerNames are specified, only get logs for those containers.
func (df *DataFetcher) writeContainerLogs(pod *v1.Pod, directory string, containerNames map[string]struct{}) {
containers := pod.Spec.Containers
containers = append(containers, pod.Spec.InitContainers...)
for _, container := range containers {
if _, ok := containerNames[container.Name]; containerNames != nil && !ok {
continue
}
current, previous := getContainerLogs(df.k8sClient.ClientSet(), pod.Namespace, pod.Name, container.Name)
writeLogsFile := func(filename string, contents io.ReadCloser) {
if contents != nil {
if err := df.writer.WriteFromReader(filename, contents); err != nil {
log.Printf("- could not write log file for container '%s' in Pod '%s/%s': %v",
container.Name, pod.Namespace, pod.Name, err)
}
}
}
writeLogsFile(filepath.Join(directory, pod.Name, fmt.Sprintf("%s-logs.txt", container.Name)), current)
writeLogsFile(filepath.Join(directory, pod.Name, fmt.Sprintf("%s-previous-logs.txt", container.Name)), previous)
}
}
var policies = []struct {
file string
resource schema.GroupVersionResource
}{
{
file: trafficSplitsFile,
resource: schema.GroupVersionResource{
Group: split.GroupVersion.Group,
Version: split.GroupVersion.Version,
Resource: "trafficsplits",
},
},
{
file: trafficTargetsFile,
resource: schema.GroupVersionResource{
Group: access.GroupVersion.Group,
Version: access.GroupVersion.Version,
Resource: "traffictargets",
},
},
{
file: httpRouteGroupsFile,
resource: schema.GroupVersionResource{
Group: specs.GroupVersion.Group,
Version: specs.GroupVersion.Version,
Resource: "httproutegroups",
},
},
{
file: tcpRoutesFile,
resource: schema.GroupVersionResource{
Group: specs.GroupVersion.Group,
Version: specs.GroupVersion.Version,
Resource: "tcproutes",
},
},
{
file: rateLimitsFile,
resource: schema.GroupVersionResource{
Group: nsmspecsv1alpha2.SchemeGroupVersion.Group,
Version: nsmspecsv1alpha2.SchemeGroupVersion.Version,
Resource: "ratelimits",
},
},
{
file: circuitBreakersFile,
resource: schema.GroupVersionResource{
Group: nsmspecsv1alpha1.SchemeGroupVersion.Group,
Version: nsmspecsv1alpha1.SchemeGroupVersion.Version,
Resource: "circuitbreakers",
},
},
}
// writeTrafficPolicies calls individual functions to write out TrafficSplits, TrafficTargets, etc.
func (df *DataFetcher) writeTrafficPolicies() {
for _, policy := range policies {
log.Printf("Getting %s\n", policy.resource.Resource)
list, err := df.k8sClient.DynamicClientSet().Resource(policy.resource).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("- could not list %s: %v\n", policy.resource.Resource, err)
return
}
err = df.createYamlFromList(list, filepath.Join(df.directory, policy.file))
if err != nil {
log.Printf("- could not write %s to file: %v", policy.resource.Resource, err)
}
}
}
// createYamlFromList takes in a list of k8s objects and outputs a single yaml with all them.
func (df *DataFetcher) createYamlFromList(list runtime.Object, file string) error {
listItems, err := meta.ExtractList(list)
if err != nil {
return err
}
for _, item := range listItems {
itemYaml, err := yaml.Marshal(item)
if err != nil {
return err
}
itemMeta, err := meta.Accessor(item)
if err != nil {
return err
}
if err = df.writer.Write(file, withHeader(itemMeta.GetName(), string(itemYaml))); err != nil {
return err
}
}
return nil
}
// getContainerLogs returns the current and previous logs for a container.
func getContainerLogs(
clientset kubernetes.Interface,
namespace,
name,
container string,
) (io.ReadCloser, io.ReadCloser) {
var current, previous io.ReadCloser
getLogs := func(previous bool) io.ReadCloser {
req := clientset.CoreV1().Pods(namespace).GetLogs(name, &v1.PodLogOptions{Previous: previous, Container: container})
logs, streamErr := req.Stream(context.TODO())
if streamErr != nil {
log.Printf("- could not read log stream: %v", streamErr)
return nil
}
return logs
}
// current logs
log.Printf("Getting logs for container '%s' in Pod '%s/%s'.\n", container, namespace, name)
current = getLogs(false)
// previous logs
log.Printf("Getting previous logs for container '%s' in Pod '%s/%s'.\n", container, namespace, name)
previous = getLogs(true)
return current, previous
}
// writeReadme writes a basic README describing the contents of the support package.
func (df *DataFetcher) writeReadme() {
readme := `
# Contents of support package
## {{.Namespace}}/
Directory containing NGINX Service Mesh control plane information.
- {{.Namespace}}/
- \<pod-name\>/ - Directory containing Pod-specific information.
- \<container-name\>-logs.txt: Logs for the container.
- \<container-name\>-previous-logs.txt: Previous logs for the container (if applicable).
- desc.txt: Description of the Pod.
- pod.yaml: Configuration of the Pod.
- configmaps.yaml: All the ConfigMap configurations in the {{.Namespace}} namespace.
- daemonsets.yaml: All the DaemonSet configurations in the {{.Namespace}} namespace.
- deployments.yaml: All the Deployment configurations in the {{.Namespace}} namespace.
- events.txt: All the Event configurations in the {{.Namespace}} namespace.
- metrics.txt: CPU and memory usage of each Pod.
- pods.txt: Output of "kubectl -n {{.Namespace}} get pods -o wide".
- secrets.yaml: All the Secret configurations in the {{.Namespace}} namespace.
- serviceaccounts.yaml: All the ServiceAccount configurations in the {{.Namespace}} namespace.
- services.yaml: All the Service configurations in the {{.Namespace}} namespace.
- statefulsets.yaml: All the StatefulSet configurations in the {{.Namespace}} namespace.
{{- if .CollectSidecarLogs }}
## \<user-namespace\>/
Directory containing sidecar information.
- \<user-namespace\>/
- \<pod-name\>/ - Directory containing Pod-specific information.
- nginx-mesh-init-logs.txt: Logs of the nginx-mesh-init container.
- nginx-mesh-sidecar-logs.txt: Logs of the nginx-mesh-sidecar container.
- nginx-mesh-init-previous-logs.txt: Previous logs of the nginx-mesh-init container (if applicable).
- nginx-mesh-sidecar-previous-logs.txt: Previous logs of the nginx-mesh-sidecar container (if applicable).
- pod.yaml: Configuration of the Pod.
{{- end }}
## Configuration files
- apiservices.yaml: All the NGINX Service Mesh APIService configurations.
- circuitbreakers.yaml: All the CircuitBreaker configurations.
- clusterrolebindings.yaml: All the NGINX Service Mesh ClusterRoleBinding configurations.
- clusterroles.yaml: All the NGINX Service Mesh ClusterRole configurations.
- crds.yaml: All the NGINX Service Mesh Custom Resource Definition (CRD) configurations.
- deploy-config.json: Deploy-time configuration of NGINX Service Mesh.
- httproutegroups.yaml: All the HTTPRouteGroup configurations.
- mesh-config.json: Output of "nginx-meshctl config".
- mutatingwebhookconfigurations.yaml: All the NGINX Service Mesh MutatingWebhookConfiguration configurations.
- ratelimits.yaml: All the RateLimit configurations.
- supportpkg-creation-logs.txt: Logs that occurred while the support package was being created.
- tcproutes.yaml: All the TCPRoute configurations.
- trafficsplits.yaml: All the TrafficSplit configurations.
- traffictargets.yaml: All the TrafficTarget configurations.
- validatingwebhookconfigurations.yaml: All the NGINX Service Mesh ValidatingWebhookConfiguration configurations.
- version.txt: Output of "nginx-meshctl version".
`
data := struct {
Namespace string
CollectSidecarLogs bool
}{
Namespace: df.meshNamespace,
CollectSidecarLogs: df.collectSidecarLogs,
}
tmpl, err := template.New("readme").Parse(readme)
if err != nil {
log.Printf("- could not parse README template: %v\n", err)
return
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
log.Printf("- could not render README: %v\n", err)
return
}
if err := df.writer.Write(filepath.Join(df.directory, "README.md"), buf.String()); err != nil {
log.Printf("- could not write README: %v\n", err)
}
}
// isInjected checks if a pod is injected.
func isInjected(pod *v1.Pod) bool {
val, ok := pod.Annotations[mesh.InjectedAnnotation]
return ok && strings.ToLower(val) == mesh.Injected
}
// withHeader adds a banner to separate multiple yamls in the same file.
func withHeader(name, yamlString string) string {
return fmt.Sprintf("---\n"+
"##################################################################################################\n"+
"# %s\n"+
"##################################################################################################\n"+
"%s", name, yamlString)
}