-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from fahedouch/inspect-image
Add Inspect image
- Loading branch information
Showing
14 changed files
with
499 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/containerd/nerdctl/pkg/idutil/imagewalker" | ||
"github.com/containerd/nerdctl/pkg/imageinspector" | ||
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat" | ||
"github.com/pkg/errors" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var imageInspectCommand = &cli.Command{ | ||
Name: "inspect", | ||
Usage: "Display detailed information on one or more images.", | ||
ArgsUsage: "[OPTIONS] IMAGE [IMAGE...]", | ||
Description: "Hint: set `--mode=native` for showing the full output", | ||
Action: ImageInspectAction, | ||
BashComplete: imageInspectBashComplete, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "mode", | ||
Usage: "Inspect mode, \"dockercompat\" for Docker-compatible output, \"native\" for containerd-native output", | ||
Value: "dockercompat", | ||
}, | ||
}, | ||
} | ||
|
||
func ImageInspectAction(clicontext *cli.Context) error { | ||
if clicontext.NArg() == 0 { | ||
return errors.Errorf("requires at least 1 argument") | ||
} | ||
|
||
client, ctx, cancel, err := newClient(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
f := &imageInspector{ | ||
mode: clicontext.String("mode"), | ||
} | ||
walker := &imagewalker.ImageWalker{ | ||
Client: client, | ||
OnFound: func(ctx context.Context, found imagewalker.Found) error { | ||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
|
||
n, err := imageinspector.Inspect(ctx, client, found.Image) | ||
if err != nil { | ||
return err | ||
} | ||
switch f.mode { | ||
case "native": | ||
f.entries = append(f.entries, n) | ||
case "dockercompat": | ||
d, err := dockercompat.ImageFromNative(n) | ||
if err != nil { | ||
return err | ||
} | ||
f.entries = append(f.entries, d) | ||
default: | ||
return errors.Errorf("unknown mode %q", f.mode) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
var errs []error | ||
for _, req := range clicontext.Args().Slice() { | ||
n, err := walker.Walk(ctx, req) | ||
if err != nil { | ||
errs = append(errs, err) | ||
} else if n == 0 { | ||
errs = append(errs, errors.Errorf("no such object: %s", req)) | ||
} | ||
} | ||
|
||
b, err := json.MarshalIndent(f.entries, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(clicontext.App.Writer, string(b)) | ||
|
||
if len(errs) > 0 { | ||
return errors.Errorf("%d errors: %v", len(errs), errs) | ||
} | ||
return nil | ||
} | ||
|
||
type imageInspector struct { | ||
mode string | ||
entries []interface{} | ||
} | ||
|
||
func imageInspectBashComplete(clicontext *cli.Context) { | ||
coco := parseCompletionContext(clicontext) | ||
if coco.boring { | ||
defaultBashComplete(clicontext) | ||
return | ||
} | ||
if coco.flagTakesValue { | ||
w := clicontext.App.Writer | ||
switch coco.flagName { | ||
case "mode": | ||
fmt.Fprintln(w, "dockercompat") | ||
fmt.Fprintln(w, "native") | ||
return | ||
} | ||
defaultBashComplete(clicontext) | ||
return | ||
} | ||
// show image names | ||
bashCompleteImageNames(clicontext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestImageInspectContainsSomeStuff(t *testing.T) { | ||
base := testutil.NewBase(t) | ||
defer base.Cmd("rmi", "-f", testutil.NginxAlpineImage).Run() | ||
|
||
base.Cmd("pull", testutil.NginxAlpineImage).AssertOK() | ||
inspect := base.InspectImage(testutil.NginxAlpineImage) | ||
|
||
assert.Assert(base.T, len(inspect.RootFS.Layers) > 0) | ||
assert.Assert(base.T, inspect.RootFS.Type != "") | ||
assert.Assert(base.T, inspect.Architecture != "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.