Skip to content
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

refactor: container wait flagging process #1934

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 13 additions & 53 deletions cmd/nerdctl/container_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
package main

import (
"context"
"fmt"
"io"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/hashicorp/go-multierror"
"github.com/containerd/nerdctl/pkg/cmd/container"
"github.com/spf13/cobra"
)

Expand All @@ -41,66 +37,30 @@ func newWaitCommand() *cobra.Command {
return waitCommand
}

func containerWaitAction(cmd *cobra.Command, args []string) error {
func processContainerWaitOptions(cmd *cobra.Command) (types.ContainerWaitOptions, error) {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
if err != nil {
return err
}
defer cancel()

var containers []containerd.Container
walker := &containerwalker.ContainerWalker{
Client: client,
OnFound: func(ctx context.Context, found containerwalker.Found) error {
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
containers = append(containers, found.Container)
return nil
},
return types.ContainerWaitOptions{}, err
}

for _, req := range args {
n, err := walker.Walk(ctx, req)
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("no such container: %s", req)
}
}
var allErr error
w := cmd.OutOrStdout()
for _, container := range containers {
if waitErr := waitContainer(ctx, w, container); waitErr != nil {
allErr = multierror.Append(allErr, waitErr)
}
}
return allErr
return types.ContainerWaitOptions{
Stdout: cmd.OutOrStdout(),
GOptions: globalOptions,
}, nil
}

func waitContainer(ctx context.Context, w io.Writer, container containerd.Container) error {
task, err := container.Task(ctx, nil)
func containerWaitAction(cmd *cobra.Command, args []string) error {
options, err := processContainerWaitOptions(cmd)
if err != nil {
return err
}

statusC, err := task.Wait(ctx)
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
if err != nil {
return err
}
defer cancel()

status := <-statusC
code, _, err := status.Result()
if err != nil {
return err
}
fmt.Fprintln(w, code)
return nil
return container.Wait(ctx, client, args, options)
}

func waitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/types/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ type ContainerLogsOptions struct {
Until string
}

// ContainerWaitOptions specifies options for `nerdctl (container) wait`.
type ContainerWaitOptions struct {
Stdout io.Writer
// GOptions is the global options.
GOptions GlobalCommandOptions
}

// ContainerExecOptions specifies options for `nerdctl (container) exec`
type ContainerExecOptions struct {
GOptions GlobalCommandOptions
Expand Down
81 changes: 81 additions & 0 deletions pkg/cmd/container/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
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 container

import (
"context"
"fmt"
"io"

"github.com/containerd/containerd"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/hashicorp/go-multierror"
)

// Wait blocks until all the containers specified by reqs have stopped, then print their exit codes.
func Wait(ctx context.Context, client *containerd.Client, reqs []string, options types.ContainerWaitOptions) error {
var containers []containerd.Container
walker := &containerwalker.ContainerWalker{
Client: client,
OnFound: func(ctx context.Context, found containerwalker.Found) error {
if found.MatchCount > 1 {
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}
containers = append(containers, found.Container)
return nil
},
}

for _, req := range reqs {
n, err := walker.Walk(ctx, req)
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("no such container: %s", req)
}
}
var allErr error
w := options.Stdout
for _, container := range containers {
if waitErr := waitContainer(ctx, w, container); waitErr != nil {
allErr = multierror.Append(allErr, waitErr)
}
}
return allErr
}

func waitContainer(ctx context.Context, w io.Writer, container containerd.Container) error {
task, err := container.Task(ctx, nil)
if err != nil {
return err
}

statusC, err := task.Wait(ctx)
if err != nil {
return err
}

status := <-statusC
code, _, err := status.Result()
if err != nil {
return err
}
fmt.Fprintln(w, code)
return nil
}