Skip to content

Commit

Permalink
Merge pull request #815 from waynz0r/schedule-backup-labels
Browse files Browse the repository at this point in the history
Set schedule labels to subsequent backups
  • Loading branch information
skriss authored Sep 6, 2018
2 parents 5ccc27a + 211aa7b commit bc51611
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/cli/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
16 changes: 13 additions & 3 deletions pkg/controller/schedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions pkg/controller/schedule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
Expand All @@ -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{},
},
Expand Down Expand Up @@ -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"},
Expand All @@ -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 {
Expand All @@ -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)
})
}
Expand Down

0 comments on commit bc51611

Please sign in to comment.