Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create optional backups flag #103

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions internal/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ HA
if stdout, stderr, err := getBackup(config, args, "text", ""); err != nil {
return err
} else {
cmd.Printf(stdout)
cmd.Printf("%s", stdout)
if stderr != "" {
cmd.Printf("\nError returned: %s\n", stderr)
}
Expand All @@ -106,7 +106,7 @@ HA
if stdout, stderr, err := getHA(config, args, "pretty"); err != nil {
return err
} else {
cmd.Printf(stdout)
cmd.Printf("%s", stdout)
if stderr != "" {
cmd.Printf("\nError returned: %s\n", stderr)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ stanza: db
stdout, stderr, err := getBackup(config, args, outputEnum.String(), repoNum)

if err == nil {
cmd.Printf(stdout)
cmd.Printf("%s", stdout)
if stderr != "" {
cmd.Printf("\nError returned: %s\n", stderr)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ pgo show ha hippo --output json
stdout, stderr, err := getHA(config, args, outputEnum.String())

if err == nil {
cmd.Printf(stdout)
cmd.Printf("%s", stdout)
if stderr != "" {
cmd.Printf("\nError returned: %s\n", stderr)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ postgresclusters/hippo stop initiated`)

msg, err := patchClusterShutdown(cluster, client, requestArgs)
if msg != "" {
cmd.Printf(msg)
cmd.Printf("%s", msg)
}
if err != nil {
return err
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
Loading