Record backup completion time before uploading

Signed-off-by: Nolan Brubaker <nolan@heptio.com>
pull/667/head
Nolan Brubaker 2018-07-10 18:17:53 -04:00
parent 9ca76226e2
commit b71a37dbfc
3 changed files with 16 additions and 2 deletions

View File

@ -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"`
}

View File

@ -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"))
@ -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)

View File

@ -17,8 +17,10 @@ limitations under the License.
package controller
import (
"bytes"
"encoding/json"
"io"
"strings"
"testing"
"time"
@ -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)