Skip to content

Conversation

@thaJeztah
Copy link
Member

Relates to docker/docker-py#2382 (comment)

errdefs: convert containerd errors to the correct status code

In situations where the containerd error is consumed directly
and not received over gRPC, errors were not translated.

Add regression tests for invalid platform status codes

Before we handled containerd errors, using an invalid platform produced a 500 status:

curl -v \
-X POST \
--unix-socket /var/run/docker.sock \
"http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \
-H "Content-Type: application/json"
* Connected to localhost (docker.sock) port 80 (#0)
> POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1
> Host: localhost:2375
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 500 Internal Server Error
< Api-Version: 1.40
< Content-Length: 85
< Content-Type: application/json
< Date: Mon, 15 Jul 2019 15:25:44 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/19.03.0-rc2 (linux)
<
{"message":"\"foobar\": unknown operating system or architecture: invalid argument"}

That problem is now fixed, and the API correctly returns a 4xx status:

curl -v \
-X POST \
--unix-socket /var/run/docker.sock \
"http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \
-H "Content-Type: application/json"
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1
> Host: localhost:2375
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 400 Bad Request
< Api-Version: 1.41
< Content-Type: application/json
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/dev (linux)
< Date: Mon, 15 Jul 2019 15:13:42 GMT
< Content-Length: 85
<
{"message":"\"foobar\": unknown operating system or architecture: invalid argument"}
* Curl_http_done: called premature == 0

This patch adds tests to validate the behaviour

- How to verify it

make \
  DOCKER_GRAPHDRIVER=vfs \
  TESTDIRS='github.com/docker/docker/integration/image' \
  TESTFLAGS='-test.run ^TestImagePullPlatformInvalid$' \
  binary test-integration


make \
  DOCKER_GRAPHDRIVER=vfs \
  TESTDIRS='github.com/docker/docker/integration/build' \
  TESTFLAGS='-test.run ^TestBuildPlatformInvalid$' \
  binary test-integration

- Description for the changelog

* Fix `POST /images/create` returning a 500 status code when providing an incorrect platform option
* Fix `POST /build` returning a 500 status code when providing an incorrect platform option

@thaJeztah
Copy link
Member Author

ping @cpuguy83 @dmcgowan PTAL

@thaJeztah thaJeztah force-pushed the pull_platform_regression branch from 2afc2bf to 0b7db1f Compare July 15, 2019 16:28
@thaJeztah thaJeztah requested a review from vdemeester July 15, 2019 16:30
@thaJeztah thaJeztah force-pushed the pull_platform_regression branch from 0b7db1f to 7f5bd0d Compare July 15, 2019 16:33
Copy link
Member

@cpuguy83 cpuguy83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah thaJeztah force-pushed the pull_platform_regression branch from 7f5bd0d to 7e54862 Compare July 15, 2019 18:36
@codecov
Copy link

codecov bot commented Jul 15, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@ffabf0d). Click here to learn what that means.
The diff coverage is 0%.

@@            Coverage Diff            @@
##             master   #39527   +/-   ##
=========================================
  Coverage          ?   37.29%           
=========================================
  Files             ?      609           
  Lines             ?    45253           
  Branches          ?        0           
=========================================
  Hits              ?    16879           
  Misses            ?    26083           
  Partials          ?     2291

In situations where the containerd error is consumed directly
and not received over gRPC, errors were not translated.

This patch converts containerd errors to the correct HTTP
status code.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Before we handled containerd errors, using an invalid platform produced a 500 status:

```bash
curl -v \
  -X POST \
  --unix-socket /var/run/docker.sock \
  "http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \
  -H "Content-Type: application/json"
```

```
* Connected to localhost (docker.sock) port 80 (#0)
> POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1
> Host: localhost:2375
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 500 Internal Server Error
< Api-Version: 1.40
< Content-Length: 85
< Content-Type: application/json
< Date: Mon, 15 Jul 2019 15:25:44 GMT
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/19.03.0-rc2 (linux)
<
{"message":"\"foobar\": unknown operating system or architecture: invalid argument"}
```

That problem is now fixed, and the API correctly returns a 4xx status:

```bash
curl -v \
  -X POST \
  --unix-socket /var/run/docker.sock \
  "http://localhost:2375/v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest" \
  -H "Content-Type: application/json"
```

```
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> POST /v1.40/images/create?fromImage=hello-world&platform=foobar&tag=latest HTTP/1.1
> Host: localhost:2375
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Type: application/json
>
< HTTP/1.1 400 Bad Request
< Api-Version: 1.41
< Content-Type: application/json
< Docker-Experimental: true
< Ostype: linux
< Server: Docker/dev (linux)
< Date: Mon, 15 Jul 2019 15:13:42 GMT
< Content-Length: 85
<
{"message":"\"foobar\": unknown operating system or architecture: invalid argument"}
* Curl_http_done: called premature == 0
```

This patch adds tests to validate the behaviour

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the pull_platform_regression branch from 7e54862 to 9d1b4f5 Compare July 15, 2019 18:37
Copy link
Member

@dmcgowan dmcgowan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


// statusCodeFromContainerdError returns status code for containerd errors when
// consumed directly (not through gRPC)
func statusCodeFromContainerdError(err error) int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With containerd errors they only need to be unwrapped once with errors.Cause then a normal switch could be used. This is fine though for now, hopefully we can make the techniques more consistent so they can all be checked together in the future (for example maybe docker's version of the Is* functions can just also check containerd errors there too).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should look at integrating them (if they all can be mapped). As to unwrapping; it's possible (although not the case currently) that errors are wrapped multiple times, so I guess recursively unwrapping is still ok to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants