summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/api/handlers/compat/containers.go29
-rw-r--r--pkg/api/handlers/libpod/containers.go13
-rw-r--r--pkg/api/handlers/utils/containers.go231
-rw-r--r--pkg/bindings/containers/types.go2
-rw-r--r--pkg/bindings/containers/types_wait_options.go10
-rw-r--r--pkg/bindings/test/attach_test.go2
-rw-r--r--pkg/bindings/test/common_test.go2
-rw-r--r--pkg/bindings/test/containers_test.go4
-rw-r--r--pkg/domain/entities/containers.go2
-rw-r--r--pkg/domain/infra/abi/containers.go6
10 files changed, 218 insertions, 83 deletions
diff --git a/pkg/api/handlers/compat/containers.go b/pkg/api/handlers/compat/containers.go
index 86508f938..9c0893a80 100644
--- a/pkg/api/handlers/compat/containers.go
+++ b/pkg/api/handlers/compat/containers.go
@@ -23,10 +23,8 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/docker/go-units"
- "github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/pkg/errors"
- "github.com/sirupsen/logrus"
)
func RemoveContainer(w http.ResponseWriter, r *http.Request) {
@@ -233,8 +231,11 @@ func KillContainer(w http.ResponseWriter, r *http.Request) {
return
}
if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
- if _, err := utils.WaitContainer(w, r); err != nil {
-
+ opts := entities.WaitOptions{
+ Condition: []define.ContainerStatus{define.ContainerStateExited, define.ContainerStateStopped},
+ Interval: time.Millisecond * 250,
+ }
+ if _, err := containerEngine.ContainerWait(r.Context(), []string{name}, opts); err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err)
return
}
@@ -245,26 +246,8 @@ func KillContainer(w http.ResponseWriter, r *http.Request) {
}
func WaitContainer(w http.ResponseWriter, r *http.Request) {
- var msg string
// /{version}/containers/(name)/wait
- exitCode, err := utils.WaitContainer(w, r)
- if err != nil {
- if errors.Cause(err) == define.ErrNoSuchCtr {
- logrus.Warnf("container not found %q: %v", utils.GetName(r), err)
- return
- }
- logrus.Warnf("failed to wait on container %q: %v", mux.Vars(r)["name"], err)
- return
- }
-
- utils.WriteResponse(w, http.StatusOK, handlers.ContainerWaitOKBody{
- StatusCode: int(exitCode),
- Error: struct {
- Message string
- }{
- Message: msg,
- },
- })
+ utils.WaitContainerDocker(w, r)
}
func LibpodToContainer(l *libpod.Container, sz bool) (*handlers.Container, error) {
diff --git a/pkg/api/handlers/libpod/containers.go b/pkg/api/handlers/libpod/containers.go
index f6e348cef..619cbfd8b 100644
--- a/pkg/api/handlers/libpod/containers.go
+++ b/pkg/api/handlers/libpod/containers.go
@@ -4,7 +4,6 @@ import (
"io/ioutil"
"net/http"
"os"
- "strconv"
"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/define"
@@ -146,17 +145,7 @@ func GetContainer(w http.ResponseWriter, r *http.Request) {
}
func WaitContainer(w http.ResponseWriter, r *http.Request) {
- exitCode, err := utils.WaitContainer(w, r)
- if err != nil {
- name := utils.GetName(r)
- if errors.Cause(err) == define.ErrNoSuchCtr {
- utils.ContainerNotFound(w, name, err)
- return
- }
- logrus.Warnf("failed to wait on container %q: %v", name, err)
- return
- }
- utils.WriteResponse(w, http.StatusOK, strconv.Itoa(int(exitCode)))
+ utils.WaitContainerLibpod(w, r)
}
func UnmountContainer(w http.ResponseWriter, r *http.Request) {
diff --git a/pkg/api/handlers/utils/containers.go b/pkg/api/handlers/utils/containers.go
index fac237f87..518309a03 100644
--- a/pkg/api/handlers/utils/containers.go
+++ b/pkg/api/handlers/utils/containers.go
@@ -1,67 +1,230 @@
package utils
import (
+ "context"
+ "fmt"
"net/http"
+ "strconv"
"time"
- "github.com/containers/podman/v2/libpod"
- "github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/domain/infra/abi"
+
+ "github.com/containers/podman/v2/pkg/api/handlers"
+ "github.com/sirupsen/logrus"
+
+ "github.com/containers/podman/v2/libpod/define"
+
+ "github.com/containers/podman/v2/libpod"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
-func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) {
+type waitQueryDocker struct {
+ Condition string `schema:"condition"`
+}
+
+type waitQueryLibpod struct {
+ Interval string `schema:"interval"`
+ Condition []define.ContainerStatus `schema:"condition"`
+}
+
+func WaitContainerDocker(w http.ResponseWriter, r *http.Request) {
+ var err error
+ ctx := r.Context()
+
+ query := waitQueryDocker{}
+
+ decoder := ctx.Value("decoder").(*schema.Decoder)
+ if err = decoder.Decode(&query, r.URL.Query()); err != nil {
+ Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
+ return
+ }
+
+ interval := time.Nanosecond
+
+ condition := "not-running"
+ if _, found := r.URL.Query()["condition"]; found {
+ condition = query.Condition
+ if !isValidDockerCondition(query.Condition) {
+ BadRequest(w, "condition", condition, errors.New("not a valid docker condition"))
+ return
+ }
+ }
+
+ name := GetName(r)
+
+ exists, err := containerExists(ctx, name)
+
+ if err != nil {
+ InternalServerError(w, err)
+ return
+ }
+ if !exists {
+ ContainerNotFound(w, name, define.ErrNoSuchCtr)
+ return
+ }
+
+ // In docker compatibility mode we have to send headers in advance,
+ // otherwise docker client would freeze.
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(200)
+ if flusher, ok := w.(http.Flusher); ok {
+ flusher.Flush()
+ }
+
+ exitCode, err := waitDockerCondition(ctx, name, interval, condition)
+ msg := ""
+ if err != nil {
+ logrus.Errorf("error while waiting on condtion: %q", err)
+ msg = err.Error()
+ }
+ responseData := handlers.ContainerWaitOKBody{
+ StatusCode: int(exitCode),
+ Error: struct {
+ Message string
+ }{
+ Message: msg,
+ },
+ }
+ enc := json.NewEncoder(w)
+ enc.SetEscapeHTML(true)
+ err = enc.Encode(&responseData)
+ if err != nil {
+ logrus.Errorf("unable to write json: %q", err)
+ }
+}
+
+func WaitContainerLibpod(w http.ResponseWriter, r *http.Request) {
var (
- err error
- interval time.Duration
+ err error
+ interval = time.Millisecond * 250
+ conditions = []define.ContainerStatus{define.ContainerStateStopped, define.ContainerStateExited}
)
- runtime := r.Context().Value("runtime").(*libpod.Runtime)
- // Now use the ABI implementation to prevent us from having duplicate
- // code.
- containerEngine := abi.ContainerEngine{Libpod: runtime}
decoder := r.Context().Value("decoder").(*schema.Decoder)
- query := struct {
- Interval string `schema:"interval"`
- Condition define.ContainerStatus `schema:"condition"`
- }{
- // Override golang default values for types
- }
+ query := waitQueryLibpod{}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
- return 0, err
- }
- options := entities.WaitOptions{
- Condition: define.ContainerStateStopped,
}
- name := GetName(r)
+
if _, found := r.URL.Query()["interval"]; found {
interval, err = time.ParseDuration(query.Interval)
if err != nil {
InternalServerError(w, err)
- return 0, err
+ return
}
- } else {
- interval, err = time.ParseDuration("250ms")
- if err != nil {
+ }
+
+ if _, found := r.URL.Query()["condition"]; found {
+ if len(query.Condition) > 0 {
+ conditions = query.Condition
+ }
+ }
+
+ name := GetName(r)
+
+ waitFn := createContainerWaitFn(r.Context(), name, interval)
+
+ exitCode, err := waitFn(conditions...)
+ if err != nil {
+ if errors.Cause(err) == define.ErrNoSuchCtr {
+ ContainerNotFound(w, name, err)
+ return
+ } else {
InternalServerError(w, err)
- return 0, err
+ return
}
}
- options.Interval = interval
+ WriteResponse(w, http.StatusOK, strconv.Itoa(int(exitCode)))
+}
- if _, found := r.URL.Query()["condition"]; found {
- options.Condition = query.Condition
+type containerWaitFn func(conditions ...define.ContainerStatus) (int32, error)
+
+func createContainerWaitFn(ctx context.Context, containerName string, interval time.Duration) containerWaitFn {
+
+ runtime := ctx.Value("runtime").(*libpod.Runtime)
+ var containerEngine entities.ContainerEngine = &abi.ContainerEngine{Libpod: runtime}
+
+ return func(conditions ...define.ContainerStatus) (int32, error) {
+ opts := entities.WaitOptions{
+ Condition: conditions,
+ Interval: interval,
+ }
+ ctrWaitReport, err := containerEngine.ContainerWait(ctx, []string{containerName}, opts)
+ if err != nil {
+ return -1, err
+ }
+ if len(ctrWaitReport) != 1 {
+ return -1, fmt.Errorf("the ContainerWait() function returned unexpected count of reports: %d", len(ctrWaitReport))
+ }
+ return ctrWaitReport[0].ExitCode, ctrWaitReport[0].Error
}
+}
- report, err := containerEngine.ContainerWait(r.Context(), []string{name}, options)
+func isValidDockerCondition(cond string) bool {
+ switch cond {
+ case "next-exit", "removed", "not-running", "":
+ return true
+ }
+ return false
+}
+
+func waitDockerCondition(ctx context.Context, containerName string, interval time.Duration, dockerCondition string) (int32, error) {
+
+ containerWait := createContainerWaitFn(ctx, containerName, interval)
+
+ var err error
+ var code int32
+ switch dockerCondition {
+ case "next-exit":
+ code, err = waitNextExit(containerWait)
+ case "removed":
+ code, err = waitRemoved(containerWait)
+ case "not-running", "":
+ code, err = waitNotRunning(containerWait)
+ default:
+ panic("not a valid docker condition")
+ }
+ return code, err
+}
+
+var notRunningStates = []define.ContainerStatus{
+ define.ContainerStateCreated,
+ define.ContainerStateRemoving,
+ define.ContainerStateStopped,
+ define.ContainerStateExited,
+ define.ContainerStateConfigured,
+}
+
+func waitRemoved(ctrWait containerWaitFn) (int32, error) {
+ code, err := ctrWait(define.ContainerStateUnknown)
+ if err != nil && errors.Cause(err) == define.ErrNoSuchCtr {
+ return code, nil
+ } else {
+ return code, err
+ }
+}
+
+func waitNextExit(ctrWait containerWaitFn) (int32, error) {
+ _, err := ctrWait(define.ContainerStateRunning)
if err != nil {
- return 0, err
+ return -1, err
}
- if len(report) == 0 {
- InternalServerError(w, errors.New("No reports returned"))
- return 0, err
+ return ctrWait(notRunningStates...)
+}
+
+func waitNotRunning(ctrWait containerWaitFn) (int32, error) {
+ return ctrWait(notRunningStates...)
+}
+
+func containerExists(ctx context.Context, name string) (bool, error) {
+ runtime := ctx.Value("runtime").(*libpod.Runtime)
+ var containerEngine entities.ContainerEngine = &abi.ContainerEngine{Libpod: runtime}
+
+ var ctrExistsOpts entities.ContainerExistsOptions
+ ctrExistRep, err := containerEngine.ContainerExists(ctx, name, ctrExistsOpts)
+ if err != nil {
+ return false, err
}
- return report[0].ExitCode, report[0].Error
+ return ctrExistRep.Value, nil
}
diff --git a/pkg/bindings/containers/types.go b/pkg/bindings/containers/types.go
index 771cde72c..4889b444a 100644
--- a/pkg/bindings/containers/types.go
+++ b/pkg/bindings/containers/types.go
@@ -176,7 +176,7 @@ type UnpauseOptions struct{}
//go:generate go run ../generator/generator.go WaitOptions
// WaitOptions are optional options for waiting on containers
type WaitOptions struct {
- Condition *define.ContainerStatus
+ Condition []define.ContainerStatus
Interval *string
}
diff --git a/pkg/bindings/containers/types_wait_options.go b/pkg/bindings/containers/types_wait_options.go
index 005cc38cb..a3f1e3b8c 100644
--- a/pkg/bindings/containers/types_wait_options.go
+++ b/pkg/bindings/containers/types_wait_options.go
@@ -76,19 +76,19 @@ func (o *WaitOptions) ToParams() (url.Values, error) {
}
// WithCondition
-func (o *WaitOptions) WithCondition(value define.ContainerStatus) *WaitOptions {
- v := &value
+func (o *WaitOptions) WithCondition(value []define.ContainerStatus) *WaitOptions {
+ v := value
o.Condition = v
return o
}
// GetCondition
-func (o *WaitOptions) GetCondition() define.ContainerStatus {
- var condition define.ContainerStatus
+func (o *WaitOptions) GetCondition() []define.ContainerStatus {
+ var condition []define.ContainerStatus
if o.Condition == nil {
return condition
}
- return *o.Condition
+ return o.Condition
}
// WithInterval
diff --git a/pkg/bindings/test/attach_test.go b/pkg/bindings/test/attach_test.go
index 9a46f6309..771b2d528 100644
--- a/pkg/bindings/test/attach_test.go
+++ b/pkg/bindings/test/attach_test.go
@@ -75,7 +75,7 @@ var _ = Describe("Podman containers attach", func() {
Expect(err).ShouldNot(HaveOccurred())
wait := define.ContainerStateRunning
- _, err = containers.Wait(bt.conn, ctnr.ID, new(containers.WaitOptions).WithCondition(wait))
+ _, err = containers.Wait(bt.conn, ctnr.ID, new(containers.WaitOptions).WithCondition([]define.ContainerStatus{wait}))
Expect(err).ShouldNot(HaveOccurred())
tickTock := time.NewTimer(2 * time.Second)
diff --git a/pkg/bindings/test/common_test.go b/pkg/bindings/test/common_test.go
index c2b1347d2..8fbc631d8 100644
--- a/pkg/bindings/test/common_test.go
+++ b/pkg/bindings/test/common_test.go
@@ -207,7 +207,7 @@ func (b *bindingTest) RunTopContainer(containerName *string, insidePod *bool, po
return "", err
}
wait := define.ContainerStateRunning
- _, err = containers.Wait(b.conn, ctr.ID, new(containers.WaitOptions).WithCondition(wait))
+ _, err = containers.Wait(b.conn, ctr.ID, new(containers.WaitOptions).WithCondition([]define.ContainerStatus{wait}))
return ctr.ID, err
}
diff --git a/pkg/bindings/test/containers_test.go b/pkg/bindings/test/containers_test.go
index 9b9f98047..14eb1ffc6 100644
--- a/pkg/bindings/test/containers_test.go
+++ b/pkg/bindings/test/containers_test.go
@@ -281,7 +281,7 @@ var _ = Describe("Podman containers ", func() {
_, err := bt.RunTopContainer(&name, nil, nil)
Expect(err).To(BeNil())
go func() {
- exitCode, err = containers.Wait(bt.conn, name, new(containers.WaitOptions).WithCondition(pause))
+ exitCode, err = containers.Wait(bt.conn, name, new(containers.WaitOptions).WithCondition([]define.ContainerStatus{pause}))
errChan <- err
close(errChan)
}()
@@ -295,7 +295,7 @@ var _ = Describe("Podman containers ", func() {
go func() {
defer GinkgoRecover()
- _, waitErr := containers.Wait(bt.conn, name, new(containers.WaitOptions).WithCondition(running))
+ _, waitErr := containers.Wait(bt.conn, name, new(containers.WaitOptions).WithCondition([]define.ContainerStatus{running}))
unpauseErrChan <- waitErr
close(unpauseErrChan)
}()
diff --git a/pkg/domain/entities/containers.go b/pkg/domain/entities/containers.go
index 63be5578f..2d50d6826 100644
--- a/pkg/domain/entities/containers.go
+++ b/pkg/domain/entities/containers.go
@@ -51,7 +51,7 @@ type ContainerRunlabelReport struct {
}
type WaitOptions struct {
- Condition define.ContainerStatus
+ 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 d0599a595..7a672d863 100644
--- a/pkg/domain/infra/abi/containers.go
+++ b/pkg/domain/infra/abi/containers.go
@@ -100,7 +100,7 @@ func (ic *ContainerEngine) ContainerWait(ctx context.Context, namesOrIds []strin
responses := make([]entities.WaitReport, 0, len(ctrs))
for _, c := range ctrs {
response := entities.WaitReport{Id: c.ID()}
- exitCode, err := c.WaitForConditionWithInterval(options.Interval, options.Condition)
+ exitCode, err := c.WaitForConditionWithInterval(ctx, options.Interval, options.Condition...)
if err != nil {
response.Error = err
} else {
@@ -728,7 +728,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
return reports, errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
- if ecode, err := ctr.Wait(); err != nil {
+ if ecode, err := ctr.Wait(ctx); err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
// Check events
event, err := ic.Libpod.GetLastContainerEvent(ctx, ctr.ID(), events.Exited)
@@ -867,7 +867,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
return &report, err
}
- if ecode, err := ctr.Wait(); err != nil {
+ if ecode, err := ctr.Wait(ctx); err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr {
// Check events
event, err := ic.Libpod.GetLastContainerEvent(ctx, ctr.ID(), events.Exited)