forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve DataUpload into backup result ConfigMap during volume snapsh…
…ot restore. Fix issue vmware-tanzu#6117. Add CSI plugin needs builder functions. Signed-off-by: Xun Jiang <jxun@vmware.com>
- Loading branch information
Xun Jiang
committed
Jun 21, 2023
1 parent
6f3adcf
commit aa50363
Showing
9 changed files
with
329 additions
and
0 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 @@ | ||
Retrieve DataUpload into backup result ConfigMap during volume snapshot restore. |
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,64 @@ | ||
/* | ||
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 builder | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1" | ||
) | ||
|
||
// DataDownloadBuilder builds DataDownload objects. | ||
type DataDownloadBuilder struct { | ||
object *velerov2alpha1api.DataDownload | ||
} | ||
|
||
// ForDataDownload is the constructor of DataDownloadBuilder | ||
func ForDataDownload(namespace, name string) *DataDownloadBuilder { | ||
return &DataDownloadBuilder{ | ||
object: &velerov2alpha1api.DataDownload{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "DataDownload", | ||
APIVersion: velerov2alpha1api.SchemeGroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Result returns the built DataDownload | ||
func (b *DataDownloadBuilder) Result() *velerov2alpha1api.DataDownload { | ||
return b.object | ||
} | ||
|
||
// TargetVolume sets DataDownload's spec.targetVolume | ||
func (b *DataDownloadBuilder) TargetVolume(targetVolume velerov2alpha1api.TargetVolumeSpec) *DataDownloadBuilder { | ||
b.object.Spec.TargetVolume = targetVolume | ||
return b | ||
} | ||
|
||
// ObjectMeta applies functional options to the DataDownload's ObjectMeta. | ||
func (b *DataDownloadBuilder) ObjectMeta(opts ...ObjectMetaOpt) *DataDownloadBuilder { | ||
for _, opt := range opts { | ||
opt(b.object) | ||
} | ||
|
||
return b | ||
} |
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,104 @@ | ||
/* | ||
Copyright 2020 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 restore | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"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" | ||
corev1client "k8s.io/client-go/kubernetes/typed/core/v1" | ||
|
||
velerov1api "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/label" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
) | ||
|
||
type DataUploadRetrieveAction struct { | ||
logger logrus.FieldLogger | ||
configMapClient corev1client.ConfigMapInterface | ||
} | ||
|
||
func NewDataUploadRetrieveAction(logger logrus.FieldLogger, configMapClient corev1client.ConfigMapInterface) *DataUploadRetrieveAction { | ||
return &DataUploadRetrieveAction{ | ||
logger: logger, | ||
configMapClient: configMapClient, | ||
} | ||
} | ||
|
||
func (d *DataUploadRetrieveAction) AppliesTo() (velero.ResourceSelector, error) { | ||
return velero.ResourceSelector{ | ||
IncludedResources: []string{"datauploads.velero.io"}, | ||
}, nil | ||
} | ||
|
||
func (d *DataUploadRetrieveAction) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) { | ||
d.logger.Info("Executing DataUploadRetrieveAction") | ||
|
||
dataUpload := velerov2alpha1.DataUpload{} | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.ItemFromBackup.UnstructuredContent(), &dataUpload); err != nil { | ||
d.logger.Errorf("unable to convert unstructured item to DataUpload: %s", err.Error()) | ||
return nil, errors.Wrap(err, "unable to convert unstructured item to DataUpload.") | ||
} | ||
|
||
dataUploadResult := velerov2alpha1.DataUploadResult{ | ||
BackupStorageLocation: dataUpload.Spec.BackupStorageLocation, | ||
DataMover: dataUpload.Spec.DataMover, | ||
SnapshotID: dataUpload.Status.SnapshotID, | ||
SourceNamespace: dataUpload.Spec.SourceNamespace, | ||
DataMoverResult: dataUpload.Status.DataMoverResult, | ||
} | ||
|
||
jsonBytes, err := json.Marshal(dataUploadResult) | ||
if err != nil { | ||
d.logger.Errorf("fail to convert DataUploadResult to JSON: %s", err.Error()) | ||
return nil, errors.Wrap(err, "fail to convert DataUploadResult to JSON") | ||
} | ||
|
||
cm := corev1api.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "ConfigMap", | ||
APIVersion: corev1api.SchemeGroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: dataUpload.Name + "-", | ||
Namespace: dataUpload.Namespace, | ||
Labels: map[string]string{ | ||
velerov1api.RestoreUIDLabel: label.GetValidName(string(input.Restore.UID)), | ||
velerov1api.PVCNamespaceNameLabel: dataUpload.Spec.SourceNamespace + "." + dataUpload.Spec.SourcePVC, | ||
}, | ||
}, | ||
Data: map[string]string{ | ||
string(input.Restore.UID): string(jsonBytes), | ||
}, | ||
} | ||
|
||
_, err = d.configMapClient.Create(context.Background(), &cm, metav1.CreateOptions{}) | ||
if err != nil { | ||
d.logger.Errorf("fail to create DataUploadResult ConfigMap %s/%s: %s", cm.Namespace, cm.Name, err.Error()) | ||
return nil, errors.Wrap(err, "fail to create DataUploadResult ConfigMap") | ||
} | ||
|
||
return &velero.RestoreItemActionExecuteOutput{ | ||
SkipRestore: true, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2020 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 restore | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
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/builder" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
velerotest "github.com/vmware-tanzu/velero/pkg/test" | ||
) | ||
|
||
func TestDataUploadRetrieveActionExectue(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
dataUpload *velerov2alpha1.DataUpload | ||
restore *velerov1.Restore | ||
expectedDataUploadResult *corev1.ConfigMap | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "DataUploadRetrieve Action test", | ||
dataUpload: builder.ForDataUpload("velero", "testDU").SourceNamespace("testNamespace").SourcePVC("testPVC").Result(), | ||
restore: builder.ForRestore("velero", "testRestore").ObjectMeta(builder.WithUID("testingUID")).Result(), | ||
expectedDataUploadResult: builder.ForConfigMap("velero", "").ObjectMeta(builder.WithGenerateName("testDU-"), builder.WithLabels(velerov1.PVCNamespaceNameLabel, "testNamespace.testPVC", velerov1.RestoreUIDLabel, "testingUID")).Data("testingUID", `{"backupStorageLocation":"","sourceNamespace":"testNamespace"}`).Result(), | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
logger := velerotest.NewLogger() | ||
cmClient := fake.NewSimpleClientset() | ||
|
||
var unstructuredDataUpload map[string]interface{} | ||
if tc.dataUpload != nil { | ||
var err error | ||
unstructuredDataUpload, err = runtime.DefaultUnstructuredConverter.ToUnstructured(tc.dataUpload) | ||
require.NoError(t, err) | ||
} | ||
input := velero.RestoreItemActionExecuteInput{ | ||
Restore: tc.restore, | ||
ItemFromBackup: &unstructured.Unstructured{Object: unstructuredDataUpload}, | ||
} | ||
|
||
action := NewDataUploadRetrieveAction(logger, cmClient.CoreV1().ConfigMaps("velero")) | ||
_, err := action.Execute(&input) | ||
if tc.expectedErr != "" { | ||
require.Equal(t, tc.expectedErr, err.Error()) | ||
} | ||
require.NoError(t, err) | ||
|
||
if tc.expectedDataUploadResult != nil { | ||
cmList, err := cmClient.CoreV1().ConfigMaps("velero").List(context.Background(), metav1.ListOptions{ | ||
LabelSelector: fmt.Sprintf("%s=%s,%s=%s", velerov1.RestoreUIDLabel, "testingUID", velerov1.PVCNamespaceNameLabel, tc.dataUpload.Spec.SourceNamespace+"."+tc.dataUpload.Spec.SourcePVC), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, *tc.expectedDataUploadResult, cmList.Items[0]) | ||
} | ||
}) | ||
} | ||
} |
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