Skip to content

Commit

Permalink
Fix image inspect error on dnf
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Vazquez <macedonv@amazon.com>
  • Loading branch information
austinvazquez committed Dec 27, 2024
1 parent 6f34536 commit fe0f613
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
10 changes: 9 additions & 1 deletion cmd/nerdctl/image/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package image

import (
"fmt"

"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
Expand Down Expand Up @@ -95,7 +97,13 @@ func imageInspectAction(cmd *cobra.Command, args []string) error {
}
defer cancel()

return image.Inspect(ctx, client, args, options)
err = image.Inspect(ctx, client, args, options)
if uw, ok := err.(interface{ Unwrap() []error }); ok {
errs := uw.Unwrap()
err = fmt.Errorf("%d errors: %v", len(errs), errs)
}

return err
}

func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
13 changes: 11 additions & 2 deletions cmd/nerdctl/image/image_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package image

import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -61,6 +63,11 @@ func TestImageInspectSimpleCases(t *testing.T) {
Command: test.Command("image", "inspect", testutil.CommonImage, "--format", "{{.ID}}"),
Expected: test.Expects(0, nil, nil),
},
{
Description: "Error for image not found",
Command: test.Command("image", "inspect", "dne:latest", "dne2:latest"),
Expected: test.Expects(1, []error{errors.New("2 errors: [no such image 'dne:latest' no such image 'dne2:latest']")}, nil),
},
},
}

Expand Down Expand Up @@ -171,7 +178,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) {
for _, id := range []string{"doesnotexist", "doesnotexist:either", "busybox:bogustag"} {
cmd := helpers.Command("image", "inspect", id+"@sha256:"+sha)
cmd.Run(&test.Expected{
Output: test.Equals(""),
ExitCode: 1,
Errors: []error{fmt.Errorf("1 errors: [no such image '%s@sha256:%s']", id, sha)},
})
}
},
Expand All @@ -192,7 +200,8 @@ func TestImageInspectDifferentValidReferencesForTheSameImage(t *testing.T) {
for _, id := range []string{"∞∞∞∞∞∞∞∞∞∞", "busybox:∞∞∞∞∞∞∞∞∞∞"} {
cmd := helpers.Command("image", "inspect", id)
cmd.Run(&test.Expected{
Output: test.Equals(""),
ExitCode: 1,
Errors: []error{fmt.Errorf("1 errors: [invalid reference format '%s']", id)},
})
}
},
Expand Down
6 changes: 4 additions & 2 deletions cmd/nerdctl/ipfs/ipfs_simple_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,13 @@ func TestIPFSSimple(t *testing.T) {
helpers.Ensure("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, data.Get(mainImageCIDKey), data.Identifier("encrypted"))
cmd := helpers.Command("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", data.Identifier("encrypted"))
cmd.Run(&test.Expected{
Output: test.Equals("1\n"),
ExitCode: 1,
Output: test.Equals("1\n"),
})
cmd = helpers.Command("image", "inspect", "--mode=native", "--format={{json (index .Manifest.Layers 0) }}", data.Identifier("encrypted"))
cmd.Run(&test.Expected{
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
ExitCode: 1,
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
})

// Push the encrypted image and save the CID
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package image

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -97,14 +98,15 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
defer cancel()

// Will hold the final answers
var errs []error
var entries []interface{}

snapshotter := containerdutil.SnapshotService(client, options.GOptions.Snapshotter)
// We have to query per provided identifier, as we need to post-process results for the case name + digest
for _, identifier := range identifiers {
candidateImageList, requestedName, requestedTag, err := inspectIdentifier(ctx, client, identifier)
if err != nil {
log.G(ctx).WithError(err).WithField("identifier", identifier).Error("failure calling inspect")
errs = append(errs, fmt.Errorf("invalid reference format '%s'", identifier))
continue
}

Expand Down Expand Up @@ -185,6 +187,8 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
// Store our image
// foundImages[validatedDigest] = validatedImage
entries = append(entries, validatedImage)
} else {
errs = append(errs, fmt.Errorf("no such image '%s'", identifier))
}
}

Expand All @@ -195,5 +199,5 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
}
}

return nil
return errors.Join(errs...)
}

0 comments on commit fe0f613

Please sign in to comment.