-
Notifications
You must be signed in to change notification settings - Fork 93
/
configmap.go
97 lines (79 loc) · 2.39 KB
/
configmap.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
package analyzer
import (
"encoding/json"
"fmt"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
)
type AnalyzeConfigMap struct {
analyzer *troubleshootv1beta2.AnalyzeConfigMap
}
func (a *AnalyzeConfigMap) Title() string {
title := a.analyzer.CheckName
if title == "" {
title = fmt.Sprintf("ConfigMap %s", a.analyzer.ConfigMapName)
}
return title
}
func (a *AnalyzeConfigMap) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeConfigMap) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
result, err := a.analyzeConfigMap(a.analyzer, getFile)
if err != nil {
return nil, err
}
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
}
func (a *AnalyzeConfigMap) analyzeConfigMap(analyzer *troubleshootv1beta2.AnalyzeConfigMap, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
filename := collect.GetConfigMapFileName(
&troubleshootv1beta2.ConfigMap{
Namespace: analyzer.Namespace,
Name: analyzer.ConfigMapName,
Key: analyzer.Key,
},
analyzer.ConfigMapName,
)
configMapData, err := getCollectedFileContents(filename)
if err != nil {
return nil, err
}
var foundConfigMap collect.ConfigMapOutput
if err := json.Unmarshal(configMapData, &foundConfigMap); err != nil {
return nil, err
}
result := AnalyzeResult{
Title: a.Title(),
IconKey: "kubernetes_analyze_secret", // TODO: icon
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
}
var failOutcome *troubleshootv1beta2.Outcome
for _, outcome := range analyzer.Outcomes {
if outcome.Fail != nil {
failOutcome = outcome
}
}
if !foundConfigMap.ConfigMapExists {
result.IsFail = true
result.Message = failOutcome.Fail.Message
result.URI = failOutcome.Fail.URI
return &result, nil
}
if analyzer.Key != "" {
if foundConfigMap.Key != analyzer.Key || !foundConfigMap.KeyExists {
result.IsFail = true
result.Message = failOutcome.Fail.Message
result.URI = failOutcome.Fail.URI
return &result, nil
}
}
result.IsPass = true
for _, outcome := range analyzer.Outcomes {
if outcome.Pass != nil {
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI
}
}
return &result, nil
}