-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathresources.go
436 lines (377 loc) · 13.2 KB
/
resources.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Copyright the Velero contributors.
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 install
import (
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
v1crds "github.com/vmware-tanzu/velero/config/crd/v1/crds"
v2alpha1crds "github.com/vmware-tanzu/velero/config/crd/v2alpha1/crds"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)
const (
defaultServiceAccountName = "velero"
podSecurityLevel = "privileged"
podSecurityVersion = "latest"
)
var (
// default values for Velero server pod resource request/limit
DefaultVeleroPodCPURequest = "500m"
DefaultVeleroPodMemRequest = "128Mi"
DefaultVeleroPodCPULimit = "1000m"
DefaultVeleroPodMemLimit = "512Mi"
// default values for node-agent pod resource request/limit,
// "0" means no request/limit is set, so as to make the QoS as BestEffort
DefaultNodeAgentPodCPURequest = "0"
DefaultNodeAgentPodMemRequest = "0"
DefaultNodeAgentPodCPULimit = "0"
DefaultNodeAgentPodMemLimit = "0"
DefaultVeleroNamespace = "velero"
)
func Labels() map[string]string {
return map[string]string{
"component": "velero",
}
}
func podLabels(userLabels ...map[string]string) map[string]string {
// Use the default labels as a starting point
base := Labels()
// Merge base labels with user labels to enforce CLI precedence
for _, labels := range userLabels {
for k, v := range labels {
base[k] = v
}
}
return base
}
func podAnnotations(userAnnotations map[string]string) map[string]string {
// Use the default annotations as a starting point
base := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "8085",
"prometheus.io/path": "/metrics",
}
// Merge base annotations with user annotations to enforce CLI precedence
for k, v := range userAnnotations {
base[k] = v
}
return base
}
func containerPorts() []corev1.ContainerPort {
return []corev1.ContainerPort{
{
Name: "metrics",
ContainerPort: 8085,
},
}
}
func objectMeta(namespace, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: Labels(),
}
}
func ServiceAccount(namespace string, annotations map[string]string) *corev1.ServiceAccount {
objMeta := objectMeta(namespace, defaultServiceAccountName)
objMeta.Annotations = annotations
return &corev1.ServiceAccount{
ObjectMeta: objMeta,
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: corev1.SchemeGroupVersion.String(),
},
}
}
func ClusterRoleBinding(namespace string) *rbacv1.ClusterRoleBinding {
crbName := "velero"
if namespace != DefaultVeleroNamespace {
crbName = "velero-" + namespace
}
crb := &rbacv1.ClusterRoleBinding{
ObjectMeta: objectMeta("", crbName),
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRoleBinding",
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Namespace: namespace,
Name: "velero",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "cluster-admin",
APIGroup: "rbac.authorization.k8s.io",
},
}
return crb
}
func Namespace(namespace string) *corev1.Namespace {
ns := &corev1.Namespace{
ObjectMeta: objectMeta("", namespace),
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: corev1.SchemeGroupVersion.String(),
},
}
ns.Labels["pod-security.kubernetes.io/enforce"] = podSecurityLevel
ns.Labels["pod-security.kubernetes.io/enforce-version"] = podSecurityVersion
ns.Labels["pod-security.kubernetes.io/audit"] = podSecurityLevel
ns.Labels["pod-security.kubernetes.io/audit-version"] = podSecurityVersion
ns.Labels["pod-security.kubernetes.io/warn"] = podSecurityLevel
ns.Labels["pod-security.kubernetes.io/warn-version"] = podSecurityVersion
return ns
}
func BackupStorageLocation(namespace, provider, bucket, prefix string, config map[string]string, caCert []byte) *velerov1api.BackupStorageLocation {
return &velerov1api.BackupStorageLocation{
ObjectMeta: objectMeta(namespace, "default"),
TypeMeta: metav1.TypeMeta{
Kind: "BackupStorageLocation",
APIVersion: velerov1api.SchemeGroupVersion.String(),
},
Spec: velerov1api.BackupStorageLocationSpec{
Provider: provider,
StorageType: velerov1api.StorageType{
ObjectStorage: &velerov1api.ObjectStorageLocation{
Bucket: bucket,
Prefix: prefix,
CACert: caCert,
},
},
Config: config,
Default: true,
},
}
}
func VolumeSnapshotLocation(namespace, provider string, config map[string]string) *velerov1api.VolumeSnapshotLocation {
return &velerov1api.VolumeSnapshotLocation{
ObjectMeta: objectMeta(namespace, "default"),
TypeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshotLocation",
APIVersion: velerov1api.SchemeGroupVersion.String(),
},
Spec: velerov1api.VolumeSnapshotLocationSpec{
Provider: provider,
Config: config,
},
}
}
func Secret(namespace string, data []byte) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: objectMeta(namespace, "cloud-credentials"),
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: corev1.SchemeGroupVersion.String(),
},
Data: map[string][]byte{
"cloud": data,
},
Type: corev1.SecretTypeOpaque,
}
}
func appendUnstructured(list *unstructured.UnstructuredList, obj runtime.Object) error {
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj)
// Remove the status field so we're not sending blank data to the server.
// On CRDs, having an empty status is actually a validation error.
delete(u, "status")
if err != nil {
return err
}
list.Items = append(list.Items, unstructured.Unstructured{Object: u})
return nil
}
type VeleroOptions struct {
Namespace string
Image string
ProviderName string
Bucket string
Prefix string
PodAnnotations map[string]string
PodLabels map[string]string
ServiceAccountAnnotations map[string]string
ServiceAccountName string
VeleroPodResources corev1.ResourceRequirements
NodeAgentPodResources corev1.ResourceRequirements
SecretData []byte
RestoreOnly bool
UseNodeAgent bool
UseNodeAgentWindows bool
PrivilegedNodeAgent bool
UseVolumeSnapshots bool
BSLConfig map[string]string
VSLConfig map[string]string
DefaultRepoMaintenanceFrequency time.Duration
GarbageCollectionFrequency time.Duration
PodVolumeOperationTimeout time.Duration
Plugins []string
NoDefaultBackupLocation bool
CACertData []byte
Features []string
DefaultVolumesToFsBackup bool
UploaderType string
DefaultSnapshotMoveData bool
DisableInformerCache bool
ScheduleSkipImmediately bool
PodResources kube.PodResources
KeepLatestMaintenanceJobs int
BackupRepoConfigMap string
RepoMaintenanceJobConfigMap string
NodeAgentConfigMap string
ItemBlockWorkerCount int
}
func AllCRDs() *unstructured.UnstructuredList {
resources := new(unstructured.UnstructuredList)
// Set the GVK so that the serialization framework outputs the list properly
resources.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "List"})
for _, crd := range v1crds.CRDs {
crd.SetLabels(Labels())
if err := appendUnstructured(resources, crd); err != nil {
fmt.Printf("error appending v1 CRD %s: %s\n", crd.GetName(), err.Error())
}
}
for _, crd := range v2alpha1crds.CRDs {
crd.SetLabels(Labels())
if err := appendUnstructured(resources, crd); err != nil {
fmt.Printf("error appending v2alpha1 CRD %s: %s\n", crd.GetName(), err.Error())
}
}
return resources
}
// AllResources returns a list of all resources necessary to install Velero, in the appropriate order, into a Kubernetes cluster.
// Items are unstructured, since there are different data types returned.
func AllResources(o *VeleroOptions) *unstructured.UnstructuredList {
resources := AllCRDs()
ns := Namespace(o.Namespace)
if err := appendUnstructured(resources, ns); err != nil {
fmt.Printf("error appending Namespace %s: %s\n", ns.GetName(), err.Error())
}
serviceAccountName := defaultServiceAccountName
if o.ServiceAccountName == "" {
crb := ClusterRoleBinding(o.Namespace)
if err := appendUnstructured(resources, crb); err != nil {
fmt.Printf("error appending ClusterRoleBinding %s: %s\n", crb.GetName(), err.Error())
}
sa := ServiceAccount(o.Namespace, o.ServiceAccountAnnotations)
if err := appendUnstructured(resources, sa); err != nil {
fmt.Printf("error appending ServiceAccount %s: %s\n", sa.GetName(), err.Error())
}
} else {
serviceAccountName = o.ServiceAccountName
}
if o.SecretData != nil {
sec := Secret(o.Namespace, o.SecretData)
if err := appendUnstructured(resources, sec); err != nil {
fmt.Printf("error appending Secret %s: %s\n", sec.GetName(), err.Error())
}
}
if !o.NoDefaultBackupLocation {
bsl := BackupStorageLocation(o.Namespace, o.ProviderName, o.Bucket, o.Prefix, o.BSLConfig, o.CACertData)
if err := appendUnstructured(resources, bsl); err != nil {
fmt.Printf("error appending BackupStorageLocation %s: %s\n", bsl.GetName(), err.Error())
}
}
// A snapshot location may not be desirable for users relying on pod volume backup/restore
if o.UseVolumeSnapshots {
vsl := VolumeSnapshotLocation(o.Namespace, o.ProviderName, o.VSLConfig)
if err := appendUnstructured(resources, vsl); err != nil {
fmt.Printf("error appending VolumeSnapshotLocation %s: %s\n", vsl.GetName(), err.Error())
}
}
secretPresent := o.SecretData != nil
deployOpts := []podTemplateOption{
WithAnnotations(o.PodAnnotations),
WithLabels(o.PodLabels),
WithImage(o.Image),
WithResources(o.VeleroPodResources),
WithSecret(secretPresent),
WithDefaultRepoMaintenanceFrequency(o.DefaultRepoMaintenanceFrequency),
WithServiceAccountName(serviceAccountName),
WithGarbageCollectionFrequency(o.GarbageCollectionFrequency),
WithPodVolumeOperationTimeout(o.PodVolumeOperationTimeout),
WithUploaderType(o.UploaderType),
WithScheduleSkipImmediately(o.ScheduleSkipImmediately),
WithPodResources(o.PodResources),
WithKeepLatestMaintenanceJobs(o.KeepLatestMaintenanceJobs),
WithItemBlockWorkerCount(o.ItemBlockWorkerCount),
}
if len(o.Features) > 0 {
deployOpts = append(deployOpts, WithFeatures(o.Features))
}
if o.RestoreOnly {
deployOpts = append(deployOpts, WithRestoreOnly(true))
}
if len(o.Plugins) > 0 {
deployOpts = append(deployOpts, WithPlugins(o.Plugins))
}
if o.DefaultVolumesToFsBackup {
deployOpts = append(deployOpts, WithDefaultVolumesToFsBackup(true))
}
if o.DefaultSnapshotMoveData {
deployOpts = append(deployOpts, WithDefaultSnapshotMoveData(true))
}
if o.DisableInformerCache {
deployOpts = append(deployOpts, WithDisableInformerCache(true))
}
if len(o.BackupRepoConfigMap) > 0 {
deployOpts = append(deployOpts, WithBackupRepoConfigMap(o.BackupRepoConfigMap))
}
if len(o.RepoMaintenanceJobConfigMap) > 0 {
deployOpts = append(deployOpts, WithRepoMaintenanceJobConfigMap(o.RepoMaintenanceJobConfigMap))
}
deploy := Deployment(o.Namespace, deployOpts...)
if err := appendUnstructured(resources, deploy); err != nil {
fmt.Printf("error appending Deployment %s: %s\n", deploy.GetName(), err.Error())
}
if o.UseNodeAgent || o.UseNodeAgentWindows {
dsOpts := []podTemplateOption{
WithAnnotations(o.PodAnnotations),
WithLabels(o.PodLabels),
WithImage(o.Image),
WithResources(o.NodeAgentPodResources),
WithSecret(secretPresent),
WithServiceAccountName(serviceAccountName),
}
if len(o.Features) > 0 {
dsOpts = append(dsOpts, WithFeatures(o.Features))
}
if o.PrivilegedNodeAgent {
dsOpts = append(dsOpts, WithPrivilegedNodeAgent(true))
}
if len(o.NodeAgentConfigMap) > 0 {
dsOpts = append(dsOpts, WithNodeAgentConfigMap(o.NodeAgentConfigMap))
}
if o.UseNodeAgent {
ds := DaemonSet(o.Namespace, dsOpts...)
if err := appendUnstructured(resources, ds); err != nil {
fmt.Printf("error appending DaemonSet %s: %s\n", ds.GetName(), err.Error())
}
}
if o.UseNodeAgentWindows {
dsOpts = append(dsOpts, WithForWindows())
dsWin := DaemonSet(o.Namespace, dsOpts...)
if err := appendUnstructured(resources, dsWin); err != nil {
fmt.Printf("error appending DaemonSet %s: %s\n", dsWin.GetName(), err.Error())
}
}
}
return resources
}