Skip to content

Commit

Permalink
backup: init migrate controller (#408)
Browse files Browse the repository at this point in the history
* backup: init migrate controller

Signed-off-by: Xieql <[email protected]>

* fix

Signed-off-by: Xieql <[email protected]>

---------

Signed-off-by: Xieql <[email protected]>
  • Loading branch information
Xieql authored Oct 24, 2023
1 parent d08adb9 commit a5a20e2
Showing 5 changed files with 475 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/fleet-manager/backup/backup.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions pkg/apis/backups/v1alpha1/migrate_type.go
Original file line number Diff line number Diff line change
@@ -102,11 +102,11 @@ const (
// the controller's validations and therefore will not run.
MigratePhaseFailedValidation MigratePhase = "FailedValidation"

// MigratePhaseWaitingForSource means the migrate is currently fetching source cluster resource.
MigratePhaseWaitingForSource MigratePhase = "WaitingForSource"
// MigratePhaseBackupInProgress indicates that the backup phase of the migrate is currently in progress.
MigratePhaseBackupInProgress MigratePhase = "BackupInProgress"

// MigratePhaseInProgress means the migrate is currently executing migrating.
MigratePhaseInProgress MigratePhase = "InProgress"
// MigratePhaseRestoreInProgress indicates that the restore phase of the migrate is currently in progress.
MigratePhaseRestoreInProgress MigratePhase = "RestoreInProgress"

// MigratePhaseCompleted means the migrate has run successfully
// without errors.
37 changes: 37 additions & 0 deletions pkg/fleet-manager/backup_restore_migrate_shared.go
Original file line number Diff line number Diff line change
@@ -457,3 +457,40 @@ func syncVeleroRestoreStatus(ctx context.Context, destinationClusters map[Cluste

return clusterDetails, nil
}

// 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)
}
145 changes: 145 additions & 0 deletions pkg/fleet-manager/backup_restore_migrate_shared_test.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import (
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"
@@ -657,3 +658,147 @@ func TestAllRestoreCompleted(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)
assert.Equal(t, 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)
assert.Equal(t, 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 a5a20e2

Please sign in to comment.