Skip to content

Commit

Permalink
Treat namespace as a regular restorable item
Browse files Browse the repository at this point in the history
Fixes vmware-tanzu#1970

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
  • Loading branch information
reasonerjt committed Nov 24, 2023
1 parent b8a5859 commit 4dc4f80
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions pkg/restore/restore.go
Expand Up @@ -686,6 +686,9 @@ func (ctx *restoreContext) processSelectedResource(

for namespace, selectedItems := range selectedResource.selectedItemsByNamespace {
for _, selectedItem := range selectedItems {
if groupResource == kuberesource.Namespaces {
namespace = selectedItem.name
}
// 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
// (in order to get any backed-up metadata), but if we don't find it there,
Expand Down Expand Up @@ -722,6 +725,10 @@ func (ctx *restoreContext) processSelectedResource(
// have to try to create them multiple times.
existingNamespaces.Insert(selectedItem.targetNamespace)
}
// For namespaces resources we don't need to following steps
if groupResource == kuberesource.Namespaces {
continue
}

obj, err := archive.Unmarshal(ctx.fileSystem, selectedItem.path)
if err != nil {
Expand Down Expand Up @@ -2248,12 +2255,6 @@ func (ctx *restoreContext) getOrderedResourceCollection(
continue
}

// We don't want to explicitly restore namespace API objs because we'll handle
// them as a special case prior to restoring anything into them
if groupResource == kuberesource.Namespaces {
continue
}

// Check if the resource is present in the backup
resourceList := backupResources[groupResource.String()]
if resourceList == nil {
Expand All @@ -2269,24 +2270,17 @@ func (ctx *restoreContext) getOrderedResourceCollection(
continue
}

// get target namespace to restore into, if different
// from source namespace
targetNamespace := namespace
if target, ok := ctx.restore.Spec.NamespaceMapping[namespace]; ok {
targetNamespace = target
}

if targetNamespace == "" && boolptr.IsSetToFalse(ctx.restore.Spec.IncludeClusterResources) {
if namespace == "" && boolptr.IsSetToFalse(ctx.restore.Spec.IncludeClusterResources) {
ctx.log.Infof("Skipping resource %s because it's cluster-scoped", resource)
continue
}

if targetNamespace == "" && !boolptr.IsSetToTrue(ctx.restore.Spec.IncludeClusterResources) && !ctx.namespaceIncludesExcludes.IncludeEverything() {
if namespace == "" && !boolptr.IsSetToTrue(ctx.restore.Spec.IncludeClusterResources) && !ctx.namespaceIncludesExcludes.IncludeEverything() {
ctx.log.Infof("Skipping resource %s because it's cluster-scoped and only specific namespaces are included in the restore", resource)
continue
}

res, w, e := ctx.getSelectedRestoreableItems(groupResource.String(), targetNamespace, namespace, items)
res, w, e := ctx.getSelectedRestoreableItems(groupResource.String(), ctx.restore.Spec.NamespaceMapping, namespace, items)
warnings.Merge(&w)
errs.Merge(&e)

Expand All @@ -2302,7 +2296,7 @@ func (ctx *restoreContext) getOrderedResourceCollection(
// getSelectedRestoreableItems applies Kubernetes selectors on individual items
// of each resource type to create a list of items which will be actually
// restored.
func (ctx *restoreContext) getSelectedRestoreableItems(resource, targetNamespace, originalNamespace string, items []string) (restoreableResource, results.Result, results.Result) {
func (ctx *restoreContext) getSelectedRestoreableItems(resource string, namespaceMapping map[string]string, originalNamespace string, items []string) (restoreableResource, results.Result, results.Result) {
warnings, errs := results.Result{}, results.Result{}

restorable := restoreableResource{
Expand All @@ -2313,6 +2307,11 @@ func (ctx *restoreContext) getSelectedRestoreableItems(resource, targetNamespace
restorable.selectedItemsByNamespace = make(map[string][]restoreableItem)
}

targetNamespace := originalNamespace
if target, ok := namespaceMapping[originalNamespace]; ok {
targetNamespace = target
}

if targetNamespace != "" {
ctx.log.Infof("Resource '%s' will be restored into namespace '%s'", resource, targetNamespace)
} else {
Expand Down Expand Up @@ -2374,6 +2373,15 @@ func (ctx *restoreContext) getSelectedRestoreableItems(resource, targetNamespace
continue
}

if resource == kuberesource.Namespaces.String() {
// handle remapping for namespace resource
if target, ok := namespaceMapping[item]; ok {
targetNamespace = target
} else {
targetNamespace = item
}
}

selectedItem := restoreableItem{
path: itemPath,
name: item,
Expand Down

0 comments on commit 4dc4f80

Please sign in to comment.