Skip to content

Commit 3f28188

Browse files
authored
gather PGUpgrade resource (#132)
* gather PGUpgrade resource * use CommandContext to get around linters * rename 'get' to 'getCluster' for clarity
1 parent 8fb0eea commit 3f28188

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

internal/cmd/export.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ Collecting PGO CLI logs...
351351
return err
352352
}
353353

354-
get, err := postgresClient.Namespace(namespace).Get(ctx,
354+
getCluster, err := postgresClient.Namespace(namespace).Get(ctx,
355355
clusterName, metav1.GetOptions{})
356-
if err != nil || get == nil {
356+
if err != nil || getCluster == nil {
357357
if apierrors.IsForbidden(err) || apierrors.IsNotFound(err) {
358358
return err
359359
}
@@ -425,7 +425,7 @@ Collecting PGO CLI logs...
425425
}
426426

427427
// Gather PostgresCluster manifest
428-
err = gatherClusterSpec(get, clusterName, tw, cmd)
428+
err = gatherClusterSpec(getCluster, clusterName, tw, cmd)
429429
if err != nil {
430430
writeInfo(cmd, fmt.Sprintf("Error gathering PostgresCluster manifest: %s", err))
431431
}
@@ -462,7 +462,7 @@ Collecting PGO CLI logs...
462462
// All Postgres Logs on the Postgres Instances (primary and replicas)
463463
if numLogs > 0 {
464464
err = gatherPostgresLogsAndConfigs(ctx, clientset, restConfig,
465-
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd, get)
465+
namespace, clusterName, outputDir, outputFile, numLogs, tw, cmd, getCluster)
466466
if err != nil {
467467
writeInfo(cmd, fmt.Sprintf("Error gathering Postgres Logs and Config: %s", err))
468468
}
@@ -561,6 +561,22 @@ Collecting PGO CLI logs...
561561
writeInfo(cmd, fmt.Sprintf("Error gathering kubectl plugins: %s", err))
562562
}
563563

564+
// Get PGUpgrade spec (if available)
565+
writeInfo(cmd, "Collecting PGUpgrade spec (if available)...")
566+
567+
key := util.AllowUpgradeAnnotation()
568+
value, exists := getCluster.GetAnnotations()[key]
569+
570+
if exists {
571+
writeInfo(cmd, fmt.Sprintf("The PGUpgrade object is: %s", value))
572+
err = gatherPGUpgradeSpec(clusterName, namespace, value, tw, cmd)
573+
if err != nil {
574+
writeInfo(cmd, fmt.Sprintf("Error gathering PGUpgrade spec: %s", err))
575+
}
576+
} else {
577+
writeInfo(cmd, fmt.Sprintf("There is no PGUpgrade object associated with cluster '%s'", clusterName))
578+
}
579+
564580
// Print cli output
565581
writeInfo(cmd, "Collecting PGO CLI logs...")
566582
path := clusterName + "/cli.log"
@@ -579,7 +595,10 @@ Collecting PGO CLI logs...
579595
}
580596

581597
func gatherPluginList(clusterName string, tw *tar.Writer, cmd *cobra.Command) error {
582-
ex := exec.Command("kubectl", "plugin", "list")
598+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
599+
defer cancel() // Ensure the context is canceled to avoid leaks
600+
601+
ex := exec.CommandContext(ctx, "kubectl", "plugin", "list")
583602
msg, err := ex.Output()
584603

585604
if err != nil {
@@ -594,6 +613,29 @@ func gatherPluginList(clusterName string, tw *tar.Writer, cmd *cobra.Command) er
594613
return nil
595614
}
596615

616+
func gatherPGUpgradeSpec(clusterName, namespace, pgUpgrade string, tw *tar.Writer, cmd *cobra.Command) error {
617+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
618+
defer cancel() // Ensure the context is canceled to avoid leaks
619+
620+
ex := exec.CommandContext(ctx, "kubectl", "get", "pgupgrade", pgUpgrade, "-n", namespace, "-o", "yaml")
621+
msg, err := ex.Output()
622+
623+
if err != nil {
624+
msg = append(msg, err.Error()...)
625+
msg = append(msg, []byte(`
626+
There was an error running 'kubectl get pgupgrade'. Verify permissions and that the resource exists.`)...)
627+
628+
writeInfo(cmd, fmt.Sprintf("Error: '%s'", msg))
629+
}
630+
631+
path := clusterName + "/pgupgrade.yaml"
632+
if err := writeTar(tw, msg, path, cmd); err != nil {
633+
return err
634+
}
635+
636+
return nil
637+
}
638+
597639
// exportSizeReport defines the message displayed when a support export archive
598640
// is created. If the size of the archive file is greater than 25MiB, an alternate
599641
// message is displayed.

internal/util/naming.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,9 @@ func PostgresUserSecretLabels(clusterName string) string {
8989
return LabelCluster + "=" + clusterName + "," +
9090
LabelRole + "=" + RolePostgresUser
9191
}
92+
93+
// AllowUpgradeAnnotation is the annotation key to allow of PostgresCluster
94+
// to upgrade. Its value is the name of the PGUpgrade object.
95+
func AllowUpgradeAnnotation() string {
96+
return labelPrefix + "allow-upgrade"
97+
}

0 commit comments

Comments
 (0)