This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
/
backup_provider.go
202 lines (169 loc) 路 5.66 KB
/
backup_provider.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
// Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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.
package alicloud
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
corev1 "k8s.io/api/core/v1"
)
const (
envVarAccessKeyID = "ALICLOUD_ACCESS_KEY_ID"
envVarAccessKeySecret = "ALICLOUD_ACCESS_KEY_SECRET"
envVarStorageEndpoint = "ALICLOUD_ENDPOINT"
DataKeyAccessKeyID = "accessKeyID"
DataKeyAccessKeySecret = "accessKeySecret"
DataKeyStorageEndpoint = "storageEndpoint"
storageProviderNameOSS = "OSS"
ossErrorCodeNoSuchBucket = "NoSuchBucket"
)
type backupProvider struct {
bucketName string
accessKeyID string
secretAccessKey string
storageEndpoint string
log *logrus.Logger
}
// NewBackupProvider creates a new oss backup provider implementation.
func NewBackupProvider(
credentialsData map[string]string,
bucketName, storageEndpoint string,
log *logrus.Logger,
) (*backupProvider, error) {
accessKeyID, ok := credentialsData[DataKeyAccessKeyID]
if !ok {
return nil, fmt.Errorf("data map doesn't have an access key id")
}
secretAccessKey, ok := credentialsData[DataKeyAccessKeySecret]
if !ok {
return nil, fmt.Errorf("data map doesn't have an access key secret")
}
return &backupProvider{
bucketName: bucketName,
accessKeyID: accessKeyID,
secretAccessKey: secretAccessKey,
storageEndpoint: storageEndpoint,
log: log,
}, nil
}
func (b *backupProvider) CreateBucket(ctx context.Context) error {
svc, err := b.getClient()
if err != nil {
return err
}
b.log.Infof("Ensuring that alicloud backup bucket %q exists", b.bucketName)
err = svc.CreateBucket(b.bucketName, oss.ACL(oss.ACLPrivate))
if err != nil {
return fmt.Errorf("failed to create alicloud backup bucket %q: %w", b.bucketName, err)
}
return nil
}
func (b *backupProvider) DeleteBucket(ctx context.Context) error {
svc, err := b.getClient()
if err != nil {
return err
}
bucket, err := svc.Bucket(b.bucketName)
if err != nil {
return fmt.Errorf("failed to get alicloud backup bucket %q: %w", b.bucketName, err)
}
b.log.Infof("Deleting objects of alicloud backup bucket %q", b.bucketName)
marker := ""
for {
b.log.Infof("Deleting objects of bucket %q starting with marker %q", b.bucketName, marker)
listResult, err := bucket.ListObjects(oss.Marker(marker))
if err != nil {
if ossErr, ok := err.(oss.ServiceError); ok {
switch ossErr.Code {
case ossErrorCodeNoSuchBucket:
return nil
default:
return fmt.Errorf("failed to list objects of alicloud backup bucket %q: %w", b.bucketName, err)
}
}
return fmt.Errorf("failed to list objects of alicloud backup bucket %q: %w", b.bucketName, err)
}
for _, object := range listResult.Objects {
err = bucket.DeleteObject(object.Key)
if err != nil {
return fmt.Errorf("failed to delete object %q of alicloud backup bucket %q: %w", object.Key, b.bucketName, err)
}
}
if listResult.IsTruncated {
marker = listResult.NextMarker
} else {
break
}
}
b.log.Infof("Deleting alicloud backup bucket %q", b.bucketName)
err = svc.DeleteBucket(b.bucketName)
if err != nil {
if ossErr, ok := err.(oss.ServiceError); ok {
switch ossErr.Code {
case ossErrorCodeNoSuchBucket:
return nil
default:
return fmt.Errorf("failed to delete alicloud backup bucket %q: %w", b.bucketName, err)
}
}
return fmt.Errorf("failed to delete alicloud backup bucket %q: %w", b.bucketName, err)
}
return nil
}
func (b *backupProvider) BucketExists(_ context.Context) (bool, error) {
svc, err := b.getClient()
if err != nil {
return false, fmt.Errorf("failed to check existence of alicloud backup bucket %q: %w", b.bucketName, err)
}
return svc.IsBucketExist(b.bucketName)
}
func (b *backupProvider) ComputeETCDBackupConfiguration(_, etcdSecretNameBackup string) (
storageProviderName string,
secretData map[string][]byte,
environment []corev1.EnvVar,
) {
storageProviderName = storageProviderNameOSS
secretData = map[string][]byte{
DataKeyAccessKeyID: []byte(b.accessKeyID),
DataKeyAccessKeySecret: []byte(b.secretAccessKey),
DataKeyStorageEndpoint: []byte(b.storageEndpoint),
}
environment = []corev1.EnvVar{
b.envVar(envVarAccessKeyID, etcdSecretNameBackup, DataKeyAccessKeyID),
b.envVar(envVarAccessKeySecret, etcdSecretNameBackup, DataKeyAccessKeySecret),
b.envVar(envVarStorageEndpoint, etcdSecretNameBackup, DataKeyStorageEndpoint),
}
return
}
func (b *backupProvider) envVar(envVarName, etcdSecretNameBackup, dataKey string) corev1.EnvVar {
return corev1.EnvVar{
Name: envVarName,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: etcdSecretNameBackup,
},
Key: dataKey,
},
},
}
}
func (b *backupProvider) getClient() (*oss.Client, error) {
client, err := oss.New(b.storageEndpoint, b.accessKeyID, b.secretAccessKey)
if err != nil {
return nil, fmt.Errorf("failed to get alicloud client to manage backup bucket: %w", err)
}
return client, nil
}