pass annotations from scheduler to created backup (#3067)

* pass annotations from scheduler to created backup

Signed-off-by: Michael <michael.ketslah@tufin.com>

* add change log

Signed-off-by: Michael <michael.ketslah@tufin.com>

* add test for annotations in controller

Signed-off-by: Michael <michael.ketslah@tufin.com>

* If no annotations are set - do not copy empty list

Signed-off-by: Michael <michael.ketslah@tufin.com>

* remove unneeded var

Signed-off-by: Michael <michael.ketslah@tufin.com>

* add empty annotations and actually check annotations in backups

Signed-off-by: Michael <michael.ketslah@tufin.com>

* add empty missing label and empty annotations

Signed-off-by: Michael <michael.ketslah@tufin.com>

* revert empty annotations as seems they are nil as expected

Signed-off-by: Michael <michael.ketslah@tufin.com>

* fix typo in changelog

Signed-off-by: Michael <michael.ketslah@tufin.com>

Co-authored-by: Michael <michael.ketslah@tufin.com>
This commit is contained in:
Misha Ketslah
2020-11-19 23:19:42 +02:00
committed by GitHub
parent c10feb2cc3
commit 8c8385aabb
5 changed files with 37 additions and 2 deletions

View File

@@ -0,0 +1,2 @@
Pass annotations from schedule to backup it creates the same way it is done for labels
Add WithannotationsMap function to builder to be able to pass map instead of key/val list

View File

@@ -83,6 +83,11 @@ func (b *BackupBuilder) FromSchedule(schedule *velerov1api.Schedule) *BackupBuil
b.object.Spec = schedule.Spec.Template
b.ObjectMeta(WithLabelsMap(labels))
if schedule.Annotations != nil {
b.ObjectMeta(WithAnnotationsMap(schedule.Annotations))
}
return b
}

View File

@@ -76,6 +76,24 @@ func WithAnnotations(vals ...string) func(obj metav1.Object) {
}
}
// WithAnnotationsMap is a functional option that applies the specified annotations map to
// an object.
func WithAnnotationsMap(annotations map[string]string) func(obj metav1.Object) {
return func(obj metav1.Object) {
objAnnotations := obj.GetAnnotations()
if objAnnotations == nil {
objAnnotations = make(map[string]string)
}
// If the label already exists in the object, it will be overwritten
for k, v := range annotations {
objAnnotations[k] = v
}
obj.SetAnnotations(objAnnotations)
}
}
func setMapEntries(m map[string]string, vals ...string) map[string]string {
if m == nil {
m = make(map[string]string)

View File

@@ -68,7 +68,7 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
})
expectedBackupSpec := builder.ForBackup("test", testNamespace).IncludedNamespaces("test").Result().Spec
schedule := builder.ForSchedule(testNamespace, "test").Template(expectedBackupSpec).ObjectMeta(builder.WithLabels("velero.io/test", "true")).Result()
schedule := builder.ForSchedule(testNamespace, "test").Template(expectedBackupSpec).ObjectMeta(builder.WithLabels("velero.io/test", "true"), builder.WithAnnotations("velero.io/test", "true")).Result()
o.client.VeleroV1().Schedules(testNamespace).Create(context.TODO(), schedule, metav1.CreateOptions{})
t.Run("existing schedule", func(t *testing.T) {
@@ -80,6 +80,9 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
"velero.io/test": "true",
velerov1api.ScheduleNameLabel: "test",
}, backup.GetLabels())
assert.Equal(t, map[string]string{
"velero.io/test": "true",
}, backup.GetAnnotations())
})
t.Run("command line labels take precedence over schedule labels", func(t *testing.T) {

View File

@@ -431,11 +431,17 @@ func TestGetBackup(t *testing.T) {
Result(),
},
{
name: "ensure schedule labels is copied",
name: "ensure schedule labels are copied",
schedule: builder.ForSchedule("foo", "bar").ObjectMeta(builder.WithLabels("foo", "bar", "bar", "baz")).Result(),
testClockTime: "2017-07-25 14:15:00",
expectedBackup: builder.ForBackup("foo", "bar-20170725141500").ObjectMeta(builder.WithLabels(velerov1api.ScheduleNameLabel, "bar", "bar", "baz", "foo", "bar")).Result(),
},
{
name: "ensure schedule annotations are copied",
schedule: builder.ForSchedule("foo", "bar").ObjectMeta(builder.WithAnnotations("foo", "bar", "bar", "baz")).Result(),
testClockTime: "2017-07-25 14:15:00",
expectedBackup: builder.ForBackup("foo", "bar-20170725141500").ObjectMeta(builder.WithLabels(velerov1api.ScheduleNameLabel, "bar"), builder.WithAnnotations("bar", "baz", "foo", "bar")).Result(),
},
}
for _, test := range tests {
@@ -448,6 +454,7 @@ func TestGetBackup(t *testing.T) {
assert.Equal(t, test.expectedBackup.Namespace, backup.Namespace)
assert.Equal(t, test.expectedBackup.Name, backup.Name)
assert.Equal(t, test.expectedBackup.Labels, backup.Labels)
assert.Equal(t, test.expectedBackup.Annotations, backup.Annotations)
assert.Equal(t, test.expectedBackup.Spec, backup.Spec)
})
}