Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
add ingress information to bug report
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Stringer <[email protected]>
  • Loading branch information
trstringer authored and nojnhuh committed Jan 7, 2022
1 parent 04f7062 commit 6ed8486
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/cli/support_bugreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/openservicemesh/osm/pkg/bugreport"
"github.com/openservicemesh/osm/pkg/constants"

policyClientset "github.com/openservicemesh/osm/pkg/gen/client/policy/clientset/versioned"
"github.com/openservicemesh/osm/pkg/k8s"
)

Expand Down Expand Up @@ -66,11 +68,13 @@ type bugReportCmd struct {
stdout io.Writer
stderr io.Writer
kubeClient kubernetes.Interface
policyClient policyClientset.Interface
all bool
appNamespaces []string
appDeployments []string
appPods []string
outFile string
collectIngress bool
}

func newSupportBugReportCmd(config *action.Configuration, stdout io.Writer, stderr io.Writer) *cobra.Command {
Expand All @@ -93,6 +97,10 @@ func newSupportBugReportCmd(config *action.Configuration, stdout io.Writer, stde
if err != nil {
return errors.Errorf("Could not access Kubernetes cluster, check kubeconfig: %s", err)
}
bugReportCmd.policyClient, err = policyClientset.NewForConfig(config)
if err != nil {
return errors.Errorf("Could not access OSM, check configuration: %s", err)
}
return bugReportCmd.run()
},
Example: bugReportExample,
Expand All @@ -103,6 +111,7 @@ func newSupportBugReportCmd(config *action.Configuration, stdout io.Writer, stde
f.StringSliceVar(&bugReportCmd.appNamespaces, "app-namespaces", nil, "Application namespaces")
f.StringSliceVar(&bugReportCmd.appDeployments, "app-deployments", nil, "Application deployments: <namespace>/<deployment>")
f.StringSliceVar(&bugReportCmd.appPods, "app-pods", nil, "Application pods: <namespace>/<pod>")
f.BoolVar(&bugReportCmd.collectIngress, "ingress", false, "Collect ingress")
f.StringVarP(&bugReportCmd.outFile, "out-file", "o", "", "Output file with archive format extension")

return cmd
Expand All @@ -112,6 +121,7 @@ func (cmd *bugReportCmd) run() error {
var appPods, appDeployments []types.NamespacedName

if cmd.all {
cmd.collectIngress = true
ctx := context.Background()
cmd.appNamespaces = nil
namespaces, err := cmd.kubeClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
Expand Down Expand Up @@ -159,11 +169,13 @@ func (cmd *bugReportCmd) run() error {
Stdout: cmd.stdout,
Stderr: cmd.stderr,
KubeClient: cmd.kubeClient,
PolicyClient: cmd.policyClient,
ControlPlaneNamepace: settings.Namespace(),
AppNamespaces: cmd.appNamespaces,
AppDeployments: appDeployments,
AppPods: appPods,
OutFile: cmd.outFile,
CollectIngress: cmd.collectIngress,
}

return bugReportCfg.Run()
Expand Down
138 changes: 138 additions & 0 deletions pkg/bugreport/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package bugreport

import (
"context"
"path"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
nginxIngressControllerLabelSelector string = "app.kubernetes.io/name=ingress-nginx"
contourIngressControllerLabelSelector string = "app.kubernetes.io/name=contour"

ingressResourceName string = "ingresses"
ingressBackendResourceName string = "ingressbackends"
)

func (c *Config) collectIngressReport() error {
namespaceList, err := c.KubeClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}

for _, namespace := range namespaceList.Items {
ingressList, err := c.KubeClient.NetworkingV1().Ingresses(namespace.Name).List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}
for _, ingress := range ingressList.Items {
for _, cmd := range ingressReportCommands(namespace.Name, ingress.Name, ingressResourceName) {
filename := path.Join(
c.rootNamespaceDirPath(),
namespace.Name,
ingressResourceName,
ingress.Name,
commandsDirName,
strings.Join(cmd, "_"),
)
if err := runCmdAndWriteToFile(cmd, filename); err != nil {
return err
}
}
c.completionSuccess("Collected report for ingress %s/%s", ingress.Namespace, ingress.Name)
}

ingressBackendList, err := c.PolicyClient.PolicyV1alpha1().IngressBackends(namespace.Name).List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}

for _, ingressBackend := range ingressBackendList.Items {
for _, cmd := range ingressReportCommands(namespace.Name, ingressBackend.Name, ingressBackendResourceName) {
filename := path.Join(
c.rootNamespaceDirPath(),
namespace.Name,
ingressBackendResourceName,
ingressBackend.Name,
commandsDirName,
strings.Join(cmd, "_"),
)
if err := runCmdAndWriteToFile(cmd, filename); err != nil {
return err
}
}
c.completionSuccess("Collected report for ingress backend %s/%s", ingressBackend.Namespace, ingressBackend.Name)
}
}

return nil
}

func (c *Config) collectIngressControllerReport() error {
ingressControllerLabelsSelectors := []string{
nginxIngressControllerLabelSelector,
contourIngressControllerLabelSelector,
}
for _, labelSelector := range ingressControllerLabelsSelectors {
if err := c.collectIngressControllerReportByLabelSelector(labelSelector); err != nil {
return err
}
}

return nil
}

func (c *Config) collectIngressControllerReportByLabelSelector(labelSelector string) error {
namespaceList, err := c.KubeClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}

for _, namespace := range namespaceList.Items {
podList, err := c.KubeClient.CoreV1().Pods(namespace.Name).List(
context.Background(),
metav1.ListOptions{LabelSelector: labelSelector},
)
if err != nil {
return err
}

for _, pod := range podList.Items {
cmds := append(
ingressControllerReportCommands(pod.Namespace, pod.Name),
ingressReportCommands(pod.Namespace, pod.Name, "pods")...,
)
for _, cmd := range cmds {
filename := path.Join(
c.rootNamespaceDirPath(),
namespace.Name,
rootPodDirName,
pod.Name,
commandsDirName,
strings.Join(cmd, "_"),
)
if err := runCmdAndWriteToFile(cmd, filename); err != nil {
return err
}
}
c.completionSuccess("Collected report for ingress controller %s/%s", pod.Namespace, pod.Name)
}
}

return nil
}

func ingressReportCommands(namespace, resourceName, resourceType string) [][]string {
return [][]string{
{"kubectl", "get", resourceType, "-n", namespace, resourceName, "-o", "yaml"},
{"kubectl", "describe", resourceType, "-n", namespace, resourceName},
}
}

func ingressControllerReportCommands(namespace, resourceName string) [][]string {
return [][]string{
{"kubectl", "logs", "-n", namespace, resourceName},
}
}
15 changes: 15 additions & 0 deletions pkg/bugreport/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func (c *Config) Run() error {
c.completionSuccess("Finished generating report for control plane pods")
c.endSection()

// Generate report for ingress
if c.CollectIngress {
fmt.Fprintf(c.Stdout, "[+] Collecting ingress information\n")
if err := c.collectIngressReport(); err != nil {
c.completionFailure("Error getting ingress")
return errors.Wrap(err, "Error getting ingress")
}
if err := c.collectIngressControllerReport(); err != nil {
c.completionFailure("Error getting ingress controller")
return errors.Wrap(err, "Error getting ingress controller")
}
c.completionSuccess("Finished generating report for ingress")
c.endSection()
}

// Generate output file if not provided
if c.OutFile == "" {
outFd, err := ioutil.TempFile("", "*_osm-bug-report.tar.gz")
Expand Down
4 changes: 4 additions & 0 deletions pkg/bugreport/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import (

"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"

policyClientset "github.com/openservicemesh/osm/pkg/gen/client/policy/clientset/versioned"
)

// Config is the type used to hold the bug report configuration
type Config struct {
Stdout io.Writer
Stderr io.Writer
KubeClient kubernetes.Interface
PolicyClient policyClientset.Interface
ControlPlaneNamepace string
AppNamespaces []string
AppDeployments []types.NamespacedName
AppPods []types.NamespacedName
OutFile string
CollectIngress bool
stagingDir string
}

0 comments on commit 6ed8486

Please sign in to comment.