From 91dca3e52641a92f5267409ab546360f8c9d193d Mon Sep 17 00:00:00 2001 From: Thomas Stringer Date: Mon, 25 Oct 2021 13:02:53 -0400 Subject: [PATCH] Add bug report feature to get all pod data in the mesh with the --all parameter (#4310) * add parameter to get all pods and namespaces in the mesh for the bug report Signed-off-by: Thomas Stringer * set appNamespaces to nil and fix error message wording Signed-off-by: Thomas Stringer --- cmd/cli/support_bugreport.go | 55 ++++++++++++++++++++++++++++-------- pkg/bugreport/run.go | 2 +- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/cmd/cli/support_bugreport.go b/cmd/cli/support_bugreport.go index 75bd097dd0..25bc93804e 100644 --- a/cmd/cli/support_bugreport.go +++ b/cmd/cli/support_bugreport.go @@ -1,16 +1,19 @@ package main import ( + "context" "fmt" "io" "github.com/pkg/errors" "github.com/spf13/cobra" "helm.sh/helm/v3/pkg/action" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" "github.com/openservicemesh/osm/pkg/bugreport" + "github.com/openservicemesh/osm/pkg/constants" "github.com/openservicemesh/osm/pkg/k8s" ) @@ -63,6 +66,7 @@ type bugReportCmd struct { stdout io.Writer stderr io.Writer kubeClient kubernetes.Interface + all bool appNamespaces []string appDeployments []string appPods []string @@ -95,6 +99,7 @@ func newSupportBugReportCmd(config *action.Configuration, stdout io.Writer, stde } f := cmd.Flags() + f.BoolVar(&bugReportCmd.all, "all", false, "All pods in the mesh") f.StringSliceVar(&bugReportCmd.appNamespaces, "app-namespaces", nil, "Application namespaces") f.StringSliceVar(&bugReportCmd.appDeployments, "app-deployments", nil, "Application deployments: /") f.StringSliceVar(&bugReportCmd.appPods, "app-pods", nil, "Application pods: /") @@ -106,22 +111,48 @@ func newSupportBugReportCmd(config *action.Configuration, stdout io.Writer, stde func (cmd *bugReportCmd) run() error { var appPods, appDeployments []types.NamespacedName - for _, pod := range cmd.appPods { - p, err := k8s.NamespacedNameFrom(pod) + if cmd.all { + ctx := context.Background() + cmd.appNamespaces = nil + namespaces, err := cmd.kubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{ + LabelSelector: constants.OSMKubeResourceMonitorAnnotation, + }) if err != nil { - fmt.Fprintf(cmd.stderr, "Pod name %s is not namespaced, skipping it", pod) - continue + fmt.Fprintf(cmd.stderr, "Unable to list mesh namespaces") + } + for _, namespace := range namespaces.Items { + namespaceName := namespace.ObjectMeta.Name + cmd.appNamespaces = append(cmd.appNamespaces, namespaceName) + pods, err := cmd.kubeClient.CoreV1().Pods(namespaceName).List(ctx, metav1.ListOptions{}) + if err != nil { + fmt.Fprintf(cmd.stderr, "Unable to get pods from namespace %s", namespaceName) + } + for _, pod := range pods.Items { + nsName := types.NamespacedName{ + Namespace: pod.Namespace, + Name: pod.Name, + } + appPods = append(appPods, nsName) + } + } + } else { + for _, pod := range cmd.appPods { + p, err := k8s.NamespacedNameFrom(pod) + if err != nil { + fmt.Fprintf(cmd.stderr, "Pod name %s is not namespaced, skipping it", pod) + continue + } + appPods = append(appPods, p) } - appPods = append(appPods, p) - } - for _, deployment := range cmd.appDeployments { - d, err := k8s.NamespacedNameFrom(deployment) - if err != nil { - fmt.Fprintf(cmd.stderr, "Deployment name %s is not namespaced, skipping it", deployment) - continue + for _, deployment := range cmd.appDeployments { + d, err := k8s.NamespacedNameFrom(deployment) + if err != nil { + fmt.Fprintf(cmd.stderr, "Deployment name %s is not namespaced, skipping it", deployment) + continue + } + appDeployments = append(appDeployments, d) } - appDeployments = append(appDeployments, d) } bugReportCfg := &bugreport.Config{ diff --git a/pkg/bugreport/run.go b/pkg/bugreport/run.go index 0bb5b4e9d9..685479a808 100644 --- a/pkg/bugreport/run.go +++ b/pkg/bugreport/run.go @@ -49,7 +49,7 @@ func (c *Config) Run() error { // Generate report for each app pod c.AppPods = c.getUniquePods() - fmt.Fprintf(c.Stdout, "[+] Collecting information from individual app namespaces\n") + fmt.Fprintf(c.Stdout, "[+] Collecting information from individual app pods\n") c.collectPerPodReport() c.completionSuccess("Finished generating report for individual app pods") c.endSection()