From a85e5ed4c9db0f5765e9ee0562b4ef39ff62ec87 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Tue, 16 Feb 2021 04:14:35 +0300 Subject: [PATCH] Work around containers/podman#9351 --- .../containerbackend/containerbackend.go | 5 ++++ .../instance/containerbackend/docker.go | 1 + .../instance/containerbackend/podman_linux.go | 27 ++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/executor/instance/containerbackend/containerbackend.go b/internal/executor/instance/containerbackend/containerbackend.go index 86ac3e28..a1a45bd8 100644 --- a/internal/executor/instance/containerbackend/containerbackend.go +++ b/internal/executor/instance/containerbackend/containerbackend.go @@ -86,10 +86,15 @@ type ContainerWaitResult struct { } type SystemInfo struct { + Version string TotalCPUs int64 TotalMemoryBytes int64 } +type Version struct { + Version string +} + const ( BackendAuto = "auto" BackendDocker = "docker" diff --git a/internal/executor/instance/containerbackend/docker.go b/internal/executor/instance/containerbackend/docker.go index e83d4647..0a430374 100644 --- a/internal/executor/instance/containerbackend/docker.go +++ b/internal/executor/instance/containerbackend/docker.go @@ -314,6 +314,7 @@ func (backend *Docker) SystemInfo(ctx context.Context) (*SystemInfo, error) { } return &SystemInfo{ + Version: info.ServerVersion, TotalCPUs: int64(info.NCPU), TotalMemoryBytes: info.MemTotal, }, nil diff --git a/internal/executor/instance/containerbackend/podman_linux.go b/internal/executor/instance/containerbackend/podman_linux.go index b5925689..ec7ecf13 100644 --- a/internal/executor/instance/containerbackend/podman_linux.go +++ b/internal/executor/instance/containerbackend/podman_linux.go @@ -31,6 +31,8 @@ type Podman struct { basePath string httpClient *http.Client cli *swagger.APIClient + + usingNumericalContainerState bool } func NewPodman() (ContainerBackend, error) { @@ -74,6 +76,15 @@ func NewPodman() (ContainerBackend, error) { HTTPClient: podman.httpClient, }) + // Query server's version and activate bug workarounds (if applicable) + version, err := podman.SystemInfo(context.Background()) + if err != nil { + return nil, err + } + if version.Version == "3.0.0" { + podman.usingNumericalContainerState = true + } + return podman, nil } @@ -416,9 +427,16 @@ func (backend *Podman) ContainerWait(ctx context.Context, id string) (<-chan Con errChan := make(chan error) go func() { + condition := "stopped" + + if backend.usingNumericalContainerState { + // https://github.com/containers/podman/blob/v3.0.0/libpod/define/containerstate.go#L22 + condition = "4" + } + // nolint:bodyclose // already closed by Swagger-generated code resp, _, err := backend.cli.ContainersApi.LibpodWaitContainer(ctx, id, &swagger.ContainersApiLibpodWaitContainerOpts{ - Condition: optional.NewString("stopped"), + Condition: optional.NewString(condition), }) if err != nil { @@ -524,7 +542,14 @@ func (backend *Podman) SystemInfo(ctx context.Context) (*SystemInfo, error) { return nil, err } + var version string + + if info.Version != nil { + version = info.Version.Version + } + return &SystemInfo{ + Version: version, TotalCPUs: info.Host.Cpus, TotalMemoryBytes: info.Host.MemTotal, }, nil