-
Notifications
You must be signed in to change notification settings - Fork 638
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 #1555 from djdongjin/compose-images
Add `compose images` command
- Loading branch information
Showing
4 changed files
with
289 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
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" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"text/tabwriter" | ||
|
||
"github.com/containerd/containerd" | ||
"github.com/containerd/containerd/pkg/progress" | ||
"github.com/containerd/containerd/snapshots" | ||
"github.com/containerd/nerdctl/pkg/imgutil" | ||
"github.com/containerd/nerdctl/pkg/labels" | ||
"github.com/containerd/nerdctl/pkg/strutil" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func newComposeImagesCommand() *cobra.Command { | ||
var composeImagesCommand = &cobra.Command{ | ||
Use: "images [flags] [SERVICE...]", | ||
Short: "List images used by created containers in services", | ||
RunE: composeImagesAction, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
composeImagesCommand.Flags().BoolP("quiet", "q", false, "Only show numeric image IDs") | ||
|
||
return composeImagesCommand | ||
} | ||
|
||
func composeImagesAction(cmd *cobra.Command, args []string) error { | ||
quiet, err := cmd.Flags().GetBool("quiet") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, ctx, cancel, err := newClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
c, err := getComposer(cmd, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serviceNames, err := c.ServiceNames(args...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
containers, err := c.Containers(ctx, serviceNames...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if quiet { | ||
return printComposeImageIDs(ctx, containers) | ||
} | ||
|
||
snapshotter, err := cmd.Flags().GetString("snapshotter") | ||
if err != nil { | ||
return err | ||
} | ||
sn := client.SnapshotService(snapshotter) | ||
|
||
return printComposeImages(ctx, cmd, containers, sn) | ||
} | ||
|
||
func printComposeImageIDs(ctx context.Context, containers []containerd.Container) error { | ||
ids := []string{} | ||
for _, c := range containers { | ||
image, err := c.Image(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
metaImage := image.Metadata() | ||
id := metaImage.Target.Digest.String() | ||
if !strutil.InStringSlice(ids, id) { | ||
ids = append(ids, id) | ||
} | ||
} | ||
|
||
for _, id := range ids { | ||
// always truncate image ids. | ||
fmt.Println(strings.Split(id, ":")[1][:12]) | ||
} | ||
return nil | ||
} | ||
|
||
func printComposeImages(ctx context.Context, cmd *cobra.Command, containers []containerd.Container, sn snapshots.Snapshotter) error { | ||
type composeImagePrintable struct { | ||
ContainerName string | ||
Repository string | ||
Tag string | ||
ImageID string | ||
Size string | ||
} | ||
|
||
var ( | ||
imagePrintables = []composeImagePrintable{} | ||
mu sync.Mutex | ||
) | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
for _, c := range containers { | ||
c := c | ||
eg.Go(func() error { | ||
info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata) | ||
if err != nil { | ||
return err | ||
} | ||
containerName := info.Labels[labels.Name] | ||
|
||
image, err := c.Image(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
size, err := unpackedImageSize(ctx, sn, image) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
metaImage := image.Metadata() | ||
repository, tag := imgutil.ParseRepoTag(metaImage.Name) | ||
imageID := metaImage.Target.Digest.String() | ||
if repository == "" { | ||
repository = "<none>" | ||
} | ||
if tag == "" { | ||
tag = "<none>" | ||
} | ||
imageID = strings.Split(imageID, ":")[1][:12] | ||
|
||
printable := composeImagePrintable{ | ||
ContainerName: containerName, | ||
Repository: repository, | ||
Tag: tag, | ||
ImageID: imageID, | ||
Size: progress.Bytes(size).String(), | ||
} | ||
|
||
mu.Lock() | ||
defer mu.Unlock() | ||
imagePrintables = append(imagePrintables, printable) | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
if err := eg.Wait(); err != nil { | ||
return err | ||
} | ||
|
||
w := tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0) | ||
fmt.Fprintln(w, "Container\tRepository\tTag\tImage Id\tSize") | ||
for _, p := range imagePrintables { | ||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", | ||
p.ContainerName, | ||
p.Repository, | ||
p.Tag, | ||
p.ImageID, | ||
p.Size, | ||
); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return w.Flush() | ||
} |
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,86 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
) | ||
|
||
func TestComposeImages(t *testing.T) { | ||
base := testutil.NewBase(t) | ||
var dockerComposeYAML = fmt.Sprintf(` | ||
version: '3.1' | ||
services: | ||
wordpress: | ||
image: %s | ||
ports: | ||
- 8080:80 | ||
environment: | ||
WORDPRESS_DB_HOST: db | ||
WORDPRESS_DB_USER: exampleuser | ||
WORDPRESS_DB_PASSWORD: examplepass | ||
WORDPRESS_DB_NAME: exampledb | ||
volumes: | ||
- wordpress:/var/www/html | ||
db: | ||
image: %s | ||
environment: | ||
MYSQL_DATABASE: exampledb | ||
MYSQL_USER: exampleuser | ||
MYSQL_PASSWORD: examplepass | ||
MYSQL_RANDOM_ROOT_PASSWORD: '1' | ||
volumes: | ||
- db:/var/lib/mysql | ||
volumes: | ||
wordpress: | ||
db: | ||
`, testutil.WordpressImage, testutil.MariaDBImage) | ||
|
||
comp := testutil.NewComposeDir(t, dockerComposeYAML) | ||
defer comp.CleanUp() | ||
projectName := comp.ProjectName() | ||
t.Logf("projectName=%q", projectName) | ||
|
||
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK() | ||
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run() | ||
|
||
ImageAssertHandler := func(svc string, image string, exist bool) func(stdout string) error { | ||
return func(stdout string) error { | ||
if strings.Contains(stdout, image) != exist { | ||
return fmt.Errorf("image %s from service %s: expect in output (%t), actual (%t)", image, svc, exist, !exist) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
wordpressImageName := strings.Split(testutil.WordpressImage, ":")[0] | ||
dbImageName := strings.Split(testutil.MariaDBImage, ":")[0] | ||
|
||
// check one service image | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutWithFunc(ImageAssertHandler("db", dbImageName, true)) | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutWithFunc(ImageAssertHandler("wordpress", wordpressImageName, false)) | ||
|
||
// check all service images | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutWithFunc(ImageAssertHandler("db", dbImageName, true)) | ||
base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutWithFunc(ImageAssertHandler("wordpress", wordpressImageName, true)) | ||
} |