Treat namespace as a regular restorable item (#7143)

Fixes #1970

Namespaces will be handled as cluster-scope resource, but for
consistency they will still created via "Ensure namespace" flow for
consistency.

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
Daniel Jiang
2023-11-29 00:20:36 +08:00
committed by GitHub
parent 85482aefaf
commit b8604b6a89
2 changed files with 26 additions and 17 deletions

View File

@@ -0,0 +1 @@
Treat namespace as a regular restorable item

View File

@@ -713,6 +713,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,
@@ -749,6 +752,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 {
@@ -2267,12 +2274,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 {
@@ -2288,24 +2289,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)
@@ -2321,7 +2315,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{
@@ -2332,6 +2326,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 {
@@ -2393,6 +2392,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,