From d4e5c0dba40c7f61343f3a55c615101c9113fe8d Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Sun, 11 Sep 2022 18:28:24 -0400 Subject: [PATCH 01/12] discovery support bundle specs from secrets matching label selector --- cmd/troubleshoot/cli/root.go | 2 ++ cmd/troubleshoot/cli/run.go | 68 +++++++++++++++++++++++++----------- pkg/specs/secrets.go | 30 ++++++++++++++++ pkg/supportbundle/collect.go | 4 +-- pkg/supportbundle/load.go | 27 ++++++++++++++ 5 files changed, 109 insertions(+), 22 deletions(-) diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index d64039d8..0e52b00a 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -47,6 +47,8 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust cmd.Flags().Bool("redact", true, "enable/disable default redactions") cmd.Flags().Bool("interactive", true, "enable/disable interactive mode") cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions") + cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "nathan") + cmd.Flags().Bool("load-specs-from-secrets", false, "nathan") cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)") cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle") diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 6b1a9bca..31a070a4 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -22,15 +22,20 @@ import ( "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme" "github.com/replicatedhq/troubleshoot/pkg/convert" - "github.com/replicatedhq/troubleshoot/pkg/docrewrite" "github.com/replicatedhq/troubleshoot/pkg/httputil" "github.com/replicatedhq/troubleshoot/pkg/k8sutil" + "github.com/replicatedhq/troubleshoot/pkg/specs" "github.com/replicatedhq/troubleshoot/pkg/supportbundle" "github.com/spf13/viper" spin "github.com/tj/go-spin" + "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/rest" ) +const ( + SupportBundleSecretKey = "support-bundle-spec" +) + func runTroubleshoot(v *viper.Viper, arg []string) error { interactive := v.GetBool("interactive") && isatty.IsTerminal(os.Stdout.Fd()) @@ -71,18 +76,17 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { var mainBundle *troubleshootv1beta2.SupportBundle troubleshootclientsetscheme.AddToScheme(scheme.Scheme) - decode := scheme.Codecs.UniversalDeserializer().Decode additionalRedactors := &troubleshootv1beta2.Redactor{} for i, v := range arg { collectorContent, err := supportbundle.LoadSupportBundleSpec(v) if err != nil { - return errors.Wrap(err, "failed to load collector spec") + return errors.Wrap(err, "failed to load support bundle spec") } multidocs := strings.Split(string(collectorContent), "\n---\n") supportBundle, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0])) if err != nil { - return errors.Wrap(err, "failed to parse collector") + return errors.Wrap(err, "failed to parse support bundle spec") } if i == 0 { @@ -91,23 +95,47 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { mainBundle = supportbundle.ConcatSpec(mainBundle, supportBundle) } - for i, additionalDoc := range multidocs { - if i == 0 { - continue + parsedRedactors, err := supportbundle.ParseRedactorsFromSpec(multidocs) + if err != nil { + return errors.Wrap(err, "failed to parse redactors from doc") + } + additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...) + } + + if v.GetBool("load-specs-from-secrets") { + labelSelector := strings.Join(v.GetStringSlice("selector"), ",") + + parsedSelector, err := labels.Parse(labelSelector) + if err != nil { + return errors.Wrap(err, "unable to parse selector") + } + + namespace := "" + if v.GetString("namespace") != "" { + namespace = v.GetString("namespace") + } + + bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(parsedSelector.String(), namespace, SupportBundleSecretKey) + if err != nil { + return errors.Wrap(err, "failed to load support bundle spec from secrets") + } + + if bundlesFromSecrets != nil { + for _, bundle := range bundlesFromSecrets { + multidocs := strings.Split(string(bundle), "\n---\n") + parsedBundlesFromSecrets, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0])) + if err != nil { + return errors.Wrap(err, "failed to parse support bundle spec") + } + + mainBundle = supportbundle.ConcatSpec(mainBundle, parsedBundlesFromSecrets) + + parsedRedactors, err := supportbundle.ParseRedactorsFromSpec(multidocs) + if err != nil { + return errors.Wrap(err, "failed to parse redactors from doc") + } + additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...) } - additionalDoc, err := docrewrite.ConvertToV1Beta2([]byte(additionalDoc)) - if err != nil { - return errors.Wrap(err, "failed to convert to v1beta2") - } - obj, _, err := decode(additionalDoc, nil, nil) - if err != nil { - return errors.Wrapf(err, "failed to parse additional doc %d", i) - } - multidocRedactors, ok := obj.(*troubleshootv1beta2.Redactor) - if !ok { - continue - } - additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, multidocRedactors.Spec.Redactors...) } } diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index 8106fe81..950b3c8a 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -32,3 +32,33 @@ func LoadFromSecret(namespace string, secretName string, key string) ([]byte, er return spec, nil } + +func LoadFromSecretMatchingLabel(labelSelector string, namespace string, key string) ([]string, error) { + var allSecrets []string + + config, err := k8sutil.GetRESTConfig() + if err != nil { + return nil, errors.Wrap(err, "failed to convert kube flags to rest config") + } + + client, err := kubernetes.NewForConfig(config) + if err != nil { + return nil, errors.Wrap(err, "failed to convert create k8s client") + } + + daSecrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) + if err != nil { + return nil, errors.Wrap(err, "failed to get secret") + } + + for _, secret := range daSecrets.Items { + spec, ok := secret.Data[key] + if !ok { + return nil, errors.Errorf("support bundle spec not found in secret with matching label %s", secret.Name) + } + //multidocs := strings.Split(string(spec), "\n---\n") + allSecrets = append(allSecrets, string(spec)) + } + + return allSecrets, nil +} diff --git a/pkg/supportbundle/collect.go b/pkg/supportbundle/collect.go index 36251f8c..c0a34357 100644 --- a/pkg/supportbundle/collect.go +++ b/pkg/supportbundle/collect.go @@ -75,8 +75,8 @@ func runCollectors(collectors []*troubleshootv1beta2.Collect, additionalRedactor collectSpecs := make([]*troubleshootv1beta2.Collect, 0) collectSpecs = append(collectSpecs, collectors...) - collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}) - collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}}) + /*collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}) + collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}})*/ var cleanedCollectors collect.Collectors for _, desiredCollector := range collectSpecs { diff --git a/pkg/supportbundle/load.go b/pkg/supportbundle/load.go index f3857947..9d7ca456 100644 --- a/pkg/supportbundle/load.go +++ b/pkg/supportbundle/load.go @@ -219,3 +219,30 @@ func loadSpecFromURL(arg string) ([]byte, error) { return body, nil } } + +func ParseRedactorsFromSpec(docs []string) ([]*troubleshootv1beta2.Redact, error) { + var redactors []*troubleshootv1beta2.Redact + + decode := scheme.Codecs.UniversalDeserializer().Decode + + for i, additionalDoc := range docs { + if i == 0 { + continue + } + additionalDoc, err := docrewrite.ConvertToV1Beta2([]byte(additionalDoc)) + if err != nil { + return nil, errors.Wrap(err, "failed to convert to v1beta2") + } + obj, _, err := decode(additionalDoc, nil, nil) + if err != nil { + return nil, errors.Wrapf(err, "failed to parse additional doc %d", i) + } + multidocRedactors, ok := obj.(*troubleshootv1beta2.Redactor) + if !ok { + continue + } + redactors = append(redactors, multidocRedactors.Spec.Redactors...) + } + + return redactors, nil +} From ed34afc51b08f00586137e74223e7d32c0da3e28 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Sun, 11 Sep 2022 18:33:35 -0400 Subject: [PATCH 02/12] adding back default collectors --- pkg/specs/secrets.go | 10 +++++----- pkg/supportbundle/collect.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index 950b3c8a..b25c134b 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -34,7 +34,7 @@ func LoadFromSecret(namespace string, secretName string, key string) ([]byte, er } func LoadFromSecretMatchingLabel(labelSelector string, namespace string, key string) ([]string, error) { - var allSecrets []string + var secretsMatchingKey []string config, err := k8sutil.GetRESTConfig() if err != nil { @@ -46,19 +46,19 @@ func LoadFromSecretMatchingLabel(labelSelector string, namespace string, key str return nil, errors.Wrap(err, "failed to convert create k8s client") } - daSecrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) + secrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) if err != nil { return nil, errors.Wrap(err, "failed to get secret") } - for _, secret := range daSecrets.Items { + for _, secret := range secrets.Items { spec, ok := secret.Data[key] if !ok { return nil, errors.Errorf("support bundle spec not found in secret with matching label %s", secret.Name) } //multidocs := strings.Split(string(spec), "\n---\n") - allSecrets = append(allSecrets, string(spec)) + secretsMatchingKey = append(secretsMatchingKey, string(spec)) } - return allSecrets, nil + return secretsMatchingKey, nil } diff --git a/pkg/supportbundle/collect.go b/pkg/supportbundle/collect.go index c0a34357..36251f8c 100644 --- a/pkg/supportbundle/collect.go +++ b/pkg/supportbundle/collect.go @@ -75,8 +75,8 @@ func runCollectors(collectors []*troubleshootv1beta2.Collect, additionalRedactor collectSpecs := make([]*troubleshootv1beta2.Collect, 0) collectSpecs = append(collectSpecs, collectors...) - /*collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}) - collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}})*/ + collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterInfo: &troubleshootv1beta2.ClusterInfo{}}) + collectSpecs = ensureCollectorInList(collectSpecs, troubleshootv1beta2.Collect{ClusterResources: &troubleshootv1beta2.ClusterResources{}}) var cleanedCollectors collect.Collectors for _, desiredCollector := range collectSpecs { From 7120516b1389537414894c0feb2b3742e7da88ad Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Sun, 11 Sep 2022 18:39:55 -0400 Subject: [PATCH 03/12] fixing description for cli flags --- cmd/troubleshoot/cli/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index 0e52b00a..4589d43c 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -47,8 +47,8 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust cmd.Flags().Bool("redact", true, "enable/disable default redactions") cmd.Flags().Bool("interactive", true, "enable/disable interactive mode") cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions") - cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "nathan") - cmd.Flags().Bool("load-specs-from-secrets", false, "nathan") + cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "Selector to filter on for loading additional support bundle specs found in secrets within the cluster") + cmd.Flags().Bool("load-specs-from-secrets", false, "enable/disable loading additional support bundle specs found in secrets within the cluster") cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)") cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle") From ad23763d6ba1ddb4171c625e8240ceafa402bcdb Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Sun, 11 Sep 2022 18:46:31 -0400 Subject: [PATCH 04/12] remove comment --- pkg/specs/secrets.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index b25c134b..e89ca960 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -56,7 +56,6 @@ func LoadFromSecretMatchingLabel(labelSelector string, namespace string, key str if !ok { return nil, errors.Errorf("support bundle spec not found in secret with matching label %s", secret.Name) } - //multidocs := strings.Split(string(spec), "\n---\n") secretsMatchingKey = append(secretsMatchingKey, string(spec)) } From 93da8b6ac7d3b232ed4f07996691e25848f2e532 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Tue, 13 Sep 2022 18:42:33 -0400 Subject: [PATCH 05/12] change name of flag --- cmd/troubleshoot/cli/root.go | 2 +- cmd/troubleshoot/cli/run.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index 4589d43c..55392505 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -48,7 +48,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust cmd.Flags().Bool("interactive", true, "enable/disable interactive mode") cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions") cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "Selector to filter on for loading additional support bundle specs found in secrets within the cluster") - cmd.Flags().Bool("load-specs-from-secrets", false, "enable/disable loading additional support bundle specs found in secrets within the cluster") + cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional support bundle specs found in secrets within the cluster") cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)") cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle") diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 31a070a4..0bff1873 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -102,7 +102,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...) } - if v.GetBool("load-specs-from-secrets") { + if v.GetBool("load-cluster-specs") { labelSelector := strings.Join(v.GetStringSlice("selector"), ",") parsedSelector, err := labels.Parse(labelSelector) From e53871b4dc3d7c4b64aa787800dd4a8c06cd2711 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Tue, 13 Sep 2022 23:00:57 -0400 Subject: [PATCH 06/12] adding tests --- cmd/troubleshoot/cli/run.go | 13 +- pkg/specs/secrets.go | 14 +- pkg/specs/secrets_test.go | 400 ++++++++++++++++++++++++ pkg/supportbundle/supportbundle_test.go | 56 ++-- 4 files changed, 442 insertions(+), 41 deletions(-) create mode 100644 pkg/specs/secrets_test.go diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 0bff1873..f7d93a64 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -29,6 +29,7 @@ import ( "github.com/spf13/viper" spin "github.com/tj/go-spin" "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" ) @@ -115,7 +116,17 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { namespace = v.GetString("namespace") } - bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(parsedSelector.String(), namespace, SupportBundleSecretKey) + config, err := k8sutil.GetRESTConfig() + if err != nil { + return errors.Wrap(err, "failed to convert kube flags to rest config") + } + + client, err := kubernetes.NewForConfig(config) + if err != nil { + return errors.Wrap(err, "failed to convert create k8s client") + } + + bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, SupportBundleSecretKey) if err != nil { return errors.Wrap(err, "failed to load support bundle spec from secrets") } diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index e89ca960..584fb065 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -33,26 +33,16 @@ func LoadFromSecret(namespace string, secretName string, key string) ([]byte, er return spec, nil } -func LoadFromSecretMatchingLabel(labelSelector string, namespace string, key string) ([]string, error) { +func LoadFromSecretMatchingLabel(client kubernetes.Interface, labelSelector string, namespace string, key string) ([]string, error) { var secretsMatchingKey []string - config, err := k8sutil.GetRESTConfig() - if err != nil { - return nil, errors.Wrap(err, "failed to convert kube flags to rest config") - } - - client, err := kubernetes.NewForConfig(config) - if err != nil { - return nil, errors.Wrap(err, "failed to convert create k8s client") - } - secrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) if err != nil { return nil, errors.Wrap(err, "failed to get secret") } for _, secret := range secrets.Items { - spec, ok := secret.Data[key] + spec, ok := secret.StringData[key] if !ok { return nil, errors.Errorf("support bundle spec not found in secret with matching label %s", secret.Name) } diff --git a/pkg/specs/secrets_test.go b/pkg/specs/secrets_test.go new file mode 100644 index 00000000..03bdb31a --- /dev/null +++ b/pkg/specs/secrets_test.go @@ -0,0 +1,400 @@ +package specs + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + testclient "k8s.io/client-go/kubernetes/fake" +) + +func Test_LoadFromSecretMatchingLabel(t *testing.T) { + type args struct { + ctx context.Context + client kubernetes.Interface + } + tests := []struct { + name string + supportBundleSecrets []corev1.Secret + want []string + wantErr bool + }{ + { + name: "support bundle secret with matching label and key", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "default", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - runPod: + name: "run-ping" + namespace: default + podSpec: + containers: + - name: run-ping + image: busybox:1 + command: ["ping"] + args: ["-w", "5", "www.google.com"]`, + }, + }, + }, + want: []string{ + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - runPod: + name: "run-ping" + namespace: default + podSpec: + containers: + - name: run-ping + image: busybox:1 + command: ["ping"] + args: ["-w", "5", "www.google.com"]`, + }, + }, + { + name: "support bundle secret with missing label", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "default", + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - data: + name: static/data.txt + data: | + static data`, + }, + }, + }, + want: []string(nil), + }, + { + name: "support bundle secret with matching label but wrong key", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "default", + }, + StringData: map[string]string{ + "support-bundle-specc": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - data: + name: static/data.txt + data: | + static data`, + }, + }, + }, + want: []string(nil), + }, + { + name: "multiple support bundle secrets in the same namespace with matching label and key", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "default", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-info +spec: + collectors: + - clusterInfo: {}`, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret-2", + Namespace: "default", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + }, + want: []string{ + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-info +spec: + collectors: + - clusterInfo: {}`, + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + { + name: "multiple support bundle secrets in different namespaces with matching label and key", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "some-namespace", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-info +spec: + collectors: + - clusterInfo: {}`, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret-2", + Namespace: "some-namespace-2", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + }, + want: []string{ + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-info +spec: + collectors: + - clusterInfo: {}`, + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + { + name: "multiple support bundle secrets in different namespaces but only one with correct label and key", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "some-namespace", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec-wrong", + }, + }, + StringData: map[string]string{ + "support-bundle-spec-wrong": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-info +spec: + collectors: + - clusterInfo: {}`, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret-2", + Namespace: "some-namespace-2", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + }, + want: []string{ + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: cluster-resources +spec: + collectors: + - clusterResources: {}`, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + client := testclient.NewSimpleClientset() + for _, secret := range tt.supportBundleSecrets { + _, err := client.CoreV1().Secrets(secret.Namespace).Create(ctx, &secret, metav1.CreateOptions{}) + require.NoError(t, err) + } + got, err := LoadFromSecretMatchingLabel(client, "troubleshoot.io/kind=supportbundle-spec", "", "support-bundle-spec") + if tt.wantErr { + assert.Error(t, err) + } else { + require.NoError(t, err) + assert.Equal(t, tt.want, got) + } + }) + } +} + +func TestUserProvidedNamespace_LoadFromSecretMatchingLabel(t *testing.T) { + type args struct { + ctx context.Context + client kubernetes.Interface + } + tests := []struct { + name string + supportBundleSecrets []corev1.Secret + want []string + wantErr bool + }{ + { + name: "support bundle secret with matching label and key in user provided namespace", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "some-namespace", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - data: + name: static/data.txt + data: | + static data`, + }, + }, + }, + want: []string{ + `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - data: + name: static/data.txt + data: | + static data`, + }, + }, + { + name: "support bundle secret with matching label and key outside of user provided namespace", + supportBundleSecrets: []corev1.Secret{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "secret", + Namespace: "not-your-namespace", + Labels: map[string]string{ + "troubleshoot.io/kind": "supportbundle-spec", + }, + }, + StringData: map[string]string{ + "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 +kind: SupportBundle +metadata: + name: test +spec: + collectors: + - data: + name: static/data.txt + data: | + static data`, + }, + }, + }, + want: []string(nil), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + client := testclient.NewSimpleClientset() + for _, secret := range tt.supportBundleSecrets { + _, err := client.CoreV1().Secrets(secret.Namespace).Create(ctx, &secret, metav1.CreateOptions{}) + require.NoError(t, err) + } + got, err := LoadFromSecretMatchingLabel(client, "troubleshoot.io/kind=supportbundle-spec", "some-namespace", "support-bundle-spec") + if tt.wantErr { + assert.Error(t, err) + } else { + require.NoError(t, err) + assert.Equal(t, tt.want, got) + } + }) + } +} diff --git a/pkg/supportbundle/supportbundle_test.go b/pkg/supportbundle/supportbundle_test.go index 821f28fd..d29891ac 100644 --- a/pkg/supportbundle/supportbundle_test.go +++ b/pkg/supportbundle/supportbundle_test.go @@ -7,40 +7,40 @@ import ( func Test_LoadAndConcatSpec(t *testing.T) { - bundle1doc,err := LoadSupportBundleSpec("test/supportbundle1.yaml") - if err != nil { - t.Error("couldn't load bundle1 from file") - } + bundle1doc, err := LoadSupportBundleSpec("test/supportbundle1.yaml") + if err != nil { + t.Error("couldn't load bundle1 from file") + } - bundle2doc,err := LoadSupportBundleSpec("test/supportbundle2.yaml") - if err != nil { - t.Error("couldn't load bundle2 from file") - } + bundle2doc, err := LoadSupportBundleSpec("test/supportbundle2.yaml") + if err != nil { + t.Error("couldn't load bundle2 from file") + } - bundle1,err := ParseSupportBundleFromDoc(bundle1doc) - if err != nil { - t.Error("couldn't parse bundle 1") - } + bundle1, err := ParseSupportBundleFromDoc(bundle1doc) + if err != nil { + t.Error("couldn't parse bundle 1") + } - bundle2,err := ParseSupportBundleFromDoc(bundle2doc) - if err != nil { - t.Error("couldn't parse bundle 2") - } + bundle2, err := ParseSupportBundleFromDoc(bundle2doc) + if err != nil { + t.Error("couldn't parse bundle 2") + } - fulldoc,err := LoadSupportBundleSpec("test/completebundle.yaml") - if err != nil { - t.Error("couldn't load full bundle from file") - } + fulldoc, err := LoadSupportBundleSpec("test/completebundle.yaml") + if err != nil { + t.Error("couldn't load full bundle from file") + } - fullbundle,err := ParseSupportBundleFromDoc(fulldoc) - if err != nil { - t.Error("couldn't parse full bundle") - } + fullbundle, err := ParseSupportBundleFromDoc(fulldoc) + if err != nil { + t.Error("couldn't parse full bundle") + } - bundle3 := ConcatSpec(bundle1,bundle2) + bundle3 := ConcatSpec(bundle1, bundle2) - if reflect.DeepEqual(fullbundle, bundle3) == false { - t.Error("Full bundle and concatenated bundle are not the same.") - } + if reflect.DeepEqual(fullbundle, bundle3) == false { + t.Error("Full bundle and concatenated bundle are not the same.") + } } From ec6ec5930362c55fd078cd66cdeedffd543fe9ae Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Tue, 13 Sep 2022 23:27:49 -0400 Subject: [PATCH 07/12] fixing tests --- pkg/specs/secrets.go | 8 +++-- pkg/specs/secrets_test.go | 66 +++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index 584fb065..0c236fb1 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/troubleshoot/pkg/k8sutil" + "github.com/replicatedhq/troubleshoot/pkg/logger" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" ) @@ -38,13 +39,14 @@ func LoadFromSecretMatchingLabel(client kubernetes.Interface, labelSelector stri secrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) if err != nil { - return nil, errors.Wrap(err, "failed to get secret") + return nil, errors.Wrap(err, "failed to search for secrets containing support bundles in the cluster") } for _, secret := range secrets.Items { - spec, ok := secret.StringData[key] + spec, ok := secret.Data[key] if !ok { - return nil, errors.Errorf("support bundle spec not found in secret with matching label %s", secret.Name) + logger.Printf("expected key of %s not found in secret %s, skipping\n", key, secret.Name) + continue } secretsMatchingKey = append(secretsMatchingKey, string(spec)) } diff --git a/pkg/specs/secrets_test.go b/pkg/specs/secrets_test.go index 03bdb31a..00b97a08 100644 --- a/pkg/specs/secrets_test.go +++ b/pkg/specs/secrets_test.go @@ -34,8 +34,8 @@ func Test_LoadFromSecretMatchingLabel(t *testing.T) { "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: test @@ -49,7 +49,7 @@ spec: - name: run-ping image: busybox:1 command: ["ping"] - args: ["-w", "5", "www.google.com"]`, + args: ["-w", "5", "www.google.com"]`), }, }, }, @@ -79,8 +79,8 @@ spec: Name: "secret", Namespace: "default", }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: test @@ -89,7 +89,7 @@ spec: - data: name: static/data.txt data: | - static data`, + static data`), }, }, }, @@ -103,8 +103,8 @@ spec: Name: "secret", Namespace: "default", }, - StringData: map[string]string{ - "support-bundle-specc": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-specc": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: test @@ -113,7 +113,7 @@ spec: - data: name: static/data.txt data: | - static data`, + static data`), }, }, }, @@ -130,14 +130,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-info spec: collectors: - - clusterInfo: {}`, + - clusterInfo: {}`), }, }, { @@ -148,14 +148,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-resources spec: collectors: - - clusterResources: {}`, + - clusterResources: {}`), }, }, }, @@ -187,14 +187,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-info spec: collectors: - - clusterInfo: {}`, + - clusterInfo: {}`), }, }, { @@ -205,14 +205,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-resources spec: collectors: - - clusterResources: {}`, + - clusterResources: {}`), }, }, }, @@ -244,14 +244,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec-wrong", }, }, - StringData: map[string]string{ - "support-bundle-spec-wrong": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec-wrong": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-info spec: collectors: - - clusterInfo: {}`, + - clusterInfo: {}`), }, }, { @@ -262,14 +262,14 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: cluster-resources spec: collectors: - - clusterResources: {}`, + - clusterResources: {}`), }, }, }, @@ -325,8 +325,8 @@ func TestUserProvidedNamespace_LoadFromSecretMatchingLabel(t *testing.T) { "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: test @@ -335,7 +335,7 @@ spec: - data: name: static/data.txt data: | - static data`, + static data`), }, }, }, @@ -363,8 +363,8 @@ spec: "troubleshoot.io/kind": "supportbundle-spec", }, }, - StringData: map[string]string{ - "support-bundle-spec": `apiVersion: troubleshoot.sh/v1beta2 + Data: map[string][]byte{ + "support-bundle-spec": []byte(`apiVersion: troubleshoot.sh/v1beta2 kind: SupportBundle metadata: name: test @@ -373,7 +373,7 @@ spec: - data: name: static/data.txt data: | - static data`, + static data`), }, }, }, From e7fe012f2fc54facaf43db0b1310c2b2494929f2 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Wed, 14 Sep 2022 00:36:04 -0400 Subject: [PATCH 08/12] handling when no args are provided --- cmd/troubleshoot/cli/root.go | 6 +++--- cmd/troubleshoot/cli/run.go | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/troubleshoot/cli/root.go b/cmd/troubleshoot/cli/root.go index 55392505..f98fb713 100644 --- a/cmd/troubleshoot/cli/root.go +++ b/cmd/troubleshoot/cli/root.go @@ -17,7 +17,7 @@ import ( func RootCmd() *cobra.Command { cmd := &cobra.Command{ Use: "support-bundle [url]", - Args: cobra.MinimumNArgs(1), + Args: cobra.MinimumNArgs(0), Short: "Generate a support bundle", Long: `A support bundle is an archive of files, output, metrics and state from a server that can be used to assist when troubleshooting a Kubernetes cluster.`, @@ -47,8 +47,8 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust cmd.Flags().Bool("redact", true, "enable/disable default redactions") cmd.Flags().Bool("interactive", true, "enable/disable interactive mode") cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions") - cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "Selector to filter on for loading additional support bundle specs found in secrets within the cluster") - cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional support bundle specs found in secrets within the cluster") + cmd.Flags().StringSliceP("selector", "l", []string{"troubleshoot.io/kind=supportbundle-spec"}, "selector to filter on for loading additional support bundle specs found in secrets within the cluster") + cmd.Flags().Bool("load-cluster-specs", false, "enable/disable loading additional support bundle specs found in secrets within the cluster. required when no specs are provided on the command line") cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)") cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.") cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle") diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index f7d93a64..e57b9ec8 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -38,6 +38,10 @@ const ( ) func runTroubleshoot(v *viper.Viper, arg []string) error { + if v.GetBool("load-cluster-specs") == false && len(arg) == 0 { + return errors.New("flag load-cluster-specs must be set if no specs are provided on the command line") + } + interactive := v.GetBool("interactive") && isatty.IsTerminal(os.Stdout.Fd()) if interactive { @@ -139,7 +143,11 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { return errors.Wrap(err, "failed to parse support bundle spec") } - mainBundle = supportbundle.ConcatSpec(mainBundle, parsedBundlesFromSecrets) + if mainBundle == nil { + mainBundle = parsedBundlesFromSecrets + } else { + supportbundle.ConcatSpec(mainBundle, parsedBundlesFromSecrets) + } parsedRedactors, err := supportbundle.ParseRedactorsFromSpec(multidocs) if err != nil { @@ -150,6 +158,10 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { } } + if mainBundle == nil { + return errors.New("no specs provided to run") + } + for idx, redactor := range v.GetStringSlice("redactors") { redactorObj, err := supportbundle.GetRedactorFromURI(redactor) if err != nil { From 126346e1feea61f468bc7366c280c58f506e8041 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Wed, 14 Sep 2022 00:57:33 -0400 Subject: [PATCH 09/12] update run.go --- cmd/troubleshoot/cli/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index e57b9ec8..d07e6836 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -38,7 +38,7 @@ const ( ) func runTroubleshoot(v *viper.Viper, arg []string) error { - if v.GetBool("load-cluster-specs") == false && len(arg) == 0 { + if v.GetBool("load-cluster-specs") == false && len(arg) < 1 { return errors.New("flag load-cluster-specs must be set if no specs are provided on the command line") } From 7eecf6c5268beabe8b62a3627f245549db5b466c Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Wed, 14 Sep 2022 10:58:08 -0400 Subject: [PATCH 10/12] improving error handling --- cmd/troubleshoot/cli/run.go | 12 +++++++++--- pkg/specs/secrets.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index d07e6836..6bc3efe7 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -24,6 +24,7 @@ import ( "github.com/replicatedhq/troubleshoot/pkg/convert" "github.com/replicatedhq/troubleshoot/pkg/httputil" "github.com/replicatedhq/troubleshoot/pkg/k8sutil" + "github.com/replicatedhq/troubleshoot/pkg/logger" "github.com/replicatedhq/troubleshoot/pkg/specs" "github.com/replicatedhq/troubleshoot/pkg/supportbundle" "github.com/spf13/viper" @@ -132,7 +133,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { bundlesFromSecrets, err := specs.LoadFromSecretMatchingLabel(client, parsedSelector.String(), namespace, SupportBundleSecretKey) if err != nil { - return errors.Wrap(err, "failed to load support bundle spec from secrets") + logger.Printf("failed to load support bundle spec from secrets: %s", err) } if bundlesFromSecrets != nil { @@ -140,7 +141,8 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { multidocs := strings.Split(string(bundle), "\n---\n") parsedBundlesFromSecrets, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0])) if err != nil { - return errors.Wrap(err, "failed to parse support bundle spec") + logger.Printf("failed to parse support bundle spec: %w", err) + continue } if mainBundle == nil { @@ -151,11 +153,15 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { parsedRedactors, err := supportbundle.ParseRedactorsFromSpec(multidocs) if err != nil { - return errors.Wrap(err, "failed to parse redactors from doc") + logger.Printf("failed to parse redactors from doc: %w", err) + continue } additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...) } } + if mainBundle == nil { + return errors.New("no specs found in cluster") + } } if mainBundle == nil { diff --git a/pkg/specs/secrets.go b/pkg/specs/secrets.go index 0c236fb1..a3d5b401 100644 --- a/pkg/specs/secrets.go +++ b/pkg/specs/secrets.go @@ -39,7 +39,7 @@ func LoadFromSecretMatchingLabel(client kubernetes.Interface, labelSelector stri secrets, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) if err != nil { - return nil, errors.Wrap(err, "failed to search for secrets containing support bundles in the cluster") + return nil, errors.Wrap(err, "failed to search for secrets in the cluster") } for _, secret := range secrets.Items { From be070c98e1b80dbd4791be7c7ef23306b9cfd3a0 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Wed, 14 Sep 2022 11:19:44 -0400 Subject: [PATCH 11/12] add handling for when no collectors or host collectors are specified --- cmd/troubleshoot/cli/run.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index 6bc3efe7..bb47eba7 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -165,7 +165,9 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { } if mainBundle == nil { - return errors.New("no specs provided to run") + return errors.New("no support bundle specs provided to run") + } else if mainBundle.Spec.Collectors == nil && mainBundle.Spec.HostCollectors == nil { + return errors.New("no collectors specified in support bundle") } for idx, redactor := range v.GetStringSlice("redactors") { From b66145e514995df7f75cdb44fc53184be0a97a90 Mon Sep 17 00:00:00 2001 From: Diamon Wiggins Date: Wed, 14 Sep 2022 11:56:16 -0400 Subject: [PATCH 12/12] fixing logger statements --- cmd/troubleshoot/cli/run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/troubleshoot/cli/run.go b/cmd/troubleshoot/cli/run.go index bb47eba7..a7f792a8 100644 --- a/cmd/troubleshoot/cli/run.go +++ b/cmd/troubleshoot/cli/run.go @@ -141,7 +141,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { multidocs := strings.Split(string(bundle), "\n---\n") parsedBundlesFromSecrets, err := supportbundle.ParseSupportBundleFromDoc([]byte(multidocs[0])) if err != nil { - logger.Printf("failed to parse support bundle spec: %w", err) + logger.Printf("failed to parse support bundle spec: %s", err) continue } @@ -153,7 +153,7 @@ func runTroubleshoot(v *viper.Viper, arg []string) error { parsedRedactors, err := supportbundle.ParseRedactorsFromSpec(multidocs) if err != nil { - logger.Printf("failed to parse redactors from doc: %w", err) + logger.Printf("failed to parse redactors from doc: %s", err) continue } additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, parsedRedactors...)