-
Notifications
You must be signed in to change notification settings - Fork 3
/
troubleshoot.go
120 lines (102 loc) · 2.77 KB
/
troubleshoot.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
package kots
import (
"context"
_ "embed"
"fmt"
"path"
"strings"
troubleshootscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
"github.com/replicatedhq/troubleshoot/pkg/constants"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var decoder runtime.Decoder
func init() {
_ = v1.AddToScheme(troubleshootscheme.Scheme) // for secrets and configmaps
decoder = troubleshootscheme.Codecs.UniversalDeserializer()
}
func GetEmbeddedTroubleshootSpecs(ctx context.Context, specsFiles SpecFiles) SpecFiles {
tsSpecs := SpecFiles{}
for _, specFile := range specsFiles {
troubleshootSpecs := findTroubleshootSpecs(ctx, specFile.Content)
for _, tsSpec := range troubleshootSpecs {
tsSpecs = append(tsSpecs, SpecFile{
Name: path.Join(specFile.Name, tsSpec.Name),
Path: specFile.Name,
Content: tsSpec.Content,
AllowDuplicates: tsSpec.AllowDuplicates,
})
}
}
return tsSpecs
}
// Extract troubleshoot specs from ConfigMap and Secret specs
func findTroubleshootSpecs(ctx context.Context, fileData string) SpecFiles {
tsSpecs := SpecFiles{}
srcDocs := strings.Split(fileData, "\n---\n")
for _, srcDoc := range srcDocs {
obj, _, err := decoder.Decode([]byte(srcDoc), nil, nil)
if err != nil {
log.Debugf("failed to decode raw spec: %s", srcDoc)
continue
}
switch v := obj.(type) {
case *v1.ConfigMap:
specs := getSpecFromConfigMap(v, fmt.Sprintf("%s-", v.Name))
tsSpecs = append(tsSpecs, specs...)
case *v1.Secret:
specs := getSpecFromSecret(v, fmt.Sprintf("%s-", v.Name))
tsSpecs = append(tsSpecs, specs...)
}
}
return tsSpecs
}
func getSpecFromConfigMap(cm *v1.ConfigMap, namePrefix string) SpecFiles {
possibleKeys := []string{
constants.SupportBundleKey,
constants.RedactorKey,
constants.PreflightKey,
constants.PreflightKey2,
}
specs := SpecFiles{}
for _, key := range possibleKeys {
str, ok := cm.Data[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: str,
AllowDuplicates: true,
})
}
}
return specs
}
func getSpecFromSecret(secret *v1.Secret, namePrefix string) SpecFiles {
possibleKeys := []string{
constants.SupportBundleKey,
constants.RedactorKey,
constants.PreflightKey,
constants.PreflightKey2,
}
specs := SpecFiles{}
for _, key := range possibleKeys {
data, ok := secret.Data[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: string(data),
AllowDuplicates: true,
})
}
str, ok := secret.StringData[key]
if ok {
specs = append(specs, SpecFile{
Name: namePrefix + key,
Content: str,
AllowDuplicates: true,
})
}
}
return specs
}