Skip to content

Commit

Permalink
Require a Postgres version when creating a PostgresCluster
Browse files Browse the repository at this point in the history
This change adds a new flag 'pg-major-version' to the 'create
postgrescluster' command to allow a specific Postgres version to
be used for the PostgresCluster. The new flag is required and does
not default to a particular version. As before, the image used by
the cluster will be pulled from the corresponding 'RELATED_IMAGE'
value set in the environment.

KUTTL test updates are also included, along with a bump to Postgres
15 and a timeout adjustment.

Issue: PGO-446
  • Loading branch information
tjmoore4 committed Oct 25, 2023
1 parent 8267ab1 commit 59724c4
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 20 deletions.
7 changes: 4 additions & 3 deletions docs/content/reference/pgo_create_postgrescluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ pgo create postgrescluster CLUSTER_NAME [flags]
### Examples

```
# Create a postgrescluster
pgo create postgrescluster hippo
# Create a postgrescluster with Postgres 15
pgo create postgrescluster hippo --pg-major-version 15
```

### Options

```
-h, --help help for postgrescluster
-h, --help help for postgrescluster
--pg-major-version int Set the Postgres major version
```

### Options inherited from parent commands
Expand Down
17 changes: 11 additions & 6 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"
"strconv"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -59,9 +60,13 @@ func newCreateClusterCommand(config *internal.Config) *cobra.Command {

cmd.Args = cobra.ExactArgs(1)

var pgMajorVersion int
cmd.Flags().IntVarP(&pgMajorVersion, "pg-major-version", "", 0, "Set the Postgres major version")
cobra.CheckErr(cmd.MarkFlagRequired("pg-major-version"))

cmd.Example = internal.FormatExample(`
# Create a postgrescluster
pgo create postgrescluster hippo
# Create a postgrescluster with Postgres 15
pgo create postgrescluster hippo --pg-major-version 15
`)

cmd.RunE = func(cmd *cobra.Command, args []string) error {
Expand All @@ -79,7 +84,7 @@ pgo create postgrescluster hippo
return err
}

cluster, err := generateUnstructuredClusterYaml(clusterName)
cluster, err := generateUnstructuredClusterYaml(clusterName, strconv.Itoa(pgMajorVersion))
if err != nil {
return err
}
Expand All @@ -101,15 +106,15 @@ pgo create postgrescluster hippo

// generateUnstructuredClusterYaml takes a name and returns a PostgresCluster
// in the unstructured format.
func generateUnstructuredClusterYaml(name string) (*unstructured.Unstructured, error) {
func generateUnstructuredClusterYaml(name, pgMajorVersion string) (*unstructured.Unstructured, error) {
var cluster unstructured.Unstructured
err := yaml.Unmarshal([]byte(fmt.Sprintf(`
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: %s
spec:
postgresVersion: 14
postgresVersion: %s
instances:
- dataVolumeClaimSpec:
accessModes:
Expand All @@ -128,7 +133,7 @@ spec:
resources:
requests:
storage: 1Gi
`, name)), &cluster)
`, name, pgMajorVersion)), &cluster)

if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ spec:
resources:
requests:
storage: 1Gi
postgresVersion: 14
postgresVersion: 15
`

u, err := generateUnstructuredClusterYaml("hippo")
u, err := generateUnstructuredClusterYaml("hippo", "15")
assert.NilError(t, err)

assert.Assert(t, cmp.MarshalMatches(
Expand Down
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/create/00--create_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: kubectl-pgo --namespace $NAMESPACE create postgrescluster kuttl-create-cluster
- script: kubectl-pgo --namespace $NAMESPACE create postgrescluster --pg-major-version 15 kuttl-create-cluster
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/create/00-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spec:
requests:
storage: 1Gi
replicas: 1
postgresVersion: 14
postgresVersion: 15
status:
instances:
- name: "00"
Expand Down
46 changes: 46 additions & 0 deletions testing/kuttl/e2e/create/01--check_version_validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: |
# Verify the error when the Postgres version given is too low.
TOO_LOW=$(kubectl-pgo create postgrescluster --pg-major-version=1 toolow 2>&1)
if [[ "${TOO_LOW}" != "Error:"*"Invalid value"* ]]; then
printf 'Expected invalid value error, got %q\n' "${TOO_LOW}"
exit 1
fi
# Verify the error when the Postgres version given is too high.
TOO_HIGH=$(kubectl-pgo create postgrescluster --pg-major-version=100 toohigh 2>&1)
if [[ "${TOO_HIGH}" != "Error:"*"Invalid value"* ]]; then
printf 'Expected invalid value error, got %q\n' "${TOO_HIGH}"
exit 1
fi
# Verify the error when the Postgres version is not an integer.
NOT_INT=$(kubectl-pgo create postgrescluster --pg-major-version=15.1 notint 2>&1)
if [[ "${NOT_INT}" != "Error: invalid argument"* ]]; then
printf 'Expected invalid argument error, got %q\n' "${NOT_INT}"
exit 1
fi
# Verify the error when the Postgres version is not a number.
NOT_NUM=$(kubectl-pgo create postgrescluster --pg-major-version=x notnum 2>&1)
if [[ "${NOT_NUM}" != "Error: invalid argument"* ]]; then
printf 'Expected invalid argument error, got %q\n' "${NOT_NUM}"
exit 1
fi
# Verify the error when the Postgres version flag is not provided.
MISSING=$(kubectl-pgo create postgrescluster missing 2>&1)
if [[ "${MISSING}" != "Error: required flag"* ]]; then
printf 'Expected required flag error, got %q\n' "${MISSING}"
exit 1
fi
# Verify the error when the Postgres version value is empty.
NOT_SET=$(kubectl-pgo create postgrescluster --pg-major-version= notset 2>&1)
if [[ "${NOT_SET}" != "Error: invalid argument"* ]]; then
printf 'Expected invalid argument error, got %q\n' "${NOT_SET}"
exit 1
fi
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/delete-cluster/00--cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: PostgresCluster
metadata:
name: delete-cluster
spec:
postgresVersion: 14
postgresVersion: 15
instances:
- name: instance1
dataVolumeClaimSpec:
Expand Down
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/restore/00--cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: PostgresCluster
metadata:
name: restore-cluster
spec:
postgresVersion: 14
postgresVersion: 15
instances:
- name: instance1
dataVolumeClaimSpec:
Expand Down
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/support-export/00--create_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: kubectl-pgo --namespace $NAMESPACE create postgrescluster kuttl-support-cluster
- script: kubectl-pgo --namespace $NAMESPACE create postgrescluster --pg-major-version 15 kuttl-support-cluster
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/support-export/00-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spec:
requests:
storage: 1Gi
replicas: 1
postgresVersion: 14
postgresVersion: 15
status:
instances:
- name: "00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: PostgresCluster
metadata:
name: kuttl-support-monitoring-cluster
spec:
postgresVersion: 14
postgresVersion: 15
instances:
- dataVolumeClaimSpec:
accessModes: [ReadWriteOnce]
Expand Down
2 changes: 1 addition & 1 deletion testing/kuttl/e2e/support-export/30-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spec:
requests:
storage: 1Gi
replicas: 1
postgresVersion: 14
postgresVersion: 15
status:
instances:
- name: "00"
Expand Down
2 changes: 1 addition & 1 deletion testing/kuttl/kuttl-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: kuttl.dev/v1beta1
kind: TestSuite
testDirs:
- testing/kuttl/e2e/
timeout: 180
timeout: 300
parallel: 2
# by default kuttl will run in a generated namespace to override
# that functionality simply uncomment the line below and replace
Expand Down

0 comments on commit 59724c4

Please sign in to comment.