Skip to content

Commit

Permalink
Create optional backups flag (CrunchyData#103)
Browse files Browse the repository at this point in the history
Make backups optional for create

As of PGO v5.7, backups can now be disabled or enabled at any point in the
postgrescluster's lifecycle. We don't want to bring that functionality entire
over into the CLI, but we do want to start by allowing users to create a cluster
without backups.

Since this is a not recommended for production, we require confirmation.

This should not block later changes that may allow users to disable/enable backups
after creation.

Issue: [PGO-1530]
  • Loading branch information
benjaminjb authored Oct 3, 2024
1 parent d41109f commit 7c1c509
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ help: ## Display this help
.PHONY: all
all: check build

bin/kubectl-pgo-%: ## Build the binary
bin/kubectl-pgo-%: go.* $(shell ls -1 cmd/**/*.go internal/**/*.go)
GOOS=$(word 1,$(subst -, ,$*)) GOARCH=$(word 2,$(subst -, ,$*)) $(GO_BUILD) -o $@ ./cmd/kubectl-pgo

Expand All @@ -43,7 +44,7 @@ build: bin/kubectl-pgo-$(subst $(eval) ,-,$(shell $(GO) env GOOS GOARCH))
ln -fs $(notdir $<) ./bin/kubectl-pgo

.PHONY: check
check:
check: ## Run tests
$(GO_TEST) -cover ./...

# Expects operator to be running
Expand Down
5 changes: 5 additions & 0 deletions docs/content/reference/pgo_create_postgrescluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pgo create postgrescluster CLUSTER_NAME [flags]
# Create a postgrescluster with Postgres 15
pgo create postgrescluster hippo --pg-major-version 15
# Create a postgrescluster with backups disabled (only available in CPK v5.7+)
# Requires confirmation
pgo create postgrescluster hippo --disable-backups
```
### Example output
```
Expand All @@ -35,6 +39,7 @@ postgresclusters/hippo created
### Options

```
--disable-backups Disable backups
-h, --help help for postgrescluster
--pg-major-version int Set the Postgres major version
```
Expand Down
27 changes: 27 additions & 0 deletions internal/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"context"
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -26,6 +27,7 @@ import (

"github.com/crunchydata/postgres-operator-client/internal"
"github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1"
"github.com/crunchydata/postgres-operator-client/internal/util"
)

// newCreateCommand returns the create subcommand of the PGO plugin.
Expand Down Expand Up @@ -66,9 +68,16 @@ func newCreateClusterCommand(config *internal.Config) *cobra.Command {
cmd.Flags().IntVar(&pgMajorVersion, "pg-major-version", 0, "Set the Postgres major version")
cobra.CheckErr(cmd.MarkFlagRequired("pg-major-version"))

var backupsDisabled bool
cmd.Flags().BoolVar(&backupsDisabled, "disable-backups", false, "Disable backups")

cmd.Example = internal.FormatExample(`# Create a postgrescluster with Postgres 15
pgo create postgrescluster hippo --pg-major-version 15
# Create a postgrescluster with backups disabled (only available in CPK v5.7+)
# Requires confirmation
pgo create postgrescluster hippo --disable-backups
### Example output
postgresclusters/hippo created`)

Expand All @@ -92,6 +101,24 @@ postgresclusters/hippo created`)
return err
}

if backupsDisabled {
fmt.Print("WARNING: Running a production postgrescluster without backups " +
"is not recommended. \nAre you sure you want " +
"to continue without backups? (yes/no): ")
var confirmed *bool
for i := 0; confirmed == nil && i < 10; i++ {
// retry 10 times or until a confirmation is given or denied,
// whichever comes first
confirmed = util.Confirm(os.Stdin, os.Stdout)
}

if confirmed == nil || !*confirmed {
return nil
}

unstructured.RemoveNestedField(cluster.Object, "spec", "backups")
}

u, err := client.
Namespace(namespace).
Create(ctx, cluster, config.Patch.CreateOptions(metav1.CreateOptions{}))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: echo no | kubectl-pgo --namespace $NAMESPACE create postgrescluster --pg-major-version 16 --disable-backups created-without-backups
20 changes: 20 additions & 0 deletions testing/kuttl/e2e/create-without-backups/00-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: created-without-backups
spec:
instances:
- dataVolumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
replicas: 1
postgresVersion: 16
status:
instances:
- name: "00"
readyReplicas: 1
replicas: 1
updatedReplicas: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: echo yes | kubectl-pgo --namespace $NAMESPACE create postgrescluster --pg-major-version 16 --disable-backups created-without-backups
20 changes: 20 additions & 0 deletions testing/kuttl/e2e/create-without-backups/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: created-without-backups
spec:
instances:
- dataVolumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
replicas: 1
postgresVersion: 16
status:
instances:
- name: "00"
readyReplicas: 1
replicas: 1
updatedReplicas: 1
4 changes: 4 additions & 0 deletions testing/kuttl/e2e/create-without-backups/02-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: created-without-backups-repo-host

0 comments on commit 7c1c509

Please sign in to comment.