Skip to content

Commit 06081c0

Browse files
author
Mateus Oliveira
authored
fix: Enforce NonAdminRestore spec field values (#1600)
Signed-off-by: Mateus Oliveira <[email protected]>
1 parent e1060a8 commit 06081c0

File tree

7 files changed

+832
-13
lines changed

7 files changed

+832
-13
lines changed

api/v1alpha1/oadp_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ type NonAdmin struct {
446446

447447
// which restore spec field values to enforce
448448
// +optional
449-
// EnforceRestoreSpecs *velero.RestoreSpec `json:"enforceRestoreSpecs,omitempty"`
450-
// TODO not allow enforcing backupName!!!!
449+
EnforceRestoreSpec *velero.RestoreSpec `json:"enforceRestoreSpec,omitempty"`
451450
}
452451

453452
// DataMover defines the various config for DPA data mover

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/oadp.openshift.io_dataprotectionapplications.yaml

Lines changed: 382 additions & 0 deletions
Large diffs are not rendered by default.

config/crd/bases/oadp.openshift.io_dataprotectionapplications.yaml

Lines changed: 382 additions & 0 deletions
Large diffs are not rendered by default.

controllers/nonadmin_controller.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const (
2525
nonAdminObjectName = "non-admin-controller"
2626
controlPlaneKey = "control-plane"
2727

28-
enforcedBackupSpecKey = "enforced-backup-spec"
28+
enforcedBackupSpecKey = "enforced-backup-spec"
29+
enforcedRestoreSpecKey = "enforced-restore-spec"
2930
)
3031

3132
var (
@@ -41,8 +42,10 @@ var (
4142
"app.kubernetes.io/part-of": common.OADPOperator,
4243
}
4344

44-
previousEnforcedBackupSpec *velero.BackupSpec = nil
45-
dpaBackupSpecResourceVersion = ""
45+
previousEnforcedBackupSpec *velero.BackupSpec = nil
46+
dpaBackupSpecResourceVersion = ""
47+
previousEnforcedRestoreSpec *velero.RestoreSpec = nil
48+
dpaRestoreSpecResourceVersion = ""
4649
)
4750

4851
func (r *DPAReconciler) ReconcileNonAdminController(log logr.Logger) (bool, error) {
@@ -156,9 +159,13 @@ func ensureRequiredSpecs(deploymentObject *appsv1.Deployment, dpa *oadpv1alpha1.
156159
dpaBackupSpecResourceVersion = dpa.GetResourceVersion()
157160
}
158161
previousEnforcedBackupSpec = dpa.Spec.NonAdmin.EnforceBackupSpec
159-
// TODO same thing for restore
162+
if len(dpaRestoreSpecResourceVersion) == 0 || !reflect.DeepEqual(dpa.Spec.NonAdmin.EnforceRestoreSpec, previousEnforcedRestoreSpec) {
163+
dpaRestoreSpecResourceVersion = dpa.GetResourceVersion()
164+
}
165+
previousEnforcedRestoreSpec = dpa.Spec.NonAdmin.EnforceRestoreSpec
160166
enforcedSpecAnnotation := map[string]string{
161-
enforcedBackupSpecKey: dpaBackupSpecResourceVersion,
167+
enforcedBackupSpecKey: dpaBackupSpecResourceVersion,
168+
enforcedRestoreSpecKey: dpaRestoreSpecResourceVersion,
162169
}
163170

164171
deploymentObject.Spec.Replicas = ptr.To(int32(1))
@@ -179,7 +186,7 @@ func ensureRequiredSpecs(deploymentObject *appsv1.Deployment, dpa *oadpv1alpha1.
179186
deploymentObject.Spec.Template.SetAnnotations(enforcedSpecAnnotation)
180187
} else {
181188
templateObjectAnnotations[enforcedBackupSpecKey] = enforcedSpecAnnotation[enforcedBackupSpecKey]
182-
// TODO same thing for restore
189+
templateObjectAnnotations[enforcedRestoreSpecKey] = enforcedSpecAnnotation[enforcedRestoreSpecKey]
183190
deploymentObject.Spec.Template.SetAnnotations(templateObjectAnnotations)
184191
}
185192

controllers/nonadmin_controller_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,11 @@ func TestEnsureRequiredSpecs(t *testing.T) {
316316
if len(deployment.Spec.Template.Annotations[enforcedBackupSpecKey]) == 0 {
317317
t.Errorf("Deployment does not have Annotation")
318318
}
319-
previousAnnotationValue := deployment.DeepCopy().Spec.Template.Annotations[enforcedBackupSpecKey]
319+
if len(deployment.Spec.Template.Annotations[enforcedRestoreSpecKey]) == 0 {
320+
t.Errorf("Deployment does not have Annotation")
321+
}
322+
previousBackupAnnotationValue := deployment.DeepCopy().Spec.Template.Annotations[enforcedBackupSpecKey]
323+
previousRestoreAnnotationValue := deployment.DeepCopy().Spec.Template.Annotations[enforcedRestoreSpecKey]
320324
updatedDPA := &oadpv1alpha1.DataProtectionApplication{
321325
ObjectMeta: metav1.ObjectMeta{
322326
ResourceVersion: "147258369",
@@ -331,7 +335,10 @@ func TestEnsureRequiredSpecs(t *testing.T) {
331335
if err != nil {
332336
t.Errorf("ensureRequiredSpecs() errored out: %v", err)
333337
}
334-
if previousAnnotationValue != deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
338+
if previousBackupAnnotationValue != deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
339+
t.Errorf("Deployment have different Annotation")
340+
}
341+
if previousRestoreAnnotationValue != deployment.Spec.Template.Annotations[enforcedRestoreSpecKey] {
335342
t.Errorf("Deployment have different Annotation")
336343
}
337344
updatedDPA = &oadpv1alpha1.DataProtectionApplication{
@@ -351,10 +358,14 @@ func TestEnsureRequiredSpecs(t *testing.T) {
351358
if err != nil {
352359
t.Errorf("ensureRequiredSpecs() errored out: %v", err)
353360
}
354-
if previousAnnotationValue == deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
361+
if previousBackupAnnotationValue == deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
355362
t.Errorf("Deployment does not have different Annotation")
356363
}
357-
previousAnnotationValue = deployment.DeepCopy().Spec.Template.Annotations[enforcedBackupSpecKey]
364+
if previousRestoreAnnotationValue != deployment.Spec.Template.Annotations[enforcedRestoreSpecKey] {
365+
t.Errorf("Deployment have different Annotation")
366+
}
367+
previousBackupAnnotationValue = deployment.DeepCopy().Spec.Template.Annotations[enforcedBackupSpecKey]
368+
previousRestoreAnnotationValue = deployment.DeepCopy().Spec.Template.Annotations[enforcedRestoreSpecKey]
358369
updatedDPA = &oadpv1alpha1.DataProtectionApplication{
359370
ObjectMeta: metav1.ObjectMeta{
360371
ResourceVersion: "112233445",
@@ -365,16 +376,22 @@ func TestEnsureRequiredSpecs(t *testing.T) {
365376
EnforceBackupSpec: &v1.BackupSpec{
366377
SnapshotMoveData: ptr.To(false),
367378
},
379+
EnforceRestoreSpec: &v1.RestoreSpec{
380+
RestorePVs: ptr.To(true),
381+
},
368382
},
369383
},
370384
}
371385
err = ensureRequiredSpecs(deployment, updatedDPA, defaultNonAdminImage, corev1.PullAlways)
372386
if err != nil {
373387
t.Errorf("ensureRequiredSpecs() errored out: %v", err)
374388
}
375-
if previousAnnotationValue != deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
389+
if previousBackupAnnotationValue != deployment.Spec.Template.Annotations[enforcedBackupSpecKey] {
376390
t.Errorf("Deployment have different Annotation")
377391
}
392+
if previousRestoreAnnotationValue == deployment.Spec.Template.Annotations[enforcedRestoreSpecKey] {
393+
t.Errorf("Deployment does not have different Annotation")
394+
}
378395
}
379396

380397
func TestDPAReconcilerCheckNonAdminEnabled(t *testing.T) {

controllers/validator_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,33 @@ func TestDPAReconciler_ValidateDataProtectionCR(t *testing.T) {
15451545
},
15461546
},
15471547
},
1548+
{
1549+
name: "[valid] DPA CR: spec.nonAdmin.enforceRestoreSpec set",
1550+
dpa: &oadpv1alpha1.DataProtectionApplication{
1551+
ObjectMeta: metav1.ObjectMeta{
1552+
Name: "test-DPA-CR",
1553+
Namespace: "test-ns",
1554+
},
1555+
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
1556+
Configuration: &oadpv1alpha1.ApplicationConfig{
1557+
Velero: &oadpv1alpha1.VeleroConfig{
1558+
NoDefaultBackupLocation: true,
1559+
},
1560+
},
1561+
BackupImages: ptr.To(false),
1562+
NonAdmin: &oadpv1alpha1.NonAdmin{
1563+
Enable: ptr.To(true),
1564+
EnforceRestoreSpec: &v1.RestoreSpec{
1565+
IncludedNamespaces: []string{"banana"},
1566+
RestorePVs: ptr.To(true),
1567+
},
1568+
},
1569+
UnsupportedOverrides: map[oadpv1alpha1.UnsupportedImageKey]string{
1570+
oadpv1alpha1.TechPreviewAck: "true",
1571+
},
1572+
},
1573+
},
1574+
},
15481575
{
15491576
name: "[valid] DPA CR: spec.nonAdmin.enable true",
15501577
dpa: &oadpv1alpha1.DataProtectionApplication{

0 commit comments

Comments
 (0)