Set default backup TTL

Set default backup TTL to 30 days when TTL
is not provided in the backup yaml configuration.

Updates #138

Signed-off-by: Rohan Vora <vorar@vmware.com>
This commit is contained in:
Rohan Vora
2019-04-04 14:59:13 -07:00
parent a519547efc
commit 4beb8aab3c
5 changed files with 78 additions and 4 deletions

View File

@@ -0,0 +1 @@
set default TTL for backups

View File

@@ -66,7 +66,9 @@ spec:
volumeSnapshotLocations: volumeSnapshotLocations:
- aws-primary - aws-primary
- gcp-primary - gcp-primary
# The amount of time before this backup is eligible for garbage collection. # The amount of time before this backup is eligible for garbage collection. If not specified,
# a default value of 30 days will be used. The default can be configured on the velero server
# by passing the flag --default-backup-ttl.
ttl: 24h0m0s ttl: 24h0m0s
# Actions to perform at different times during a backup. The only hook currently supported is # Actions to perform at different times during a backup. The only hook currently supported is
# executing a command in a container in a pod using the pod exec API. Optional. # executing a command in a container in a pod using the pod exec API. Optional.

View File

@@ -83,11 +83,15 @@ const (
defaultClientBurst int = 30 defaultClientBurst int = 30
defaultProfilerAddress = "localhost:6060" defaultProfilerAddress = "localhost:6060"
// the default TTL for a backup
defaultBackupTTL = 30 * 24 * time.Hour
) )
type serverConfig struct { type serverConfig struct {
pluginDir, metricsAddress, defaultBackupLocation string pluginDir, metricsAddress, defaultBackupLocation string
backupSyncPeriod, podVolumeOperationTimeout, resourceTerminatingTimeout time.Duration backupSyncPeriod, podVolumeOperationTimeout, resourceTerminatingTimeout time.Duration
defaultBackupTTL time.Duration
restoreResourcePriorities []string restoreResourcePriorities []string
defaultVolumeSnapshotLocations map[string]string defaultVolumeSnapshotLocations map[string]string
restoreOnly bool restoreOnly bool
@@ -106,6 +110,7 @@ func NewCommand() *cobra.Command {
defaultBackupLocation: "default", defaultBackupLocation: "default",
defaultVolumeSnapshotLocations: make(map[string]string), defaultVolumeSnapshotLocations: make(map[string]string),
backupSyncPeriod: defaultBackupSyncPeriod, backupSyncPeriod: defaultBackupSyncPeriod,
defaultBackupTTL: defaultBackupTTL,
podVolumeOperationTimeout: defaultPodVolumeOperationTimeout, podVolumeOperationTimeout: defaultPodVolumeOperationTimeout,
restoreResourcePriorities: defaultRestorePriorities, restoreResourcePriorities: defaultRestorePriorities,
clientQPS: defaultClientQPS, clientQPS: defaultClientQPS,
@@ -171,6 +176,7 @@ func NewCommand() *cobra.Command {
command.Flags().IntVar(&config.clientBurst, "client-burst", config.clientBurst, "maximum number of requests by the server to the Kubernetes API in a short period of time") command.Flags().IntVar(&config.clientBurst, "client-burst", config.clientBurst, "maximum number of requests by the server to the Kubernetes API in a short period of time")
command.Flags().StringVar(&config.profilerAddress, "profiler-address", config.profilerAddress, "the address to expose the pprof profiler") command.Flags().StringVar(&config.profilerAddress, "profiler-address", config.profilerAddress, "the address to expose the pprof profiler")
command.Flags().DurationVar(&config.resourceTerminatingTimeout, "terminating-resource-timeout", config.resourceTerminatingTimeout, "how long to wait on persistent volumes and namespaces to terminate during a restore before timing out") command.Flags().DurationVar(&config.resourceTerminatingTimeout, "terminating-resource-timeout", config.resourceTerminatingTimeout, "how long to wait on persistent volumes and namespaces to terminate during a restore before timing out")
command.Flags().DurationVar(&config.defaultBackupTTL, "default-backup-ttl", config.defaultBackupTTL, "how long to wait by default before backups can be garbage collected")
return command return command
} }
@@ -565,6 +571,7 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string
backupTracker, backupTracker,
s.sharedInformerFactory.Velero().V1().BackupStorageLocations(), s.sharedInformerFactory.Velero().V1().BackupStorageLocations(),
s.config.defaultBackupLocation, s.config.defaultBackupLocation,
s.config.defaultBackupTTL,
s.sharedInformerFactory.Velero().V1().VolumeSnapshotLocations(), s.sharedInformerFactory.Velero().V1().VolumeSnapshotLocations(),
defaultVolumeSnapshotLocations, defaultVolumeSnapshotLocations,
s.metrics, s.metrics,

View File

@@ -64,6 +64,7 @@ type backupController struct {
backupTracker BackupTracker backupTracker BackupTracker
backupLocationLister listers.BackupStorageLocationLister backupLocationLister listers.BackupStorageLocationLister
defaultBackupLocation string defaultBackupLocation string
defaultBackupTTL time.Duration
snapshotLocationLister listers.VolumeSnapshotLocationLister snapshotLocationLister listers.VolumeSnapshotLocationLister
defaultSnapshotLocations map[string]string defaultSnapshotLocations map[string]string
metrics *metrics.ServerMetrics metrics *metrics.ServerMetrics
@@ -80,6 +81,7 @@ func NewBackupController(
backupTracker BackupTracker, backupTracker BackupTracker,
backupLocationInformer informers.BackupStorageLocationInformer, backupLocationInformer informers.BackupStorageLocationInformer,
defaultBackupLocation string, defaultBackupLocation string,
defaultBackupTTL time.Duration,
volumeSnapshotLocationInformer informers.VolumeSnapshotLocationInformer, volumeSnapshotLocationInformer informers.VolumeSnapshotLocationInformer,
defaultSnapshotLocations map[string]string, defaultSnapshotLocations map[string]string,
metrics *metrics.ServerMetrics, metrics *metrics.ServerMetrics,
@@ -95,6 +97,7 @@ func NewBackupController(
backupTracker: backupTracker, backupTracker: backupTracker,
backupLocationLister: backupLocationInformer.Lister(), backupLocationLister: backupLocationInformer.Lister(),
defaultBackupLocation: defaultBackupLocation, defaultBackupLocation: defaultBackupLocation,
defaultBackupTTL: defaultBackupTTL,
snapshotLocationLister: volumeSnapshotLocationInformer.Lister(), snapshotLocationLister: volumeSnapshotLocationInformer.Lister(),
defaultSnapshotLocations: defaultSnapshotLocations, defaultSnapshotLocations: defaultSnapshotLocations,
metrics: metrics, metrics: metrics,
@@ -247,11 +250,14 @@ func (c *backupController) prepareBackupRequest(backup *velerov1api.Backup) *pkg
// set backup version // set backup version
request.Status.Version = pkgbackup.BackupVersion request.Status.Version = pkgbackup.BackupVersion
// calculate expiration if request.Spec.TTL.Duration == 0 {
if request.Spec.TTL.Duration > 0 { // set default backup TTL
request.Status.Expiration = metav1.NewTime(c.clock.Now().Add(request.Spec.TTL.Duration)) request.Spec.TTL.Duration = c.defaultBackupTTL
} }
// calculate expiration
request.Status.Expiration = metav1.NewTime(c.clock.Now().Add(request.Spec.TTL.Duration))
// default storage location if not specified // default storage location if not specified
if request.Spec.StorageLocation == "" { if request.Spec.StorageLocation == "" {
request.Spec.StorageLocation = c.defaultBackupLocation request.Spec.StorageLocation = c.defaultBackupLocation

View File

@@ -170,6 +170,7 @@ func TestProcessBackupValidationFailures(t *testing.T) {
backupLocationLister: sharedInformers.Velero().V1().BackupStorageLocations().Lister(), backupLocationLister: sharedInformers.Velero().V1().BackupStorageLocations().Lister(),
snapshotLocationLister: sharedInformers.Velero().V1().VolumeSnapshotLocations().Lister(), snapshotLocationLister: sharedInformers.Velero().V1().VolumeSnapshotLocations().Lister(),
defaultBackupLocation: defaultBackupLocation.Name, defaultBackupLocation: defaultBackupLocation.Name,
clock: &clock.RealClock{},
} }
require.NotNil(t, test.backup) require.NotNil(t, test.backup)
@@ -198,6 +199,61 @@ func TestProcessBackupValidationFailures(t *testing.T) {
} }
} }
func TestDefaultBackupTTL(t *testing.T) {
var (
defaultBackupTTL = metav1.Duration{Duration: 24 * 30 * time.Hour}
)
now, err := time.Parse(time.RFC1123Z, time.RFC1123Z)
require.NoError(t, err)
now = now.Local()
tests := []struct {
name string
backup *v1.Backup
backupLocation *v1.BackupStorageLocation
expectedTTL metav1.Duration
expectedExpiration metav1.Time
}{
{
name: "backup with no TTL specified",
backup: velerotest.NewTestBackup().WithName("backup-1").Backup,
expectedTTL: defaultBackupTTL,
expectedExpiration: metav1.NewTime(now.Add(defaultBackupTTL.Duration)),
},
{
name: "backup with TTL specified",
backup: velerotest.NewTestBackup().WithName("backup-1").WithTTL(1 * time.Hour).Backup,
expectedTTL: metav1.Duration{Duration: 1 * time.Hour},
expectedExpiration: metav1.NewTime(now.Add(1 * time.Hour)),
},
}
for _, test := range tests {
var (
clientset = fake.NewSimpleClientset(test.backup)
logger = logging.DefaultLogger(logrus.DebugLevel)
sharedInformers = informers.NewSharedInformerFactory(clientset, 0)
)
t.Run(test.name, func(t *testing.T) {
c := &backupController{
genericController: newGenericController("backup-test", logger),
backupLocationLister: sharedInformers.Velero().V1().BackupStorageLocations().Lister(),
snapshotLocationLister: sharedInformers.Velero().V1().VolumeSnapshotLocations().Lister(),
defaultBackupTTL: defaultBackupTTL.Duration,
clock: clock.NewFakeClock(now),
}
res := c.prepareBackupRequest(test.backup)
assert.NotNil(t, res)
assert.Equal(t, test.expectedTTL, res.Spec.TTL)
assert.Equal(t, test.expectedExpiration, res.Status.Expiration)
})
}
}
func TestProcessBackupCompletions(t *testing.T) { func TestProcessBackupCompletions(t *testing.T) {
defaultBackupLocation := velerotest.NewTestBackupStorageLocation().WithName("loc-1").BackupStorageLocation defaultBackupLocation := velerotest.NewTestBackupStorageLocation().WithName("loc-1").BackupStorageLocation
@@ -231,6 +287,7 @@ func TestProcessBackupCompletions(t *testing.T) {
Version: 1, Version: 1,
StartTimestamp: metav1.NewTime(now), StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now), CompletionTimestamp: metav1.NewTime(now),
Expiration: metav1.NewTime(now),
}, },
}, },
}, },
@@ -254,6 +311,7 @@ func TestProcessBackupCompletions(t *testing.T) {
Version: 1, Version: 1,
StartTimestamp: metav1.NewTime(now), StartTimestamp: metav1.NewTime(now),
CompletionTimestamp: metav1.NewTime(now), CompletionTimestamp: metav1.NewTime(now),
Expiration: metav1.NewTime(now),
}, },
}, },
}, },