Skip to content

Commit

Permalink
Record backup completion time before uploading
Browse files Browse the repository at this point in the history
Signed-off-by: Nolan Brubaker <nolan@heptio.com>
  • Loading branch information
Nolan Brubaker committed Jul 11, 2018
1 parent 9ca7622 commit b71a37d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/apis/ark/v1/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type BackupStatus struct {

// CompletionTimestamp records the time a backup was completed.
// Completion time is recorded even on failed backups.
// Completion time is recorded before uploading the backup object.
// The server's time is used for CompletionTimestamps
CompletionTimestamp metav1.Time `json:"completionTimestamp"`
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ func (controller *backupController) runBackup(backup *api.Backup, bucket string)
backup.Status.Phase = api.BackupPhaseCompleted
}

// Mark completion timestamp before serializing and uploading.
// Otherwise, the JSON file in object storage has a CompletionTimestamp of 'null'.
backup.Status.CompletionTimestamp.Time = controller.clock.Now()

backupJSON := new(bytes.Buffer)
if err := encode.EncodeTo(backup, "json", backupJSON); err != nil {
errs = append(errs, errors.Wrap(err, "error encoding backup"))
Expand All @@ -393,7 +397,6 @@ func (controller *backupController) runBackup(backup *api.Backup, bucket string)
backupScheduleName := backup.GetLabels()["ark-schedule"]
controller.metrics.SetBackupTarballSizeBytesGauge(backupScheduleName, backupSizeBytes)

backup.Status.CompletionTimestamp.Time = controller.clock.Now()
backupDuration := backup.Status.CompletionTimestamp.Time.Sub(backup.Status.StartTimestamp.Time)
backupDurationSeconds := float64(backupDuration / time.Second)
controller.metrics.RegisterBackupDuration(backupScheduleName, backupDurationSeconds)
Expand Down
12 changes: 11 additions & 1 deletion pkg/controller/backup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package controller

import (
"bytes"
"encoding/json"
"io"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -201,7 +203,15 @@ func TestProcessBackup(t *testing.T) {
backup.Status.Version = 1
backupper.On("Backup", backup, mock.Anything, mock.Anything, mock.Anything).Return(nil)

cloudBackups.On("UploadBackup", "bucket", backup.Name, mock.Anything, mock.Anything, mock.Anything).Return(nil)
// Ensure we have a CompletionTimestamp when uploading.
// Failures will display the bytes in buf.
completionTimestampIsPresent := func(buf *bytes.Buffer) bool {
json := buf.String()
timeString := `"completionTimestamp": "2006-01-02T15:04:05Z"`

return strings.Contains(json, timeString)
}
cloudBackups.On("UploadBackup", "bucket", backup.Name, mock.MatchedBy(completionTimestampIsPresent), mock.Anything, mock.Anything).Return(nil)

pluginManager.On("GetBackupItemActions", backup.Name).Return(nil, nil)
pluginManager.On("CloseBackupItemActions", backup.Name).Return(nil)
Expand Down

0 comments on commit b71a37d

Please sign in to comment.