Skip to content

Commit

Permalink
fix broken dryrun logic (istio#44433)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxiaop authored Apr 20, 2023
1 parent 9abe5e0 commit e64918a
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions tools/bug-report/pkg/bugreport/bugreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func runBugReportCommand(_ *cobra.Command, logOpts *log.Options) error {
return err
}

dumpRevisionsAndVersions(resources, config.KubeConfigPath, config.Context, config.IstioNamespace)
dumpRevisionsAndVersions(resources, config.KubeConfigPath, config.Context, config.IstioNamespace, config.DryRun)

log.Infof("Cluster resource tree:\n\n%s\n\n", resources)
paths, err := filter.GetMatchingPaths(config, resources)
Expand All @@ -170,7 +170,7 @@ func runBugReportCommand(_ *cobra.Command, logOpts *log.Options) error {
log.Errorf(err.Error())
continue
}
writeFile(filepath.Join(archive.ProxyOutputPath(tempDir, namespace, pod), common.ProxyContainerName+".log"), text)
writeFile(filepath.Join(archive.ProxyOutputPath(tempDir, namespace, pod), common.ProxyContainerName+".log"), text, config.DryRun)
}

outDir, err := os.Getwd()
Expand All @@ -182,24 +182,28 @@ func runBugReportCommand(_ *cobra.Command, logOpts *log.Options) error {
outDir = outputDir
}
outPath := filepath.Join(outDir, "bug-report.tar.gz")
common.LogAndPrintf("Creating an archive at %s.\n", outPath)

archiveDir := archive.DirToArchive(tempDir)
if tempDir != "" {
archiveDir = tempDir
}
if err := archive.Create(archiveDir, outPath); err != nil {
return err
}
common.LogAndPrintf("Cleaning up temporary files in %s.\n", archiveDir)
if err := os.RemoveAll(archiveDir); err != nil {
return err
if !config.DryRun {
common.LogAndPrintf("Creating an archive at %s.\n", outPath)
archiveDir := archive.DirToArchive(tempDir)
if tempDir != "" {
archiveDir = tempDir
}
if err := archive.Create(archiveDir, outPath); err != nil {
return err
}
common.LogAndPrintf("Cleaning up temporary files in %s.\n", archiveDir)
if err := os.RemoveAll(archiveDir); err != nil {
return err
}
} else {
common.LogAndPrintf("Dry run, skipping archive creation at %s.\n", outPath)
}
common.LogAndPrintf("Done.\n")
return nil
}

func dumpRevisionsAndVersions(resources *cluster2.Resources, kubeconfig, configContext, istioNamespace string) {
func dumpRevisionsAndVersions(resources *cluster2.Resources, kubeconfig, configContext, istioNamespace string, dryRun bool) {
text := ""
text += fmt.Sprintf("CLI version:\n%s\n\n", version.Info.LongForm())

Expand All @@ -214,7 +218,7 @@ func dumpRevisionsAndVersions(resources *cluster2.Resources, kubeconfig, configC
text += fmt.Sprintf("Revision %s: Versions {%s}\n", rev, strings.Join(ver, ", "))
}
common.LogAndPrintf(text)
writeFile(filepath.Join(archive.OutputRootDir(tempDir), "versions"), text)
writeFile(filepath.Join(archive.OutputRootDir(tempDir), "versions"), text, dryRun)
}

// getIstioRevisions returns a slice with all Istio revisions detected in the cluster.
Expand Down Expand Up @@ -360,7 +364,7 @@ func getFromCluster(f func(params *content.Params) (map[string]string, error), p
out, err := f(params)
appendGlobalErr(err)
if err == nil {
writeFiles(dir, out)
writeFiles(dir, out, params.DryRun)
}
log.Infof("Done with %s", runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
}()
Expand Down Expand Up @@ -398,7 +402,7 @@ func getIstiodLogs(runner *kubectlcmd.Runner, config *config.BugReportConfig, re
defer wg.Done()
clog, _, _, err := getLog(runner, resources, config, namespace, pod, common.DiscoveryContainerName)
appendGlobalErr(err)
writeFile(filepath.Join(archive.IstiodPath(tempDir, namespace, pod), "discovery.log"), clog)
writeFile(filepath.Join(archive.IstiodPath(tempDir, namespace, pod), "discovery.log"), clog, config.DryRun)
log.Infof("Done with logs %s", pod)
}()
}
Expand All @@ -413,7 +417,7 @@ func getOperatorLogs(runner *kubectlcmd.Runner, config *config.BugReportConfig,
defer wg.Done()
clog, _, _, err := getLog(runner, resources, config, namespace, pod, common.OperatorContainerName)
appendGlobalErr(err)
writeFile(filepath.Join(archive.OperatorPath(tempDir, namespace, pod), "operator.log"), clog)
writeFile(filepath.Join(archive.OperatorPath(tempDir, namespace, pod), "operator.log"), clog, config.DryRun)
log.Infof("Done with logs %s", pod)
}()
}
Expand Down Expand Up @@ -451,16 +455,19 @@ func runAnalyze(config *config.BugReportConfig, params *content.Params, analyzeT
common.LogAndPrintf("\nAnalysis Report:\n")
common.LogAndPrintf(out[common.StrNamespaceAll])
common.LogAndPrintf("\n")
writeFiles(archive.AnalyzePath(tempDir, common.StrNamespaceAll), out)
writeFiles(archive.AnalyzePath(tempDir, common.StrNamespaceAll), out, config.DryRun)
}

func writeFiles(dir string, files map[string]string) {
func writeFiles(dir string, files map[string]string, dryRun bool) {
for fname, text := range files {
writeFile(filepath.Join(dir, fname), text)
writeFile(filepath.Join(dir, fname), text, dryRun)
}
}

func writeFile(path, text string) {
func writeFile(path, text string, dryRun bool) {
if dryRun {
return
}
if strings.TrimSpace(text) == "" {
return
}
Expand Down

0 comments on commit e64918a

Please sign in to comment.