Add BackupType in backup.spec.

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>
This commit is contained in:
Xun Jiang
2026-07-15 08:35:06 +00:00
committed by xun.jiang
parent 30d05a3e40
commit d82c552aaa
11 changed files with 182 additions and 3 deletions
+20 -1
View File
@@ -108,6 +108,7 @@ type CreateOptions struct {
ResPoliciesConfigmap string
client kbclient.WithWatch
ParallelFilesUpload int
BackupType string
}
func NewCreateOptions() *CreateOptions {
@@ -156,6 +157,7 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.ResPoliciesConfigmap, "resource-policies-configmap", "", "Reference to the resource policies configmap that backup should use")
flags.StringVar(&o.DataMover, "data-mover", "", "Specify the data mover to be used by the backup. If the parameter is not set or set as 'velero', the built-in data mover will be used")
flags.IntVar(&o.ParallelFilesUpload, "parallel-files-upload", 0, "Number of files uploads simultaneously when running a backup. This is only applicable for the kopia uploader")
flags.StringVar(&o.BackupType, "backup-type", "", "Specify how volume data is backed up, with possible values including Full and Incremental.")
}
// BindWait binds the wait flag separately so it is not called by other create
@@ -217,6 +219,10 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
}
}
if err := o.validateBackupType(); err != nil {
return err
}
return nil
}
@@ -231,6 +237,18 @@ func (o *CreateOptions) validateFromScheduleFlag(c *cobra.Command) error {
return nil
}
func (o *CreateOptions) validateBackupType() error {
backupType := strings.TrimSpace(o.BackupType)
switch backupType {
case "", "Incremental", "Full":
default:
return fmt.Errorf("invalid backup type %s - valid values are 'Incremental', and 'Full'", backupType)
}
return nil
}
func (o *CreateOptions) Complete(args []string, f client.Factory) error {
// If an explicit name is specified, use that name
if len(args) > 0 {
@@ -393,7 +411,8 @@ func (o *CreateOptions) BuildBackup(namespace string) (*velerov1api.Backup, erro
VolumeSnapshotLocations(o.SnapshotLocations...).
CSISnapshotTimeout(o.CSISnapshotTimeout).
ItemOperationTimeout(o.ItemOperationTimeout).
DataMover(o.DataMover)
DataMover(o.DataMover).
BackupType(velerov1api.BackupType(o.BackupType))
if len(o.OrderedResources) > 0 {
orders, err := ParseOrderedResources(o.OrderedResources)
if err != nil {
+39
View File
@@ -122,6 +122,42 @@ func TestCreateOptions_ValidateFromScheduleFlag(t *testing.T) {
})
}
func TestCreateOptions_ValidateBackupType(t *testing.T) {
t.Run("valid backup types", func(t *testing.T) {
o := NewCreateOptions()
o.BackupType = ""
err := o.validateBackupType()
require.NoError(t, err)
o.BackupType = "Incremental"
err = o.validateBackupType()
require.NoError(t, err)
o.BackupType = "Full"
err = o.validateBackupType()
require.NoError(t, err)
o.BackupType = " Incremental "
err = o.validateBackupType()
require.NoError(t, err)
})
t.Run("invalid backup type", func(t *testing.T) {
o := NewCreateOptions()
o.BackupType = "incremental"
err := o.validateBackupType()
require.Error(t, err)
require.Equal(t, "invalid backup type incremental - valid values are 'Incremental', and 'Full'", err.Error())
o.BackupType = "invalid"
err = o.validateBackupType()
require.Error(t, err)
require.Equal(t, "invalid backup type invalid - valid values are 'Incremental', and 'Full'", err.Error())
})
}
func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
o := NewCreateOptions()
o.FromSchedule = "test"
@@ -231,6 +267,7 @@ func TestCreateCommand(t *testing.T) {
resPoliciesConfigmap := "cm-name-2"
dataMover := "velero"
parallelFilesUpload := 10
backupType := "Incremental"
flags := new(flag.FlagSet)
o := NewCreateOptions()
o.BindFlags(flags)
@@ -260,6 +297,7 @@ func TestCreateCommand(t *testing.T) {
flags.Parse([]string{"--resource-policies-configmap", resPoliciesConfigmap})
flags.Parse([]string{"--data-mover", dataMover})
flags.Parse([]string{"--parallel-files-upload", strconv.Itoa(parallelFilesUpload)})
flags.Parse([]string{"--backup-type", backupType})
//flags.Parse([]string{"--wait"})
client := velerotest.NewFakeControllerRuntimeClient(t).(kbclient.WithWatch)
@@ -310,6 +348,7 @@ func TestCreateCommand(t *testing.T) {
require.Equal(t, resPoliciesConfigmap, o.ResPoliciesConfigmap)
require.Equal(t, dataMover, o.DataMover)
require.Equal(t, parallelFilesUpload, o.ParallelFilesUpload)
require.Equal(t, backupType, o.BackupType)
//assert.Equal(t, true, o.Wait)
// verify oldAndNewFilterParametersUsedTogether