Skip to content

Commit

Permalink
Add existingResourcePolicy restore CR validation to controller.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Apr 29, 2024
1 parent 8f78aaa commit 8e1bcbf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7757-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add existingResourcePolicy restore CR validation to controller
10 changes: 2 additions & 8 deletions pkg/cmd/cli/restore/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}
6 changes: 0 additions & 6 deletions pkg/cmd/cli/restore/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/restore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand Down
12 changes: 12 additions & 0 deletions pkg/restore/util/util.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions pkg/restore/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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(""))
}

0 comments on commit 8e1bcbf

Please sign in to comment.