-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy patharango_client_impl.go
302 lines (244 loc) · 8.51 KB
/
arango_client_impl.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
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package backup
import (
"context"
"encoding/json"
"fmt"
"time"
"k8s.io/client-go/kubernetes"
"github.com/arangodb/go-driver"
backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1"
database "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/util/arangod"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)
type arangoClientBackupImpl struct {
deployment *database.ArangoDeployment
backup *backupApi.ArangoBackup
driver driver.Client
kubecli kubernetes.Interface
}
func newArangoClientBackupFactory(handler *handler) ArangoClientFactory {
return func(deployment *database.ArangoDeployment, backup *backupApi.ArangoBackup) (ArangoBackupClient, error) {
ctx := context.Background()
client, err := arangod.CreateArangodDatabaseClient(ctx, handler.kubeClient.CoreV1(), deployment, false, true)
if err != nil {
return nil, err
}
return &arangoClientBackupImpl{
deployment: deployment,
backup: backup,
driver: client,
kubecli: handler.kubeClient,
}, nil
}
}
func (ac *arangoClientBackupImpl) List() (map[driver.BackupID]driver.BackupMeta, error) {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
backups, err := ac.driver.Backup().List(ctx, nil)
if err != nil {
return nil, err
}
return backups, nil
}
func (ac *arangoClientBackupImpl) Create() (ArangoBackupCreateResponse, error) {
dt := globals.GetGlobalTimeouts().BackupArangoClientTimeout().Get()
co := driver.BackupCreateOptions{}
if opt := ac.backup.Spec.Options; opt != nil {
if allowInconsistent := opt.AllowInconsistent; allowInconsistent != nil {
co.AllowInconsistent = *allowInconsistent
}
if timeout := opt.Timeout; timeout != nil {
co.Timeout = time.Duration(*timeout * float32(time.Second))
dt += co.Timeout
}
}
ctx, cancel := context.WithTimeout(context.Background(), dt)
defer cancel()
id, resp, err := ac.driver.Backup().Create(ctx, &co)
if err != nil {
return ArangoBackupCreateResponse{}, err
}
// Now ask for the version
meta, err := ac.Get(id)
if err != nil {
return ArangoBackupCreateResponse{}, err
}
return ArangoBackupCreateResponse{
PotentiallyInconsistent: resp.PotentiallyInconsistent,
BackupMeta: meta,
}, nil
}
func (ac *arangoClientBackupImpl) CreateAsync(jobID string) (ArangoBackupCreateResponse, error) {
dt := globals.GetGlobalTimeouts().BackupArangoClientTimeout().Get()
co := driver.BackupCreateOptions{}
if opt := ac.backup.Spec.Options; opt != nil {
if allowInconsistent := opt.AllowInconsistent; allowInconsistent != nil {
co.AllowInconsistent = *allowInconsistent
}
}
ctx, cancel := context.WithTimeout(context.Background(), dt)
defer cancel()
if jobID == "" {
ctx = driver.WithAsync(ctx)
} else {
ctx = driver.WithAsyncID(ctx, jobID)
}
id, resp, err := ac.driver.Backup().Create(ctx, &co)
if err != nil {
return ArangoBackupCreateResponse{}, err
}
// Now ask for the version
meta, err := ac.Get(id)
if err != nil {
return ArangoBackupCreateResponse{}, err
}
return ArangoBackupCreateResponse{
PotentiallyInconsistent: resp.PotentiallyInconsistent,
BackupMeta: meta,
}, nil
}
func (ac *arangoClientBackupImpl) Get(backupID driver.BackupID) (driver.BackupMeta, error) {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
// list, err := ac.driver.Backup().List(ctx, &driver.BackupListOptions{ID: backupID})
list, err := ac.driver.Backup().List(ctx, nil)
if err != nil {
return driver.BackupMeta{}, err
}
meta, ok := list[backupID]
if ok {
return meta, nil
}
return driver.BackupMeta{}, driver.ArangoError{
ErrorMessage: fmt.Sprintf("backup %s was not found", backupID),
Code: 404,
}
}
func (ac *arangoClientBackupImpl) getCredentialsFromSecret(ctx context.Context, secretName string) (interface{}, error) {
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
defer cancel()
token, err := k8sutil.GetTokenSecret(ctxChild, ac.kubecli.CoreV1().Secrets(ac.backup.Namespace), secretName)
if err != nil {
return nil, err
}
var raw json.RawMessage
if err := json.Unmarshal([]byte(token), &raw); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal credentials: ")
}
return raw, nil
}
func (ac *arangoClientBackupImpl) Upload(backupID driver.BackupID) (driver.BackupTransferJobID, error) {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientUploadTimeout().WithTimeout(context.Background())
defer cancel()
uploadSpec := ac.backup.Spec.Upload
if uploadSpec == nil {
return "", errors.Errorf("upload was called but no upload spec was given")
}
cred, err := ac.getCredentialsFromSecret(ctx, uploadSpec.CredentialsSecretName)
if err != nil {
return "", err
}
return ac.driver.Backup().Upload(ctx, backupID, uploadSpec.RepositoryURL, cred)
}
func (ac *arangoClientBackupImpl) Download(backupID driver.BackupID) (driver.BackupTransferJobID, error) {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientUploadTimeout().WithTimeout(context.Background())
defer cancel()
downloadSpec := ac.backup.Spec.Download
if downloadSpec == nil {
return "", errors.Errorf("Download was called but not download spec was given")
}
cred, err := ac.getCredentialsFromSecret(ctx, downloadSpec.CredentialsSecretName)
if err != nil {
return "", err
}
return ac.driver.Backup().Download(ctx, backupID, downloadSpec.RepositoryURL, cred)
}
func (ac *arangoClientBackupImpl) Progress(jobID driver.BackupTransferJobID) (ArangoBackupProgress, error) {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
report, err := ac.driver.Backup().Progress(ctx, jobID)
if err != nil {
return ArangoBackupProgress{}, err
}
if report.Cancelled {
return ArangoBackupProgress{
Failed: true,
FailMessage: "Upload cancelled",
}, nil
}
var ret ArangoBackupProgress
var completedCount int
var total int
var done int
for _, status := range report.DBServers {
total += status.Progress.Total
done += status.Progress.Done
switch status.Status {
case driver.TransferFailed:
ret.Failed = true
ret.FailMessage = status.ErrorMessage
case driver.TransferCompleted:
completedCount++
case driver.TransferAcknowledged:
case driver.TransferStarted:
case "":
completedCount++
default:
return ArangoBackupProgress{}, errors.Errorf("Unknown transfer status: %s", status.Status)
}
}
// Check if all defined servers are completed and the total number of files is greater than 0 (there is at least 1 file per server)
ret.Completed = completedCount == len(report.DBServers) && total > 0
if total != 0 {
ret.Progress = (100 * done) / total
}
return ret, nil
}
func (ac *arangoClientBackupImpl) Exists(backupID driver.BackupID) (bool, error) {
_, err := ac.Get(backupID)
if err != nil {
if driver.IsNotFoundGeneral(err) {
return false, nil
}
return false, err
}
return true, nil
}
func (ac *arangoClientBackupImpl) Delete(backupID driver.BackupID) error {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
return ac.driver.Backup().Delete(ctx, backupID)
}
func (ac *arangoClientBackupImpl) Abort(jobID driver.BackupTransferJobID) error {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
return ac.driver.Backup().Abort(ctx, jobID)
}
func (ac *arangoClientBackupImpl) HealthCheck() error {
ctx, cancel := globals.GetGlobalTimeouts().BackupArangoClientTimeout().WithTimeout(context.Background())
defer cancel()
_, err := ac.driver.Version(ctx)
return err
}