-
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.
Merge pull request #6547 from reasonerjt/del-moved-snapshot
Delete moved snapshots when the backup is deleted
- Loading branch information
Showing
10 changed files
with
257 additions
and
11 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 @@ | ||
Delete moved snapshots when the backup is deleted |
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
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
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
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
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
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,86 @@ | ||
package datamover | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
corev1api "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
velerov2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
"github.com/vmware-tanzu/velero/pkg/repository" | ||
) | ||
|
||
type DataUploadDeleteAction struct { | ||
logger logrus.FieldLogger | ||
client client.Client | ||
} | ||
|
||
func (d *DataUploadDeleteAction) AppliesTo() (velero.ResourceSelector, error) { | ||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"datauploads.velero.io"}, | ||
}, nil | ||
} | ||
|
||
func (d *DataUploadDeleteAction) Execute(input *velero.DeleteItemActionExecuteInput) error { | ||
d.logger.Infof("Executing DataUploadDeleteAction") | ||
du := &velerov2alpha1.DataUpload{} | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), &du); err != nil { | ||
return errors.WithStack(errors.Wrapf(err, "failed to convert input.Item from unstructured")) | ||
} | ||
cm := genConfigmap(input.Backup, *du) | ||
if cm == nil { | ||
// will not fail the backup deletion | ||
return nil | ||
} | ||
err := d.client.Create(context.Background(), cm) | ||
if err != nil { | ||
return errors.WithStack(errors.Wrapf(err, "failed to create the configmap for DataUpload %s/%s", du.Namespace, du.Name)) | ||
} | ||
return nil | ||
} | ||
|
||
// generate the configmap which is to be created and used as a way to communicate the snapshot info to the backup deletion controller | ||
func genConfigmap(bak *velerov1.Backup, du velerov2alpha1.DataUpload) *corev1api.ConfigMap { | ||
if !IsBuiltInUploader(du.Spec.DataMover) || du.Status.SnapshotID == "" { | ||
return nil | ||
} | ||
snapshot := repository.SnapshotIdentifier{ | ||
VolumeNamespace: du.Spec.SourceNamespace, | ||
BackupStorageLocation: bak.Spec.StorageLocation, | ||
SnapshotID: du.Status.SnapshotID, | ||
RepositoryType: GetUploaderType(du.Spec.DataMover), | ||
} | ||
b, _ := json.Marshal(snapshot) | ||
data := make(map[string]string) | ||
_ = json.Unmarshal(b, &data) | ||
return &corev1api.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: corev1api.SchemeGroupVersion.String(), | ||
Kind: "ConfigMap", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: bak.Namespace, | ||
Name: fmt.Sprintf("%s-info", du.Name), | ||
Labels: map[string]string{ | ||
velerov1.BackupNameLabel: bak.Name, | ||
velerov1.DataUploadSnapshotInfoLabel: "true", | ||
}, | ||
}, | ||
Data: data, | ||
} | ||
} | ||
|
||
func NewDataUploadDeleteAction(logger logrus.FieldLogger, client client.Client) *DataUploadDeleteAction { | ||
return &DataUploadDeleteAction{ | ||
logger: logger, | ||
client: client, | ||
} | ||
} |
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
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,70 @@ | ||
package datamover | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsBuiltInUploader(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
dataMover string | ||
want bool | ||
}{ | ||
{ | ||
name: "empty dataMover is builtin", | ||
dataMover: "", | ||
want: true, | ||
}, | ||
{ | ||
name: "velero dataMover is builtin", | ||
dataMover: "velero", | ||
want: true, | ||
}, | ||
{ | ||
name: "kopia dataMover is not builtin", | ||
dataMover: "kopia", | ||
want: false, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(tt *testing.T) { | ||
assert.Equal(tt, tc.want, IsBuiltInUploader(tc.dataMover)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetUploaderType(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
input string | ||
want string | ||
}{ | ||
{ | ||
name: "empty dataMover is kopia", | ||
input: "", | ||
want: "kopia", | ||
}, | ||
{ | ||
name: "velero dataMover is kopia", | ||
input: "velero", | ||
want: "kopia", | ||
}, | ||
{ | ||
name: "kopia dataMover is kopia", | ||
input: "kopia", | ||
want: "kopia", | ||
}, | ||
{ | ||
name: "restic dataMover is restic", | ||
input: "restic", | ||
want: "restic", | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(tt *testing.T) { | ||
assert.Equal(tt, tc.want, GetUploaderType(tc.input)) | ||
}) | ||
} | ||
} |
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