/
credentials.go
366 lines (330 loc) · 11.9 KB
/
credentials.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package credentials
import (
"errors"
"os"
"strings"
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
"github.com/openshift/oadp-operator/pkg/common"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
type DefaultPluginFields struct {
IsCloudProvider bool
SecretName string
MountPath string
EnvCredentialsFile string
PluginImage string
PluginSecretKey string
PluginName string
}
const (
cloudFieldPath = "cloud"
)
var (
PluginSpecificFields = map[oadpv1alpha1.DefaultPlugin]DefaultPluginFields{
oadpv1alpha1.DefaultPluginAWS: {
IsCloudProvider: true,
SecretName: "cloud-credentials",
MountPath: "/credentials",
EnvCredentialsFile: common.AWSSharedCredentialsFileEnvKey,
PluginName: common.VeleroPluginForAWS,
PluginSecretKey: "cloud",
},
oadpv1alpha1.DefaultPluginGCP: {
IsCloudProvider: true,
SecretName: "cloud-credentials-gcp",
MountPath: "/credentials-gcp",
EnvCredentialsFile: common.GCPCredentialsEnvKey,
PluginName: common.VeleroPluginForGCP,
PluginSecretKey: "cloud",
},
oadpv1alpha1.DefaultPluginMicrosoftAzure: {
IsCloudProvider: true,
SecretName: "cloud-credentials-azure",
MountPath: "/credentials-azure",
EnvCredentialsFile: common.AzureCredentialsFileEnvKey,
PluginName: common.VeleroPluginForAzure,
PluginSecretKey: "cloud",
},
oadpv1alpha1.DefaultPluginOpenShift: {
IsCloudProvider: false,
PluginName: common.VeleroPluginForOpenshift,
},
oadpv1alpha1.DefaultPluginCSI: {
IsCloudProvider: false,
//TODO: Check if the Registry needs to an upstream one from CSI
PluginName: common.VeleroPluginForCSI,
},
oadpv1alpha1.DefaultPluginVSM: {
IsCloudProvider: false,
PluginName: common.VeleroPluginForVSM,
},
oadpv1alpha1.DefaultPluginKubeVirt: {
IsCloudProvider: false,
PluginName: common.KubeVirtPlugin,
},
}
)
// Get secretName and secretKey from "secretName/secretKey"
func GetSecretNameKeyFromCredentialsFileConfigString(credentialsFile string) (string, string, error) {
credentialsFile = strings.TrimSpace(credentialsFile)
if credentialsFile == "" {
return "", "", nil
}
nameKeyArray := strings.Split(credentialsFile, "/")
if len(nameKeyArray) != 2 {
return "", "", errors.New("credentials file is not supported")
}
return nameKeyArray[0], nameKeyArray[1], nil
}
func GetSecretNameFromCredentialsFileConfigString(credentialsFile string) (string, error) {
name, _, err := GetSecretNameKeyFromCredentialsFileConfigString(credentialsFile)
return name, err
}
func getAWSPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.AWSPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.AWSPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_AWS") == "" {
return common.AWSPluginImage
}
return os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_AWS")
}
func getCSIPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.CSIPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.CSIPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_CSI") == "" {
return common.CSIPluginImage
}
return os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_CSI")
}
func getVSMPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.VSMPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.VSMPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_VSM") == "" {
return common.VSMPluginImage
}
return os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_VSM")
}
func getGCPPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.GCPPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.GCPPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_GCP") == "" {
return common.GCPPluginImage
}
return os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_GCP")
}
func getOpenshiftPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.OpenShiftPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.OpenShiftPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_OPENSHIFT_VELERO_PLUGIN") == "" {
return common.OpenshiftPluginImage
}
return os.Getenv("RELATED_IMAGE_OPENSHIFT_VELERO_PLUGIN")
}
func getAzurePluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.AzurePluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.AzurePluginImageKey]
}
if os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_MICROSOFT_AZURE") == "" {
return common.AzurePluginImage
}
return os.Getenv("RELATED_IMAGE_VELERO_PLUGIN_FOR_MICROSOFT_AZURE")
}
func getKubeVirtPluginImage(dpa *oadpv1alpha1.DataProtectionApplication) string {
if dpa.Spec.UnsupportedOverrides[oadpv1alpha1.KubeVirtPluginImageKey] != "" {
return dpa.Spec.UnsupportedOverrides[oadpv1alpha1.KubeVirtPluginImageKey]
}
if os.Getenv("RELATED_IMAGE_KUBEVIRT_VELERO_PLUGIN") == "" {
return common.KubeVirtPluginImage
}
return os.Getenv("RELATED_IMAGE_KUBEVIRT_VELERO_PLUGIN")
}
func getPluginImage(pluginName string, dpa *oadpv1alpha1.DataProtectionApplication) string {
switch pluginName {
case common.VeleroPluginForAWS:
return getAWSPluginImage(dpa)
case common.VeleroPluginForCSI:
return getCSIPluginImage(dpa)
case common.VeleroPluginForVSM:
return getVSMPluginImage(dpa)
case common.VeleroPluginForGCP:
return getGCPPluginImage(dpa)
case common.VeleroPluginForOpenshift:
return getOpenshiftPluginImage(dpa)
case common.VeleroPluginForAzure:
return getAzurePluginImage(dpa)
case common.KubeVirtPlugin:
return getKubeVirtPluginImage(dpa)
}
return ""
}
func AppendCloudProviderVolumes(dpa *oadpv1alpha1.DataProtectionApplication, ds *appsv1.DaemonSet, providerNeedsDefaultCreds map[string]bool, hasCloudStorage bool) error {
if dpa.Spec.Configuration.Velero == nil {
return errors.New("velero configuration not found")
}
var resticContainer *corev1.Container
// Find Velero container
for i, container := range ds.Spec.Template.Spec.Containers {
if container.Name == common.NodeAgent {
resticContainer = &ds.Spec.Template.Spec.Containers[i]
}
}
for _, plugin := range dpa.Spec.Configuration.Velero.DefaultPlugins {
// Check that this is a cloud provider plugin in the cloud provider map
// ok is boolean that will be true if `plugin` is a valid key in `PluginSpecificFields` map
// pattern from https://golang.org/doc/effective_go#maps
// this replaces the need to iterate through the `pluginSpecificFields` O(n) -> O(1)
if cloudProviderMap, ok := PluginSpecificFields[plugin]; ok &&
cloudProviderMap.IsCloudProvider && //if plugin is a cloud provider plugin, and one of the following condition is true
(!dpa.Spec.Configuration.Velero.NoDefaultBackupLocation || // it has a backup location in OADP/velero context OR
dpa.Spec.UnsupportedOverrides[oadpv1alpha1.OperatorTypeKey] == oadpv1alpha1.OperatorTypeMTC) { // OADP is installed via MTC
pluginNeedsCheck, foundProviderPlugin := providerNeedsDefaultCreds[string(plugin)]
if !foundProviderPlugin && !hasCloudStorage {
pluginNeedsCheck = true
}
if !cloudProviderMap.IsCloudProvider || !pluginNeedsCheck {
continue
}
// default secret name
secretName := cloudProviderMap.SecretName
ds.Spec.Template.Spec.Volumes = append(
ds.Spec.Template.Spec.Volumes,
corev1.Volume{
Name: secretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretName,
},
},
},
)
if resticContainer != nil {
resticContainer.VolumeMounts = append(
resticContainer.VolumeMounts,
corev1.VolumeMount{
Name: secretName,
MountPath: cloudProviderMap.MountPath,
},
)
resticContainer.Env = append(
resticContainer.Env,
corev1.EnvVar{
Name: cloudProviderMap.EnvCredentialsFile,
Value: cloudProviderMap.MountPath + "/" + cloudFieldPath,
},
)
}
}
}
for _, bslSpec := range dpa.Spec.BackupLocations {
if _, ok := bslSpec.Velero.Config["credentialsFile"]; ok {
if secretName, err := GetSecretNameFromCredentialsFileConfigString(bslSpec.Velero.Config["credentialsFile"]); err == nil {
ds.Spec.Template.Spec.Volumes = append(
ds.Spec.Template.Spec.Volumes,
corev1.Volume{
Name: secretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretName,
},
},
},
)
}
}
}
return nil
}
// add plugin specific specs to velero deployment
func AppendPluginSpecificSpecs(dpa *oadpv1alpha1.DataProtectionApplication, veleroDeployment *appsv1.Deployment, veleroContainer *corev1.Container, providerNeedsDefaultCreds map[string]bool, hasCloudStorage bool) error {
init_container_resources := veleroContainer.Resources
for _, plugin := range dpa.Spec.Configuration.Velero.DefaultPlugins {
if pluginSpecificMap, ok := PluginSpecificFields[plugin]; ok {
veleroDeployment.Spec.Template.Spec.InitContainers = append(
veleroDeployment.Spec.Template.Spec.InitContainers,
corev1.Container{
Image: getPluginImage(pluginSpecificMap.PluginName, dpa),
Name: pluginSpecificMap.PluginName,
ImagePullPolicy: corev1.PullAlways,
Resources: init_container_resources,
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: "File",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/target",
Name: "plugins",
},
},
})
pluginNeedsCheck, foundInBSLorVSL := providerNeedsDefaultCreds[string(plugin)]
if !foundInBSLorVSL && !hasCloudStorage {
pluginNeedsCheck = true
}
if !pluginSpecificMap.IsCloudProvider || !pluginNeedsCheck {
continue
}
if dpa.Spec.Configuration.Velero.NoDefaultBackupLocation &&
dpa.Spec.UnsupportedOverrides[oadpv1alpha1.OperatorTypeKey] != oadpv1alpha1.OperatorTypeMTC &&
pluginSpecificMap.IsCloudProvider {
continue
}
// set default secret name to use
secretName := pluginSpecificMap.SecretName
// append plugin specific volume mounts
if veleroContainer != nil {
veleroContainer.VolumeMounts = append(
veleroContainer.VolumeMounts,
corev1.VolumeMount{
Name: secretName,
MountPath: pluginSpecificMap.MountPath,
})
// append plugin specific env vars
veleroContainer.Env = append(
veleroContainer.Env,
corev1.EnvVar{
Name: pluginSpecificMap.EnvCredentialsFile,
Value: pluginSpecificMap.MountPath + "/" + cloudFieldPath,
})
}
// append plugin specific volumes
veleroDeployment.Spec.Template.Spec.Volumes = append(
veleroDeployment.Spec.Template.Spec.Volumes,
corev1.Volume{
Name: secretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretName,
DefaultMode: common.DefaultModePtr(),
},
},
})
}
}
// append custom plugin init containers
if dpa.Spec.Configuration.Velero.CustomPlugins != nil {
for _, plugin := range dpa.Spec.Configuration.Velero.CustomPlugins {
veleroDeployment.Spec.Template.Spec.InitContainers = append(
veleroDeployment.Spec.Template.Spec.InitContainers,
corev1.Container{
Image: plugin.Image,
Name: plugin.Name,
ImagePullPolicy: corev1.PullAlways,
Resources: init_container_resources,
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: "File",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/target",
Name: "plugins",
},
},
})
}
}
return nil
}