Skip to content

Commit

Permalink
use old namespace in resource modifier (#6724)
Browse files Browse the repository at this point in the history
* use old namespace in resource modifier

Signed-off-by: lou <alex1988@outlook.com>

* add changelog

Signed-off-by: lou <alex1988@outlook.com>

* update docs

Signed-off-by: lou <alex1988@outlook.com>

* updated after review

Signed-off-by: lou <alex1988@outlook.com>

---------

Signed-off-by: lou <alex1988@outlook.com>
  • Loading branch information
27149chen authored Sep 8, 2023
1 parent a4b5b0a commit 246831d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6724-27149chen
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use old(origin) namespace in resource modifier conditions in case namespace may change during restore
1 change: 1 addition & 0 deletions design/Implemented/json-substitution-action-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ resourceModifierRules:
path: "/metadata/labels/test"
```
- The above configmap will apply the JSON Patch to all the PVCs in the namespaces bar and foo with name starting with mysql. The JSON Patch will replace the storageClassName with "premium" and remove the label "test" from the PVCs.
- Note that the Namespace here is the original namespace of the backed up resource, not the new namespace where the resource is going to be restored.
- The user can specify multiple JSON Patches for a particular resource. The patches will be applied in the order specified in the configmap. A subsequent patch is applied in order and if multiple patches are specified for the same path, the last patch will override the previous patches.
- The user can specify multiple resourceModifierRules in the configmap. The rules will be applied in the order specified in the configmap.
Expand Down
16 changes: 8 additions & 8 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,14 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
}
}

if ctx.resourceModifiers != nil {
if errList := ctx.resourceModifiers.ApplyResourceModifierRules(obj, groupResource.String(), ctx.log); errList != nil {
for _, err := range errList {
errs.Add(namespace, err)
}
}
}

// Necessary because we may have remapped the namespace if the namespace is
// blank, don't create the key.
originalNamespace := obj.GetNamespace()
Expand All @@ -1364,14 +1372,6 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
// and which backup they came from.
addRestoreLabels(obj, ctx.restore.Name, ctx.restore.Spec.BackupName)

if ctx.resourceModifiers != nil {
if errList := ctx.resourceModifiers.ApplyResourceModifierRules(obj, groupResource.String(), ctx.log); errList != nil {
for _, err := range errList {
errs.Add(namespace, err)
}
}
}

// The object apiVersion might get modified by a RestorePlugin so we need to
// get a new client to reflect updated resource path.
newGR := schema.GroupResource{Group: obj.GroupVersionKind().Group, Resource: groupResource.Resource}
Expand Down
1 change: 1 addition & 0 deletions site/content/docs/main/restore-resource-modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ resourceModifierRules:
```
- The above configmap will apply the JSON Patch to all the PVCs in the namespaces bar and foo with name starting with mysql and match label `foo: bar`. The JSON Patch will replace the storageClassName with "premium" and remove the label "test" from the PVCs.
- Note that the Namespace here is the original namespace of the backed up resource, not the new namespace where the resource is going to be restored.
- You can specify multiple JSON Patches for a particular resource. The patches will be applied in the order specified in the configmap. A subsequent patch is applied in order and if multiple patches are specified for the same path, the last patch will override the previous patches.
- You can specify multiple resourceModifierRules in the configmap. The rules will be applied in the order specified in the configmap.

Expand Down
1 change: 1 addition & 0 deletions site/content/docs/v1.12/restore-resource-modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ resourceModifierRules:
```
- The above configmap will apply the JSON Patch to all the PVCs in the namespaces bar and foo with name starting with mysql and match label `foo: bar`. The JSON Patch will replace the storageClassName with "premium" and remove the label "test" from the PVCs.
- Note that the Namespace here is the original namespace of the backed up resource, not the new namespace where the resource is going to be restored.
- You can specify multiple JSON Patches for a particular resource. The patches will be applied in the order specified in the configmap. A subsequent patch is applied in order and if multiple patches are specified for the same path, the last patch will override the previous patches.
- You can specify multiple resourceModifierRules in the configmap. The rules will be applied in the order specified in the configmap.

Expand Down

0 comments on commit 246831d

Please sign in to comment.