Skip to content

Commit 39350b5

Browse files
authored
ConfigMap collector and secrets can be collected by selectors (#384)
* ConfigMap collector and secrets can be collected by selectors * follow docs * Pass context and kubernetes client to collectors * collect tests * analyze tests * fix tests * improvements
1 parent d7b6aa2 commit 39350b5

File tree

18 files changed

+1433
-72
lines changed

18 files changed

+1433
-72
lines changed

pkg/analyze/analyzer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
145145
}
146146
return []*AnalyzeResult{result}, nil
147147
}
148+
if analyzer.ConfigMap != nil {
149+
isExcluded, err := isExcluded(analyzer.ConfigMap.Exclude)
150+
if err != nil {
151+
return nil, err
152+
}
153+
if isExcluded {
154+
return nil, nil
155+
}
156+
result, err := analyzeConfigMap(analyzer.ConfigMap, getFile)
157+
if err != nil {
158+
return nil, err
159+
}
160+
return []*AnalyzeResult{result}, nil
161+
}
148162
if analyzer.ImagePullSecret != nil {
149163
isExcluded, err := isExcluded(analyzer.ImagePullSecret.Exclude)
150164
if err != nil {

pkg/analyze/configmap.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package analyzer
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
8+
"github.com/replicatedhq/troubleshoot/pkg/collect"
9+
)
10+
11+
func analyzeConfigMap(analyzer *troubleshootv1beta2.AnalyzeConfigMap, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
12+
filename := collect.GetConfigMapFileName(
13+
&troubleshootv1beta2.ConfigMap{
14+
Namespace: analyzer.Namespace,
15+
Name: analyzer.ConfigMapName,
16+
Key: analyzer.Key,
17+
},
18+
analyzer.ConfigMapName,
19+
)
20+
21+
configMapData, err := getCollectedFileContents(filename)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
var foundConfigMap collect.ConfigMapOutput
27+
if err := json.Unmarshal(configMapData, &foundConfigMap); err != nil {
28+
return nil, err
29+
}
30+
31+
title := analyzer.CheckName
32+
if title == "" {
33+
title = fmt.Sprintf("ConfigMap %s", analyzer.ConfigMapName)
34+
}
35+
36+
result := AnalyzeResult{
37+
Title: title,
38+
IconKey: "kubernetes_analyze_secret", // TODO: icon
39+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
40+
}
41+
42+
var failOutcome *troubleshootv1beta2.Outcome
43+
for _, outcome := range analyzer.Outcomes {
44+
if outcome.Fail != nil {
45+
failOutcome = outcome
46+
}
47+
}
48+
49+
if !foundConfigMap.ConfigMapExists {
50+
result.IsFail = true
51+
result.Message = failOutcome.Fail.Message
52+
result.URI = failOutcome.Fail.URI
53+
54+
return &result, nil
55+
}
56+
57+
if analyzer.Key != "" {
58+
if foundConfigMap.Key != analyzer.Key || !foundConfigMap.KeyExists {
59+
result.IsFail = true
60+
result.Message = failOutcome.Fail.Message
61+
result.URI = failOutcome.Fail.URI
62+
63+
return &result, nil
64+
}
65+
}
66+
67+
result.IsPass = true
68+
for _, outcome := range analyzer.Outcomes {
69+
if outcome.Pass != nil {
70+
result.Message = outcome.Pass.Message
71+
result.URI = outcome.Pass.URI
72+
}
73+
}
74+
75+
return &result, nil
76+
}

pkg/analyze/configmap_test.go

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package analyzer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pkg/errors"
7+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
8+
"github.com/replicatedhq/troubleshoot/pkg/collect"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func Test_analyzeConfigMap(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
analyzer *troubleshootv1beta2.AnalyzeConfigMap
17+
mockFiles map[string][]byte
18+
want *AnalyzeResult
19+
wantErr bool
20+
}{
21+
{
22+
name: "found",
23+
analyzer: &troubleshootv1beta2.AnalyzeConfigMap{
24+
Namespace: "test-namespace",
25+
ConfigMapName: "test-configmap",
26+
Outcomes: []*troubleshootv1beta2.Outcome{
27+
{
28+
Fail: &troubleshootv1beta2.SingleOutcome{
29+
Message: "Not found",
30+
},
31+
},
32+
{
33+
Pass: &troubleshootv1beta2.SingleOutcome{
34+
Message: "Found",
35+
},
36+
},
37+
},
38+
},
39+
mockFiles: map[string][]byte{
40+
"configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{
41+
Namespace: "test-namespace",
42+
Name: "test-configmap",
43+
ConfigMapExists: true,
44+
}),
45+
},
46+
want: &AnalyzeResult{
47+
IsPass: true,
48+
Message: "Found",
49+
Title: "ConfigMap test-configmap",
50+
IconKey: "kubernetes_analyze_secret",
51+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
52+
},
53+
},
54+
{
55+
name: "not found",
56+
analyzer: &troubleshootv1beta2.AnalyzeConfigMap{
57+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
58+
CheckName: "test configmap analyzer",
59+
},
60+
Namespace: "test-namespace",
61+
ConfigMapName: "test-configmap",
62+
Outcomes: []*troubleshootv1beta2.Outcome{
63+
{
64+
Fail: &troubleshootv1beta2.SingleOutcome{
65+
Message: "Not found",
66+
},
67+
},
68+
{
69+
Pass: &troubleshootv1beta2.SingleOutcome{
70+
Message: "Found",
71+
},
72+
},
73+
},
74+
},
75+
mockFiles: map[string][]byte{
76+
"configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{
77+
Namespace: "test-namespace",
78+
Name: "test-configmap",
79+
ConfigMapExists: false,
80+
}),
81+
},
82+
want: &AnalyzeResult{
83+
IsFail: true,
84+
Message: "Not found",
85+
Title: "test configmap analyzer",
86+
IconKey: "kubernetes_analyze_secret",
87+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
88+
},
89+
},
90+
{
91+
name: "key found",
92+
analyzer: &troubleshootv1beta2.AnalyzeConfigMap{
93+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
94+
CheckName: "test configmap analyzer",
95+
},
96+
Namespace: "test-namespace",
97+
ConfigMapName: "test-configmap",
98+
Key: "test-key",
99+
Outcomes: []*troubleshootv1beta2.Outcome{
100+
{
101+
Fail: &troubleshootv1beta2.SingleOutcome{
102+
Message: "Key not found",
103+
},
104+
},
105+
{
106+
Pass: &troubleshootv1beta2.SingleOutcome{
107+
Message: "Key found",
108+
},
109+
},
110+
},
111+
},
112+
mockFiles: map[string][]byte{
113+
"configmaps/test-namespace/test-configmap/test-key.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{
114+
Namespace: "test-namespace",
115+
Name: "test-configmap",
116+
Key: "test-key",
117+
ConfigMapExists: true,
118+
KeyExists: true,
119+
}),
120+
},
121+
want: &AnalyzeResult{
122+
IsPass: true,
123+
Message: "Key found",
124+
Title: "test configmap analyzer",
125+
IconKey: "kubernetes_analyze_secret",
126+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
127+
},
128+
},
129+
{
130+
name: "key not found",
131+
analyzer: &troubleshootv1beta2.AnalyzeConfigMap{
132+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
133+
CheckName: "test configmap analyzer",
134+
},
135+
Namespace: "test-namespace",
136+
ConfigMapName: "test-configmap",
137+
Key: "test-key",
138+
Outcomes: []*troubleshootv1beta2.Outcome{
139+
{
140+
Fail: &troubleshootv1beta2.SingleOutcome{
141+
Message: "Key not found",
142+
},
143+
},
144+
{
145+
Pass: &troubleshootv1beta2.SingleOutcome{
146+
Message: "Key found",
147+
},
148+
},
149+
},
150+
},
151+
mockFiles: map[string][]byte{
152+
"configmaps/test-namespace/test-configmap/test-key.json": mustJSONMarshalIndent(t, collect.ConfigMapOutput{
153+
Namespace: "test-namespace",
154+
Name: "test-configmap",
155+
Key: "test-key",
156+
ConfigMapExists: true,
157+
KeyExists: false,
158+
}),
159+
},
160+
want: &AnalyzeResult{
161+
IsFail: true,
162+
Message: "Key not found",
163+
Title: "test configmap analyzer",
164+
IconKey: "kubernetes_analyze_secret",
165+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
166+
},
167+
},
168+
{
169+
name: "key not found configmap not found",
170+
analyzer: &troubleshootv1beta2.AnalyzeConfigMap{
171+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
172+
CheckName: "test configmap analyzer",
173+
},
174+
Namespace: "test-namespace",
175+
ConfigMapName: "test-configmap",
176+
Key: "test-key",
177+
Outcomes: []*troubleshootv1beta2.Outcome{
178+
{
179+
Fail: &troubleshootv1beta2.SingleOutcome{
180+
Message: "Key not found",
181+
},
182+
},
183+
{
184+
Pass: &troubleshootv1beta2.SingleOutcome{
185+
Message: "Key found",
186+
},
187+
},
188+
},
189+
},
190+
wantErr: true, // TODO: should this be a not found error? This will not work with selectors.
191+
},
192+
}
193+
for _, tt := range tests {
194+
t.Run(tt.name, func(t *testing.T) {
195+
getCollectedFileContents := func(fileName string) ([]byte, error) {
196+
contents, ok := tt.mockFiles[fileName]
197+
if !ok {
198+
return nil, errors.Errorf("file %s was not collected", fileName)
199+
}
200+
return contents, nil
201+
}
202+
got, err := analyzeConfigMap(tt.analyzer, getCollectedFileContents)
203+
if tt.wantErr {
204+
assert.Error(t, err)
205+
} else {
206+
require.NoError(t, err)
207+
assert.Equal(t, tt.want, got)
208+
}
209+
})
210+
}
211+
}

pkg/analyze/secret.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ import (
99
)
1010

1111
func analyzeSecret(analyzer *troubleshootv1beta2.AnalyzeSecret, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
12-
secretData, err := getCollectedFileContents(fmt.Sprintf("secrets/%s/%s.json", analyzer.Namespace, analyzer.SecretName))
12+
filename := collect.GetSecretFileName(
13+
&troubleshootv1beta2.Secret{
14+
Namespace: analyzer.Namespace,
15+
Name: analyzer.SecretName,
16+
Key: analyzer.Key,
17+
},
18+
analyzer.SecretName,
19+
)
20+
21+
secretData, err := getCollectedFileContents(filename)
1322
if err != nil {
1423
return nil, err
1524
}
1625

17-
var foundSecret collect.FoundSecret
26+
var foundSecret collect.SecretOutput
1827
if err := json.Unmarshal(secretData, &foundSecret); err != nil {
1928
return nil, err
2029
}

0 commit comments

Comments
 (0)