Commit Graph

23 Commits

Author SHA1 Message Date
Michael Smithhisler
06704ef904 client: use stop function to deregister context.AfterFunc
Signed-off-by: Michael Smithhisler <smithhisler.mike@gmail.com>
2026-01-05 08:24:48 -05:00
Sebastiaan van Stijn
bcc1db1ce5 client: WithAPIVersion, WithAPIVersionFromEnv: validate well-formedness
Make these options more strict to not allow arbitrary values. Historically,
the `DOCKER_API_VERSION` env-var did not perform any validation as it was
intended for testing-purposes, but given the wider use of this env-var,
we should perform some amount of validation.

Both `WithAPIVersion` and `WithAPIVersionFromEnv` still allow specifying
API versions that are not supported by the client for testing purposes
(e.g. to use API versions beyond `MinAPIVersion` and `MaxAPIVersion`),
but must be well-formed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-14 13:54:31 +01:00
Sebastiaan van Stijn
d11232bad9 client: add newCancelReadCloser utility
Small utility to automatically close an io.ReadCloser when the
context is cancelled, but allowing the caller to close manually
as well.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-05 23:17:14 +01:00
Sebastiaan van Stijn
58356450fa client: remove redundant closing and draining of response
For methods using the decodeWithRaw utility, we were handling closing
of the body twice. The ensureReaderClosed utility also drains the
response to let the transport reuse the connnection. Let's use that
utility in decodeWithRaw as well.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-24 19:30:22 +02:00
Sebastiaan van Stijn
ddbb503dc7 client: change Raw fields to be json.RawMessage
These fields store the raw JSON data that we received, and should
never container bytes that are non-JSON (as we'd error out when
failing to unmarshal).

Change the type to a json.RawMessage, which:

- Is more explicit on intent
- Can still be used as a regular []byte in all cases

And, while it's not expected to be marshaled to JSON, doing so will also
print the output in a readable format instead of base64 encoding;

    package main

    import (
        "encoding/json"
        "fmt"
    )

    func main() {
        foo := struct {
            Bytes []byte
            Raw   json.RawMessage
        }{
            Bytes: []byte(`{"hello": "world"}`),
            Raw:   json.RawMessage(`{"hello": "world"}`),
        }

        out, _ := json.MarshalIndent(foo, "", "  ")
        fmt.Println(string(out))
    }

Will print:

    {
      "Bytes": "eyJoZWxsbyI6ICJ3b3JsZCJ9",
      "Raw": {
        "hello": "world"
      }
    }

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-24 19:07:26 +02:00
Paweł Gronowski
abf5679049 client/inspect: Better Raw handling
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-10-20 22:29:36 +02:00
Cory Snider
7ea066c8d1 client: add Filters type
Add a new type to use for building filter predicates for API requests,
replacing "./api/types/filters".Args in the client. Remove the now
unused api/types/filters package.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-10-08 12:06:31 -04:00
Sebastiaan van Stijn
839e46f97c client: remove support for API < v1.22 filter format
The format for filters changed in 93d1dd8036
(docker v1.10 / API v1.22). As part of that implementation, the daemon
would parse the new format, and fall back to parsing the old format if
this failed. This fallback was not based on API version, so any version
of the API released since would continue to accept both the legacy and
curent format.

For the client, the change in format caused a regression when connecting
to an older daemon; a `ToParamWithVersion` utility was introduced in
[docker/engine-api@81388f0] to produce the old format when the client was
connected to a docker v1.9 or older daemon, using an old API version.

Given that any version of docker 1.10 or above would support both formats,
regardless of the API version used, and API v1.22 is no longer supported,
it should be safe to assume we can drop the version-specific format in the
client. Even if the client would be using API v1.22 (or older), the format
would only be necessary for an actual docker v1.9 daemon, which would be
very unlikely, and a daemon that's 9 Years old.

[docker/engine-api@81388f0]: 81388f00dd

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-15 21:27:58 +02:00
Sebastiaan van Stijn
6c7e2909c2 api/types/filters: remove deprecated ToParamWithVersion
It's only used by the client to support API versions older than v1.22.
Make it an internal utility that doesn't depend on internal fields of
`filter.Args`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-07-29 13:04:39 +02:00
Derek McGowan
afd6487b2e Create github.com/moby/moby/api module
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-07-21 09:30:05 -07:00
Sebastiaan van Stijn
fcf3ff1b2f client: remove getDockerOS utility in favor of "Ostype" header
This utility was added in 83b5729f64 to
replace httputils.ParseServerHeader, which was added to print a warning
on Windows in 126529c6d0. At the time, the
only available option to detect the daemon's OS was to parse the `Server`
header, which contained the version of Docker as well as the OS.

However, 7199522ea2 introduced an `OSType`
("Ostype") header that's included on all responses, and a later commit
e9dac5ef5e changed that to also be included
when producing an error for unsupported API versions.

Note that the casing in the midddleware was changed from `OSType` to
`Ostype` (normalized form) in 76a5ca1d4d,
but headers are case-insensitive, and `header.Get()` should handle either
case in the response.

In short; every API response contains an "Ostype" header, which already
contains the OS ("windows" or "linux") that doesn't require any parsing,
so let's put that header to use.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-27 18:14:04 +02:00
Matthieu MOREL
bc9ec5fc02 fix emptyStringTest from go-critic
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-06-07 09:57:59 +02:00
Sebastiaan van Stijn
4856e8ffad client: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:10 +02:00
Paweł Gronowski
6bde39b729 client/utils: use containerd errdefs checks
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-05-19 20:37:39 +02:00
Sebastiaan van Stijn
329b2a26f3 client: normalize and validate empty ID / name arguments to fail early
In situations where an empty ID was passed, the client would construct an
invalid API endpoint URL, which either resulted in the "not found" handler
being hit (resulting in a "page not found" error), or even the wrong endpoint
being hit if the client follows redirects.

For example, `/containers/<empty id>/json` (inspect) redirects to `/containers/json`
(docker ps))

Given that empty IDs should never be expected (especially if they're part of
the API URL path), we can validate these and return early.

Its worth noting that a few methods already had an error in place; those
methods were related to the situation mentioned above, where (e.g.) an
"inspect" would redirect to a "list" endpoint. The existing errors, for
convenience, mimicked a "not found" error; this patch changes such errors
to an "Invalid Parameter" instead, which is more correct, but it could be
a breaking change for some edge cases where users parsed the output;

    git grep 'objectNotFoundError{'
    client/config_inspect.go:        return swarm.Config{}, nil, objectNotFoundError{object: "config", id: id}
    client/container_inspect.go:     return container.InspectResponse{}, nil, objectNotFoundError{object: "container", id: containerID}
    client/container_inspect.go:     return container.InspectResponse{}, objectNotFoundError{object: "container", id: containerID}
    client/distribution_inspect.go:  return distributionInspect, objectNotFoundError{object: "distribution", id: imageRef}
    client/image_inspect.go:         return image.InspectResponse{}, nil, objectNotFoundError{object: "image", id: imageID}
    client/network_inspect.go:       return network.Inspect{}, nil, objectNotFoundError{object: "network", id: networkID}
    client/node_inspect.go:          return swarm.Node{}, nil, objectNotFoundError{object: "node", id: nodeID}
    client/plugin_inspect.go:        return nil, nil, objectNotFoundError{object: "plugin", id: name}
    client/secret_inspect.go:        return swarm.Secret{}, nil, objectNotFoundError{object: "secret", id: id}
    client/service_inspect.go:       return swarm.Service{}, nil, objectNotFoundError{object: "service", id: serviceID}
    client/task_inspect.go:          return swarm.Task{}, nil, objectNotFoundError{object: "task", id: taskID}
    client/volume_inspect.go:        return volume.Volume{}, nil, objectNotFoundError{object: "volume", id: volumeID}

Two such errors are still left, as "ID or name" would probably be confusing,
but perhaps we can use a more generic error to include those as well (e.g.
"invalid <object> reference: value is empty");

    client/distribution_inspect.go:  return distributionInspect, objectNotFoundError{object: "distribution", id: imageRef}
    client/image_inspect.go:         return image.InspectResponse{}, nil, objectNotFoundError{object: "image", id: imageID}

Before this patch:

    docker container start ""
    Error response from daemon: page not found
    Error: failed to start containers:

    docker container start " "
    Error response from daemon: No such container:
    Error: failed to start containers:

With this patch:

    docker container start ""
    invalid container name or ID: value is empty
    Error: failed to start containers:

    docker container start " "
    invalid container name or ID: value is empty
    Error: failed to start containers:

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-03 11:21:51 +01:00
Sebastiaan van Stijn
d61a6924d7 client: use lazyregexp to compile regexes on first use
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-02 21:37:32 +01:00
avoidaway
694c01620b chore: fix some function names in comment
Signed-off-by: avoidaway <cmoman@126.com>
2024-12-09 21:05:29 +08:00
Sebastiaan van Stijn
96039276b6 client: add utilities to encode platforms
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-10-31 20:40:27 +01:00
Daniel Nephin
4f0d95fa6e Add canonical import comment
Signed-off-by: Daniel Nephin <dnephin@docker.com>
2018-02-05 16:51:57 -05:00
Sebastiaan van Stijn
a4efe66cf2 Replace uses of filters.ToParam(), FromParam() with filters.ToJSON(), FromJSON()
`filters.ToParam()` and `filters.FromParam()` were deprecated in favor of
`filters.ToJSON()` and `filters.FromJSON()` in 065118390a,
but still used in various locations.

This patch replaces uses of `filters.ToParam()` and  `filters.FromParam()` with
`filters.ToJSON()` and `filters.FromJSON()`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-09-26 13:59:45 +02:00
Nishant Totla
c1635c1ae3 Moving docker service digest pinning to client side
Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
2017-05-15 16:42:43 -07:00
Yong Tang
a6be56b54e Convert DanglingOnly to Filters for docker image prune
This fix convert DanglingOnly in ImagesPruneConfig to Filters,
so that it is possible to maintain API compatibility in the future.

Several integration tests have been added to cover changes.

This fix is related to 28497.

A follow up to this PR will be done once this PR is merged.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
2016-12-02 20:08:49 -08:00
John Howard
d8dcbf3ec3 Tidy GetDockerOS() function
Signed-off-by: John Howard <jhoward@microsoft.com>
2016-11-09 14:46:53 -08:00