Skip to content

Commit

Permalink
backup: init migrate controller
Browse files Browse the repository at this point in the history
Signed-off-by: Xieql <xieqianglong@huawei.com>
  • Loading branch information
Xieql committed Oct 21, 2023
1 parent 9524091 commit 63dbcc7
Show file tree
Hide file tree
Showing 4 changed files with 487 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/fleet-manager/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ func InitControllers(ctx context.Context, opts *options.Options, mgr ctrl.Manage
return err
}

if err := (&fleet.MigrateManager{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: opts.Concurrency, RecoverPanic: true}); err != nil {
log.Error(err, "unable to create controller", "controller", "Restore")
return err
}

return nil
}
37 changes: 37 additions & 0 deletions pkg/fleet-manager/backup_restore_migrate_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,40 @@ func listResourcesFromClusterClient(ctx context.Context, namespace string, label
}
return clusterClient.List(ctx, objList, opts)
}

// isMigrateSourceReady checks if the 'SourceReadyCondition' of a Migrate object is set to 'True'.
func isMigrateSourceReady(migrate *backupapi.Migrate) bool {
for _, condition := range migrate.Status.Conditions {
if condition.Type == backupapi.SourceReadyCondition && condition.Status == "True" {
return true
}
}
return false
}

// buildVeleroBackupFromMigrate constructs a Velero Backup instance based on the provided migrate specification.
func buildVeleroBackupFromMigrate(migrateSpec *backupapi.MigrateSpec, labels map[string]string, veleroBackupName string) *velerov1.Backup {
// Only consider migrateSpec.Policy.ResourceFilter and migrateSpec.Policy.OrderedResources when building the Velero backup.
backupParam := &backupapi.BackupSpec{}
if migrateSpec.Policy != nil {
backupParam.Policy = &backupapi.BackupPolicy{
ResourceFilter: migrateSpec.Policy.ResourceFilter,
OrderedResources: migrateSpec.Policy.OrderedResources,
}
}
return buildVeleroBackupInstance(backupParam, labels, veleroBackupName)
}

// buildVeleroRestoreFromMigrate constructs a Velero Restore instance based on the provided migrate specification.
func buildVeleroRestoreFromMigrate(migrateSpec *backupapi.MigrateSpec, labels map[string]string, veleroBackupName, veleroRestoreName string) *velerov1.Restore {
// Only consider migrateSpec.Policy.NamespaceMapping, migrateSpec.Policy.PreserveNodePorts, and migrateSpec.Policy.MigrateStatus when building the Velero restore.
restoreParam := &backupapi.RestoreSpec{}
if migrateSpec.Policy != nil {
restoreParam.Policy = &backupapi.RestorePolicy{
NamespaceMapping: migrateSpec.Policy.NamespaceMapping,
PreserveNodePorts: migrateSpec.Policy.PreserveNodePorts,
PreserveStatus: migrateSpec.Policy.MigrateStatus,
}
}
return buildVeleroRestoreInstance(restoreParam, labels, veleroBackupName, veleroRestoreName)

Check failure on line 349 in pkg/fleet-manager/backup_restore_migrate_shared.go

View workflow job for this annotation

GitHub Actions / Unit test

undefined: buildVeleroRestoreInstance

Check failure on line 349 in pkg/fleet-manager/backup_restore_migrate_shared.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: buildVeleroRestoreInstance

Check failure on line 349 in pkg/fleet-manager/backup_restore_migrate_shared.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: buildVeleroRestoreInstance

Check failure on line 349 in pkg/fleet-manager/backup_restore_migrate_shared.go

View workflow job for this annotation

GitHub Actions / Compile

undefined: buildVeleroRestoreInstance
}
150 changes: 150 additions & 0 deletions pkg/fleet-manager/backup_restore_migrate_shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ package fleet

import (
"os"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/yaml"

backupapi "kurator.dev/kurator/pkg/apis/backups/v1alpha1"
Expand Down Expand Up @@ -429,3 +431,151 @@ func TestGetCronInterval(t *testing.T) {
})
}
}

func TestIsMigrateSourceReady(t *testing.T) {
tests := []struct {
name string
migrate *backupapi.Migrate
expected bool
}{
{
name: "SourceReadyCondition is True",
migrate: &backupapi.Migrate{
Status: backupapi.MigrateStatus{
Conditions: capiv1.Conditions{
{
Type: backupapi.SourceReadyCondition,
Status: "True",
},
},
},
},
expected: true,
},
{
name: "SourceReadyCondition is not True",
migrate: &backupapi.Migrate{
Status: backupapi.MigrateStatus{
Conditions: capiv1.Conditions{
{
Type: backupapi.SourceReadyCondition,
Status: "False",
},
},
},
},
expected: false,
},
{
name: "SourceReadyCondition does not exist",
migrate: &backupapi.Migrate{
Status: backupapi.MigrateStatus{
Conditions: capiv1.Conditions{
{
Type: "AnotherCondition",
Status: "True",
},
},
},
},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isMigrateSourceReady(tt.migrate)
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}

func TestBuildVeleroBackupFromMigrate(t *testing.T) {
tests := []struct {
name string
migrateSpec *backupapi.MigrateSpec
labels map[string]string
veleroBackupName string
expected *velerov1.BackupSpec
}{
{
name: "all values set",
migrateSpec: &backupapi.MigrateSpec{
Policy: &backupapi.MigratePolicy{
ResourceFilter: &backupapi.ResourceFilter{
IncludedNamespaces: []string{"ns1", "ns2"},
ExcludedResources: []string{"pods"},
},
OrderedResources: map[string]string{
"pods": "ns1/pod1, ns1/pod2, ns1/pod3",
"persistentvolumes": "pv4, pv8",
},
},
},
labels: map[string]string{
"test": "value",
},
veleroBackupName: "test-backup",
expected: &velerov1.BackupSpec{
IncludedNamespaces: []string{"ns1", "ns2"},
ExcludedResources: []string{"pods"},
OrderedResources: map[string]string{
"pods": "ns1/pod1, ns1/pod2, ns1/pod3",
"persistentvolumes": "pv4, pv8",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := buildVeleroBackupFromMigrate(tt.migrateSpec, tt.labels, tt.veleroBackupName)
if !reflect.DeepEqual(result.Spec, tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, result.Spec)
}
})
}
}

func TestBuildVeleroRestoreFromMigrate(t *testing.T) {
tests := []struct {
name string
migrateSpec *backupapi.MigrateSpec
expected *velerov1.RestoreSpec
}{
{
name: "all values set",
migrateSpec: &backupapi.MigrateSpec{
Policy: &backupapi.MigratePolicy{
NamespaceMapping: map[string]string{"src": "dst"},
MigrateStatus: &backupapi.PreserveStatus{
IncludedResources: []string{"deployments", "services"},
ExcludedResources: []string{"pods"},
},
PreserveNodePorts: boolPtr(true),
},
},
expected: &velerov1.RestoreSpec{
NamespaceMapping: map[string]string{"src": "dst"},
RestoreStatus: &velerov1.RestoreStatusSpec{
IncludedResources: []string{"deployments", "services"},
ExcludedResources: []string{"pods"},
},
PreserveNodePorts: boolPtr(true),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildVeleroRestoreFromMigrate(tt.migrateSpec, nil, "", "")
assert.Equal(t, tt.expected, &got.Spec)
})
}
}

func boolPtr(b bool) *bool {
return &b
}
Loading

0 comments on commit 63dbcc7

Please sign in to comment.