/
operations.go
257 lines (211 loc) · 5.89 KB
/
operations.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
package sidecar
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/instaclustr/cassandra-operator/pkg/common/operations"
)
//go:generate jsonenums -type=Kind
type Kind int
// Operations
const (
noop Kind = iota
cleanup
upgradesstables
decommission
backup
rebuild
scrub
)
type OperationsFilter struct {
Types []Kind
States []operations.OperationState
}
func (o OperationsFilter) buildFilteredEndpoint(endpoint string) string {
var filterT, filterS string
if len(o.Types) > 0 {
var kinds []string
for _, kind := range o.Types {
kinds = append(kinds, _KindValueToName[kind])
}
filterT = "type=" + strings.Join(kinds, ",")
}
if len(o.States) > 0 {
var states []string
for _, state := range o.States {
states = append(states, string(state))
}
filterS = "state=" + strings.Join(states, ",")
}
filter := strings.Join([]string{filterT, filterS}, "&")
if len(filter) > 0 {
return endpoint + "?" + filter
}
return endpoint
}
type OperationRequest interface {
Init()
}
type Operation struct {
Type Kind `json:"type"`
}
type DecommissionRequest struct {
Operation
}
func (d *DecommissionRequest) Init() {
d.Type = decommission
}
type CleanupRequest struct {
Operation
Jobs int32 `json:"jobs,omitempty"`
Tables []string `json:"tables,omitempty"`
Keyspace string `json:"keyspace"`
}
func (c *CleanupRequest) Init() {
c.Type = cleanup
}
type BackupRequest struct {
Operation
StorageLocation string `json:"storageLocation"`
SnapshotTag string `json:"snapshotTag,omitempty"`
ConcurrentConnections int `json:"concurrentConnections,omitempty"`
Entities string `json:"entities,omitempty"`
Secret string `json:"k8sSecretName"`
KubernetesNamespace string `json:"k8sNamespace"`
GlobalRequest bool `json:"globalRequest,omitempty"`
CreateMissingBucket bool `json:"createMissingBucket,omitempty"`
SkipBucketVerification bool `json:"skipBucketVerification,omitempty"`
MetadataDirective string `json:"metadataDirective,omitempty"`
Insecure bool `json:"insecure,omitempty"`
}
func (b *BackupRequest) Init() {
b.Type = backup
}
type UpgradeSSTablesRequest struct {
Operation
IncludeAllSSTables bool `json:"includeAllSSTables,omitempty"`
Jobs int32 `json:"jobs,omitempty"`
Tables []string `json:"tables,omitempty"`
Keyspace string `json:"keyspace"`
}
func (u *UpgradeSSTablesRequest) Init() {
u.Type = upgradesstables
}
type TokenRange struct {
Start string `json:"start"`
End string `json:"end"`
}
type RebuildRequest struct {
Operation
SourceDC string `json:"sourceDC,omitempty"`
Keyspace string `json:"keyspace"`
SpecificSources []string `json:"specificSources,omitempty"`
SpecificTokens []TokenRange `json:"specificTokens,omitempty"`
}
func (r *RebuildRequest) Init() {
r.Type = rebuild
}
type ScrubRequest struct {
Operation
DisableSnapshot bool `json:"disableSnapshot,omitempty"`
SkipCorrupted bool `json:"skipCorrupted,omitempty"`
NoValidate bool `json:"noValidate,omitempty"`
ReinsertOverflowedTTL bool `json:"reinsertOverflowedTTL,omitempty"`
Jobs int32 `json:"jobs,omitempty"`
Tables []string `json:"tables,omitempty"`
Keyspace string `json:"keyspace"`
}
func (s *ScrubRequest) Init() {
s.Type = scrub
}
type OperationResponse map[string]interface{}
type Operations []OperationResponse
type BasicResponse struct {
Id uuid.UUID `json:"id"`
CreationTime time.Time `json:"creationTime"`
State operations.OperationState `json:"state"`
Progress float32 `json:"progress"`
StartTime time.Time `json:"startTime"`
CompletionTime time.Time `json:"completionTime"`
}
// decommission Operations
type DecommissionOperationResponse struct {
BasicResponse
DecommissionRequest
}
// cleanup Operations
type CleanupOperationResponse struct {
BasicResponse
CleanupRequest
}
func (c *CleanupOperationResponse) String() string {
op, _ := json.Marshal(c)
return string(op)
}
func (client *Client) ListCleanups() ([]*CleanupOperationResponse, error) {
var cleanups []*CleanupOperationResponse
ops, err := client.GetFilteredOperations(&OperationsFilter{Types: []Kind{cleanup}})
if ops == nil || err != nil {
return cleanups, err
}
for _, op := range *ops {
cleanOp, err := ParseOperation(op, cleanup)
if err != nil {
return []*CleanupOperationResponse{}, err
}
cleanups = append(cleanups, cleanOp.(*CleanupOperationResponse))
}
return cleanups, nil
}
func (client *Client) FindBackup(id uuid.UUID) (backupResponse *BackupResponse, err error) {
if op, err := client.GetOperation(id); err != nil {
return nil, err
} else if b, err := ParseOperation(*op, backup); err != nil {
return nil, err
} else if backupResponse, ok := b.(*BackupResponse); !ok {
return nil, fmt.Errorf("can't parse operation to backup")
} else {
return backupResponse, nil
}
}
// backup Operations
type BackupResponse struct {
BasicResponse
BackupRequest
}
func (b *BackupResponse) String() string {
op, _ := json.Marshal(b)
return string(op)
}
func (client *Client) ListBackups() ([]*BackupResponse, error) {
ops, err := client.GetOperations()
if ops == nil || err != nil {
return []*BackupResponse{}, err
}
backupOps, err := FilterOperations(*ops, backup)
if err != nil {
return []*BackupResponse{}, err
}
var backups []*BackupResponse
for _, op := range backupOps {
backups = append(backups, op.(*BackupResponse))
}
return backups, nil
}
// UpgradeSSTables
type UpgradeSSTablesResponse struct {
BasicResponse
UpgradeSSTablesRequest
}
// rebuild
type RebuildResponse struct {
BasicResponse
RebuildRequest
}
// scrub
type ScrubResponse struct {
BasicResponse
ScrubRequest
}