-
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.
Add restored resource list in the output of restore describe command
Add restored resource list in the output of restore describe command Fixes #5199 Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
- Loading branch information
Showing
18 changed files
with
353 additions
and
53 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 @@ | ||
Add restored resource list in the restore describe command |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,79 @@ | ||
/* | ||
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 restore | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
"github.com/vmware-tanzu/velero/pkg/volume" | ||
) | ||
|
||
const ( | ||
itemRestoreResultCreated = "created" | ||
itemRestoreResultUpdated = "updated" | ||
itemRestoreResultFailed = "failed" | ||
itemRestoreResultSkipped = "skipped" | ||
) | ||
|
||
type itemKey struct { | ||
resource string | ||
namespace string | ||
name string | ||
} | ||
|
||
func resourceKey(obj runtime.Object) string { | ||
gvk := obj.GetObjectKind().GroupVersionKind() | ||
return fmt.Sprintf("%s/%s", gvk.GroupVersion().String(), gvk.Kind) | ||
} | ||
|
||
type Request struct { | ||
*velerov1api.Restore | ||
|
||
Log logrus.FieldLogger | ||
Backup *velerov1api.Backup | ||
PodVolumeBackups []*velerov1api.PodVolumeBackup | ||
VolumeSnapshots []*volume.Snapshot | ||
BackupReader io.Reader | ||
RestoredItems map[itemKey]string | ||
} | ||
|
||
// RestoredResourceList returns the list of restored resources grouped by the API | ||
// Version and Kind | ||
func (r *Request) RestoredResourceList() map[string][]string { | ||
resources := map[string][]string{} | ||
for i, action := range r.RestoredItems { | ||
entry := i.name | ||
if i.namespace != "" { | ||
entry = fmt.Sprintf("%s/%s", i.namespace, i.name) | ||
} | ||
entry = fmt.Sprintf("%s(%s)", entry, action) | ||
resources[i.resource] = append(resources[i.resource], entry) | ||
} | ||
|
||
// sort namespace/name entries for each GVK | ||
for _, v := range resources { | ||
sort.Strings(v) | ||
} | ||
|
||
return resources | ||
} |
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,67 @@ | ||
/* | ||
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 restore | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
coreV1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestResourceKey(t *testing.T) { | ||
namespace := &coreV1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Namespace", | ||
}, | ||
} | ||
assert.Equal(t, "v1/Namespace", resourceKey(namespace)) | ||
|
||
cr := &coreV1.Namespace{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "customized/v1", | ||
Kind: "Cron", | ||
}, | ||
} | ||
assert.Equal(t, "customized/v1/Cron", resourceKey(cr)) | ||
} | ||
|
||
func TestRestoredResourceList(t *testing.T) { | ||
request := &Request{ | ||
RestoredItems: map[itemKey]string{ | ||
{ | ||
resource: "v1/Namespace", | ||
namespace: "", | ||
name: "default", | ||
}: "created", | ||
{ | ||
resource: "v1/ConfigMap", | ||
namespace: "default", | ||
name: "cm", | ||
}: "skipped", | ||
}, | ||
} | ||
|
||
expected := map[string][]string{ | ||
"v1/ConfigMap": {"default/cm(skipped)"}, | ||
"v1/Namespace": {"default(created)"}, | ||
} | ||
|
||
assert.EqualValues(t, expected, request.RestoredResourceList()) | ||
} |
Oops, something went wrong.