Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
minor refactoring to improve edge cases without backrest logs. renaming functions for consistency.
  • Loading branch information
Philip Hurst committed Aug 30, 2024
1 parent f597da8 commit cd48a19
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ Collecting PGO CLI logs...
// All Postgres Logs on the Postgres Instances (primary and replicas)
if numLogs > 0 {
if err == nil {
err = gatherAllPostgresLogs(ctx, clientset, restConfig, namespace, clusterName, numLogs, tw, cmd)
err = gatherPostgresLogsAndConfigs(ctx, clientset, restConfig, namespace, clusterName, numLogs, tw, cmd)
}
}

Expand Down Expand Up @@ -948,9 +948,9 @@ func gatherEvents(ctx context.Context,
return nil
}

// gatherAllPostgresLogs take a client and writes logs from primary and replicas
// gatherPostgresLogsAndConfigs take a client and writes logs from primary and replicas
// to a buffer
func gatherAllPostgresLogs(ctx context.Context,
func gatherPostgresLogsAndConfigs(ctx context.Context,
clientset *kubernetes.Clientset,
config *rest.Config,
namespace string,
Expand All @@ -974,7 +974,8 @@ func gatherAllPostgresLogs(ctx context.Context,
}

if len(dbPods.Items) == 0 {
writeDebug(cmd, "No database instance pod found for gathering logs")
writeInfo(cmd, "No database instance pod found for gathering logs and config")
return nil
}

writeDebug(cmd, fmt.Sprintf("Found %d Pods\n", len(dbPods.Items)))
Expand All @@ -996,18 +997,23 @@ func gatherAllPostgresLogs(ctx context.Context,
// Get Postgres Log Files
stdout, stderr, err := Executor(exec).listPGLogFiles(numLogs)
if err != nil {
if strings.Contains(stderr, "No such file or directory") {
writeDebug(cmd, "Cannot find any Postgres log files. This is acceptable in some configurations.\n")
}
if apierrors.IsForbidden(err) {
writeInfo(cmd, err.Error())
return nil
}
return err
continue
}
if stderr != "" {
writeInfo(cmd, stderr)
writeDebug(cmd, stderr)
continue
}

logFiles := strings.Split(strings.TrimSpace(stdout), "\n")
for _, logFile := range logFiles {
writeDebug(cmd, fmt.Sprintf("LOG FILE: %s\n", logFile))
var buf bytes.Buffer

stdout, stderr, err := Executor(exec).catFile(logFile)
Expand Down Expand Up @@ -1102,7 +1108,8 @@ func gatherDbBackrestLogs(ctx context.Context,
}

if len(dbPods.Items) == 0 {
writeDebug(cmd, "No database instance pod found for gathering logs")
writeInfo(cmd, "No database instance pod found for gathering logs")
return nil
}

writeDebug(cmd, fmt.Sprintf("Found %d Pods\n", len(dbPods.Items)))
Expand All @@ -1121,10 +1128,9 @@ func gatherDbBackrestLogs(ctx context.Context,
stdin, stdout, stderr, command...)
}

// Get pgBackRest Log Files
stdout, stderr, err := Executor(exec).listBackrestLogFiles()

if err != nil {
writeInfo(cmd, err.Error())
if strings.Contains(stderr, "No such file or directory") {
writeDebug(cmd, "Cannot find any Backrest log files. This is acceptable in some configurations.\n")
}
Expand All @@ -1135,7 +1141,8 @@ func gatherDbBackrestLogs(ctx context.Context,
continue
}
if stderr != "" {
writeInfo(cmd, stderr)
writeDebug(cmd, stderr)
continue
}

logFiles := strings.Split(strings.TrimSpace(stdout), "\n")
Expand Down Expand Up @@ -1195,19 +1202,12 @@ func gatherRepoHostLogs(ctx context.Context,
}

if len(repoHostPods.Items) == 0 {
writeDebug(cmd, "No Repo Host pod found for gathering logs")
writeInfo(cmd, "No Repo Host pod found for gathering logs")
}

writeDebug(cmd, fmt.Sprintf("Found %d Repo Host Pod\n", len(repoHostPods.Items)))

var pods = repoHostPods.Items

if len(pods) == 0 {
writeDebug(cmd, "No pods found for gathering logs")
return nil
}

for _, pod := range pods {
for _, pod := range repoHostPods.Items {
writeDebug(cmd, fmt.Sprintf("Pod Name is %s\n", pod.Name))

podExec, err := util.NewPodExecutor(config)
Expand All @@ -1221,20 +1221,26 @@ func gatherRepoHostLogs(ctx context.Context,
stdin, stdout, stderr, command...)
}

// Get BackRest Repo Host Log Files
stdout, stderr, err := Executor(exec).listBackrestRepoHostLogFiles()
if err != nil {
if strings.Contains(stderr, "No such file or directory") {
writeDebug(cmd, "Cannot find any Repo Host log files. This is acceptable in some configurations.\n")
}
if apierrors.IsForbidden(err) {
writeInfo(cmd, err.Error())
return nil
}
return err
continue
}
if stderr != "" {
writeInfo(cmd, stderr)
writeDebug(cmd, stderr)
continue
}

logFiles := strings.Split(strings.TrimSpace(stdout), "\n")
for _, logFile := range logFiles {
writeDebug(cmd, fmt.Sprintf("LOG FILE: %s\n", logFile))
var buf bytes.Buffer

stdout, stderr, err := Executor(exec).catFile(logFile)
Expand Down

0 comments on commit cd48a19

Please sign in to comment.