Skip to content

Commit

Permalink
Output progress to console, capture log, create archive, cleanup (ist…
Browse files Browse the repository at this point in the history
…io#27025)

* Output progress to console, capture log, create archive, cleanup

* Fix binary params
  • Loading branch information
ostromart authored Sep 10, 2020
1 parent 10d12b8 commit b0fbe73
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion istioctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ debug and diagnose their Istio mesh.
hideInheritedFlags(upgradeCmd, "namespace", "istioNamespace")
rootCmd.AddCommand(upgradeCmd)

bugReportCmd := bugreport.Cmd()
bugReportCmd := bugreport.Cmd(loggingOptions)
hideInheritedFlags(bugReportCmd, "namespace", "istioNamespace")
rootCmd.AddCommand(bugReportCmd)

Expand Down
3 changes: 2 additions & 1 deletion tools/bug-report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
"os"

"istio.io/istio/tools/bug-report/pkg/bugreport"
"istio.io/pkg/log"
)

func main() {
if err := bugreport.Cmd().Execute(); err != nil {
if err := bugreport.Cmd(log.DefaultOptions()).Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
Expand Down
4 changes: 4 additions & 0 deletions tools/bug-report/pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var (
initDir sync.Once
)

func OutputRootDir(rootDir string) string {
return getRootDir(rootDir)
}

func ProxyLogPath(rootDir, namespace, pod string) string {
dir := filepath.Join(getRootDir(rootDir), proxyLogsPathSubdir, namespace)
return filepath.Join(dir, pod+".log")
Expand Down
52 changes: 48 additions & 4 deletions tools/bug-report/pkg/bugreport/bugreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ var (
)

// Cmd returns a cobra command for bug-report.
func Cmd() *cobra.Command {
func Cmd(logOpts *log.Options) *cobra.Command {
rootCmd := &cobra.Command{
Use: "bug-report",
Short: "Cluster information and log capture support tool.",
SilenceUsage: true,
Long: "This command selectively captures cluster information and logs into an archive to help " +
"diagnose problems. It optionally uploads the archive to a GCS bucket.",
RunE: func(cmd *cobra.Command, args []string) error {
return runBugReportCommand(cmd)
return runBugReportCommand(cmd, logOpts)
},
}
rootCmd.AddCommand(version.CobraCommand())
Expand All @@ -83,7 +83,10 @@ var (
lock = sync.RWMutex{}
)

func runBugReportCommand(_ *cobra.Command) error {
func runBugReportCommand(_ *cobra.Command, logOpts *log.Options) error {
if err := configLogs(logOpts); err != nil {
return err
}
config, err := parseConfig()
if err != nil {
return err
Expand All @@ -108,7 +111,7 @@ func runBugReportCommand(_ *cobra.Command) error {
return err
}

log.Infof("Fetching logs for the following containers:\n\n%s\n", strings.Join(paths, "\n"))
logAndPrintf("Fetching proxy logs for the following containers:\n\n%s\n", strings.Join(paths, "\n"))

gatherInfo(client, config, resources, paths)
if len(gErrors) != 0 {
Expand All @@ -124,6 +127,24 @@ func runBugReportCommand(_ *cobra.Command) error {
}
writeFile(archive.ProxyLogPath(tempDir, namespace, pod), text)
}

outDir, err := os.Getwd()
if err != nil {
log.Errorf("using ./ to write archive: %s", err.Error())
outDir = "."
}
outPath := filepath.Join(outDir, "bug-report.tgz")
logAndPrintf("Creating archive at %s.\n", outPath)

tempRoot := archive.OutputRootDir(tempDir)
if err := archive.Create(tempRoot, outPath); err != nil {
return err
}
logAndPrintf("Cleaning up temporary files in %s.\n", tempRoot)
if err := os.RemoveAll(tempRoot); err != nil {
return err
}
logAndPrintf("Done.\n")
return nil
}

Expand All @@ -141,6 +162,7 @@ func gatherInfo(client kube.ExtendedClient, config *config.BugReportConfig, reso
Client: client,
DryRun: config.DryRun,
}
logAndPrintf("\nFetching Istio control plane information from cluster.\n\n")
getFromCluster(content.GetK8sResources, params, clusterDir, &mandatoryWg)
getFromCluster(content.GetCRs, params, clusterDir, &mandatoryWg)
getFromCluster(content.GetEvents, params, clusterDir, &mandatoryWg)
Expand Down Expand Up @@ -303,3 +325,25 @@ func BuildClientsFromConfig(kubeConfig []byte) (kube.Client, error) {
}
return clients, nil
}

func logAndPrintf(format string, a ...interface{}) {
fmt.Printf(format, a...)
log.Infof(format, a...)
}

func configLogs(opt *log.Options) error {
logDir := filepath.Join(archive.OutputRootDir(tempDir), "bug-report.log")
mkdirOrExit(logDir)
f, err := os.Create(logDir)
if err != nil {
return err
}
f.Close()
op := []string{logDir}
opt2 := *opt
opt2.OutputPaths = op
opt2.ErrorOutputPaths = op
opt2.SetOutputLevel("default", log.InfoLevel)

return log.Configure(&opt2)
}

0 comments on commit b0fbe73

Please sign in to comment.