Skip to content

Commit bf7d658

Browse files
author
Kyle Sorensen
authored
troubleshoot enables collecting all data from a configmap (#395)
Enabled collecting all data from a ConfigMap instead of by key
1 parent e2dbb62 commit bf7d658

File tree

4 files changed

+139
-12
lines changed

4 files changed

+139
-12
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: troubleshoot.sh/v1beta2
2+
kind: SupportBundle
3+
metadata:
4+
name: example-collect-all-configmap-data
5+
spec:
6+
collectors:
7+
- configMap:
8+
namespace: kurl
9+
name: kurl-current-config
10+
includeAllData: true
11+
- configMap:
12+
namespace: kurl
13+
name: kurl-last-config
14+
includeAllData: true
15+
16+

pkg/apis/troubleshoot/v1beta2/collector_shared.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ type Secret struct {
3333
}
3434

3535
type ConfigMap struct {
36-
CollectorMeta `json:",inline" yaml:",inline"`
37-
Name string `json:"name,omitempty" yaml:"name,omitempty"`
38-
Selector []string `json:"selector,omitempty" yaml:"selector,omitempty"`
39-
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
40-
Key string `json:"key,omitempty" yaml:"key,omitempty"`
41-
IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"`
36+
CollectorMeta `json:",inline" yaml:",inline"`
37+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
38+
Selector []string `json:"selector,omitempty" yaml:"selector,omitempty"`
39+
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
40+
Key string `json:"key,omitempty" yaml:"key,omitempty"`
41+
IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"`
42+
IncludeAllData bool `json:"includeAllData,omitempty" yaml:"includeAllData,omitempty"`
4243
}
4344

4445
type LogLimits struct {

pkg/collect/configmap.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
)
1717

1818
type ConfigMapOutput struct {
19-
Namespace string `json:"namespace"`
20-
Name string `json:"name"`
21-
Key string `json:"key"`
22-
ConfigMapExists bool `json:"configMapExists"`
23-
KeyExists bool `json:"keyExists"`
24-
Value string `json:"value,omitempty"`
19+
Namespace string `json:"namespace"`
20+
Name string `json:"name"`
21+
Key string `json:"key"`
22+
ConfigMapExists bool `json:"configMapExists"`
23+
KeyExists bool `json:"keyExists"`
24+
Value string `json:"value,omitempty"`
25+
Data map[string]string `json:"data,omitonempty"`
2526
}
2627

2728
func ConfigMap(ctx context.Context, client kubernetes.Interface, configMapCollector *troubleshootv1beta2.ConfigMap) (map[string][]byte, error) {
@@ -81,6 +82,9 @@ func configMapToOutput(configMapCollector *troubleshootv1beta2.ConfigMap, config
8182

8283
if configMap != nil {
8384
foundConfigMap.ConfigMapExists = true
85+
if configMapCollector.IncludeAllData {
86+
foundConfigMap.Data = configMap.Data
87+
}
8488
if configMapCollector.Key != "" {
8589
if val, ok := configMap.Data[configMapCollector.Key]; ok {
8690
foundConfigMap.KeyExists = true

pkg/collect/configmap_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,112 @@ func TestConfigMap(t *testing.T) {
213213
}),
214214
},
215215
},
216+
{
217+
name: "collectAll",
218+
configMapCollector: &troubleshootv1beta2.ConfigMap{
219+
Namespace: "test-namespace",
220+
Name: "test-configmap",
221+
IncludeAllData: true,
222+
},
223+
mockConfigMaps: []corev1.ConfigMap{
224+
{
225+
ObjectMeta: metav1.ObjectMeta{
226+
Name: "test-configmap",
227+
Namespace: "test-namespace",
228+
},
229+
Data: map[string]string{
230+
"test-key1": "test-value1",
231+
"test-key2": "test-value2",
232+
},
233+
},
234+
{
235+
ObjectMeta: metav1.ObjectMeta{
236+
Name: "other-configmap",
237+
Namespace: "test-namespace",
238+
},
239+
Data: map[string]string{
240+
"test-key": "test-value",
241+
},
242+
},
243+
},
244+
want: map[string][]byte{
245+
"configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, ConfigMapOutput{
246+
Namespace: "test-namespace",
247+
Name: "test-configmap",
248+
ConfigMapExists: true,
249+
Data: map[string]string{
250+
"test-key1": "test-value1",
251+
"test-key2": "test-value2",
252+
},
253+
}),
254+
},
255+
},
256+
{
257+
name: "collectAll no data",
258+
configMapCollector: &troubleshootv1beta2.ConfigMap{
259+
Namespace: "test-namespace",
260+
Name: "test-configmap",
261+
IncludeAllData: true,
262+
},
263+
mockConfigMaps: []corev1.ConfigMap{
264+
{
265+
ObjectMeta: metav1.ObjectMeta{
266+
Name: "test-configmap",
267+
Namespace: "test-namespace",
268+
},
269+
},
270+
{
271+
ObjectMeta: metav1.ObjectMeta{
272+
Name: "other-configmap",
273+
Namespace: "test-namespace",
274+
},
275+
Data: map[string]string{
276+
"test-key": "test-value",
277+
},
278+
},
279+
},
280+
want: map[string][]byte{
281+
"configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, ConfigMapOutput{
282+
Namespace: "test-namespace",
283+
Name: "test-configmap",
284+
ConfigMapExists: true,
285+
}),
286+
},
287+
},
288+
{
289+
name: "collectAll with slectKey",
290+
configMapCollector: &troubleshootv1beta2.ConfigMap{
291+
Namespace: "test-namespace",
292+
Name: "test-configmap",
293+
Key: "test-key1",
294+
IncludeAllData: true,
295+
},
296+
mockConfigMaps: []corev1.ConfigMap{
297+
{
298+
ObjectMeta: metav1.ObjectMeta{
299+
Name: "test-configmap",
300+
Namespace: "test-namespace",
301+
},
302+
Data: map[string]string{
303+
"test-key1": "test-value1",
304+
"test-key2": "test-value2",
305+
},
306+
},
307+
},
308+
want: map[string][]byte{
309+
"configmaps/test-namespace/test-configmap/test-key1.json": mustJSONMarshalIndent(t, ConfigMapOutput{
310+
Namespace: "test-namespace",
311+
Name: "test-configmap",
312+
ConfigMapExists: true,
313+
Key: "test-key1",
314+
Data: map[string]string{
315+
"test-key1": "test-value1",
316+
"test-key2": "test-value2",
317+
},
318+
KeyExists: true,
319+
}),
320+
},
321+
},
216322
}
217323
for _, tt := range tests {
218324
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)