Skip to content

Commit

Permalink
fix nilpointer panic when decode addlitional item failed
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbeeSo committed Dec 30, 2024
1 parent 78c97d9 commit 3060b6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,7 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
additionalObj, err := archive.Unmarshal(ctx.fileSystem, itemPath)
if err != nil {
errs.Add(namespace, errors.Wrapf(err, "error restoring additional item %s", additionalResourceID))
continue
}

additionalItemNamespace := additionalItem.Namespace
Expand Down
38 changes: 36 additions & 2 deletions pkg/restore/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ func TestInvalidTarballContents(t *testing.T) {
backup *velerov1api.Backup
apiResources []*test.APIResource
tarball io.Reader
actions []riav2.RestoreItemAction
want map[*test.APIResource][]string
wantErrs Result
wantWarnings Result
Expand Down Expand Up @@ -991,6 +992,39 @@ func TestInvalidTarballContents(t *testing.T) {
},
},
},
{
name: "invalid JSON for additional item is reported as an error and restore continues",
restore: defaultRestore().IncludedNamespaces("ns-1").Result(),
backup: defaultBackup().Result(),
tarball: test.NewTarWriter(t).
AddItems("pods", builder.ForPod("ns-1", "pod-1").Result()).
Add("resources/persistentvolumes/cluster/pv-1.json", []byte("invalid JSON")).
Done(),
apiResources: []*test.APIResource{
test.Pods(),
test.PVs(),
},
actions: []riav2.RestoreItemAction{
&pluggableAction{
executeFunc: func(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) {
return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: input.Item,
AdditionalItems: []velero.ResourceIdentifier{
{GroupResource: kuberesource.PersistentVolumes, Name: "pv-1"},
},
}, nil
},
},
},
want: map[*test.APIResource][]string{
test.Pods(): {"ns-1/pod-1"},
},
wantErrs: Result{
Namespaces: map[string][]string{
"ns-1": {"error restoring additional item persistentvolumes/pv-1"},
},
},
},
}

for _, tc := range tests {
Expand All @@ -1012,8 +1046,8 @@ func TestInvalidTarballContents(t *testing.T) {
}
warnings, errs := h.restorer.Restore(
data,
nil, // restoreItemActions
nil, // volume snapshotter getter
tc.actions, // restoreItemActions
nil, // volume snapshotter getter
)
assertWantErrsOrWarnings(t, tc.wantWarnings, warnings)
assertWantErrsOrWarnings(t, tc.wantErrs, errs)
Expand Down

0 comments on commit 3060b6d

Please sign in to comment.