-
Notifications
You must be signed in to change notification settings - Fork 772
Add compose ps flags #2509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AkihiroSuda
merged 1 commit into
containerd:main
from
tanghaowillow:add-compose-ps-flags
Sep 21, 2023
Merged
Add compose ps flags #2509
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -19,9 +19,13 @@ package main | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "text/tabwriter" | ||
| "time" | ||
|
|
||
| "github.com/containerd/containerd" | ||
| "github.com/containerd/containerd/errdefs" | ||
| "github.com/containerd/containerd/runtime/restart" | ||
| gocni "github.com/containerd/go-cni" | ||
| "github.com/containerd/nerdctl/pkg/clientutil" | ||
| "github.com/containerd/nerdctl/pkg/cmd/compose" | ||
|
|
@@ -42,7 +46,12 @@ func newComposePsCommand() *cobra.Command { | |
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| } | ||
| composePsCommand.Flags().String("format", "", "Format the output. Supported values: [json]") | ||
| composePsCommand.Flags().String("format", "table", "Format the output. Supported values: [table|json]") | ||
| composePsCommand.Flags().String("filter", "", "Filter matches containers based on given conditions") | ||
| composePsCommand.Flags().StringArray("status", []string{}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]") | ||
| composePsCommand.Flags().BoolP("quiet", "q", false, "Only display container IDs") | ||
| composePsCommand.Flags().Bool("services", false, "Display services") | ||
| composePsCommand.Flags().BoolP("all", "a", false, "Show all containers (default shows just running)") | ||
| return composePsCommand | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
|
|
@@ -71,8 +80,40 @@ func composePsAction(cmd *cobra.Command, args []string) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| if format != "json" && format != "" { | ||
| return fmt.Errorf("unsupported format %s, supported formats are: [json]", format) | ||
| if format != "json" && format != "table" { | ||
| return fmt.Errorf("unsupported format %s, supported formats are: [table|json]", format) | ||
| } | ||
| status, err := cmd.Flags().GetStringArray("status") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| quiet, err := cmd.Flags().GetBool("quiet") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| displayServices, err := cmd.Flags().GetBool("services") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| filter, err := cmd.Flags().GetString("filter") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if filter != "" { | ||
| splited := strings.SplitN(filter, "=", 2) | ||
| if len(splited) != 2 { | ||
| return fmt.Errorf("invalid argument \"%s\" for \"-f, --filter\": bad format of filter (expected name=value)", filter) | ||
| } | ||
| // currently only the 'status' filter is supported | ||
| if splited[0] != "status" { | ||
| return fmt.Errorf("invalid filter '%s'", splited[0]) | ||
| } | ||
| status = append(status, splited[1]) | ||
| } | ||
|
|
||
| all, err := cmd.Flags().GetBool("all") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) | ||
|
|
@@ -97,6 +138,41 @@ func composePsAction(cmd *cobra.Command, args []string) error { | |
| return err | ||
| } | ||
|
|
||
| if !all { | ||
| var upContainers []containerd.Container | ||
| for _, container := range containers { | ||
| // cStatus := formatter.ContainerStatus(ctx, c) | ||
| cStatus, err := containerutil.ContainerStatus(ctx, container) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| if cStatus.Status == containerd.Running { | ||
| upContainers = append(upContainers, container) | ||
| } | ||
| } | ||
| containers = upContainers | ||
| } | ||
|
|
||
| if len(status) != 0 { | ||
| var filterdContainers []containerd.Container | ||
| for _, container := range containers { | ||
| cStatus := statusForFilter(ctx, container) | ||
| for _, s := range status { | ||
| if cStatus == s { | ||
| filterdContainers = append(filterdContainers, container) | ||
| } | ||
| } | ||
| } | ||
| containers = filterdContainers | ||
| } | ||
|
|
||
| if quiet { | ||
| for _, c := range containers { | ||
| fmt.Fprintln(cmd.OutOrStdout(), c.ID()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| containersPrintable := make([]composeContainerPrintable, len(containers)) | ||
| eg, ctx := errgroup.WithContext(ctx) | ||
| for i, container := range containers { | ||
|
|
@@ -121,6 +197,12 @@ func composePsAction(cmd *cobra.Command, args []string) error { | |
| return err | ||
| } | ||
|
|
||
| if displayServices { | ||
| for _, p := range containersPrintable { | ||
| fmt.Fprintln(cmd.OutOrStdout(), p.Service) | ||
| } | ||
| return nil | ||
| } | ||
| if format == "json" { | ||
| outJSON, err := formatter.ToJSON(containersPrintable, "", "") | ||
| if err != nil { | ||
|
|
@@ -198,9 +280,11 @@ func composeContainerPrintableJSON(ctx context.Context, container containerd.Con | |
| if err == nil { | ||
| // show exitCode only when container is exited/stopped | ||
| if status.Status == containerd.Stopped { | ||
| state = "exited" | ||
| exitCode = status.ExitStatus | ||
| } else { | ||
| state = string(status.Status) | ||
| } | ||
| state = string(status.Status) | ||
| } else { | ||
| state = string(containerd.Unknown) | ||
| } | ||
|
|
@@ -255,3 +339,41 @@ func formatPublishers(labelMap map[string]string) []PortPublisher { | |
| } | ||
| return dockerPorts | ||
| } | ||
|
|
||
| // statusForFilter returns the status value to be matched with the 'status' filter | ||
| func statusForFilter(ctx context.Context, c containerd.Container) string { | ||
| // Just in case, there is something wrong in server. | ||
| ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| task, err := c.Task(ctx, nil) | ||
| if err != nil { | ||
| // NOTE: NotFound doesn't mean that container hasn't started. | ||
| // In docker/CRI-containerd plugin, the task will be deleted | ||
| // when it exits. So, the status will be "created" for this | ||
| // case. | ||
| if errdefs.IsNotFound(err) { | ||
| return string(containerd.Created) | ||
| } | ||
| return string(containerd.Unknown) | ||
| } | ||
|
|
||
| status, err := task.Status(ctx) | ||
| if err != nil { | ||
| return string(containerd.Unknown) | ||
| } | ||
| labels, err := c.Labels(ctx) | ||
| if err != nil { | ||
| return string(containerd.Unknown) | ||
| } | ||
|
|
||
| switch s := status.Status; s { | ||
| case containerd.Stopped: | ||
| if labels[restart.StatusLabel] == string(containerd.Running) && restart.Reconcile(status, labels) { | ||
| return "restarting" | ||
| } | ||
| return "exited" | ||
| default: | ||
| return string(s) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -24,9 +24,6 @@ import ( | |
| ) | ||
|
|
||
| func TestComposeRestart(t *testing.T) { | ||
| // docker-compose v2 hides exited containers in `compose ps`, and shows | ||
| // them if `-a` is passed, which is not supported yet by `nerdctl compose`. | ||
| testutil.DockerIncompatible(t) | ||
| base := testutil.NewBase(t) | ||
| var dockerComposeYAML = fmt.Sprintf(` | ||
| version: '3.1' | ||
|
|
@@ -68,13 +65,13 @@ volumes: | |
|
|
||
| // stop and restart a single service. | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "stop", "db").AssertOK() | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Exit", "exited") | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db", "-a").AssertOutContainsAny("Exit", "exited") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DockerIncompatible() can be now probably removed |
||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "restart", "db").AssertOK() | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running") | ||
|
|
||
| // stop one service and restart all (also check `--timeout` arg). | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "stop", "db").AssertOK() | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Exit", "exited") | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db", "-a").AssertOutContainsAny("Exit", "exited") | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "restart", "--timeout", "5").AssertOK() | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "db").AssertOutContainsAny("Up", "running") | ||
| base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "wordpress").AssertOutContainsAny("Up", "running") | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DockerIncompatible() can be now probably removed