diff options
author | Brent Baude <bbaude@redhat.com> | 2020-03-20 14:00:05 -0500 |
---|---|---|
committer | Brent Baude <bbaude@redhat.com> | 2020-03-20 14:33:33 -0500 |
commit | c81e065149da73ae904aa19ee46a23d1ab8ce55c (patch) | |
tree | 52a8692894690a8f09e025f90d6d2b5045259e4f /pkg | |
parent | 4a00409bf8c88174cbdbc12760d3f8ddce9621de (diff) | |
download | podman-c81e065149da73ae904aa19ee46a23d1ab8ce55c.tar.gz podman-c81e065149da73ae904aa19ee46a23d1ab8ce55c.tar.bz2 podman-c81e065149da73ae904aa19ee46a23d1ab8ce55c.zip |
podmanv2 enable remote wait
enable remote container wait with condition
Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/bindings/containers/containers.go | 5 | ||||
-rw-r--r-- | pkg/bindings/test/common_test.go | 5 | ||||
-rw-r--r-- | pkg/bindings/test/containers_test.go | 7 | ||||
-rw-r--r-- | pkg/bindings/test/pods_test.go | 2 | ||||
-rw-r--r-- | pkg/domain/entities/containers.go | 8 | ||||
-rw-r--r-- | pkg/domain/infra/abi/containers.go | 7 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/containers.go | 19 | ||||
-rw-r--r-- | pkg/domain/infra/tunnel/helpers.go | 41 |
8 files changed, 77 insertions, 17 deletions
diff --git a/pkg/bindings/containers/containers.go b/pkg/bindings/containers/containers.go index f298dbba1..534555a00 100644 --- a/pkg/bindings/containers/containers.go +++ b/pkg/bindings/containers/containers.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/containers/libpod/libpod" + "github.com/containers/libpod/libpod/define" lpapiv2 "github.com/containers/libpod/pkg/api/handlers/libpod" "github.com/containers/libpod/pkg/bindings" ) @@ -212,7 +213,7 @@ func Unpause(ctx context.Context, nameOrID string) error { // Wait blocks until the given container reaches a condition. If not provided, the condition will // default to stopped. If the condition is stopped, an exit code for the container will be provided. The // nameOrID can be a container name or a partial/full ID. -func Wait(ctx context.Context, nameOrID string, condition *string) (int32, error) { +func Wait(ctx context.Context, nameOrID string, condition *define.ContainerStatus) (int32, error) { //nolint var exitCode int32 conn, err := bindings.GetClient(ctx) if err != nil { @@ -220,7 +221,7 @@ func Wait(ctx context.Context, nameOrID string, condition *string) (int32, error } params := url.Values{} if condition != nil { - params.Set("condition", *condition) + params.Set("condition", condition.String()) } response, err := conn.DoRequest(nil, http.MethodPost, "/containers/%s/wait", params, nameOrID) if err != nil { diff --git a/pkg/bindings/test/common_test.go b/pkg/bindings/test/common_test.go index 409e620a3..6b8d6788c 100644 --- a/pkg/bindings/test/common_test.go +++ b/pkg/bindings/test/common_test.go @@ -3,6 +3,7 @@ package test_bindings import ( "context" "fmt" + "github.com/containers/libpod/libpod/define" "io/ioutil" "os" "os/exec" @@ -205,8 +206,8 @@ func (b *bindingTest) RunTopContainer(containerName *string, insidePod *bool, po if err != nil { return "", err } - waiting := "running" - _, err = containers.Wait(b.conn, ctr.ID, &waiting) + wait := define.ContainerStateRunning + _, err = containers.Wait(b.conn, ctr.ID, &wait) return ctr.ID, err } diff --git a/pkg/bindings/test/containers_test.go b/pkg/bindings/test/containers_test.go index afb0cc19b..f5465c803 100644 --- a/pkg/bindings/test/containers_test.go +++ b/pkg/bindings/test/containers_test.go @@ -1,6 +1,7 @@ package test_bindings import ( + "github.com/containers/libpod/libpod/define" "net/http" "strconv" "strings" @@ -282,8 +283,8 @@ var _ = Describe("Podman containers ", func() { var ( name = "top" exitCode int32 = -1 - pause = "paused" - unpause = "running" + pause = define.ContainerStatePaused + running = define.ContainerStateRunning ) errChan := make(chan error) _, err := bt.RunTopContainer(&name, nil, nil) @@ -301,7 +302,7 @@ var _ = Describe("Podman containers ", func() { errChan = make(chan error) go func() { - _, waitErr := containers.Wait(bt.conn, name, &unpause) + _, waitErr := containers.Wait(bt.conn, name, &running) errChan <- waitErr close(errChan) }() diff --git a/pkg/bindings/test/pods_test.go b/pkg/bindings/test/pods_test.go index fae1deca4..bcf8e69b8 100644 --- a/pkg/bindings/test/pods_test.go +++ b/pkg/bindings/test/pods_test.go @@ -81,7 +81,7 @@ var _ = Describe("Podman pods", func() { It("List pods with filters", func() { var newpod2 string = "newpod2" bt.Podcreate(&newpod2) - _, err = bt.RunTopContainer(nil, &trueFlag, &newpod) + _, err = bt.RunTopContainer(nil, &bindings.PTrue, &newpod) Expect(err).To(BeNil()) // Expected err with invalid filter params diff --git a/pkg/domain/entities/containers.go b/pkg/domain/entities/containers.go index 45ce1f1fb..0e1208b3b 100644 --- a/pkg/domain/entities/containers.go +++ b/pkg/domain/entities/containers.go @@ -1,9 +1,13 @@ package entities -import "time" +import ( + "time" + + "github.com/containers/libpod/libpod/define" +) type WaitOptions struct { - Condition string + Condition define.ContainerStatus Interval time.Duration Latest bool } diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index 2332c2874..cdcd77246 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -24,18 +24,13 @@ func (ic *ContainerEngine) ContainerWait(ctx context.Context, namesOrIds []strin var ( responses []entities.WaitReport ) - condition, err := define.StringToContainerStatus(options.Condition) - if err != nil { - return nil, err - } - ctrs, err := shortcuts.GetContainersByContext(false, options.Latest, namesOrIds, ic.Libpod) if err != nil { return nil, err } for _, c := range ctrs { response := entities.WaitReport{Id: c.ID()} - exitCode, err := c.WaitForConditionWithInterval(options.Interval, condition) + exitCode, err := c.WaitForConditionWithInterval(options.Interval, options.Condition) if err != nil { response.Error = err } else { diff --git a/pkg/domain/infra/tunnel/containers.go b/pkg/domain/infra/tunnel/containers.go index d2d93d51b..8bf74126d 100644 --- a/pkg/domain/infra/tunnel/containers.go +++ b/pkg/domain/infra/tunnel/containers.go @@ -13,7 +13,24 @@ func (ic *ContainerEngine) ContainerExists(ctx context.Context, nameOrId string) } func (ic *ContainerEngine) ContainerWait(ctx context.Context, namesOrIds []string, options entities.WaitOptions) ([]entities.WaitReport, error) { - return nil, nil + var ( + responses []entities.WaitReport + ) + cons, err := getContainersByContext(ic.ClientCxt, false, namesOrIds) + if err != nil { + return nil, err + } + for _, c := range cons { + response := entities.WaitReport{Id: c.ID} + exitCode, err := containers.Wait(ic.ClientCxt, c.ID, &options.Condition) + if err != nil { + response.Error = err + } else { + response.ExitCode = exitCode + } + responses = append(responses, response) + } + return responses, nil } func (r *ContainerEngine) ContainerDelete(ctx context.Context, opts entities.ContainerDeleteOptions) (*entities.ContainerDeleteReport, error) { diff --git a/pkg/domain/infra/tunnel/helpers.go b/pkg/domain/infra/tunnel/helpers.go new file mode 100644 index 000000000..d5a3224c2 --- /dev/null +++ b/pkg/domain/infra/tunnel/helpers.go @@ -0,0 +1,41 @@ +package tunnel + +import ( + "context" + + "github.com/containers/libpod/pkg/api/handlers/libpod" + "github.com/containers/libpod/pkg/bindings" + "github.com/containers/libpod/pkg/bindings/containers" + "github.com/containers/libpod/pkg/util" + "github.com/pkg/errors" +) + +func getContainersByContext(contextWithConnection context.Context, all bool, namesOrIds []string) ([]libpod.ListContainer, error) { + var ( + cons []libpod.ListContainer + ) + if all && len(namesOrIds) > 0 { + return nil, errors.New("cannot lookup containers and all") + } + c, err := containers.List(contextWithConnection, nil, &bindings.PTrue, nil, nil, nil, &bindings.PTrue) + if err != nil { + return nil, err + } + if all { + return c, err + } + for _, id := range namesOrIds { + var found bool + for _, con := range c { + if id == con.ID || util.StringInSlice(id, con.Names) { + cons = append(cons, con) + found = true + break + } + } + if !found { + return nil, errors.Errorf("unable to find container %q", id) + } + } + return cons, nil +} |