Skip to content

Commit 67ecd67

Browse files
authored
Merge pull request containerd#3888 from weiyuhang2011/fix-3876
Fix FATA error when inspecting images in native mode
2 parents a1a2ec2 + 5ffcba6 commit 67ecd67

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

cmd/nerdctl/image/image_inspect.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package image
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/spf13/cobra"
2123

2224
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
@@ -27,7 +29,7 @@ import (
2729
)
2830

2931
func newImageInspectCommand() *cobra.Command {
30-
var imageInspectCommand = &cobra.Command{
32+
imageInspectCommand := &cobra.Command{
3133
Use: "inspect [flags] IMAGE [IMAGE...]",
3234
Args: cobra.MinimumNArgs(1),
3335
Short: "Display detailed information on one or more images.",
@@ -89,6 +91,11 @@ func imageInspectAction(cmd *cobra.Command, args []string) error {
8991
return err
9092
}
9193

94+
// Verify we have a valid mode
95+
if options.Mode != "native" && options.Mode != "dockercompat" {
96+
return fmt.Errorf("unknown mode %q", options.Mode)
97+
}
98+
9299
client, ctx, cancel, err := clientutil.NewClientWithPlatform(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address, options.Platform)
93100
if err != nil {
94101
return err

cmd/nerdctl/ipfs/ipfs_simple_linux_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,11 @@ func TestIPFSSimple(t *testing.T) {
175175
helpers.Ensure("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, data.Get(mainImageCIDKey), data.Identifier("encrypted"))
176176
cmd := helpers.Command("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", data.Identifier("encrypted"))
177177
cmd.Run(&test.Expected{
178-
ExitCode: 1,
179-
Output: test.Equals("1\n"),
178+
Output: test.Equals("1\n"),
180179
})
181180
cmd = helpers.Command("image", "inspect", "--mode=native", "--format={{json (index .Manifest.Layers 0) }}", data.Identifier("encrypted"))
182181
cmd.Run(&test.Expected{
183-
ExitCode: 1,
184-
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
182+
Output: test.Contains("org.opencontainers.image.enc.keys.jwe"),
185183
})
186184

187185
// Push the encrypted image and save the CID

pkg/cmd/image/inspect.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ func inspectIdentifier(ctx context.Context, client *containerd.Client, identifie
8888

8989
// Inspect prints detailed information of each image in `images`.
9090
func Inspect(ctx context.Context, client *containerd.Client, identifiers []string, options types.ImageInspectOptions) error {
91-
// Verify we have a valid mode
92-
// TODO: move this out of here, to Cobra command line arg validation
93-
if options.Mode != "native" && options.Mode != "dockercompat" {
94-
return fmt.Errorf("unknown mode %q", options.Mode)
95-
}
9691
// Set a timeout
9792
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
9893
defer cancel()
@@ -180,15 +175,17 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
180175
}
181176

182177
// Done iterating through candidates. Did we find anything that matches?
183-
if validatedImage != nil {
178+
if options.Mode == "dockercompat" {
179+
if validatedImage == nil {
180+
errs = append(errs, fmt.Errorf("no such image: %s", identifier))
181+
continue
182+
}
184183
// Then slap in the repoTags and repoDigests we found from the other candidates
185184
validatedImage.RepoTags = append(validatedImage.RepoTags, repoTags...)
186185
validatedImage.RepoDigests = append(validatedImage.RepoDigests, repoDigests...)
187186
// Store our image
188187
// foundImages[validatedDigest] = validatedImage
189188
entries = append(entries, validatedImage)
190-
} else {
191-
errs = append(errs, fmt.Errorf("no such image: %s", identifier))
192189
}
193190
}
194191

0 commit comments

Comments
 (0)