-
Notifications
You must be signed in to change notification settings - Fork 163
/
config.go
338 lines (289 loc) · 9.7 KB
/
config.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright © 2018 Banzai Cloud
//
// 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 ark
import (
"encoding/json"
"fmt"
"emperror.dev/errors"
"github.com/banzaicloud/integrated-service-sdk/api/v1alpha1/backup"
v1 "k8s.io/api/core/v1"
"github.com/banzaicloud/pipeline/internal/ark/client"
"github.com/banzaicloud/pipeline/internal/ark/providers/amazon"
"github.com/banzaicloud/pipeline/internal/ark/providers/azure"
"github.com/banzaicloud/pipeline/internal/ark/providers/google"
"github.com/banzaicloud/pipeline/internal/global"
"github.com/banzaicloud/pipeline/pkg/any"
pkgErrors "github.com/banzaicloud/pipeline/pkg/errors"
"github.com/banzaicloud/pipeline/pkg/jsonstructure"
"github.com/banzaicloud/pipeline/pkg/providers"
"github.com/banzaicloud/pipeline/src/secret"
)
// ChartConfig describes an ARK deployment chart config
type ChartConfig struct {
Namespace string
Chart string
Name string
Version string
ValueOverrides []byte
}
// ConfigRequest describes an ARK config request
type ConfigRequest struct {
Cluster clusterConfig
ClusterSecret *secret.SecretItemResponse
Bucket bucketConfig
BucketSecret *secret.SecretItemResponse
SecretName string
UseClusterSecret bool
ServiceAccountRoleARN string
UseProviderSecret bool
RestoreMode bool
}
type clusterConfig struct {
Name string
Provider string
Distribution string
Location string
RBACEnabled bool
azureClusterConfig
}
type azureClusterConfig struct {
ResourceGroup string
}
type bucketConfig struct {
Name string
Prefix string
Provider string
Location string
azureBucketConfig
}
type azureBucketConfig struct {
StorageAccount string
ResourceGroup string
}
type HelmValueOverrides struct {
backup.ValueOverrides
InitContainers []v1.Container `json:"initContainers,omitempty"`
}
type Image struct {
Repository string `json:"repository"`
Tag string `json:"tag"`
PullPolicy string `json:"pullPolicy"`
}
// GetChartConfig get a ChartConfig
func GetChartConfig() ChartConfig {
return ChartConfig{
Name: "velero",
Namespace: global.Config.Cluster.DisasterRecovery.Namespace,
Chart: global.Config.Cluster.DisasterRecovery.Charts.Ark.Chart,
Version: global.Config.Cluster.DisasterRecovery.Charts.Ark.Version,
}
}
func (req ConfigRequest) getInitContainers(bsp backup.BackupStorageLocation, vsl backup.VolumeSnapshotLocation) []v1.Container {
initContainers := make([]v1.Container, 0, 2)
if bsp.Provider == amazon.BackupStorageProvider || vsl.Provider == amazon.PersistentVolumeProvider {
pluginImage := fmt.Sprintf("%s:%s", global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Aws.Repository,
global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Aws.Tag)
initContainers = append(initContainers, v1.Container{
Name: "velero-plugin-for-aws",
Image: pluginImage,
ImagePullPolicy: getPullPolicy(global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Aws.PullPolicy),
VolumeMounts: []v1.VolumeMount{
{
Name: "plugins",
MountPath: "/target",
},
},
})
}
if bsp.Provider == google.BackupStorageProvider || vsl.Provider == google.PersistentVolumeProvider {
pluginImage := fmt.Sprintf("%s:%s", global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Gcp.Repository,
global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Gcp.Tag)
initContainers = append(initContainers, v1.Container{
Name: "velero-plugin-for-gcp",
Image: pluginImage,
ImagePullPolicy: getPullPolicy(global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Gcp.PullPolicy),
VolumeMounts: []v1.VolumeMount{
{
Name: "plugins",
MountPath: "/target",
},
},
})
}
if bsp.Provider == azure.BackupStorageProvider || vsl.Provider == azure.PersistentVolumeProvider {
pluginImage := fmt.Sprintf("%s:%s", global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Azure.Repository,
global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Azure.Tag)
initContainers = append(initContainers, v1.Container{
Name: "velero-plugin-for-azure",
Image: pluginImage,
ImagePullPolicy: getPullPolicy(global.Config.Cluster.DisasterRecovery.Charts.Ark.PluginImages.Azure.PullPolicy),
VolumeMounts: []v1.VolumeMount{
{
Name: "plugins",
MountPath: "/target",
},
},
})
}
return initContainers
}
// Get gets helm deployment value overrides
func (req ConfigRequest) Get() (values backup.ValueOverrides, err error) {
var provider backup.Provider
switch req.Bucket.Provider {
case providers.Amazon:
provider = backup.AWSProvider
case providers.Azure:
provider = backup.AzureProvider
case providers.Google:
provider = backup.GCPProvider
default:
return values, pkgErrors.ErrorNotSupportedCloudType
}
vsl, err := req.getVolumeSnapshotLocation()
if err != nil {
return values, err
}
bsp, err := req.getBackupStorageLocation()
if err != nil {
return values, err
}
values = backup.ValueOverrides{
Configuration: backup.Configuration{
Provider: provider,
VolumeSnapshotLocation: vsl,
BackupStorageLocation: bsp,
RestoreOnlyMode: req.RestoreMode,
LogLevel: "debug",
},
RBAC: backup.Rbac{
Create: req.Cluster.RBACEnabled,
},
Credentials: backup.Credentials{
ExistingSecret: req.SecretName,
},
// cleanup crd's only in restore mode
CleanUpCRDs: req.RestoreMode,
ServiceAccount: backup.ServiceAccount{
Server: backup.Server{
Create: true,
},
},
}
if vsl.Provider == amazon.PersistentVolumeProvider && req.ServiceAccountRoleARN != "" {
values.ServiceAccount = backup.ServiceAccount{
Server: backup.Server{
Create: true,
Name: "velero-sa",
Annotations: map[string]string{
"eks.amazonaws.com/role-arn": req.ServiceAccountRoleARN,
},
},
}
values.SecurityContext = backup.SecurityContext{
FsGroup: 1337,
}
}
return values, nil
}
func (req *ConfigRequest) getChartConfig() (config ChartConfig, err error) {
config = GetChartConfig()
arkConfig, err := req.Get()
if err != nil {
err = errors.Wrap(err, "error getting config")
return
}
helmConfig := HelmValueOverrides{
ValueOverrides: arkConfig,
}
// in case of deploying as an integrated service, image and plugin images are set by IS operator
helmConfig.InitContainers = req.getInitContainers(helmConfig.Configuration.BackupStorageLocation,
helmConfig.Configuration.VolumeSnapshotLocation)
// merge values from config with arkConfig
valuesBytes, err := mergeValues(helmConfig, global.Config.Cluster.DisasterRecovery.Charts.Ark.Values)
if err != nil {
err = errors.Wrap(err, "json convert failed")
return
}
config.ValueOverrides = valuesBytes
return
}
func mergeValues(chartValues interface{}, configValues interface{}) ([]byte, error) {
out, err := jsonstructure.Encode(chartValues)
if err != nil {
return nil, errors.WrapIf(err, "failed to encode chart values")
}
result, err := any.Merge(configValues, out, jsonstructure.DefaultMergeOptions())
if err != nil {
return nil, errors.WrapIf(err, "failed to merge values")
}
return json.Marshal(result)
}
func (req ConfigRequest) getVolumeSnapshotLocation() (backup.VolumeSnapshotLocation, error) {
var config backup.VolumeSnapshotLocation
var vslconfig backup.VolumeSnapshotLocationConfig
var pvcProvider backup.Provider
switch req.Cluster.Provider {
case providers.Amazon:
pvcProvider = amazon.PersistentVolumeProvider
vslconfig.Region = req.Cluster.Location
if req.UseProviderSecret {
vslconfig.Profile = "bucket"
}
case providers.Azure:
pvcProvider = azure.PersistentVolumeProvider
vslconfig.ApiTimeout = "3m0s"
vslconfig.ResourceGroup = azure.GetAzureClusterResourceGroupName(req.Cluster.Distribution, req.Cluster.ResourceGroup, req.Cluster.Name, req.Cluster.Location)
case providers.Google:
pvcProvider = google.PersistentVolumeProvider
default:
return config, pkgErrors.ErrorNotSupportedCloudType
}
return backup.VolumeSnapshotLocation{
Name: client.DefaultVolumeSnapshotLocationName,
Provider: pvcProvider,
Config: vslconfig,
}, nil
}
func (req ConfigRequest) getBackupStorageLocation() (backup.BackupStorageLocation, error) {
config := backup.BackupStorageLocation{
Name: client.DefaultBackupStorageLocationName,
Bucket: req.Bucket.Name,
Prefix: req.Bucket.Prefix,
}
switch req.Bucket.Provider {
case providers.Amazon:
config.Provider = amazon.BackupStorageProvider
config.Config.Region = req.Bucket.Location
config.Config.Profile = "bucket"
case providers.Azure:
config.Provider = azure.BackupStorageProvider
config.Config.StorageAccount = req.Bucket.StorageAccount
config.Config.ResourceGroup = req.Bucket.ResourceGroup
config.Config.StorageAccountKeyEnvVar = "AZURE_STORAGE_KEY"
case providers.Google:
config.Provider = google.BackupStorageProvider
default:
return config, pkgErrors.ErrorNotSupportedCloudType
}
return config, nil
}
func getPullPolicy(pullPolicy string) v1.PullPolicy {
switch pullPolicy {
case string(v1.PullAlways), string(v1.PullIfNotPresent), string(v1.PullNever): // Note: known values.
return v1.PullPolicy(pullPolicy)
default:
return v1.PullIfNotPresent
}
}