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

Improve swagger.yaml to match the 1.41 api version #42621

Merged
merged 6 commits into from
Aug 21, 2021

Conversation

gesellix
Copy link
Contributor

I recently tried to rely on the swagger.yaml for code generation of an api client. Some parts required manual fixes of the swagger docs to match the current api version 1.41.

All changes described below relate to a dedicated commit, because they aim at different parts of the api description and aren't releated per se. If you'd prefer to merge them all into a single commit, please leave a note.

- What I did

  1. Add RestartPolicy "no" to swagger docs
  2. Add "changes" query parameter for /image/create to swagger docs
  3. Fix ContainerSummary swagger docs (flattened)
  4. Use explicit object names for improved swagger based code generation (otherwise generic names had been generated)
  5. Fix swagger docs to match the opencontainers image-spec

- How I did it

Used the 1.41 swagger.yaml with swagger's codegen to generate Java code. Used that code to perform requests to a 1.41 Docker engine. Applied some fixes and hack/validate/swagger and make swagger-docs to validate the changes.

- How to verify it

Compare the actual 1.41 api with the swagger.yaml.

- Description for the changelog

Update the swagger.yaml to match the version 1.41 api.

- A picture of a cute animal (not mandatory but encouraged)

062321_JC_mountain-cat_feat

@gesellix
Copy link
Contributor Author

Looks like I failed to actually validate the yaml file (the validate job fails):

[2021-07-11T21:19:57.490Z] The swagger spec at "api/swagger.yaml" is invalid against swagger specification 2.0. see errors :

[2021-07-11T21:19:57.490Z] - /containers/json in body must be of type object: "array"

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

thanks! Changes look ok; I left a comment on how to fix the failing validation; if you can amend that commit to fix it

Comment on lines -4027 to -4026
type: "array"
items:
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this definition is referenced in two places in the swagger;

  • /system/df -> responses.200.Containers which uses it as items in an array; so the swagger incorrectly defined it as an "array of arrays"
  • /containers/json -> responses -> 200 which used it directly, so an "array of objects"

I think your change makes sense; i.e. change ContainerSummary to an object (not an array of objects), so to make swagger validation pass, the /containers/json response needs to be updated.

If you amend the third commit ("Fix ContainerSummary swagger docs ") with the patch below, swagger validation should pass:

diff --git a/api/swagger.yaml b/api/swagger.yaml
index fa71b38f09..efa2b81587 100644
--- a/api/swagger.yaml
+++ b/api/swagger.yaml
@@ -5261,7 +5261,9 @@ paths:
         200:
           description: "no error"
           schema:
-            $ref: "#/definitions/ContainerSummary"
+            type: "array"
+            items:
+              $ref: "#/definitions/ContainerSummary"
           examples:
             application/json:
               - Id: "8dfafdbc3a40"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your suggestion, looks perfect. I just applied your patch, let's see what the CI will find :)

gesellix added a commit to docker-client/docker-engine that referenced this pull request Jul 12, 2021
@gesellix
Copy link
Contributor Author

Now the validation step reports a diff like this:

diff --git a/api/types/container/container_wait.go b/api/types/container/container_wait.go
index 49e05ae669..c2e69b5b25 100644
--- a/api/types/container/container_wait.go
+++ b/api/types/container/container_wait.go
@@ -6,14 +6,6 @@ package container // import "github.com/docker/docker/api/types/container"
 // See hack/generate-swagger-api.sh
 // ----------------------------------------------------------------------------
 
-// ContainerWaitOKBodyError container waiting error, if any
-// swagger:model ContainerWaitOKBodyError
-type ContainerWaitOKBodyError struct {
-
-	// Details of an error
-	Message string `json:"Message,omitempty"`
-}
-
 // ContainerWaitOKBody OK response to ContainerWait operation
 // swagger:model ContainerWaitOKBody
 type ContainerWaitOKBody struct {
@@ -26,3 +18,11 @@ type ContainerWaitOKBody struct {
 	// Required: true
 	StatusCode int64 `json:"StatusCode"`
 }
+
+// ContainerWaitOKBodyError container waiting error, if any
+// swagger:model ContainerWaitOKBodyError
+type ContainerWaitOKBodyError struct {
+
+	// Details of an error
+	Message string `json:"Message,omitempty"`
+}

This one looks like the struct has only moved down, but its content isn't changed. I added another commit to apply the codegen's changes.

@gesellix
Copy link
Contributor Author

Will the CI retry automatically on errors like this?

ERROR: Get https://download.opensuse.org/repositories/devel:/tools:/criu/Debian_10/Release.key: net/http: TLS handshake timeout

@thaJeztah
Copy link
Member

This one looks like the struct has only moved down, but its content isn't changed. I added another commit to apply the codegen's changes.

Ah, dang, sorry, I should've mentioned.

That's flakiness in the code generator. For <reasons> (#42593), we're using an older version of go-swagger to generate the types. Unfortunately, that version uses a map[string]something internally, which (because go randomises map order) causes the order of some of those to be "random".

We know about that failure, so will just trigger CI again if it fails because of that (but should try to get rid of the old go-swagger version)

😓 could you remove the last commit again? 🤗

Will the CI retry automatically on errors like this?

Hmmm.. not it doesn't automatically; I noticed the same error earlier today on another PR (but restarting fixed it). Looks like there may be an issue with their package repository (or an issue with some of our CI nodes).

@gesellix
Copy link
Contributor Author

Ah alright, yes, I've removed the last commit 😌

@gesellix
Copy link
Contributor Author

ERROR: Get https://download.opensuse.org/repositories/devel:/tools:/criu/Debian_10/Release.key: net/http: TLS handshake timeout happened again, maybe we should give OpenSuse some time to stabilize their servers.

@thaJeztah
Copy link
Member

Ah alright, yes, I've removed the last commit 😌

Thank you! Sorry for that.

ERROR: Get https://download.opensuse.org/repositories/devel:/tools:/criu/Debian_10/Release.key: net/http: TLS handshake timeout happened again, maybe we should give OpenSuse some time to stabilize their servers.

Yes. I see it succeeded on some machines, but failed on other machines.

Looking at the docker info output for a "succesful" and "failing" machine, both run the same version of docker and same Ubuntu version, so from that it doesn't appear to be something on our side.

[2021-07-12T14:38:31.437Z]  Server Version: 20.10.6
...
[2021-07-12T14:37:43.053Z]  Kernel Version: 5.4.0-1048-aws
[2021-07-12T14:37:43.053Z]  Operating System: Ubuntu 18.04.5 LTS
[2021-07-12T14:38:31.437Z]  Server Version: 20.10.6
...
[2021-07-12T14:38:31.437Z]  Kernel Version: 5.4.0-1048-aws
[2021-07-12T14:38:31.437Z]  Operating System: Ubuntu 18.04.5 LTS

Never say never of course; let me delete the failing Jenkins node (in case it's something on a specific machine), just to be sure.

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

to other reviewers; use ?w=1 to view the diff without the whitespace changes; https://github.com/moby/moby/pull/42621/files?w=1

@thaJeztah
Copy link
Member

@AkihiroSuda @rvolosatovs ptal

Copy link
Contributor

@rvolosatovs rvolosatovs left a comment

Choose a reason for hiding this comment

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

LGTM

@gesellix
Copy link
Contributor Author

gesellix commented Jul 18, 2021

Since I'm still working on an api client with model classes generated from the swagger.yaml, I could add more improvements similar to the forth commit (naming inlined objects so that generated code is more expressive). I can add the additional changes here or would you prefer me to create another pull request?

I also filed an issue to discuss another topic (nil objects and slices, marshalled as JSON). Maybe you can give me some feedback at #42632 so that I can have some kind of direction. This is also related to my efforts of improving the swagger.yaml - or the runtime api.

Edit: I added another commit ff35e40 with explicit type names and a consolidated PluginPrivilegeItem. I assume that the commit should be merged with 1e4704a, but I'll keep it until you had a chance for feedback/review.

api/swagger.yaml Outdated
the plugin.
type: "object"
x-nullable: false
required: [Name, Value]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope adding the x-nullable: false and the required contrains don't break anything - or do they?

Copy link
Member

Choose a reason for hiding this comment

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

I think x-nullable: false would be the default, or is it not? If so, I guess it can be removed 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to leave it for a separate PR? That could (a bit) simplify the review at least

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the change and will create another PR.

@gesellix
Copy link
Contributor Author

@thaJeztah you certainly have more important tasks to do, yet I wanted to ask if you could give some rough feedback on the recent changes. I'd like to know if this is a good direction or if there's anything to fix. The CI run doesn't appear to be related, but you might have more insights.

Like mentioned above: I'm working on a clean code generated api model, and the changes in this pull request are only the easy ones, while #42632 is probably more challenging due to a changed api (maybe). I would be glad if such kind of cleanup of the swagger docs and the api itself could be inlcuded in the upcoming v1.42 :)

@gesellix
Copy link
Contributor Author

gesellix commented Aug 7, 2021

ping?

@gesellix
Copy link
Contributor Author

@thaJeztah @AkihiroSuda @rvolosatovs would anyone like to have a look at the recent commit? I'd like to cleanup the pull request, so that it could get a final review.

Copy link
Contributor

@rvolosatovs rvolosatovs left a comment

Choose a reason for hiding this comment

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

Overall LGTM

@@ -2190,6 +2190,26 @@ definitions:
type: "string"
x-nullable: false

PluginPrivilegeItem:
Copy link
Contributor

Choose a reason for hiding this comment

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

Extraction of this type definition should probably better be done in a separate commit

api/swagger.yaml Outdated
@@ -7507,6 +7507,12 @@ paths:
Refer to the [authentication section](#section/Authentication) for
details.
type: "string"
- name: "changes"
in: "query"
description: "Apply Dockerfile instruction to the created image."
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not very clear to me what this does given the description

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one was taken from the CLI docs (see https://github.com/docker/cli/blob/c758c3e4a5a980cf0ea3292c958fd537822ba0d5/docs/reference/commandline/import.md#description), trying to keep some consistency. Yet, the cli docs are more specific regarding the supported Dockerfile commands.

I changed the description to be more detailed.

api/swagger.yaml Outdated
the plugin.
type: "object"
x-nullable: false
required: [Name, Value]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to leave it for a separate PR? That could (a bit) simplify the review at least

Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
Signed-off-by: Tobias Gesellchen <tobias@gesellix.de>
@gesellix
Copy link
Contributor Author

@rvolosatovs From my perspective this one is ready for a final review. Please leave a suggestion if the description for /images/create?changes=... is not clear enough, yet. Thanks!

@gesellix
Copy link
Contributor Author

The failing CI checks don't seem to be related to my changes, or do they?

@thaJeztah thaJeztah added this to the 21.xx milestone Aug 21, 2021
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

arf, I see I was writing a review, but didn't submit it.

Changes LGTM, thanks!

I see some of my comments are now outdated, and I was working on some other changes, so let me get this one merged.

api/swagger.yaml Outdated
the plugin.
type: "object"
x-nullable: false
required: [Name, Value]
Copy link
Member

Choose a reason for hiding this comment

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

I think x-nullable: false would be the default, or is it not? If so, I guess it can be removed 🤔

api/swagger.yaml Show resolved Hide resolved
type: "array"
items:
type: "string"
Variant:
variant:
type: "string"
Features:
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this one can be removed now.

I was working on some other changes, and can include that in a follow-up

@thaJeztah thaJeztah merged commit 9bc0c49 into moby:master Aug 21, 2021
@gesellix gesellix deleted the swagger-wip branch August 22, 2021 09:17
@gesellix
Copy link
Contributor Author

Thanks for merging! I'm still not finished with the overall improvement for the swagger.yaml, but the upcoming changes might be harder/riskier than the ones in this PR.

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.

None yet

3 participants