From 21406e1e18d359db7e16e46db56aeb2c9875b6a1 Mon Sep 17 00:00:00 2001 From: TJ Moore Date: Mon, 30 Oct 2023 16:48:50 -0400 Subject: [PATCH] Add allow sub-command output to 'pgo show' command This commit adds the default output from 'pgo show backup' and 'pgo show ha' to the 'pgo show' command. Issue: PGO-13 --- docs/content/reference/pgo_show.md | 44 +++++++++- internal/cmd/show.go | 82 ++++++++++++++++++- .../kuttl/e2e/show-backup/00--cluster.yaml | 25 ------ testing/kuttl/e2e/show-backup/00-assert.yaml | 49 ----------- .../e2e/{show-ha => show}/00--cluster.yaml | 2 +- .../e2e/{show-ha => show}/00-assert.yaml | 8 +- .../01--pgbackrest-info.yaml | 4 +- .../02--pgbackrest-info-repo1.yaml | 4 +- .../03--pgbackrest-info-output.yaml | 4 +- .../04--patronictl-list.yaml} | 4 +- .../05--patronictl-list-json.yaml} | 4 +- testing/kuttl/e2e/show/06--combined-show.yaml | 47 +++++++++++ 12 files changed, 183 insertions(+), 94 deletions(-) delete mode 100644 testing/kuttl/e2e/show-backup/00--cluster.yaml delete mode 100644 testing/kuttl/e2e/show-backup/00-assert.yaml rename testing/kuttl/e2e/{show-ha => show}/00--cluster.yaml (95%) rename testing/kuttl/e2e/{show-ha => show}/00-assert.yaml (84%) rename testing/kuttl/e2e/{show-backup => show}/01--pgbackrest-info.yaml (88%) rename testing/kuttl/e2e/{show-backup => show}/02--pgbackrest-info-repo1.yaml (89%) rename testing/kuttl/e2e/{show-backup => show}/03--pgbackrest-info-output.yaml (89%) rename testing/kuttl/e2e/{show-ha/01--patronictl-list.yaml => show/04--patronictl-list.yaml} (82%) rename testing/kuttl/e2e/{show-ha/02--patronictl-list-json.yaml => show/05--patronictl-list-json.yaml} (81%) create mode 100644 testing/kuttl/e2e/show/06--combined-show.yaml diff --git a/docs/content/reference/pgo_show.md b/docs/content/reference/pgo_show.md index b359d2ee..b5528884 100644 --- a/docs/content/reference/pgo_show.md +++ b/docs/content/reference/pgo_show.md @@ -7,7 +7,49 @@ Show PostgresCluster details ### Synopsis -Show allows you to display particular details related to the PostgresCluster +Show allows you to display particular details related to the PostgresCluster. + +### RBAC Requirements +Resources Verbs +--------- ----- +pods [list] +pods/exec [create] + +### Usage + +``` +pgo show [flags] +``` + +### Examples + +``` +# Show the backup and HA output of the 'hippo' postgrescluster +pgo show hippo +BACKUP + +stanza: db + status: ok + cipher: none + + db (current) + wal archive min/max (14): 000000010000000000000001/000000010000000000000003 + + full backup: 20231030-183841F + timestamp start/stop: 2023-10-30 18:38:41+00 / 2023-10-30 18:38:46+00 + wal start/stop: 000000010000000000000002 / 000000010000000000000002 + database size: 25.3MB, database backup size: 25.3MB + repo1: backup set size: 3.2MB, backup size: 3.2MB + +HA + ++ Cluster: hippo-ha (7295822780081832000) -----+--------+---------+----+-----------+ +| Member | Host | Role | State | TL | Lag in MB | ++-----------------+----------------------------+--------+---------+----+-----------+ +| hippo-00-cwqq-0 | hippo-00-cwqq-0.hippo-pods | Leader | running | 1 | | ++-----------------+----------------------------+--------+---------+----+-----------+ + +``` ### Options diff --git a/internal/cmd/show.go b/internal/cmd/show.go index 79373dbf..a09dbc21 100644 --- a/internal/cmd/show.go +++ b/internal/cmd/show.go @@ -35,17 +35,91 @@ func newShowCommand(config *internal.Config) *cobra.Command { cmdShow := &cobra.Command{ Use: "show", Short: "Show PostgresCluster details", - Long: "Show allows you to display particular details related to the PostgresCluster", + Long: `Show allows you to display particular details related to the PostgresCluster. + +### RBAC Requirements +Resources Verbs +--------- ----- +pods [list] +pods/exec [create] + +### Usage`, } + cmdShow.Example = internal.FormatExample(`# Show the backup and HA output of the 'hippo' postgrescluster +pgo show hippo +BACKUP + +stanza: db + status: ok + cipher: none + + db (current) + wal archive min/max (14): 000000010000000000000001/000000010000000000000003 + + full backup: 20231030-183841F + timestamp start/stop: 2023-10-30 18:38:41+00 / 2023-10-30 18:38:46+00 + wal start/stop: 000000010000000000000002 / 000000010000000000000002 + database size: 25.3MB, database backup size: 25.3MB + repo1: backup set size: 3.2MB, backup size: 3.2MB + +HA + ++ Cluster: hippo-ha (7295822780081832000) -----+--------+---------+----+-----------+ +| Member | Host | Role | State | TL | Lag in MB | ++-----------------+----------------------------+--------+---------+----+-----------+ +| hippo-00-cwqq-0 | hippo-00-cwqq-0.hippo-pods | Leader | running | 1 | | ++-----------------+----------------------------+--------+---------+----+-----------+ +`) + cmdShow.AddCommand( newShowBackupCommand(config), newShowHACommand(config), ) - // No arguments for 'show', but there are arguments for the subcommands, e.g. - // 'show backup' - cmdShow.Args = cobra.NoArgs + // Limit the number of args, that is, only one cluster name + cmdShow.Args = cobra.ExactArgs(1) + + // Define the 'show backup' command + cmdShow.RunE = func(cmd *cobra.Command, args []string) error { + + // The only thing we need is the value after 'repo' which should be an + // integer. If anything else is provided, we let the pgbackrest command + // handle validation. + + exec, err := getPrimaryExec(config, args) + if err != nil { + return err + } + + // for 'show backup', use 'text' ouput and don't specify a repo + stdoutBackup, stderrBackup, err := Executor(exec).pgBackRestInfo("text", "") + if err != nil { + return err + } + + // Print the pgbackrest info output received. + cmd.Printf("BACKUP\n\n") + cmd.Printf(stdoutBackup) + if stderrBackup != "" { + cmd.Printf("\nError returned: %s\n", stderrBackup) + } + + // use default list command output + stdoutHA, stderrHA, err := Executor(exec).patronictl("list", false) + if err != nil { + return err + } + + // Print the patronictl list output received. + cmd.Println("\nHA\n") + cmd.Printf(stdoutHA) + if stderrHA != "" { + cmd.Printf("\nError returned: %s\n", stderrHA) + } + + return nil + } return cmdShow } diff --git a/testing/kuttl/e2e/show-backup/00--cluster.yaml b/testing/kuttl/e2e/show-backup/00--cluster.yaml deleted file mode 100644 index a5c974c6..00000000 --- a/testing/kuttl/e2e/show-backup/00--cluster.yaml +++ /dev/null @@ -1,25 +0,0 @@ -apiVersion: postgres-operator.crunchydata.com/v1beta1 -kind: PostgresCluster -metadata: - name: show-backup-cluster -spec: - postgresVersion: 15 - instances: - - name: instance1 - dataVolumeClaimSpec: - accessModes: - - "ReadWriteOnce" - resources: - requests: - storage: 1Gi - backups: - pgbackrest: - repos: - - name: repo1 - volume: - volumeClaimSpec: - accessModes: - - "ReadWriteOnce" - resources: - requests: - storage: 1Gi diff --git a/testing/kuttl/e2e/show-backup/00-assert.yaml b/testing/kuttl/e2e/show-backup/00-assert.yaml deleted file mode 100644 index 125f9632..00000000 --- a/testing/kuttl/e2e/show-backup/00-assert.yaml +++ /dev/null @@ -1,49 +0,0 @@ -apiVersion: postgres-operator.crunchydata.com/v1beta1 -kind: PostgresCluster -metadata: - name: show-backup-cluster -status: - instances: - - name: instance1 - readyReplicas: 1 - replicas: 1 - updatedReplicas: 1 - pgbackrest: - repoHost: - apiVersion: apps/v1 - kind: StatefulSet - ready: true - repos: - - bound: true - name: repo1 - replicaCreateBackupComplete: true - stanzaCreated: true ---- -apiVersion: batch/v1 -kind: Job -metadata: - labels: - postgres-operator.crunchydata.com/cluster: show-backup-cluster - postgres-operator.crunchydata.com/pgbackrest-backup: replica-create - postgres-operator.crunchydata.com/pgbackrest-repo: repo1 -status: - succeeded: 1 ---- -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - labels: - postgres-operator.crunchydata.com/cluster: show-backup-cluster - postgres-operator.crunchydata.com/data: pgbackrest - postgres-operator.crunchydata.com/pgbackrest: "" - postgres-operator.crunchydata.com/pgbackrest-repo: repo1 - postgres-operator.crunchydata.com/pgbackrest-volume: "" - name: show-backup-cluster-repo1 -spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi -status: - phase: Bound diff --git a/testing/kuttl/e2e/show-ha/00--cluster.yaml b/testing/kuttl/e2e/show/00--cluster.yaml similarity index 95% rename from testing/kuttl/e2e/show-ha/00--cluster.yaml rename to testing/kuttl/e2e/show/00--cluster.yaml index 4e034eb3..eac550f4 100644 --- a/testing/kuttl/e2e/show-ha/00--cluster.yaml +++ b/testing/kuttl/e2e/show/00--cluster.yaml @@ -1,7 +1,7 @@ apiVersion: postgres-operator.crunchydata.com/v1beta1 kind: PostgresCluster metadata: - name: show-ha-cluster + name: show-cluster spec: postgresVersion: 15 instances: diff --git a/testing/kuttl/e2e/show-ha/00-assert.yaml b/testing/kuttl/e2e/show/00-assert.yaml similarity index 84% rename from testing/kuttl/e2e/show-ha/00-assert.yaml rename to testing/kuttl/e2e/show/00-assert.yaml index ca5bd5da..a1fa7919 100644 --- a/testing/kuttl/e2e/show-ha/00-assert.yaml +++ b/testing/kuttl/e2e/show/00-assert.yaml @@ -1,7 +1,7 @@ apiVersion: postgres-operator.crunchydata.com/v1beta1 kind: PostgresCluster metadata: - name: show-ha-cluster + name: show-cluster status: instances: - name: instance1 @@ -23,7 +23,7 @@ apiVersion: batch/v1 kind: Job metadata: labels: - postgres-operator.crunchydata.com/cluster: show-ha-cluster + postgres-operator.crunchydata.com/cluster: show-cluster postgres-operator.crunchydata.com/pgbackrest-backup: replica-create postgres-operator.crunchydata.com/pgbackrest-repo: repo1 status: @@ -33,12 +33,12 @@ apiVersion: v1 kind: PersistentVolumeClaim metadata: labels: - postgres-operator.crunchydata.com/cluster: show-ha-cluster + postgres-operator.crunchydata.com/cluster: show-cluster postgres-operator.crunchydata.com/data: pgbackrest postgres-operator.crunchydata.com/pgbackrest: "" postgres-operator.crunchydata.com/pgbackrest-repo: repo1 postgres-operator.crunchydata.com/pgbackrest-volume: "" - name: show-ha-cluster-repo1 + name: show-cluster-repo1 spec: accessModes: - ReadWriteOnce diff --git a/testing/kuttl/e2e/show-backup/01--pgbackrest-info.yaml b/testing/kuttl/e2e/show/01--pgbackrest-info.yaml similarity index 88% rename from testing/kuttl/e2e/show-backup/01--pgbackrest-info.yaml rename to testing/kuttl/e2e/show/01--pgbackrest-info.yaml index c82ef6ef..c1e9ed67 100644 --- a/testing/kuttl/e2e/show-backup/01--pgbackrest-info.yaml +++ b/testing/kuttl/e2e/show/01--pgbackrest-info.yaml @@ -5,7 +5,7 @@ commands: PRIMARY=$( kubectl get pod --namespace "${NAMESPACE}" \ --output name --selector ' - postgres-operator.crunchydata.com/cluster=show-backup-cluster, + postgres-operator.crunchydata.com/cluster=show-cluster, postgres-operator.crunchydata.com/role=master' ) @@ -15,7 +15,7 @@ commands: ) CLI_INFO=$( - kubectl-pgo --namespace "${NAMESPACE}" show backup show-backup-cluster + kubectl-pgo --namespace "${NAMESPACE}" show backup show-cluster ) status=$? diff --git a/testing/kuttl/e2e/show-backup/02--pgbackrest-info-repo1.yaml b/testing/kuttl/e2e/show/02--pgbackrest-info-repo1.yaml similarity index 89% rename from testing/kuttl/e2e/show-backup/02--pgbackrest-info-repo1.yaml rename to testing/kuttl/e2e/show/02--pgbackrest-info-repo1.yaml index 21ea197c..24768e94 100644 --- a/testing/kuttl/e2e/show-backup/02--pgbackrest-info-repo1.yaml +++ b/testing/kuttl/e2e/show/02--pgbackrest-info-repo1.yaml @@ -5,7 +5,7 @@ commands: PRIMARY=$( kubectl get pod --namespace "${NAMESPACE}" \ --output name --selector ' - postgres-operator.crunchydata.com/cluster=show-backup-cluster, + postgres-operator.crunchydata.com/cluster=show-cluster, postgres-operator.crunchydata.com/role=master' ) @@ -15,7 +15,7 @@ commands: ) CLI_INFO=$( - kubectl-pgo --namespace "${NAMESPACE}" show backup show-backup-cluster \ + kubectl-pgo --namespace "${NAMESPACE}" show backup show-cluster \ --repoName=repo1 ) diff --git a/testing/kuttl/e2e/show-backup/03--pgbackrest-info-output.yaml b/testing/kuttl/e2e/show/03--pgbackrest-info-output.yaml similarity index 89% rename from testing/kuttl/e2e/show-backup/03--pgbackrest-info-output.yaml rename to testing/kuttl/e2e/show/03--pgbackrest-info-output.yaml index 64834fe0..32dd0e80 100644 --- a/testing/kuttl/e2e/show-backup/03--pgbackrest-info-output.yaml +++ b/testing/kuttl/e2e/show/03--pgbackrest-info-output.yaml @@ -5,7 +5,7 @@ commands: PRIMARY=$( kubectl get pod --namespace "${NAMESPACE}" \ --output name --selector ' - postgres-operator.crunchydata.com/cluster=show-backup-cluster, + postgres-operator.crunchydata.com/cluster=show-cluster, postgres-operator.crunchydata.com/role=master' ) @@ -15,7 +15,7 @@ commands: ) CLI_INFO=$( - kubectl-pgo --namespace "${NAMESPACE}" show backup show-backup-cluster \ + kubectl-pgo --namespace "${NAMESPACE}" show backup show-cluster \ --output=json ) diff --git a/testing/kuttl/e2e/show-ha/01--patronictl-list.yaml b/testing/kuttl/e2e/show/04--patronictl-list.yaml similarity index 82% rename from testing/kuttl/e2e/show-ha/01--patronictl-list.yaml rename to testing/kuttl/e2e/show/04--patronictl-list.yaml index 7b3b03b2..f8e42939 100644 --- a/testing/kuttl/e2e/show-ha/01--patronictl-list.yaml +++ b/testing/kuttl/e2e/show/04--patronictl-list.yaml @@ -5,7 +5,7 @@ commands: PRIMARY=$( kubectl get pod --namespace "${NAMESPACE}" \ --output name --selector ' - postgres-operator.crunchydata.com/cluster=show-ha-cluster, + postgres-operator.crunchydata.com/cluster=show-cluster, postgres-operator.crunchydata.com/role=master' ) @@ -15,7 +15,7 @@ commands: ) CLI_INFO=$( - kubectl-pgo --namespace "${NAMESPACE}" show ha show-ha-cluster + kubectl-pgo --namespace "${NAMESPACE}" show ha show-cluster ) status=$? diff --git a/testing/kuttl/e2e/show-ha/02--patronictl-list-json.yaml b/testing/kuttl/e2e/show/05--patronictl-list-json.yaml similarity index 81% rename from testing/kuttl/e2e/show-ha/02--patronictl-list-json.yaml rename to testing/kuttl/e2e/show/05--patronictl-list-json.yaml index ec79d8ac..acd36eb2 100644 --- a/testing/kuttl/e2e/show-ha/02--patronictl-list-json.yaml +++ b/testing/kuttl/e2e/show/05--patronictl-list-json.yaml @@ -5,7 +5,7 @@ commands: PRIMARY=$( kubectl get pod --namespace "${NAMESPACE}" \ --output name --selector ' - postgres-operator.crunchydata.com/cluster=show-ha-cluster, + postgres-operator.crunchydata.com/cluster=show-cluster, postgres-operator.crunchydata.com/role=master' ) @@ -15,7 +15,7 @@ commands: ) CLI_INFO=$( - kubectl-pgo --namespace "${NAMESPACE}" show ha show-ha-cluster --json + kubectl-pgo --namespace "${NAMESPACE}" show ha show-cluster --json ) status=$? diff --git a/testing/kuttl/e2e/show/06--combined-show.yaml b/testing/kuttl/e2e/show/06--combined-show.yaml new file mode 100644 index 00000000..29cb8e21 --- /dev/null +++ b/testing/kuttl/e2e/show/06--combined-show.yaml @@ -0,0 +1,47 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +commands: +- script: | + PRIMARY=$( + kubectl get pod --namespace "${NAMESPACE}" \ + --output name --selector ' + postgres-operator.crunchydata.com/cluster=show-cluster, + postgres-operator.crunchydata.com/role=master' + ) + + PGBACKREST_INFO_EXEC=$( + kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \ + pgbackrest info + ) + + PATRONI_LIST_EXEC=$( + kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \ + patronictl list + ) + + EXPECTED_SHOW_COMMAND="BACKUP + + $PGBACKREST_INFO_EXEC + + HA + + $PATRONI_LIST_EXEC" + + echo $EXPECTED_SHOW_COMMAND + + CLI_INFO=$( + kubectl-pgo --namespace "${NAMESPACE}" show show-cluster + ) + + status=$? + if [ $status -ne 0 ]; then + echo "pgo command unsuccessful" + exit 1 + fi + + # check command output is not empty and equals expected output + if [[ -n $CLI_INFO && "$EXPECTED_SHOW_COMMAND" == "$CLI_INFO" ]]; then + exit 0 + fi + + exit 1