forked from libopenstorage/stork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterdomainupdate.go
309 lines (282 loc) · 9.36 KB
/
clusterdomainupdate.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package storkctl
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"time"
storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
"github.com/pborman/uuid"
storkops "github.com/portworx/sched-ops/k8s/stork"
"github.com/portworx/sched-ops/task"
"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 clusterDomainUpdateColumns = []string{"NAME", "CLUSTER-DOMAIN", "ACTION", "STATUS", "CREATED"}
var clusterDomainUpdateSubcommand = "clusterdomainupdate"
var clusterDomainUpdateAliases = []string{"cdu"}
var clusterDomainSubcommand = "clusterdomain"
var clusterDomainAliases = []string{"cd"}
const (
retryTimeout = 2 * time.Second
timeout = 10 * time.Minute
)
func newActivateClusterDomainCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
var allClusterDomains bool
var waitForCompletion bool
var nameClusterDomainUpdate string
activateClusterDomainCommand := &cobra.Command{
Use: clusterDomainSubcommand,
Aliases: clusterDomainAliases,
Short: "Activate a cluster domain",
Run: func(c *cobra.Command, args []string) {
activationList := []string{}
if allClusterDomains {
cdsList, err := storkops.Instance().ListClusterDomainStatuses()
if err != nil {
util.CheckErr(err)
return
}
for _, cds := range cdsList.Items {
for _, cdInfo := range cds.Status.ClusterDomainInfos {
if cdInfo.State == storkv1.ClusterDomainInactive {
activationList = append(activationList, cdInfo.Name)
}
}
}
} else if len(args) == 1 {
clusterDomainName := args[0]
cdsList, err := storkops.Instance().ListClusterDomainStatuses()
if err != nil {
util.CheckErr(fmt.Errorf("failed to list existing cluster domains: %v", err))
return
} else if cdsList == nil {
util.CheckErr(fmt.Errorf("could not find any configured cluster domains"))
return
}
found := false
for _, cds := range cdsList.Items {
for _, cdsInfo := range cds.Status.ClusterDomainInfos {
if cdsInfo.Name == clusterDomainName {
found = true
break
}
}
}
if !found {
util.CheckErr(fmt.Errorf("provided cluster domain %v not found", clusterDomainName))
return
}
activationList = []string{clusterDomainName}
} else {
util.CheckErr(fmt.Errorf("exactly one cluster domain name needs to be provided to the activate command"))
return
}
updateUUID := uuid.New()
for i, clusterDomainName := range activationList {
var name string
if len(nameClusterDomainUpdate) > 0 {
if len(activationList) > 1 {
name = nameClusterDomainUpdate + "-" + strconv.FormatInt(int64(i), 10)
} else {
name = nameClusterDomainUpdate
}
} else {
if len(activationList) > 1 {
name = updateUUID + "-" + strconv.FormatInt(int64(i), 10)
} else {
name = updateUUID
}
}
clusterDomainUpdate := &storkv1.ClusterDomainUpdate{
ObjectMeta: meta.ObjectMeta{
Name: name,
},
Spec: storkv1.ClusterDomainUpdateSpec{
ClusterDomain: clusterDomainName,
Active: true,
},
}
_, err := storkops.Instance().CreateClusterDomainUpdate(clusterDomainUpdate)
if err != nil {
util.CheckErr(fmt.Errorf("failed to activate cluster domain %v: %v", clusterDomainName, err))
return
}
msg := fmt.Sprintf("Cluster Domain activate operation started successfully for %v", clusterDomainName)
printMsg(msg, ioStreams.Out)
if waitForCompletion {
if _, err := fmt.Fprintf(ioStreams.Out, "Activating.."); err != nil {
util.CheckErr(err)
return
}
msg := waitForDomainUpdate(name)
printMsg(msg, ioStreams.Out)
}
}
},
}
activateClusterDomainCommand.Flags().BoolVarP(&allClusterDomains, "all", "a", false, "Activate all inactive cluster domains")
activateClusterDomainCommand.Flags().BoolVarP(&waitForCompletion, "wait", "", false, "Wait for clusterdomain update to complete")
activateClusterDomainCommand.Flags().StringVar(&nameClusterDomainUpdate, "name", "", "Name for the activate cluster domain action")
return activateClusterDomainCommand
}
func newDeactivateClusterDomainCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
var nameClusterDomainUpdate string
var waitForCompletion bool
deactivateClusterDomainCommand := &cobra.Command{
Use: clusterDomainSubcommand,
Aliases: clusterDomainAliases,
Short: "Deactivate a cluster domain",
Run: func(c *cobra.Command, args []string) {
if len(args) == 1 {
name := uuid.New()
if len(nameClusterDomainUpdate) > 0 {
name = nameClusterDomainUpdate
}
clusterDomainName := args[0]
cdsList, err := storkops.Instance().ListClusterDomainStatuses()
if err != nil {
util.CheckErr(fmt.Errorf("failed to list existing cluster domains: %v", err))
return
} else if cdsList == nil {
util.CheckErr(fmt.Errorf("could not find any configured cluster domains"))
return
}
found := false
for _, cds := range cdsList.Items {
for _, cdsInfo := range cds.Status.ClusterDomainInfos {
if cdsInfo.Name == clusterDomainName {
found = true
break
}
}
}
if !found {
util.CheckErr(fmt.Errorf("provided cluster domain %v not found", clusterDomainName))
return
}
clusterDomainUpdate := &storkv1.ClusterDomainUpdate{
ObjectMeta: meta.ObjectMeta{
Name: name,
},
Spec: storkv1.ClusterDomainUpdateSpec{
ClusterDomain: clusterDomainName,
Active: false,
},
}
_, err = storkops.Instance().CreateClusterDomainUpdate(clusterDomainUpdate)
if err != nil {
util.CheckErr(fmt.Errorf("failed to deactivate cluster domain %v: %v", clusterDomainName, err))
return
}
msg := fmt.Sprintf("Cluster Domain deactivate operation started successfully for %v", clusterDomainName)
printMsg(msg, ioStreams.Out)
if waitForCompletion {
if _, err := fmt.Fprintf(ioStreams.Out, "Deactivating.."); err != nil {
util.CheckErr(err)
return
}
msg := waitForDomainUpdate(name)
printMsg(msg, ioStreams.Out)
}
} else {
util.CheckErr(fmt.Errorf("exactly one cluster domain name needs to be provided to the deactivate command"))
return
}
},
}
deactivateClusterDomainCommand.Flags().StringVar(&nameClusterDomainUpdate, "name", "", "Name for the deactivate cluster domain action")
deactivateClusterDomainCommand.Flags().BoolVarP(&waitForCompletion, "wait", "", false, "Wait for clusterdomain update to complete")
return deactivateClusterDomainCommand
}
func newGetClusterDomainUpdateCommand(cmdFactory Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
getClusterDomainUpdateCommand := &cobra.Command{
Use: clusterDomainUpdateSubcommand,
Aliases: clusterDomainUpdateAliases,
Short: "Get cluster domain updates",
Run: func(c *cobra.Command, args []string) {
cdStatuses := new(storkv1.ClusterDomainUpdateList)
var err error
if len(args) > 0 {
for _, clusterID := range args {
cds, err := storkops.Instance().GetClusterDomainUpdate(clusterID)
if err != nil {
util.CheckErr(err)
return
}
cdStatuses.Items = append(cdStatuses.Items, *cds)
}
} else {
cdStatuses, err = storkops.Instance().ListClusterDomainUpdates()
if err != nil {
util.CheckErr(err)
return
}
}
if len(cdStatuses.Items) == 0 {
handleEmptyList(ioStreams.Out)
return
}
if err := printObjects(c, cdStatuses, cmdFactory, clusterDomainUpdateColumns, clusterDomainUpdatePrinter, ioStreams.Out); err != nil {
util.CheckErr(err)
return
}
},
}
cmdFactory.BindGetFlags(getClusterDomainUpdateCommand.Flags())
return getClusterDomainUpdateCommand
}
func clusterDomainUpdatePrinter(
cduList *storkv1.ClusterDomainUpdateList,
options printers.GenerateOptions,
) ([]metav1beta1.TableRow, error) {
if cduList == nil {
return nil, nil
}
rows := make([]metav1beta1.TableRow, 0)
for _, cdu := range cduList.Items {
updateAction := "Activate"
if !cdu.Spec.Active {
updateAction = "Deactivate"
}
creationTime := toTimeString(cdu.CreationTimestamp.Time)
row := getRow(&cdu,
[]interface{}{cdu.Name,
cdu.Spec.ClusterDomain,
updateAction,
cdu.Status.Status,
creationTime},
)
rows = append(rows, row)
}
return rows, nil
}
func waitForDomainUpdate(name string) string {
var msg string
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
t := func() (interface{}, bool, error) {
cds, err := storkops.Instance().GetClusterDomainUpdate(name)
if err != nil {
return fmt.Sprintf("Unable to retrive cluster details %v", err), false, err
}
fmt.Printf("..%v", cds.Status.Status)
if cds.Status.Status == storkv1.ClusterDomainUpdateStatusFailed {
msg = fmt.Sprintf("\nFailed to update ClusterDomain, Reason : %v", cds.Status.Reason)
return "", false, nil
}
if cds.Status.Status == storkv1.ClusterDomainUpdateStatusSuccessful {
msg = fmt.Sprintf("\nCluster Domain %v updated successfully", cds.Spec.ClusterDomain)
return "", false, nil
}
return nil, true, fmt.Errorf("%v", cds.Status.Status)
}
if _, err := task.DoRetryWithTimeout(t, timeout, retryTimeout); err != nil {
msg = "Timed out performing task"
}
return msg
}