|
| 1 | +/* |
| 2 | + * This file is part of the Kubevirt Velero Plugin project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Copyright 2025 Red Hat, Inc. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package plugin |
| 21 | + |
| 22 | +import ( |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/api/meta" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + "kubevirt.io/kubevirt-velero-plugin/pkg/util" |
| 28 | + |
| 29 | + v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" |
| 30 | + "github.com/vmware-tanzu/velero/pkg/plugin/velero" |
| 31 | +) |
| 32 | + |
| 33 | +// PVCBackupItemAction is a backup item action for backing up PersistentVolumeClaims |
| 34 | +type PVCBackupItemAction struct { |
| 35 | + log logrus.FieldLogger |
| 36 | +} |
| 37 | + |
| 38 | +// NewPVCBackupItemAction instantiates a PVCBackupItemAction. |
| 39 | +func NewPVCBackupItemAction(log logrus.FieldLogger) *PVCBackupItemAction { |
| 40 | + return &PVCBackupItemAction{log: log} |
| 41 | +} |
| 42 | + |
| 43 | +// AppliesTo returns information about which resources this action should be invoked for. |
| 44 | +func (p *PVCBackupItemAction) AppliesTo() (velero.ResourceSelector, error) { |
| 45 | + return velero.ResourceSelector{ |
| 46 | + IncludedResources: []string{ |
| 47 | + "PersistentVolumeClaim", |
| 48 | + }, |
| 49 | + }, |
| 50 | + nil |
| 51 | +} |
| 52 | + |
| 53 | +// Execute allows the ItemAction to perform arbitrary logic with the item being backed up, |
| 54 | +// in this case, adding UID labels to PVCs for selective restore functionality. |
| 55 | +func (p *PVCBackupItemAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runtime.Unstructured, []velero.ResourceIdentifier, error) { |
| 56 | + p.log.Info("Executing PVCBackupItemAction") |
| 57 | + |
| 58 | + metadata, err := meta.Accessor(item) |
| 59 | + if err != nil { |
| 60 | + return nil, nil, err |
| 61 | + } |
| 62 | + |
| 63 | + // Add UID label for selective restore |
| 64 | + labels := metadata.GetLabels() |
| 65 | + if labels == nil { |
| 66 | + labels = make(map[string]string) |
| 67 | + } |
| 68 | + |
| 69 | + pvcUID := string(metadata.GetUID()) |
| 70 | + if pvcUID == "" { |
| 71 | + extra := []velero.ResourceIdentifier{} |
| 72 | + return item, extra, nil |
| 73 | + } |
| 74 | + |
| 75 | + // Handle collision detection - preserve original value if it exists |
| 76 | + // Even if the existing value matches the UID, we need to preserve it |
| 77 | + // because the user might have legitimately set this label themselves |
| 78 | + if existingValue, exists := labels[util.PVCUIDLabel]; exists { |
| 79 | + annotations := metadata.GetAnnotations() |
| 80 | + if annotations == nil { |
| 81 | + annotations = make(map[string]string) |
| 82 | + } |
| 83 | + annotations[util.OriginalPVCUIDAnnotation] = existingValue |
| 84 | + metadata.SetAnnotations(annotations) |
| 85 | + } |
| 86 | + |
| 87 | + labels[util.PVCUIDLabel] = pvcUID |
| 88 | + metadata.SetLabels(labels) |
| 89 | + |
| 90 | + extra := []velero.ResourceIdentifier{} |
| 91 | + return item, extra, nil |
| 92 | +} |
| 93 | + |
0 commit comments