Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing temporary annotations and labels from pods, pvcs, and pvs #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
21 changes: 21 additions & 0 deletions velero-plugins/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// temporary keys added by mig-controller
const (
transientBackupAnnotationKey = "openshift.io/migrate-type"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same annotation that we use as a permanent annotation on the Backup resource. We should probably use a different one just to make it clear that they are serving a different function, although from a code point of view, they won't clash since they're applied to different resource types.

transientBackupLabelKey = "openshift.io/pv-backup"
)

// ReplaceImageRefPrefix replaces an image reference prefix with newPrefix.
// If the input image reference does not start with oldPrefix, an error is returned
func ReplaceImageRefPrefix(s, oldPrefix, newPrefix string) (string, error) {
Expand Down Expand Up @@ -126,3 +132,18 @@ func GetOwnerReferences(item runtime.Unstructured) ([]metav1.OwnerReference, err
}
return metadata.GetOwnerReferences(), nil
}

// DeleteTemporaryKeys : deletes temporary annotations and labels added by mig-controller during backup
func DeleteTemporaryKeys(labels map[string]string, annotations map[string]string) {
delete(annotations, transientBackupAnnotationKey)
// transient labels have unique UID's at the end, need to delete all of them
transientLabels := make([]string, len(labels))
for label := range labels {
if strings.Contains(label, transientBackupLabelKey) {
transientLabels = append(transientLabels, label)
}
}
for _, label := range transientLabels {
delete(labels, label)
}
}
3 changes: 3 additions & 0 deletions velero-plugins/pod/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (p *RestorePlugin) Execute(input *velero.RestoreItemActionExecuteInput) (*v
json.Unmarshal(itemMarshal, &pod)
p.Log.Infof("[pod-restore] pod: %s", pod.Name)

// delete temporary annotations and labels used by mig-controller during backup
common.DeleteTemporaryKeys(pod.Labels, pod.Annotations)

if input.Restore.Annotations[common.MigrateCopyPhaseAnnotation] == "stage" {
common.ConfigureContainerSleep(pod.Spec.Containers, "infinity")
common.ConfigureContainerSleep(pod.Spec.InitContainers, "0")
Expand Down
3 changes: 3 additions & 0 deletions velero-plugins/pv/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (p *RestorePlugin) Execute(input *velero.RestoreItemActionExecuteInput) (*v
json.Unmarshal(itemMarshal, &pv)
p.Log.Infof("[pv-restore] pv: %s", pv.Name)

// delete temporary annotations and labels used by mig-controller during backup
common.DeleteTemporaryKeys(pv.Labels, pv.Annotations)

if pv.Annotations[common.MigrateTypeAnnotation] == "copy" {
p.Log.Infof("[pv-restore] Not a swing PV migration. Skipping pv restore, %s.", pv.Name)
return velero.NewRestoreItemActionExecuteOutput(input.Item).WithoutRestore(), nil
Expand Down
3 changes: 3 additions & 0 deletions velero-plugins/pvc/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (p *RestorePlugin) Execute(input *velero.RestoreItemActionExecuteInput) (*v
json.Unmarshal(itemMarshal, &pvc)
p.Log.Infof("[pvc-restore] pvc: %s", pvc.Name)

// delete temporary annotations and labels used by mig-controller during backup
common.DeleteTemporaryKeys(pvc.Labels, pvc.Annotations)

// Use default behavior (restore the PV) for a swing migration.
// For copy we remove annotations and PV volumeName
if pvc.Annotations[common.MigrateTypeAnnotation] == "copy" {
Expand Down