Skip to content
96 changes: 95 additions & 1 deletion internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,73 @@ Collecting PGO CLI logs...
writeInfo(cmd, fmt.Sprintf("There is no PGUpgrade object associated with cluster '%s'", clusterName))
}

// Run kubectl describe and similar commands
writeInfo(cmd, "Running kubectl describe nodes...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/nodes", "describe", "nodes")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe nodes: %s", err))
}

writeInfo(cmd, "Running kubectl cluster-info dump...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/cluster-info.json", "cluster-info", "dump")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl cluster-info dump: %s", err))
}

writeInfo(cmd, "Running kubectl describe postgrescluster...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/postgrescluster", "describe", "postgrescluster", clusterName, "-n", namespace)
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe postgrescluster: %s", err))
}

writeInfo(cmd, "Running kubectl describe crd crunchybridgeclusters...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/crunchybridgeclusters", "describe", "crds", "crunchybridgeclusters")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd crunchybridgeclusters: %s", err))
}

writeInfo(cmd, "Running kubectl describe crd pgadmins...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/pgadmins", "describe", "crds", "pgadmins")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd pgadmins: %s", err))
}

writeInfo(cmd, "Running kubectl describe crd pgupgrades...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/pgupgrades", "describe", "crds", "pgupgrades")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd pgupgrades: %s", err))
}

writeInfo(cmd, "Running kubectl describe crd postgresclusters...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/crds/postgresclusters", "describe", "crds", "postgresclusters")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe crd postgresclusters: %s", err))
}

writeInfo(cmd, "Running kubectl describe clusterrole...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/clusterrole", "describe", "clusterrole", "postgres-operator")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe clusterrole: %s", err))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Should this be tucked into the logic block btw 611-615? Or wait -- should we print out the err we got on 610 before overwriting it with the err on 614?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I think there's a way to improve the flow and really show the check for 'postgresoperator' is a special case without make the overall structure too-hard-to-follow at a glance.


writeInfo(cmd, "Running kubectl describe clusterrolebinding...")
err = runKubectlCommand(tw, cmd, clusterName+"/describe/clusterrolebinding", "describe", "clusterrolebinding", "postgres-operator")
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe clusterrolebinding: %s", err))
}

writeInfo(cmd, "Running kubectl describe lease...")
err = runKubectlCommand(tw, cmd, "operator/describe/lease", "describe", "lease", "-n", operatorNamespace)
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe lease: %s", err))
}

writeInfo(cmd, "Running kubectl describe pgadmin...")
err = runKubectlCommand(tw, cmd, "pgadmin/describe/pgadmin", "describe", "pgadmin", "-n", namespace)
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe pgadmin: %s", err))
}

// Print cli output
writeInfo(cmd, "Collecting PGO CLI logs...")
path := clusterName + "/cli.log"
Expand Down Expand Up @@ -640,6 +707,28 @@ There was an error running 'kubectl get pgupgrade'. Verify permissions and that
return nil
}

func runKubectlCommand(tw *tar.Writer, cmd *cobra.Command, path string, cmdArgs ...string) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() // Ensure the context is canceled to avoid leaks

ex := exec.CommandContext(ctx, "kubectl", cmdArgs...)
msg, err := ex.Output()

if err != nil {
msg = append(msg, err.Error()...)
msg = append(msg, []byte(`
There was an error running the command. Verify permissions and that the resource exists.`)...)

writeInfo(cmd, fmt.Sprintf("Error: '%s'", msg))
}

if err := writeTar(tw, msg, path, cmd); err != nil {
return err
}

return nil
}

// exportSizeReport defines the message displayed when a support export archive
// is created. If the size of the archive file is greater than 25MiB, an alternate
// message is displayed.
Expand Down Expand Up @@ -1229,8 +1318,9 @@ func gatherPostgresLogsAndConfigs(ctx context.Context,
commands := []Command{
{path: "pg_controldata", description: "pg_controldata"},
{path: "df -h /pgdata", description: "disk free"},
{path: "du -h /pgdata", description: "disk usage"},
{path: "du -h /pgdata | column -t -o \" \"", description: "disk usage"},
{path: "ls /pgdata/*/archive_status/*.ready | wc -l", description: "Archive Ready File Count"},
{path: "psql -P format=wrapped -P columns=180 -c \"select name,setting,source,sourcefile,sourceline FROM pg_settings order by 1\"", description: "PG Settings"},
}

var buf bytes.Buffer
Expand Down Expand Up @@ -1670,6 +1760,10 @@ func gatherPodLogs(ctx context.Context,
}

for _, pod := range pods.Items {
err = runKubectlCommand(tw, cmd, rootDir+"/describe/"+"pods/"+pod.GetName(), "describe", "pods", pod.GetName(), "-n", namespace)
if err != nil {
writeInfo(cmd, fmt.Sprintf("Error running kubectl describe pods: %s", err))
}
containers := pod.Spec.Containers
containers = append(containers, pod.Spec.InitContainers...)
for _, container := range containers {
Expand Down