Skip to content

Commit

Permalink
Add compose top
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <jindon@amazon.com>
  • Loading branch information
djdongjin committed Dec 6, 2022
1 parent e4b2b6d commit a40cd1c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ It does not necessarily mean that the corresponding features are missing in cont
- [:whale: nerdctl compose restart](#whale-nerdctl-compose-restart)
- [:whale: nerdctl compose rm](#whale-nerdctl-compose-rm)
- [:whale: nerdctl compose run](#whale-nerdctl-compose-run)
- [:whale: nerdctl compose top](#whale-nerdctl-compose-top)
- [:whale: nerdctl compose version](#whale-nerdctl-compose-version)
- [IPFS management](#ipfs-management)
- [:nerd_face: nerdctl ipfs registry up](#nerd_face-nerdctl-ipfs-registry-up)
Expand Down Expand Up @@ -1569,6 +1570,12 @@ Unimplemented `docker-compose run` (V1) flags: `--use-aliases`, `--no-TTY`

Unimplemented `docker compose run` (V2) flags: `--use-aliases`, `--no-TTY`, `--tty`

### :whale: nerdctl compose top

Display the running processes of service containers

Usage: `nerdctl compose top [SERVICES...]`

### :whale: nerdctl compose version
Show the Compose version information (which is the nerdctl version)

Expand Down Expand Up @@ -1647,7 +1654,7 @@ Registry:
- `docker search`

Compose:
- `docker-compose create|events|pause|port|scale|start|top|unpause`
- `docker-compose create|events|pause|port|scale|start|unpause`

Others:
- `docker system df`
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func newComposeCommand() *cobra.Command {
newComposeRunCommand(),
newComposeVersionCommand(),
newComposeStopCommand(),
newComposeTopCommand(),
)

return composeCommand
Expand Down
83 changes: 83 additions & 0 deletions cmd/nerdctl/compose_top.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/spf13/cobra"
)

func newComposeTopCommand() *cobra.Command {
var composeTopCommand = &cobra.Command{
Use: "top [SERVICE...]",
Short: "Display the running processes of service containers",
RunE: composeTopAction,
SilenceUsage: true,
SilenceErrors: true,
DisableFlagsInUseLine: true,
}
return composeTopCommand
}

func composeTopAction(cmd *cobra.Command, args []string) error {
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
}

stdout := cmd.OutOrStdout()
for _, c := range containers {
cStatus, err := containerStatus(ctx, c)
if err != nil {
return err
}
if cStatus.Status != containerd.Running {
continue
}

info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
if err != nil {
return err
}
fmt.Fprintf(stdout, "%s\n", info.Labels[labels.Name])
// `compose ps` uses empty ps args
err = containerTop(ctx, cmd, client, c.ID(), "")
if err != nil {
return err
}
fmt.Fprintln(stdout)
}

return nil
}
56 changes: 56 additions & 0 deletions cmd/nerdctl/compose_top_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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"
"testing"

"github.com/containerd/nerdctl/pkg/infoutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/testutil"
)

func TestComposeTop(t *testing.T) {
if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" {
t.Skip("test skipped for rootless containers on cgroup v1")
}

base := testutil.NewBase(t)
var dockerComposeYAML = fmt.Sprintf(`
version: '3.1'
services:
svc0:
image: %s
command: "sleep infinity"
svc1:
image: %s
`, testutil.CommonImage, testutil.NginxAlpineImage)

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").AssertOK()

// a running container should have the process command in output
base.ComposeCmd("-f", comp.YAMLFullPath(), "top", "svc0").AssertOutContains("sleep infinity")
base.ComposeCmd("-f", comp.YAMLFullPath(), "top", "svc1").AssertOutContains("nginx")
}

0 comments on commit a40cd1c

Please sign in to comment.