Add existingResourcePolicy restore CR validation to controller.

Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
This commit is contained in:
Tiger Kaovilai
2024-04-29 16:06:49 -04:00
parent 8f78aaa5f6
commit e1bef5b6c2
6 changed files with 36 additions and 14 deletions

View File

@@ -0,0 +1 @@
Add existingResourcePolicy restore CR validation to controller

View File

@@ -37,6 +37,7 @@ import (
"github.com/vmware-tanzu/velero/pkg/cmd"
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
"github.com/vmware-tanzu/velero/pkg/cmd/util/output"
"github.com/vmware-tanzu/velero/pkg/restore/util"
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)
@@ -199,7 +200,7 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
return errors.New("either a 'selector' or an 'or-selector' can be specified, but not both")
}
if len(o.ExistingResourcePolicy) > 0 && !isResourcePolicyValid(o.ExistingResourcePolicy) {
if len(o.ExistingResourcePolicy) > 0 && !util.IsResourcePolicyValid(o.ExistingResourcePolicy) {
return errors.New("existing-resource-policy has invalid value, it accepts only none, update as value")
}
@@ -428,10 +429,3 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
return nil
}
func isResourcePolicyValid(resourcePolicy string) bool {
if resourcePolicy == string(api.PolicyTypeNone) || resourcePolicy == string(api.PolicyTypeUpdate) {
return true
}
return false
}

View File

@@ -34,12 +34,6 @@ import (
velerotest "github.com/vmware-tanzu/velero/pkg/test"
)
func TestIsResourcePolicyValid(t *testing.T) {
require.True(t, isResourcePolicyValid(string(velerov1api.PolicyTypeNone)))
require.True(t, isResourcePolicyValid(string(velerov1api.PolicyTypeUpdate)))
require.False(t, isResourcePolicyValid(""))
}
func TestMostRecentBackup(t *testing.T) {
backups := []velerov1api.Backup{
*builder.ForBackup(cmdtest.VeleroNameSpace, "backup0").StartTimestamp(time.Now().Add(3 * time.Second)).Phase(velerov1api.BackupPhaseDeleting).Result(),

View File

@@ -53,6 +53,7 @@ import (
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt"
"github.com/vmware-tanzu/velero/pkg/plugin/framework"
pkgrestore "github.com/vmware-tanzu/velero/pkg/restore"
"github.com/vmware-tanzu/velero/pkg/restore/util"
"github.com/vmware-tanzu/velero/pkg/util/collections"
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube"
"github.com/vmware-tanzu/velero/pkg/util/logging"
@@ -346,6 +347,11 @@ func (r *restoreReconciler) validateAndComplete(restore *api.Restore) (backupInf
}
}
// validate ExistingResourcePolicy
if restore.Spec.ExistingResourcePolicy != "" && !util.IsResourcePolicyValid(string(restore.Spec.ExistingResourcePolicy)) {
restore.Status.ValidationErrors = append(restore.Status.ValidationErrors, fmt.Sprintf("Invalid ExistingResourcePolicy: %s", restore.Spec.ExistingResourcePolicy))
}
// if ScheduleName is specified, fill in BackupName with the most recent successful backup from
// the schedule
if restore.Spec.ScheduleName != "" {

12
pkg/restore/util/util.go Normal file
View File

@@ -0,0 +1,12 @@
package util
import (
api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
)
func IsResourcePolicyValid(resourcePolicy string) bool {
if resourcePolicy == string(api.PolicyTypeNone) || resourcePolicy == string(api.PolicyTypeUpdate) {
return true
}
return false
}

View File

@@ -0,0 +1,15 @@
package util
import (
"testing"
"github.com/stretchr/testify/require"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
)
func TestIsResourcePolicyValid(t *testing.T) {
require.True(t, IsResourcePolicyValid(string(velerov1api.PolicyTypeNone)))
require.True(t, IsResourcePolicyValid(string(velerov1api.PolicyTypeUpdate)))
require.False(t, IsResourcePolicyValid(""))
}