Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions agent/pkg/spec/migration/migration_from_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ const (
// imageclusterinstall_controller.go#L118
VeleroRestoreNameLabel = "velero.io/restore-name"

GlobalHubRestoreName = "globalhub"
// VeleroRestoreStatusAnnotation marks ImageClusterInstall as restored by velero during migration.
VeleroRestoreStatusAnnotation = "velero.io/restore-status"

// ClusterBackupLabel marks resources for cluster activation backup.
ClusterBackupLabel = "cluster.open-cluster-management.io/backup"

GlobalHubRestoreName = "globalhub"
ClusterActivationBackup = "cluster-activation"
)

type MigrationSourceSyncer struct {
Expand Down Expand Up @@ -440,23 +447,50 @@ func (s *MigrationSourceSyncer) processResourceByType(
delete(annotations, HivePauseAnnotation)
}
resource.SetAnnotations(annotations)
case "BareMetalHost", "DataImage":
// Remove pause annotation from BareMetalHost and DataImage
case "BareMetalHost":
// Remove pause annotation from BareMetalHost
annotations := resource.GetAnnotations()
if annotations != nil {
delete(annotations, Metal3PauseAnnotation)
}
resource.SetAnnotations(annotations)

// Add cluster backup label with correct value
labels := resource.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
if labels[ClusterBackupLabel] != ClusterActivationBackup {
labels[ClusterBackupLabel] = ClusterActivationBackup
}
resource.SetLabels(labels)
case "DataImage":
// Remove pause annotation from DataImage
annotations := resource.GetAnnotations()
if annotations != nil {
delete(annotations, Metal3PauseAnnotation)
}
resource.SetAnnotations(annotations)
case "ImageClusterInstall":
// Add velero restore label if not present
// Add velero restore label with correct value
labels := resource.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
if _, exists := labels[VeleroRestoreNameLabel]; !exists {
if labels[VeleroRestoreNameLabel] != GlobalHubRestoreName {
labels[VeleroRestoreNameLabel] = GlobalHubRestoreName
}
resource.SetLabels(labels)

// Add velero restore status annotation with correct value
annotations := resource.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
if annotations[VeleroRestoreStatusAnnotation] != "true" {
annotations[VeleroRestoreStatusAnnotation] = "true"
}
resource.SetAnnotations(annotations)
}
}

Expand Down
31 changes: 26 additions & 5 deletions agent/pkg/spec/migration/migration_from_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3193,7 +3193,7 @@ func TestProcessResourceByType(t *testing.T) {
}(),
},
{
name: "BareMetalHost: should remove pause annotation",
name: "BareMetalHost: should remove pause annotation and add backup label",
resource: func() *unstructured.Unstructured {
bmh := &unstructured.Unstructured{}
bmh.SetGroupVersionKind(schema.GroupVersionKind{
Expand Down Expand Up @@ -3228,6 +3228,9 @@ func TestProcessResourceByType(t *testing.T) {
bmh.SetAnnotations(map[string]string{
"other-annotation": "should-remain",
})
bmh.SetLabels(map[string]string{
ClusterBackupLabel: ClusterActivationBackup,
})
return bmh
}(),
},
Expand Down Expand Up @@ -3271,7 +3274,7 @@ func TestProcessResourceByType(t *testing.T) {
}(),
},
{
name: "ImageClusterInstall: should add velero restore label when not present",
name: "ImageClusterInstall: should add velero restore label and annotation when not present",
resource: func() *unstructured.Unstructured {
ici := &unstructured.Unstructured{}
ici.SetGroupVersionKind(schema.GroupVersionKind{
Expand Down Expand Up @@ -3302,11 +3305,14 @@ func TestProcessResourceByType(t *testing.T) {
ici.SetLabels(map[string]string{
VeleroRestoreNameLabel: GlobalHubRestoreName,
})
ici.SetAnnotations(map[string]string{
VeleroRestoreStatusAnnotation: "true",
})
return ici
}(),
},
{
name: "ImageClusterInstall: should not override existing velero restore label",
name: "ImageClusterInstall: should correct existing velero restore label and annotation values",
resource: func() *unstructured.Unstructured {
ici := &unstructured.Unstructured{}
ici.SetGroupVersionKind(schema.GroupVersionKind{
Expand All @@ -3320,6 +3326,10 @@ func TestProcessResourceByType(t *testing.T) {
VeleroRestoreNameLabel: "existing-restore",
"other-label": "should-remain",
})
ici.SetAnnotations(map[string]string{
VeleroRestoreStatusAnnotation: "false",
"other-annotation": "should-remain",
})
return ici
}(),
migrateResource: MigrationResource{
Expand All @@ -3339,14 +3349,18 @@ func TestProcessResourceByType(t *testing.T) {
ici.SetName("test-ici")
ici.SetNamespace("test-cluster")
ici.SetLabels(map[string]string{
VeleroRestoreNameLabel: "existing-restore",
VeleroRestoreNameLabel: GlobalHubRestoreName,
"other-label": "should-remain",
})
ici.SetAnnotations(map[string]string{
VeleroRestoreStatusAnnotation: "true",
"other-annotation": "should-remain",
})
return ici
}(),
},
{
name: "ImageClusterInstall: should preserve existing labels when adding velero label",
name: "ImageClusterInstall: should preserve existing labels and annotations when adding velero label and annotation",
resource: func() *unstructured.Unstructured {
ici := &unstructured.Unstructured{}
ici.SetGroupVersionKind(schema.GroupVersionKind{
Expand All @@ -3359,6 +3373,9 @@ func TestProcessResourceByType(t *testing.T) {
ici.SetLabels(map[string]string{
"existing-label": "value",
})
ici.SetAnnotations(map[string]string{
"existing-annotation": "value",
})
return ici
}(),
migrateResource: MigrationResource{
Expand All @@ -3381,6 +3398,10 @@ func TestProcessResourceByType(t *testing.T) {
"existing-label": "value",
VeleroRestoreNameLabel: GlobalHubRestoreName,
})
ici.SetAnnotations(map[string]string{
"existing-annotation": "value",
VeleroRestoreStatusAnnotation: "true",
})
return ici
}(),
},
Expand Down
Loading