Do not set "targetNamespace" to namepsace items

fixes #7263
This commit makes the data structures more consistent, that namespaces,
as cluster scoped resource will not have "targetNamespace" in the
"restoreableItem" instance.

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
pull/7274/head
Daniel Jiang 2024-01-04 18:52:43 +08:00
parent e84a51deec
commit a5d08ac5f0
2 changed files with 15 additions and 16 deletions

View File

@ -0,0 +1 @@
Do not set "targetNamespace" to namespace items

View File

@ -716,20 +716,27 @@ func (ctx *restoreContext) processSelectedResource(
for namespace, selectedItems := range selectedResource.selectedItemsByNamespace { for namespace, selectedItems := range selectedResource.selectedItemsByNamespace {
for _, selectedItem := range selectedItems { for _, selectedItem := range selectedItems {
targetNS := selectedItem.targetNamespace
if groupResource == kuberesource.Namespaces { if groupResource == kuberesource.Namespaces {
// namespace is a cluster-scoped resource and doesn't have "targetNamespace" attribute in the restoreableItem instance
namespace = selectedItem.name namespace = selectedItem.name
if n, ok := ctx.restore.Spec.NamespaceMapping[namespace]; ok {
targetNS = n
} else {
targetNS = namespace
}
} }
// If we don't know whether this namespace exists yet, attempt to create // If we don't know whether this namespace exists yet, attempt to create
// it in order to ensure it exists. Try to get it from the backup tarball // it in order to ensure it exists. Try to get it from the backup tarball
// (in order to get any backed-up metadata), but if we don't find it there, // (in order to get any backed-up metadata), but if we don't find it there,
// create a blank one. // create a blank one.
if namespace != "" && !existingNamespaces.Has(selectedItem.targetNamespace) { if namespace != "" && !existingNamespaces.Has(targetNS) {
logger := ctx.log.WithField("namespace", namespace) logger := ctx.log.WithField("namespace", namespace)
ns := getNamespace( ns := getNamespace(
logger, logger,
archive.GetItemFilePath(ctx.restoreDir, "namespaces", "", namespace), archive.GetItemFilePath(ctx.restoreDir, "namespaces", "", namespace),
selectedItem.targetNamespace, targetNS,
) )
_, nsCreated, err := kube.EnsureNamespaceExistsAndIsReady( _, nsCreated, err := kube.EnsureNamespaceExistsAndIsReady(
ns, ns,
@ -753,7 +760,7 @@ func (ctx *restoreContext) processSelectedResource(
// Keep track of namespaces that we know exist so we don't // Keep track of namespaces that we know exist so we don't
// have to try to create them multiple times. // have to try to create them multiple times.
existingNamespaces.Insert(selectedItem.targetNamespace) existingNamespaces.Insert(targetNS)
} }
// For namespaces resources we don't need to following steps // For namespaces resources we don't need to following steps
if groupResource == kuberesource.Namespaces { if groupResource == kuberesource.Namespaces {
@ -773,7 +780,7 @@ func (ctx *restoreContext) processSelectedResource(
continue continue
} }
w, e, _ := ctx.restoreItem(obj, groupResource, selectedItem.targetNamespace) w, e, _ := ctx.restoreItem(obj, groupResource, targetNS)
warnings.Merge(&w) warnings.Merge(&w)
errs.Merge(&e) errs.Merge(&e)
processedItems++ processedItems++
@ -2239,7 +2246,7 @@ func (ctx *restoreContext) getOrderedResourceCollection(
continue continue
} }
res, w, e := ctx.getSelectedRestoreableItems(groupResource.String(), ctx.restore.Spec.NamespaceMapping, namespace, items) res, w, e := ctx.getSelectedRestoreableItems(groupResource.String(), namespace, items)
warnings.Merge(&w) warnings.Merge(&w)
errs.Merge(&e) errs.Merge(&e)
@ -2255,7 +2262,7 @@ func (ctx *restoreContext) getOrderedResourceCollection(
// getSelectedRestoreableItems applies Kubernetes selectors on individual items // getSelectedRestoreableItems applies Kubernetes selectors on individual items
// of each resource type to create a list of items which will be actually // of each resource type to create a list of items which will be actually
// restored. // restored.
func (ctx *restoreContext) getSelectedRestoreableItems(resource string, namespaceMapping map[string]string, originalNamespace string, items []string) (restoreableResource, results.Result, results.Result) { //nolint:unparam // Ignore the warnings is always nil warning. func (ctx *restoreContext) getSelectedRestoreableItems(resource string, originalNamespace string, items []string) (restoreableResource, results.Result, results.Result) { //nolint:unparam // Ignore the warnings is always nil warning.
warnings, errs := results.Result{}, results.Result{} warnings, errs := results.Result{}, results.Result{}
restorable := restoreableResource{ restorable := restoreableResource{
@ -2267,7 +2274,7 @@ func (ctx *restoreContext) getSelectedRestoreableItems(resource string, namespac
} }
targetNamespace := originalNamespace targetNamespace := originalNamespace
if target, ok := namespaceMapping[originalNamespace]; ok { if target, ok := ctx.restore.Spec.NamespaceMapping[originalNamespace]; ok {
targetNamespace = target targetNamespace = target
} }
@ -2333,15 +2340,6 @@ func (ctx *restoreContext) getSelectedRestoreableItems(resource string, namespac
continue continue
} }
if resource == kuberesource.Namespaces.String() {
// handle remapping for namespace resource
if target, ok := namespaceMapping[item]; ok {
targetNamespace = target
} else {
targetNamespace = item
}
}
selectedItem := restoreableItem{ selectedItem := restoreableItem{
path: itemPath, path: itemPath,
name: item, name: item,