Set schedule labels to subsequent backups

Signed-off-by: Zsolt Varga <zsolt.varga@console.hu>
pull/815/head
Zsolt Varga 2018-09-03 15:01:03 +02:00
parent 283a1349bd
commit 211aa7b7fd
3 changed files with 53 additions and 3 deletions

View File

@ -104,6 +104,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
ObjectMeta: metav1.ObjectMeta{
Namespace: f.Namespace(),
Name: o.BackupOptions.Name,
Labels: o.BackupOptions.Labels.Data(),
},
Spec: api.ScheduleSpec{
Template: api.BackupSpec{

View File

@ -370,15 +370,25 @@ func getBackup(item *api.Schedule, timestamp time.Time) *api.Backup {
ObjectMeta: metav1.ObjectMeta{
Namespace: item.Namespace,
Name: fmt.Sprintf("%s-%s", item.Name, timestamp.Format("20060102150405")),
Labels: map[string]string{
"ark-schedule": item.Name,
},
},
}
// add schedule labels and 'ark-schedule' label to the backup
addLabelsToBackup(item, backup)
return backup
}
func addLabelsToBackup(item *api.Schedule, backup *api.Backup) {
labels := item.Labels
if labels == nil {
labels = make(map[string]string)
}
labels["ark-schedule"] = item.Name
backup.Labels = labels
}
func patchSchedule(original, updated *api.Schedule, client arkv1client.SchedulesGetter) (*api.Schedule, error) {
origBytes, err := json.Marshal(original)
if err != nil {

View File

@ -403,6 +403,9 @@ func TestGetBackup(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-20170725091500",
Labels: map[string]string{
"ark-schedule": "bar",
},
},
Spec: api.BackupSpec{},
},
@ -423,6 +426,9 @@ func TestGetBackup(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-20170725141500",
Labels: map[string]string{
"ark-schedule": "bar",
},
},
Spec: api.BackupSpec{},
},
@ -450,6 +456,9 @@ func TestGetBackup(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-20170725091500",
Labels: map[string]string{
"ark-schedule": "bar",
},
},
Spec: api.BackupSpec{
IncludedNamespaces: []string{"ns-1", "ns-2"},
@ -461,6 +470,35 @@ func TestGetBackup(t *testing.T) {
},
},
},
{
name: "ensure schedule labels is copied",
schedule: &api.Schedule{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar",
Labels: map[string]string{
"foo": "bar",
"bar": "baz",
},
},
Spec: api.ScheduleSpec{
Template: api.BackupSpec{},
},
},
testClockTime: "2017-07-25 14:15:00",
expectedBackup: &api.Backup{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "bar-20170725141500",
Labels: map[string]string{
"ark-schedule": "bar",
"bar": "baz",
"foo": "bar",
},
},
Spec: api.BackupSpec{},
},
},
}
for _, test := range tests {
@ -472,6 +510,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.Spec, backup.Spec)
})
}