Skip to content

Commit 72c9f51

Browse files
committed
Adds support for discarding an item before restoring it
Within a RestoreItemAction, if the Execute function sets abandonItem to true on the UpdatedItem before running it, then restoreResource skips any remaining actions on this item, skips restoring this item to the cluster, and continues on to the next item. In the plugin Execute function, the following line would trigger skipping, assuming input.Item is the object passed into NewRestoreItemActionExecuteOutput on return: unstructured.SetNestedField(input.Item.UnstructuredContent(), true, "abandonItem")
1 parent 615461e commit 72c9f51

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pkg/restore/restore.go

+24
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a
700700
applicableActions = append(applicableActions, action)
701701
}
702702

703+
fileLoop:
703704
for _, file := range files {
704705
fullPath := filepath.Join(resourcePath, file.Name())
705706
obj, err := ctx.unmarshal(fullPath)
@@ -847,6 +848,15 @@ func (ctx *context) restoreResource(resource, namespace, resourcePath string) (a
847848
continue
848849
}
849850

851+
abandon, err := abandonItem(executeOutput.UpdatedItem.(*unstructured.Unstructured))
852+
if err != nil {
853+
addToResult(&errs, namespace, fmt.Errorf("error preparing %s: %v", fullPath, err))
854+
continue
855+
}
856+
if abandon {
857+
ctx.log.Infof("Skipping restore of %s: %v because a registered plugin discarded it", obj.GroupVersionKind().Kind, name)
858+
continue fileLoop
859+
}
850860
unstructuredObj, ok := executeOutput.UpdatedItem.(*unstructured.Unstructured)
851861
if !ok {
852862
addToResult(&errs, namespace, fmt.Errorf("%s: unexpected type %T", fullPath, executeOutput.UpdatedItem))
@@ -1046,6 +1056,20 @@ func isCompleted(obj *unstructured.Unstructured, groupResource schema.GroupResou
10461056
return false, nil
10471057
}
10481058

1059+
// abandonItem returns whether or not a RestoreItemAction has decided to skip restore
1060+
// Used to identify whether or not an object should be restored.
1061+
func abandonItem(obj *unstructured.Unstructured) (bool, error) {
1062+
abandon, found, err := unstructured.NestedBool(obj.UnstructuredContent(), "abandonItem")
1063+
if err != nil {
1064+
return false, errors.WithStack(err)
1065+
}
1066+
if found {
1067+
return abandon, nil
1068+
} else {
1069+
return false, nil
1070+
}
1071+
}
1072+
10491073
// unmarshal reads the specified file, unmarshals the JSON contained within it
10501074
// and returns an Unstructured object.
10511075
func (ctx *context) unmarshal(filePath string) (*unstructured.Unstructured, error) {

0 commit comments

Comments
 (0)