forked from libopenstorage/stork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupsnapshot.go
236 lines (204 loc) · 7.11 KB
/
groupsnapshot.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package storkctl
import (
"fmt"
"strings"
storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
storkops "github.com/portworx/sched-ops/k8s/stork"
"github.com/spf13/cobra"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubernetes/pkg/printers"
)
var groupSnapshotColumns = []string{"NAME", "STATUS", "STAGE", "SNAPSHOTS", "CREATED"}
var groupSnapshotSubcommand = "groupsnapshots"
var groupSnapshotAliases = []string{"groupsnapshot"}
func newCreateGroupSnapshotCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
var groupSnapshotName string
var restoreNamespaces []string
var opts []string
var pvcSelectors []string
var preExecRule string
var postExecRule string
var maxRetries int
createGroupVolumeSnapshotCommand := &cobra.Command{
Use: groupSnapshotSubcommand,
Aliases: groupSnapshotAliases,
Short: "Create a group volume snapshot",
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
util.CheckErr(fmt.Errorf("exactly one name needs to be provided for groupsnapshot name"))
return
}
groupSnapshotName = args[0]
if len(pvcSelectors) == 0 {
util.CheckErr(fmt.Errorf("PVC label selectors must be provided"))
return
}
labelSelector, err := parseKeyValueList(pvcSelectors)
if err != nil {
util.CheckErr(err)
return
}
pvcSelectorSpec := storkv1.PVCSelectorSpec{
LabelSelector: meta.LabelSelector{
MatchLabels: labelSelector,
},
}
var optsMap map[string]string
if len(opts) > 0 {
optsMap, err = parseKeyValueList(opts)
if err != nil {
util.CheckErr(err)
return
}
}
groupSnapshot := &storkv1.GroupVolumeSnapshot{
Spec: storkv1.GroupVolumeSnapshotSpec{
PreExecRule: preExecRule,
PostExecRule: postExecRule,
PVCSelector: pvcSelectorSpec,
RestoreNamespaces: restoreNamespaces,
MaxRetries: maxRetries,
Options: optsMap,
},
}
groupSnapshot.Name = groupSnapshotName
groupSnapshot.Namespace = cmdFactory.GetNamespace()
_, err = storkops.Instance().CreateGroupSnapshot(groupSnapshot)
if err != nil {
util.CheckErr(err)
return
}
msg := fmt.Sprintf("GroupVolumeSnapshot %v created successfully", groupSnapshot.Name)
printMsg(msg, ioStreams.Out)
},
}
createGroupVolumeSnapshotCommand.Flags().StringSliceVarP(
&pvcSelectors, "pvcSelectors", "", nil,
"Comma-separated list of PVC selectors in the format key1=value1,key2=value2. "+" e.g app=mysql,tier=db")
createGroupVolumeSnapshotCommand.Flags().StringVarP(
&preExecRule, "preExecRule", "", "", "Rule to run before triggering group volume snapshot")
createGroupVolumeSnapshotCommand.Flags().StringVarP(
&postExecRule, "postExecRule", "", "", "Rule to run after triggering group volume snapshot")
createGroupVolumeSnapshotCommand.Flags().StringSliceVarP(
&restoreNamespaces, "restoreNamespaces", "", nil,
"List of namespaces to which the snapshots can be restored to")
createGroupVolumeSnapshotCommand.Flags().IntVarP(
&maxRetries, "maxRetries", "", 0, "Number of times to retry the groupvolumesnapshot on failure")
createGroupVolumeSnapshotCommand.Flags().StringSliceVarP(
&opts, "opts", "", nil,
"Comma-separated list of options to provide to the storage driver. These "+
"are in the format key1=value1,key2=value2. e.g portworx/snapshot-type=cloud")
return createGroupVolumeSnapshotCommand
}
func newGetGroupVolumeSnapshotCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
getGroupVolumeSnapshotCommand := &cobra.Command{
Use: groupSnapshotSubcommand,
Aliases: groupSnapshotAliases,
Short: "Get group volume snapshots",
Run: func(c *cobra.Command, args []string) {
var groupSnapshots *storkv1.GroupVolumeSnapshotList
var err error
namespaces, err := cmdFactory.GetAllNamespaces()
if err != nil {
util.CheckErr(err)
return
}
groupSnapshots = new(storkv1.GroupVolumeSnapshotList)
if len(args) > 0 {
for _, groupSnapshotName := range args {
for _, ns := range namespaces {
groupSnapshot, err := storkops.Instance().GetGroupSnapshot(groupSnapshotName, ns)
if err != nil {
util.CheckErr(err)
return
}
groupSnapshots.Items = append(groupSnapshots.Items, *groupSnapshot)
}
}
} else {
// Get all
for _, ns := range namespaces {
groupSnapshotsInNamespace, err := storkops.Instance().ListGroupSnapshots(ns)
if err != nil {
util.CheckErr(err)
return
}
groupSnapshots.Items = append(groupSnapshots.Items, groupSnapshotsInNamespace.Items...)
}
}
if len(groupSnapshots.Items) == 0 {
handleEmptyList(ioStreams.Out)
return
}
if err := printObjects(c, groupSnapshots, cmdFactory, groupSnapshotColumns, groupSnapshotPrinter, ioStreams.Out); err != nil {
util.CheckErr(err)
return
}
},
}
cmdFactory.BindGetFlags(getGroupVolumeSnapshotCommand.Flags())
return getGroupVolumeSnapshotCommand
}
func newDeleteGroupVolumeSnapshotCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
deleteGroupVolumeSnapshotCommand := &cobra.Command{
Use: groupSnapshotSubcommand,
Aliases: groupSnapshotAliases,
Short: "Delete group volume snapshots",
Run: func(c *cobra.Command, args []string) {
if len(args) == 0 {
util.CheckErr(fmt.Errorf("at least one argument needs to be provided for groupsnapshot name"))
return
}
deleteGroupVolumeSnapshots(args, cmdFactory.GetNamespace(), ioStreams)
},
}
return deleteGroupVolumeSnapshotCommand
}
func deleteGroupVolumeSnapshots(groupSnapshots []string, namespace string, ioStreams genericclioptions.IOStreams) {
for _, groupSnapshot := range groupSnapshots {
err := storkops.Instance().DeleteGroupSnapshot(groupSnapshot, namespace)
if err != nil {
util.CheckErr(err)
return
}
msg := fmt.Sprintf("GroupVolumeSnapshot %v deleted successfully", groupSnapshot)
printMsg(msg, ioStreams.Out)
}
}
func groupSnapshotPrinter(
groupSnapshotList *storkv1.GroupVolumeSnapshotList,
options printers.GenerateOptions,
) ([]metav1beta1.TableRow, error) {
if groupSnapshotList == nil {
return nil, nil
}
rows := make([]metav1beta1.TableRow, 0)
for _, groupSnapshot := range groupSnapshotList.Items {
creationTime := toTimeString(groupSnapshot.CreationTimestamp.Time)
row := getRow(&groupSnapshot,
[]interface{}{groupSnapshot.Name,
groupSnapshot.Status.Status,
groupSnapshot.Status.Stage,
len(groupSnapshot.Status.VolumeSnapshots),
creationTime},
)
rows = append(rows, row)
}
return rows, nil
}
// parseKeyValueList parses a list of key values into a map
func parseKeyValueList(expressions []string) (map[string]string, error) {
matchLabels := make(map[string]string)
for _, e := range expressions {
entry := strings.SplitN(e, "=", 2)
if len(entry) != 2 {
return nil, fmt.Errorf("invalid key value: %s provided. "+
"Example format: app=mysql", e)
}
matchLabels[entry[0]] = entry[1]
}
return matchLabels, nil
}