-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xun Jiang <[email protected]>
- Loading branch information
1 parent
d974cd3
commit 2001b16
Showing
59 changed files
with
5,358 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Merge CSI plugin code into Velero. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
"github.com/vmware-tanzu/velero/pkg/util/csi" | ||
) | ||
|
||
// csiVolumeSnapshotDeleteItemAction is a backup item action plugin for Velero. | ||
type csiVolumeSnapshotDeleteItemAction struct { | ||
Log logrus.FieldLogger | ||
} | ||
|
||
// AppliesTo returns information indicating that the VolumeSnapshotBackupItemAction should be invoked to backup volumesnapshots. | ||
func (p *csiVolumeSnapshotDeleteItemAction) AppliesTo() (velero.ResourceSelector, error) { | ||
p.Log.Debug("VolumeSnapshotBackupItemAction AppliesTo") | ||
|
||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"volumesnapshots.snapshot.storage.k8s.io"}, | ||
}, nil | ||
} | ||
|
||
func (p *csiVolumeSnapshotDeleteItemAction) Execute(input *velero.DeleteItemActionExecuteInput) error { | ||
p.Log.Info("Starting VolumeSnapshotDeleteItemAction for volumeSnapshot") | ||
|
||
var vs snapshotv1api.VolumeSnapshot | ||
|
||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), &vs); err != nil { | ||
return errors.Wrapf(err, "failed to convert input.Item from unstructured") | ||
} | ||
|
||
// We don't want this DeleteItemAction plugin to delete Volumesnapshot taken outside of Velero. | ||
// So skip deleting Volumesnapshot objects that were not created in the process of creating | ||
// the Velero backup being deleted. | ||
if !csi.HasBackupLabel(&vs.ObjectMeta, input.Backup.Name) { | ||
p.Log.Info("VolumeSnapshot %s/%s was not taken by backup %s, skipping deletion", vs.Namespace, vs.Name, input.Backup.Name) | ||
return nil | ||
} | ||
|
||
p.Log.Infof("Deleting Volumesnapshot %s/%s", vs.Namespace, vs.Name) | ||
_, snapClient, err := csi.GetClients() | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
if vs.Status != nil && vs.Status.BoundVolumeSnapshotContentName != nil { | ||
// we patch the DeletionPolicy of the volumesnapshotcontent to set it to Delete. | ||
// This ensures that the volume snapshot in the storage provider is also deleted. | ||
err := csi.SetVolumeSnapshotContentDeletionPolicy(*vs.Status.BoundVolumeSnapshotContentName, snapClient.SnapshotV1()) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
return errors.Wrapf(err, fmt.Sprintf("failed to patch DeletionPolicy of volume snapshot %s/%s", vs.Namespace, vs.Name)) | ||
} | ||
|
||
if apierrors.IsNotFound(err) { | ||
return nil | ||
} | ||
} | ||
err = snapClient.SnapshotV1().VolumeSnapshots(vs.Namespace).Delete(context.TODO(), vs.Name, metav1.DeleteOptions{}) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewCsiVolumeSnapshotDeleteItemAction(logger logrus.FieldLogger) (interface{}, error) { | ||
return &csiVolumeSnapshotDeleteItemAction{logger}, nil | ||
} |
81 changes: 81 additions & 0 deletions
81
internal/delete/actions/csi_volumesnapshotcontent_action.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
"github.com/vmware-tanzu/velero/pkg/util/csi" | ||
) | ||
|
||
// csiVolumeSnapshotContentDeleteItemAction is a restore item action plugin for Velero | ||
type csiVolumeSnapshotContentDeleteItemAction struct { | ||
Log logrus.FieldLogger | ||
} | ||
|
||
// AppliesTo returns information indicating VolumeSnapshotContentRestoreItemAction action should be invoked while restoring | ||
// volumesnapshotcontent.snapshot.storage.k8s.io resources | ||
func (p *csiVolumeSnapshotContentDeleteItemAction) AppliesTo() (velero.ResourceSelector, error) { | ||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"volumesnapshotcontent.snapshot.storage.k8s.io"}, | ||
}, nil | ||
} | ||
|
||
func (p *csiVolumeSnapshotContentDeleteItemAction) Execute(input *velero.DeleteItemActionExecuteInput) error { | ||
p.Log.Info("Starting VolumeSnapshotContentDeleteItemAction") | ||
|
||
var snapCont snapshotv1api.VolumeSnapshotContent | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), &snapCont); err != nil { | ||
return errors.Wrapf(err, "failed to convert input.Item from unstructured") | ||
} | ||
|
||
// We don't want this DeleteItemAction plugin to delete VolumesnapshotContent taken outside of Velero. | ||
// So skip deleting VolumesnapshotContent objects that were not created in the process of creating | ||
// the Velero backup being deleted. | ||
if !csi.HasBackupLabel(&snapCont.ObjectMeta, input.Backup.Name) { | ||
p.Log.Info("VolumeSnapshotContent %s was not taken by backup %s, skipping deletion", snapCont.Name, input.Backup.Name) | ||
return nil | ||
} | ||
|
||
p.Log.Infof("Deleting VolumeSnapshotContent %s", snapCont.Name) | ||
|
||
_, snapClient, err := csi.GetClients() | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
err = csi.SetVolumeSnapshotContentDeletionPolicy(snapCont.Name, snapClient.SnapshotV1()) | ||
if err != nil { | ||
// #4764: Leave a warning when VolumeSnapshotContent cannot be found for deletion. | ||
// Manual deleting VolumeSnapshotContent can cause this. | ||
// It's tricky for Velero to handle this inconsistency. | ||
// Even if Velero restores the VolumeSnapshotContent, CSI snapshot controller | ||
// may not delete it correctly due to the snapshot represented by VolumeSnapshotContent | ||
// already deleted on cloud provider. | ||
if apierrors.IsNotFound(err) { | ||
p.Log.Warnf("VolumeSnapshotContent %s of backup %s cannot be found. May leave orphan snapshot %s on cloud provider.", | ||
snapCont.Name, input.Backup.Name, *snapCont.Status.SnapshotHandle) | ||
return nil | ||
} | ||
return errors.Wrapf(err, fmt.Sprintf("failed to set DeletionPolicy on volumesnapshotcontent %s. Skipping deletion", snapCont.Name)) | ||
} | ||
|
||
err = snapClient.SnapshotV1().VolumeSnapshotContents().Delete(context.TODO(), snapCont.Name, metav1.DeleteOptions{}) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
p.Log.Infof("VolumeSnapshotContent %s not found", snapCont.Name) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewCsiVolumeSnapshotContentDeleteItemAction(logger logrus.FieldLogger) (interface{}, error) { | ||
return &csiVolumeSnapshotContentDeleteItemAction{logger}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.