Skip to content

Commit 8ea30a4

Browse files
Adds start and stop commands (#88)
* Adds start and stop commands Issue: PGO-450
1 parent aa50506 commit 8ea30a4

19 files changed

+606
-82
lines changed

docs/content/reference/_index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pgo is a kubectl plugin for PGO, the open source Postgres Operator from Crunchy
4444
* [pgo delete](/reference/pgo_delete/) - Delete a resource
4545
* [pgo restore](/reference/pgo_restore/) - Restore cluster
4646
* [pgo show](/reference/pgo_show/) - Show PostgresCluster details
47+
* [pgo start](/reference/pgo_start/) - Start cluster
48+
* [pgo stop](/reference/pgo_stop/) - Stop cluster
4749
* [pgo support](/reference/pgo_support/) - Crunchy Support commands for PGO
4850
* [pgo version](/reference/pgo_version/) - PGO client and operator versions
4951

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: pgo start
3+
---
4+
## pgo start
5+
6+
Start cluster
7+
8+
### Synopsis
9+
10+
Start sets the spec.shutdown field to false, allowing you to start a PostgreSQL cluster.
11+
The --force-conflicts flag may be required if the spec.shutdown field has been updated by another client.
12+
13+
### RBAC Requirements
14+
Resources Verbs
15+
--------- -----
16+
postgresclusters.postgres-operator.crunchydata.com [get patch]
17+
18+
### Usage
19+
20+
```
21+
pgo start CLUSTER_NAME [flags]
22+
```
23+
24+
### Examples
25+
26+
```
27+
# Start a 'hippo' postgrescluster.
28+
pgo start hippo
29+
30+
# Resolve ownership conflict
31+
pgo start hippo --force-conflicts
32+
33+
```
34+
### Example output
35+
```
36+
postgresclusters/hippo start initiated
37+
```
38+
39+
### Options
40+
41+
```
42+
--force-conflicts take ownership and overwrite the shutdown setting
43+
-h, --help help for start
44+
```
45+
46+
### Options inherited from parent commands
47+
48+
```
49+
--as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
50+
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
51+
--as-uid string UID to impersonate for the operation.
52+
--cache-dir string Default cache directory (default "$HOME/.kube/cache")
53+
--certificate-authority string Path to a cert file for the certificate authority
54+
--client-certificate string Path to a client certificate file for TLS
55+
--client-key string Path to a client key file for TLS
56+
--cluster string The name of the kubeconfig cluster to use
57+
--context string The name of the kubeconfig context to use
58+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
59+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
60+
-n, --namespace string If present, the namespace scope for this CLI request
61+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
62+
-s, --server string The address and port of the Kubernetes API server
63+
--tls-server-name string Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
64+
--token string Bearer token for authentication to the API server
65+
--user string The name of the kubeconfig user to use
66+
```
67+
68+
### SEE ALSO
69+
70+
* [pgo](/reference/) - pgo is a kubectl plugin for PGO, the open source Postgres Operator
71+

docs/content/reference/pgo_stop.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: pgo stop
3+
---
4+
## pgo stop
5+
6+
Stop cluster
7+
8+
### Synopsis
9+
10+
Stop sets the spec.shutdown field to true, allowing you to stop a PostgreSQL cluster.
11+
The --force-conflicts flag may be required if the spec.shutdown field has been used before.
12+
13+
### RBAC Requirements
14+
Resources Verbs
15+
--------- -----
16+
postgresclusters.postgres-operator.crunchydata.com [get patch]
17+
18+
### Usage
19+
20+
```
21+
pgo stop CLUSTER_NAME [flags]
22+
```
23+
24+
### Examples
25+
26+
```
27+
# Stop a 'hippo' postgrescluster.
28+
pgo stop hippo
29+
30+
# Resolve ownership conflict
31+
pgo stop hippo --force-conflicts
32+
33+
```
34+
### Example output
35+
```
36+
postgresclusters/hippo stop initiated
37+
```
38+
39+
### Options
40+
41+
```
42+
--force-conflicts take ownership and overwrite the shutdown setting
43+
-h, --help help for stop
44+
```
45+
46+
### Options inherited from parent commands
47+
48+
```
49+
--as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
50+
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
51+
--as-uid string UID to impersonate for the operation.
52+
--cache-dir string Default cache directory (default "$HOME/.kube/cache")
53+
--certificate-authority string Path to a cert file for the certificate authority
54+
--client-certificate string Path to a client certificate file for TLS
55+
--client-key string Path to a client key file for TLS
56+
--cluster string The name of the kubeconfig cluster to use
57+
--context string The name of the kubeconfig context to use
58+
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
59+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
60+
-n, --namespace string If present, the namespace scope for this CLI request
61+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
62+
-s, --server string The address and port of the Kubernetes API server
63+
--tls-server-name string Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
64+
--token string Bearer token for authentication to the API server
65+
--user string The name of the kubeconfig user to use
66+
```
67+
68+
### SEE ALSO
69+
70+
* [pgo](/reference/) - pgo is a kubectl plugin for PGO, the open source Postgres Operator
71+

internal/cmd/delete.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
package cmd
1616

1717
import (
18-
"bufio"
1918
"context"
2019
"fmt"
21-
"io"
2220
"os"
2321

2422
"github.com/spf13/cobra"
2523
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2624

2725
"github.com/crunchydata/postgres-operator-client/internal"
2826
"github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1"
27+
"github.com/crunchydata/postgres-operator-client/internal/util"
2928
)
3029

3130
// newDeleteCommand returns the delete subcommand of the PGO plugin.
@@ -80,7 +79,7 @@ postgresclusters/hippo deleted`)
8079
for i := 0; confirmed == nil && i < 10; i++ {
8180
// retry 10 times or until a confirmation is given or denied,
8281
// whichever comes first
83-
confirmed = confirm(os.Stdin, os.Stdout)
82+
confirmed = util.Confirm(os.Stdin, os.Stdout)
8483
}
8584

8685
if confirmed == nil || !*confirmed {
@@ -112,38 +111,6 @@ postgresclusters/hippo deleted`)
112111
return cmd
113112
}
114113

115-
// confirm uses a Scanner to parse user input. A user must type in "yes" or "no"
116-
// and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES",
117-
// and "Yes" all count as confirmations and return 'true'. Similarly, "n", "N",
118-
// "no", "No", "NO" all deny confirmation and return 'false'. If the input is not
119-
// recognized, nil is returned.
120-
func confirm(reader io.Reader, writer io.Writer) *bool {
121-
var response string
122-
var boolVar bool
123-
124-
scanner := bufio.NewScanner(reader)
125-
if scanner.Scan() {
126-
response = scanner.Text()
127-
}
128-
129-
if scanner.Err() != nil || response == "" {
130-
fmt.Fprint(writer, "Please type yes or no and then press enter: ")
131-
return nil
132-
}
133-
134-
yesResponses := []string{"y", "Y", "yes", "Yes", "YES"}
135-
noResponses := []string{"n", "N", "no", "No", "NO"}
136-
if containsString(yesResponses, response) {
137-
boolVar = true
138-
return &boolVar
139-
} else if containsString(noResponses, response) {
140-
return &boolVar
141-
} else {
142-
fmt.Fprint(writer, "Please type yes or no and then press enter: ")
143-
return nil
144-
}
145-
}
146-
147114
// containsString returns true if slice contains element
148115
func containsString(slice []string, element string) bool {
149116
for _, elem := range slice {

internal/cmd/delete_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,11 @@
1515
package cmd
1616

1717
import (
18-
"bytes"
19-
"io"
20-
"strings"
2118
"testing"
2219

2320
"gotest.tools/v3/assert"
2421
)
2522

26-
func TestConfirmDelete(t *testing.T) {
27-
28-
testsCases := []struct {
29-
input string
30-
invalidResponse bool
31-
confirmed bool
32-
}{
33-
{"abc", true, false}, // invalid
34-
{"", true, false}, // invalid
35-
{"y", false, true},
36-
{"Y", false, true},
37-
{"yes", false, true},
38-
{"Yes", false, true},
39-
{"YES", false, true},
40-
{"n", false, false},
41-
{"N", false, false},
42-
{"no", false, false},
43-
{"No", false, false},
44-
{"NO", false, false},
45-
{"yep", true, false}, // invalid
46-
{"nope", true, false}, // invalid
47-
}
48-
49-
for _, tc := range testsCases {
50-
t.Run("input is "+tc.input, func(t *testing.T) {
51-
var reader io.Reader = strings.NewReader(tc.input)
52-
var writer bytes.Buffer
53-
confirmed := confirm(reader, &writer)
54-
if tc.invalidResponse {
55-
assert.Assert(t, confirmed == nil)
56-
response, err := writer.ReadString(':')
57-
assert.NilError(t, err)
58-
assert.Equal(t, response, "Please type yes or no and then press enter:")
59-
60-
} else {
61-
assert.Assert(t, confirmed != nil)
62-
assert.Assert(t, *confirmed == tc.confirmed)
63-
}
64-
})
65-
}
66-
}
67-
6823
func TestContainsString(t *testing.T) {
6924
testsCases := []struct {
7025
desc string

internal/cmd/pgo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
133133
root.AddCommand(newShowCommand(config))
134134
root.AddCommand(newSupportCommand(config))
135135
root.AddCommand(newVersionCommand(config))
136+
root.AddCommand(newStopCommand(config))
137+
root.AddCommand(newStartCommand(config))
136138

137139
return root
138140
}

internal/cmd/restore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/crunchydata/postgres-operator-client/internal"
3030
"github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1"
31+
"github.com/crunchydata/postgres-operator-client/internal/util"
3132
)
3233

3334
func newRestoreCommand(config *internal.Config) *cobra.Command {
@@ -232,11 +233,10 @@ func (config pgBackRestRestore) Run(ctx context.Context) error {
232233

233234
func (config pgBackRestRestore) confirm(attempts int) *bool {
234235
for i := 0; i < attempts; i++ {
235-
if confirmed := confirm(config.In, config.Out); confirmed != nil {
236+
if confirmed := util.Confirm(config.In, config.Out); confirmed != nil {
236237
return confirmed
237238
}
238239
}
239-
240240
return nil
241241
}
242242

0 commit comments

Comments
 (0)