-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackups.go
More file actions
176 lines (152 loc) · 5.07 KB
/
backups.go
File metadata and controls
176 lines (152 loc) · 5.07 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
package stackit
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/stackiterrors"
)
const (
backupReadyStatus = "AVAILABLE"
backupErrorStatus = "error"
backupDescription = "Created by STACKIT CSI driver"
BackupMaxDurationSecondsPerGBDefault = 20
BackupMaxDurationPerGB = "backup-max-duration-seconds-per-gb"
backupBaseDurationSeconds = 30
backupReadyCheckIntervalSeconds = 7
)
func (os *iaasClient) CreateBackup(ctx context.Context, name, volID, snapshotID string, tags map[string]string) (*iaas.Backup, error) {
if name == "" {
return nil, errors.New("backup name cannot be empty")
}
if volID == "" && snapshotID == "" {
return nil, errors.New("either volID or snapshotID must be provided")
}
var backupSource VolumeSourceTypes
var backupSourceID string
if volID != "" {
backupSource = VolumeSource
backupSourceID = volID
}
if snapshotID != "" {
backupSource = SnapshotSource
backupSourceID = snapshotID
}
opts := iaas.CreateBackupPayload{
Name: new(name),
Source: &iaas.BackupSource{
Type: new(string(backupSource)),
Id: new(backupSourceID),
},
}
if tags != nil {
opts.Labels = new(map[string]any(labelsFromTags(tags)))
}
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
createBackupRequest := os.iaas.CreateBackup(ctxWithHTTPResp, os.projectID, os.region)
if createBackupRequest == nil {
return nil, errors.New("failed to create backup request")
}
backup, err := createBackupRequest.CreateBackupPayload(opts).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(wait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
return backup, nil
}
func (os *iaasClient) ListBackups(ctx context.Context, filters map[string]string) ([]iaas.Backup, error) {
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
// TODO: Add API filter once available.
backups, err := os.iaas.ListBackups(ctxWithHTTPResp, os.projectID, os.region).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(wait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
filteredBackups := filterBackups(*backups.Items, filters)
return filteredBackups, nil
}
func (os *iaasClient) DeleteBackup(ctx context.Context, backupID string) error {
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
err := os.iaas.DeleteBackup(ctxWithHTTPResp, os.projectID, backupID, os.region).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(wait.XRequestIDHeader)
return stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return err
}
return nil
}
func (os *iaasClient) GetBackupByID(ctx context.Context, backupID string) (*iaas.Backup, error) {
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
backup, err := os.iaas.GetBackupExecute(ctxWithHTTPResp, os.projectID, os.region, backupID)
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(wait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
return backup, nil
}
func (os *iaasClient) WaitBackupReady(ctx context.Context, backupID string, snapshotSize int64, backupMaxDurationSecondsPerGB int) (*string, error) {
var err error
duration := time.Duration(int64(backupMaxDurationSecondsPerGB)*snapshotSize + backupBaseDurationSeconds)
err = os.waitBackupReadyWithContext(backupID, duration)
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("timeout, Backup %s is still not Ready: %v", backupID, err)
}
back, _ := os.GetBackupByID(ctx, backupID)
if back != nil {
return back.Status, err
}
return new("Failed to get backup status"), err
}
func (os *iaasClient) waitBackupReadyWithContext(backupID string, duration time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), duration*time.Second)
defer cancel()
var done bool
var err error
ticker := time.NewTicker(backupReadyCheckIntervalSeconds * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
done, err = os.backupIsReady(ctx, backupID)
if err != nil {
return err
}
if done {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// Supporting function for waitBackupReadyWithContext().
// Returns true when the backup is ready.
func (os *iaasClient) backupIsReady(ctx context.Context, backupID string) (bool, error) {
backup, err := os.GetBackupByID(ctx, backupID)
if err != nil {
return false, err
}
if *backup.Status == backupErrorStatus {
return false, errors.New("backup is in error state")
}
return *backup.Status == backupReadyStatus, nil
}