-
Notifications
You must be signed in to change notification settings - Fork 276
/
diagnostic_bundle.go
406 lines (358 loc) Β· 13.5 KB
/
diagnostic_bundle.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
package diagnostics
import (
"context"
_ "embed"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/executables"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
)
//go:embed config/diagnostic-collector-rbac.yaml
var diagnosticCollectorRbac []byte
const (
troubleshootApiVersion = "troubleshoot.sh/v1beta2"
generatedBundleNameFormat = "%s-%s-bundle.yaml"
generatedAnalysisNameFormat = "%s-%s-analysis.yaml"
maxRetries = 5
backOffPeriod = 5 * time.Second
defaultClusterName = "eksa-cluster"
)
type EksaDiagnosticBundle struct {
bundle *supportBundle
bundlePath string
client BundleClient
collectorFactory CollectorFactory
clusterSpec *cluster.Spec
analyzerFactory AnalyzerFactory
kubeconfig string
kubectl *executables.Kubectl
retrier *retrier.Retrier
writer filewriter.FileWriter
analysis []*executables.SupportBundleAnalysis
}
func newDiagnosticBundleManagementCluster(af AnalyzerFactory, cf CollectorFactory, spec *cluster.Spec, client BundleClient,
kubectl *executables.Kubectl, kubeconfig string, writer filewriter.FileWriter,
) (*EksaDiagnosticBundle, error) {
b := &EksaDiagnosticBundle{
bundle: &supportBundle{
TypeMeta: metav1.TypeMeta{
Kind: "SupportBundle",
APIVersion: troubleshootApiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: "bootstrap-cluster",
},
Spec: supportBundleSpec{},
},
analyzerFactory: af,
collectorFactory: cf,
client: client,
kubectl: kubectl,
kubeconfig: kubeconfig,
retrier: retrier.NewWithMaxRetries(maxRetries, backOffPeriod),
writer: writer,
}
b.WithDefaultCollectors().
WithFileCollectors([]string{logger.GetOutputFilePath()}).
WithDefaultAnalyzers().
WithManagementCluster(true).
WithDatacenterConfig(spec.Cluster.Spec.DatacenterRef, spec).
WithLogTextAnalyzers()
err := b.WriteBundleConfig()
if err != nil {
return nil, fmt.Errorf("writing bundle config: %v", err)
}
return b, nil
}
func newDiagnosticBundleFromSpec(af AnalyzerFactory, cf CollectorFactory, spec *cluster.Spec, provider providers.Provider,
client BundleClient, kubectl *executables.Kubectl, kubeconfig string, writer filewriter.FileWriter,
) (*EksaDiagnosticBundle, error) {
b := &EksaDiagnosticBundle{
bundle: &supportBundle{
TypeMeta: metav1.TypeMeta{
Kind: "SupportBundle",
APIVersion: troubleshootApiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: spec.Cluster.Name,
},
Spec: supportBundleSpec{},
},
analyzerFactory: af,
collectorFactory: cf,
client: client,
clusterSpec: spec,
kubeconfig: kubeconfig,
kubectl: kubectl,
retrier: retrier.NewWithMaxRetries(maxRetries, backOffPeriod),
writer: writer,
}
b = b.
WithGitOpsConfig(spec.GitOpsConfig).
WithOidcConfig(spec.OIDCConfig).
WithExternalEtcd(spec.Cluster.Spec.ExternalEtcdConfiguration).
WithDatacenterConfig(spec.Cluster.Spec.DatacenterRef, spec).
WithMachineConfigs(provider.MachineConfigs(spec)).
WithManagementCluster(spec.Cluster.IsSelfManaged()).
WithDefaultAnalyzers().
WithDefaultCollectors().
WithFileCollectors([]string{logger.GetOutputFilePath()}).
WithPackagesCollectors().
WithLogTextAnalyzers()
err := b.WriteBundleConfig()
if err != nil {
return nil, fmt.Errorf("writing bundle config: %v", err)
}
return b, nil
}
func newDiagnosticBundleDefault(af AnalyzerFactory, cf CollectorFactory) *EksaDiagnosticBundle {
b := &EksaDiagnosticBundle{
bundle: &supportBundle{
TypeMeta: metav1.TypeMeta{
Kind: "SupportBundle",
APIVersion: troubleshootApiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
Spec: supportBundleSpec{},
},
analyzerFactory: af,
collectorFactory: cf,
}
return b.WithDefaultAnalyzers().
WithDefaultCollectors().
WithManagementCluster(true)
}
func newDiagnosticBundleCustom(af AnalyzerFactory, cf CollectorFactory, client BundleClient, kubectl *executables.Kubectl, bundlePath string, kubeconfig string, writer filewriter.FileWriter) *EksaDiagnosticBundle {
return &EksaDiagnosticBundle{
bundlePath: bundlePath,
analyzerFactory: af,
collectorFactory: cf,
client: client,
kubeconfig: kubeconfig,
kubectl: kubectl,
retrier: retrier.NewWithMaxRetries(maxRetries, backOffPeriod),
writer: writer,
}
}
func (e *EksaDiagnosticBundle) CollectAndAnalyze(ctx context.Context, sinceTimeValue *time.Time) error {
e.createDiagnosticNamespaceAndRoles(ctx)
logger.Info("β³ Collecting support bundle from cluster, this can take a while", "cluster", e.clusterName(), "bundle", e.bundlePath, "since", sinceTimeValue, "kubeconfig", e.kubeconfig)
archivePath, err := e.client.Collect(ctx, e.bundlePath, sinceTimeValue, e.kubeconfig)
if err != nil {
return fmt.Errorf("failed to Collect support bundle: %v", err)
}
logger.Info("Support bundle archive created", "path", archivePath)
logger.Info("Analyzing support bundle", "bundle", e.bundlePath, "archive", archivePath)
analysis, err := e.client.Analyze(ctx, e.bundlePath, archivePath)
if err != nil {
return fmt.Errorf("analyzing bundle: %v", err)
}
e.analysis = analysis
analysisPath, err := e.WriteAnalysisToFile()
if err != nil {
return err
}
logger.Info("Analysis output generated", "path", analysisPath)
e.deleteDiagnosticNamespaceAndRoles(ctx)
return nil
}
func (e *EksaDiagnosticBundle) PrintBundleConfig() error {
bundleYaml, err := yaml.Marshal(e.bundle)
if err != nil {
return fmt.Errorf("outputting yaml: %v", err)
}
fmt.Println(string(bundleYaml))
return nil
}
func (e *EksaDiagnosticBundle) WriteBundleConfig() error {
bundleYaml, err := yaml.Marshal(e.bundle)
if err != nil {
return fmt.Errorf("outputing yaml: %v", err)
}
timestamp := time.Now().Format(time.RFC3339)
filename := fmt.Sprintf(generatedBundleNameFormat, e.clusterName(), timestamp)
e.bundlePath, err = e.writer.Write(filename, bundleYaml)
if err != nil {
return err
}
logger.V(3).Info("bundle config written", "path", e.bundlePath)
return nil
}
func (e *EksaDiagnosticBundle) PrintAnalysis() error {
if e.analysis == nil {
return nil
}
analysis, err := yaml.Marshal(e.analysis)
if err != nil {
return fmt.Errorf("outputing yaml: %v", err)
}
fmt.Println(string(analysis))
return nil
}
func (e *EksaDiagnosticBundle) WriteAnalysisToFile() (path string, err error) {
if e.analysis == nil {
return "", nil
}
yamlAnalysis, err := yaml.Marshal(e.analysis)
if err != nil {
return "", fmt.Errorf("writing analysis: %v", err)
}
timestamp := time.Now().Format(time.RFC3339)
filename := fmt.Sprintf(generatedAnalysisNameFormat, e.clusterName(), timestamp)
analysisPath, err := e.writer.Write(filename, yamlAnalysis)
if err != nil {
return "", err
}
e.bundlePath = analysisPath
return analysisPath, nil
}
func (e *EksaDiagnosticBundle) WithDefaultCollectors() *EksaDiagnosticBundle {
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.DefaultCollectors()...)
return e
}
func (e *EksaDiagnosticBundle) WithDefaultAnalyzers() *EksaDiagnosticBundle {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.DefaultAnalyzers()...)
return e
}
func (e *EksaDiagnosticBundle) WithManagementCluster(isSelfManaged bool) *EksaDiagnosticBundle {
if isSelfManaged {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.ManagementClusterAnalyzers()...)
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.ManagementClusterCollectors()...)
}
return e
}
// WithFileCollectors appends collectors that collect static data from the specified paths to the bundle.
func (e *EksaDiagnosticBundle) WithFileCollectors(paths []string) *EksaDiagnosticBundle {
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.FileCollectors(paths)...)
return e
}
func (e *EksaDiagnosticBundle) WithPackagesCollectors() *EksaDiagnosticBundle {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.PackageAnalyzers()...)
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.PackagesCollectors()...)
return e
}
func (e *EksaDiagnosticBundle) WithDatacenterConfig(config v1alpha1.Ref, spec *cluster.Spec) *EksaDiagnosticBundle {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.DataCenterConfigAnalyzers(config)...)
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.DataCenterConfigCollectors(config, spec)...)
return e
}
func (e *EksaDiagnosticBundle) WithOidcConfig(config *v1alpha1.OIDCConfig) *EksaDiagnosticBundle {
if config != nil {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.EksaOidcAnalyzers()...)
}
return e
}
func (e *EksaDiagnosticBundle) WithExternalEtcd(config *v1alpha1.ExternalEtcdConfiguration) *EksaDiagnosticBundle {
if config != nil {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.EksaExternalEtcdAnalyzers()...)
}
return e
}
func (e *EksaDiagnosticBundle) WithGitOpsConfig(config *v1alpha1.GitOpsConfig) *EksaDiagnosticBundle {
if config != nil {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.EksaGitopsAnalyzers()...)
}
return e
}
func (e *EksaDiagnosticBundle) WithMachineConfigs(configs []providers.MachineConfig) *EksaDiagnosticBundle {
e.bundle.Spec.Collectors = append(e.bundle.Spec.Collectors, e.collectorFactory.EksaHostCollectors(configs)...)
return e
}
func (e *EksaDiagnosticBundle) WithLogTextAnalyzers() *EksaDiagnosticBundle {
e.bundle.Spec.Analyzers = append(e.bundle.Spec.Analyzers, e.analyzerFactory.EksaLogTextAnalyzers(e.bundle.Spec.Collectors)...)
return e
}
// createDiagnosticNamespace attempts to create the namespace eksa-diagnostics and associated RBAC objects.
// collector pods, for example host log collectors or run command collectors, will be launched in this namespace with the default service account.
// this method intentionally does not return an error
// a cluster in need of diagnosis may be unable to create new API objects and we should not stop our collection/analysis just because the namespace fails to create.
func (e *EksaDiagnosticBundle) createDiagnosticNamespaceAndRoles(ctx context.Context) {
targetCluster := &types.Cluster{
KubeconfigFile: e.kubeconfig,
}
logger.V(1).Info("creating temporary namespace for diagnostic collector", "namespace", constants.EksaDiagnosticsNamespace)
err := e.retrier.Retry(
func() error {
return e.kubectl.CreateNamespace(ctx, e.kubeconfig, constants.EksaDiagnosticsNamespace)
},
)
if err != nil {
logger.Info("WARNING: failed to create eksa-diagnostics namespace. Some collectors may fail to run.", "err", err)
}
logger.V(1).Info("creating temporary ClusterRole and RoleBinding for diagnostic collector")
err = e.retrier.Retry(
func() error {
return e.kubectl.ApplyKubeSpecFromBytes(ctx, targetCluster, diagnosticCollectorRbac)
},
)
if err != nil {
logger.Info("WARNING: failed to create roles for eksa-diagnostic-collector. Some collectors may fail to run.", "err", err)
}
}
func (e *EksaDiagnosticBundle) deleteDiagnosticNamespaceAndRoles(ctx context.Context) {
targetCluster := &types.Cluster{
KubeconfigFile: e.kubeconfig,
}
logger.V(1).Info("cleaning up temporary roles for diagnostic collectors")
err := e.retrier.Retry(
func() error {
return e.kubectl.DeleteKubeSpecFromBytes(ctx, targetCluster, diagnosticCollectorRbac)
},
)
if err != nil {
logger.Info("WARNING: failed to clean up roles for eksa-diagnostics.", "err", err)
}
logger.V(1).Info("cleaning up temporary namespace for diagnostic collectors", "namespace", constants.EksaDiagnosticsNamespace)
err = e.retrier.Retry(
func() error {
return e.kubectl.DeleteNamespace(ctx, e.kubeconfig, constants.EksaDiagnosticsNamespace)
},
)
if err != nil {
logger.Info("WARNING: failed to clean up eksa-diagnostics namespace.", "err", err, "namespace", constants.EksaDiagnosticsNamespace)
}
}
func ParseTimeFromDuration(since string) (*time.Time, error) {
var sinceTimeValue time.Time
duration, err := time.ParseDuration(since)
if err != nil {
return nil, fmt.Errorf("unable to parse since time: %v", err)
}
now := time.Now()
sinceTimeValue = now.Add(0 - duration)
return &sinceTimeValue, nil
}
func ParseTimeOptions(since string, sinceTime string) (*time.Time, error) {
var sinceTimeValue time.Time
var err error
if sinceTime == "" && since == "" {
return &sinceTimeValue, nil // returning an uninitialized (zero) Time value here results in a
// sinceTimeValue of "0001-01-01 00:00:00 +0000 UTC"
// so all pod logs will be collected from the very beginning
} else if sinceTime != "" && since != "" {
return nil, fmt.Errorf("at most one of `sinceTime` or `since` could be specified")
} else if sinceTime != "" {
sinceTimeValue, err = time.Parse(time.RFC3339, sinceTime)
return &sinceTimeValue, err
} else if since != "" {
return ParseTimeFromDuration(since)
}
return nil, nil
}
func (e *EksaDiagnosticBundle) clusterName() string {
if e.bundle != nil {
return e.bundle.Name
}
return defaultClusterName
}