From bf7d658313e8daf39771ba5e9f6a23c3c870e30e Mon Sep 17 00:00:00 2001 From: Kyle Sorensen Date: Mon, 26 Jul 2021 13:00:06 -0600 Subject: [PATCH] troubleshoot enables collecting all data from a configmap (#395) Enabled collecting all data from a ConfigMap instead of by key --- .../sample-collect-entire-configmap.yaml | 16 +++ .../troubleshoot/v1beta2/collector_shared.go | 13 ++- pkg/collect/configmap.go | 16 ++- pkg/collect/configmap_test.go | 106 ++++++++++++++++++ 4 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 examples/support-bundle/sample-collect-entire-configmap.yaml diff --git a/examples/support-bundle/sample-collect-entire-configmap.yaml b/examples/support-bundle/sample-collect-entire-configmap.yaml new file mode 100644 index 00000000..fbc5c1bb --- /dev/null +++ b/examples/support-bundle/sample-collect-entire-configmap.yaml @@ -0,0 +1,16 @@ +apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: example-collect-all-configmap-data +spec: + collectors: + - configMap: + namespace: kurl + name: kurl-current-config + includeAllData: true + - configMap: + namespace: kurl + name: kurl-last-config + includeAllData: true + + \ No newline at end of file diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index 9c2df30a..0768ac1f 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -33,12 +33,13 @@ type Secret struct { } type ConfigMap struct { - CollectorMeta `json:",inline" yaml:",inline"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Selector []string `json:"selector,omitempty" yaml:"selector,omitempty"` - Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` - Key string `json:"key,omitempty" yaml:"key,omitempty"` - IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"` + CollectorMeta `json:",inline" yaml:",inline"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Selector []string `json:"selector,omitempty" yaml:"selector,omitempty"` + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + Key string `json:"key,omitempty" yaml:"key,omitempty"` + IncludeValue bool `json:"includeValue,omitempty" yaml:"includeValue,omitempty"` + IncludeAllData bool `json:"includeAllData,omitempty" yaml:"includeAllData,omitempty"` } type LogLimits struct { diff --git a/pkg/collect/configmap.go b/pkg/collect/configmap.go index 01eacd1e..e954f323 100644 --- a/pkg/collect/configmap.go +++ b/pkg/collect/configmap.go @@ -16,12 +16,13 @@ import ( ) type ConfigMapOutput struct { - Namespace string `json:"namespace"` - Name string `json:"name"` - Key string `json:"key"` - ConfigMapExists bool `json:"configMapExists"` - KeyExists bool `json:"keyExists"` - Value string `json:"value,omitempty"` + Namespace string `json:"namespace"` + Name string `json:"name"` + Key string `json:"key"` + ConfigMapExists bool `json:"configMapExists"` + KeyExists bool `json:"keyExists"` + Value string `json:"value,omitempty"` + Data map[string]string `json:"data,omitonempty"` } 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 if configMap != nil { foundConfigMap.ConfigMapExists = true + if configMapCollector.IncludeAllData { + foundConfigMap.Data = configMap.Data + } if configMapCollector.Key != "" { if val, ok := configMap.Data[configMapCollector.Key]; ok { foundConfigMap.KeyExists = true diff --git a/pkg/collect/configmap_test.go b/pkg/collect/configmap_test.go index fb3ea4d6..de3fba1e 100644 --- a/pkg/collect/configmap_test.go +++ b/pkg/collect/configmap_test.go @@ -213,6 +213,112 @@ func TestConfigMap(t *testing.T) { }), }, }, + { + name: "collectAll", + configMapCollector: &troubleshootv1beta2.ConfigMap{ + Namespace: "test-namespace", + Name: "test-configmap", + IncludeAllData: true, + }, + mockConfigMaps: []corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-configmap", + Namespace: "test-namespace", + }, + Data: map[string]string{ + "test-key1": "test-value1", + "test-key2": "test-value2", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "other-configmap", + Namespace: "test-namespace", + }, + Data: map[string]string{ + "test-key": "test-value", + }, + }, + }, + want: map[string][]byte{ + "configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, ConfigMapOutput{ + Namespace: "test-namespace", + Name: "test-configmap", + ConfigMapExists: true, + Data: map[string]string{ + "test-key1": "test-value1", + "test-key2": "test-value2", + }, + }), + }, + }, + { + name: "collectAll no data", + configMapCollector: &troubleshootv1beta2.ConfigMap{ + Namespace: "test-namespace", + Name: "test-configmap", + IncludeAllData: true, + }, + mockConfigMaps: []corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-configmap", + Namespace: "test-namespace", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "other-configmap", + Namespace: "test-namespace", + }, + Data: map[string]string{ + "test-key": "test-value", + }, + }, + }, + want: map[string][]byte{ + "configmaps/test-namespace/test-configmap.json": mustJSONMarshalIndent(t, ConfigMapOutput{ + Namespace: "test-namespace", + Name: "test-configmap", + ConfigMapExists: true, + }), + }, + }, + { + name: "collectAll with slectKey", + configMapCollector: &troubleshootv1beta2.ConfigMap{ + Namespace: "test-namespace", + Name: "test-configmap", + Key: "test-key1", + IncludeAllData: true, + }, + mockConfigMaps: []corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-configmap", + Namespace: "test-namespace", + }, + Data: map[string]string{ + "test-key1": "test-value1", + "test-key2": "test-value2", + }, + }, + }, + want: map[string][]byte{ + "configmaps/test-namespace/test-configmap/test-key1.json": mustJSONMarshalIndent(t, ConfigMapOutput{ + Namespace: "test-namespace", + Name: "test-configmap", + ConfigMapExists: true, + Key: "test-key1", + Data: map[string]string{ + "test-key1": "test-value1", + "test-key2": "test-value2", + }, + KeyExists: true, + }), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {