-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbackup.go
More file actions
196 lines (162 loc) · 5.83 KB
/
backup.go
File metadata and controls
196 lines (162 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2021 - 2026 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"time"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"github.com/crunchydata/postgres-operator-client/internal"
"github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1"
)
// newBackupCommand returns the backup command of the PGO plugin.
// It optionally takes a `repoName` and `options` flag, which it uses
// to update the spec.
func newBackupCommand(config *internal.Config) *cobra.Command {
cmdBackup := &cobra.Command{
Use: "backup CLUSTER_NAME",
Short: "Backup cluster",
Long: `Backup allows you to backup a PostgreSQL cluster either by using
the current "spec.backups.pgbackrest.manual" settings on the PostgreSQL cluster
or by using flags to write your settings. Overwriting those settings may require
the --force-conflicts flag.
### RBAC Requirements
Resources Verbs
--------- -----
postgresclusters.postgres-operator.crunchydata.com [get patch]
### Usage`,
// Limit the number of args, that is, only one cluster name
Args: cobra.ExactArgs(1),
}
cmdBackup.Example = internal.FormatExample(`# Trigger a backup on the 'hippo' postgrescluster using the current spec options
# Note: "spec.backups.pgbackrest.manual.repoName" has to exist for the backup to begin
pgo backup hippo
# Update the 'backups.pgbackrest.manual.repoName' and 'backups.pgbackrest.manual.options' fields
# on the 'hippo' postgrescluster and trigger a backup
pgo backup hippo --repoName="repo1" --options="--type=full"
# Resolve ownership conflict
pgo backup hippo --force-conflicts
### Example output
postgresclusters/hippo backup initiated`)
// `backup` command accepts `repoName`, `force-conflicts` and `options` flags;
// multiple options flags can be used, with each becoming a new line
// in the options array on the spec
backup := pgBackRestBackupArgs{}
cmdBackup.Flags().BoolVar(&backup.ForceConflicts, "force-conflicts", false, "take ownership and overwrite the backup settings")
cmdBackup.Flags().StringVar(&backup.RepoName, "repoName", "", "repoName to backup to")
cmdBackup.Flags().StringArrayVar(&backup.Options, "options", []string{},
"options for taking a backup; can be used multiple times")
// Define the 'backup' command
cmdBackup.RunE = func(cmd *cobra.Command, args []string) error {
// configure client
mapping, client, err := v1beta1.NewPostgresClusterClient(config)
if err != nil {
return err
}
// Pass args[0] as the name of the cluster object, limited to one through `ExactArgs(1)`
backup.ClusterName = args[0]
msg, err := backup.Run(client, config)
if msg != "" {
cmd.Println(msg)
}
if err == nil {
// Our `backup` command initiates a job, but does not signal to the user
// that a backup has finished; consider a `--wait` flag to wait until the
// backup is done.
cmd.Printf("%s/%s backup initiated\n", mapping.Resource.Resource, backup.ClusterName)
}
return err
}
return cmdBackup
}
type pgBackRestBackupArgs struct {
ClusterName string
ForceConflicts bool
Options []string
RepoName string
}
func (backup pgBackRestBackupArgs) modifyIntent(
intent *unstructured.Unstructured, now time.Time,
) error {
intent.SetAnnotations(internal.MergeStringMaps(
intent.GetAnnotations(), map[string]string{
"postgres-operator.crunchydata.com/pgbackrest-backup": now.UTC().Format(time.RFC3339),
}))
if value, path := backup.Options, []string{
"spec", "backups", "pgbackrest", "manual", "options",
}; len(value) == 0 {
unstructured.RemoveNestedField(intent.Object, path...)
} else if err := unstructured.SetNestedStringSlice(
intent.Object, value, path...,
); err != nil {
return err
}
if value, path := backup.RepoName, []string{
"spec", "backups", "pgbackrest", "manual", "repoName",
}; len(value) == 0 {
unstructured.RemoveNestedField(intent.Object, path...)
} else if err := unstructured.SetNestedField(
intent.Object, value, path...,
); err != nil {
return err
}
internal.RemoveEmptySections(intent,
"spec", "backups", "pgbackrest", "manual")
return nil
}
func (backup pgBackRestBackupArgs) Run(client dynamic.NamespaceableResourceInterface,
config *internal.Config) (string, error) {
var (
cluster *unstructured.Unstructured
err error
namespace string
patch []byte
)
ctx := context.Background()
// Get the namespace. This will either be from the Kubernetes configuration
// or from the --namespace (-n) flag.
if namespace, err = config.Namespace(); err != nil {
return "", err
}
if cluster, err = client.Namespace(namespace).Get(ctx,
backup.ClusterName,
metav1.GetOptions{},
); err != nil {
return "", err
}
intent := new(unstructured.Unstructured)
if err = internal.ExtractFieldsInto(
cluster, intent, config.Patch.FieldManager); err != nil {
return "", err
}
if err = backup.modifyIntent(intent, time.Now()); err != nil {
return "", err
}
if patch, err = intent.MarshalJSON(); err != nil {
return "Error packaging payload", err
}
// Update the spec/annotate
// TODO(benjaminjb): Would we want to allow a dry-run option here?
patchOptions := metav1.PatchOptions{}
if backup.ForceConflicts {
b := true
patchOptions.Force = &b
}
if _, err = client.Namespace(namespace).Patch(ctx,
backup.ClusterName,
types.ApplyPatchType,
patch,
config.Patch.PatchOptions(patchOptions),
); err != nil {
if apierrors.IsConflict(err) {
return "SUGGESTION: The --force-conflicts flag may help in performing this operation.", err
}
return "Error requesting update", err
}
return "", err
}