From 211aa7b7fdfa6f2ba3764f38244f349fc501d00c Mon Sep 17 00:00:00 2001 From: Zsolt Varga Date: Mon, 3 Sep 2018 15:01:03 +0200 Subject: [PATCH] Set schedule labels to subsequent backups Signed-off-by: Zsolt Varga --- pkg/cmd/cli/schedule/create.go | 1 + pkg/controller/schedule_controller.go | 16 +++++++-- pkg/controller/schedule_controller_test.go | 39 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/cli/schedule/create.go b/pkg/cmd/cli/schedule/create.go index 079414615..310e43c63 100644 --- a/pkg/cmd/cli/schedule/create.go +++ b/pkg/cmd/cli/schedule/create.go @@ -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{ diff --git a/pkg/controller/schedule_controller.go b/pkg/controller/schedule_controller.go index 53a04a7dc..266b6191b 100644 --- a/pkg/controller/schedule_controller.go +++ b/pkg/controller/schedule_controller.go @@ -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 { diff --git a/pkg/controller/schedule_controller_test.go b/pkg/controller/schedule_controller_test.go index 2970c1777..67f55cdd1 100644 --- a/pkg/controller/schedule_controller_test.go +++ b/pkg/controller/schedule_controller_test.go @@ -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) }) }