-
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 2782df0
Showing
71 changed files
with
6,583 additions
and
186 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,120 @@ | ||
/* | ||
Copyright the Velero contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package csi | ||
|
||
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" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
crclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/client" | ||
plugincommon "github.com/vmware-tanzu/velero/pkg/plugin/framework/common" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
"github.com/vmware-tanzu/velero/pkg/util/csi" | ||
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube" | ||
) | ||
|
||
// volumeSnapshotDeleteItemAction is a backup item action plugin for Velero. | ||
type volumeSnapshotDeleteItemAction struct { | ||
log logrus.FieldLogger | ||
crClient crclient.Client | ||
} | ||
|
||
// AppliesTo returns information indicating that the | ||
// VolumeSnapshotBackupItemAction should be invoked to backup | ||
// VolumeSnapshots. | ||
func (p *volumeSnapshotDeleteItemAction) AppliesTo() (velero.ResourceSelector, error) { | ||
p.log.Debug("VolumeSnapshotBackupItemAction AppliesTo") | ||
|
||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"volumesnapshots.snapshot.storage.k8s.io"}, | ||
}, nil | ||
} | ||
|
||
func (p *volumeSnapshotDeleteItemAction) 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 !kubeutil.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) | ||
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, | ||
p.crClient, | ||
) | ||
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 := p.crClient.Delete(context.TODO(), &vs) | ||
if err != nil && !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewVolumeSnapshotDeleteItemAction(f client.Factory) plugincommon.HandlerInitializer { | ||
return func(logger logrus.FieldLogger) (interface{}, error) { | ||
crClient, err := f.KubebuilderClient() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
return &volumeSnapshotDeleteItemAction{ | ||
log: logger, | ||
crClient: crClient, | ||
}, nil | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
/* | ||
Copyright the Velero contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package csi | ||
|
||
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" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
crclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/client" | ||
plugincommon "github.com/vmware-tanzu/velero/pkg/plugin/framework/common" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
"github.com/vmware-tanzu/velero/pkg/util/csi" | ||
kubeutil "github.com/vmware-tanzu/velero/pkg/util/kube" | ||
) | ||
|
||
// volumeSnapshotContentDeleteItemAction is a restore item action plugin for Velero | ||
type volumeSnapshotContentDeleteItemAction struct { | ||
log logrus.FieldLogger | ||
crClient crclient.Client | ||
} | ||
|
||
// AppliesTo returns information indicating | ||
// VolumeSnapshotContentRestoreItemAction action should be invoked | ||
// while restoring VolumeSnapshotContent.snapshot.storage.k8s.io resources | ||
func (p *volumeSnapshotContentDeleteItemAction) AppliesTo() (velero.ResourceSelector, error) { | ||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"volumesnapshotcontent.snapshot.storage.k8s.io"}, | ||
}, nil | ||
} | ||
|
||
func (p *volumeSnapshotContentDeleteItemAction) 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 VolumeSnapshotContent from unstructured") | ||
} | ||
|
||
// We don't want this DeleteItemAction plugin to delete | ||
// VolumeSnapshotContent taken outside of Velero. | ||
// So skip deleting VolumeSnapshotContent not have the backup name | ||
// in its labels. | ||
if !kubeutil.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) | ||
|
||
if err := csi.SetVolumeSnapshotContentDeletionPolicy( | ||
snapCont.Name, | ||
p.crClient, | ||
); 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)) | ||
} | ||
|
||
if err := p.crClient.Delete( | ||
context.TODO(), | ||
&snapCont, | ||
); err != nil && !apierrors.IsNotFound(err) { | ||
p.log.Infof("VolumeSnapshotContent %s not found", snapCont.Name) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewVolumeSnapshotContentDeleteItemAction( | ||
f client.Factory, | ||
) plugincommon.HandlerInitializer { | ||
return func(logger logrus.FieldLogger) (interface{}, error) { | ||
crClient, err := f.KubebuilderClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &volumeSnapshotContentDeleteItemAction{ | ||
log: logger, | ||
crClient: crClient, | ||
}, 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.