Skip to content

Commit 9e075f3

Browse files
authored
Merge pull request moby#47155 from thaJeztah/remove_deprecated_api_versions
api: remove deprecated API versions (API < v1.24)
2 parents d2f12e6 + 14503cc commit 9e075f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+201
-1446
lines changed

api/common.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ package api // import "github.com/docker/docker/api"
22

33
// Common constants for daemon and client.
44
const (
5-
// DefaultVersion of Current REST API
5+
// DefaultVersion of the current REST API.
66
DefaultVersion = "1.45"
77

8+
// MinSupportedAPIVersion is the minimum API version that can be supported
9+
// by the API server, specified as "major.minor". Note that the daemon
10+
// may be configured with a different minimum API version, as returned
11+
// in [github.com/docker/docker/api/types.Version.MinAPIVersion].
12+
//
13+
// API requests for API versions lower than the configured version produce
14+
// an error.
15+
MinSupportedAPIVersion = "1.24"
16+
817
// NoBaseImageSpecifier is the symbol used by the FROM
918
// command to specify that no base image is to be used.
1019
NoBaseImageSpecifier = "scratch"

api/server/errorhandler.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

api/server/httputils/decoder.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ import (
1212
// container configuration.
1313
type ContainerDecoder interface {
1414
DecodeConfig(src io.Reader) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error)
15-
DecodeHostConfig(src io.Reader) (*container.HostConfig, error)
1615
}

api/server/middleware/version.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,48 @@ import (
66
"net/http"
77
"runtime"
88

9+
"github.com/docker/docker/api"
910
"github.com/docker/docker/api/server/httputils"
1011
"github.com/docker/docker/api/types/versions"
1112
)
1213

1314
// VersionMiddleware is a middleware that
1415
// validates the client and server versions.
1516
type VersionMiddleware struct {
16-
serverVersion string
17-
defaultVersion string
18-
minVersion string
17+
serverVersion string
18+
19+
// defaultAPIVersion is the default API version provided by the API server,
20+
// specified as "major.minor". It is usually configured to the latest API
21+
// version [github.com/docker/docker/api.DefaultVersion].
22+
//
23+
// API requests for API versions greater than this version are rejected by
24+
// the server and produce a [versionUnsupportedError].
25+
defaultAPIVersion string
26+
27+
// minAPIVersion is the minimum API version provided by the API server,
28+
// specified as "major.minor".
29+
//
30+
// API requests for API versions lower than this version are rejected by
31+
// the server and produce a [versionUnsupportedError].
32+
minAPIVersion string
1933
}
2034

21-
// NewVersionMiddleware creates a new VersionMiddleware
22-
// with the default versions.
23-
func NewVersionMiddleware(s, d, m string) VersionMiddleware {
24-
return VersionMiddleware{
25-
serverVersion: s,
26-
defaultVersion: d,
27-
minVersion: m,
35+
// NewVersionMiddleware creates a VersionMiddleware with the given versions.
36+
func NewVersionMiddleware(serverVersion, defaultAPIVersion, minAPIVersion string) (*VersionMiddleware, error) {
37+
if versions.LessThan(defaultAPIVersion, api.MinSupportedAPIVersion) || versions.GreaterThan(defaultAPIVersion, api.DefaultVersion) {
38+
return nil, fmt.Errorf("invalid default API version (%s): must be between %s and %s", defaultAPIVersion, api.MinSupportedAPIVersion, api.DefaultVersion)
39+
}
40+
if versions.LessThan(minAPIVersion, api.MinSupportedAPIVersion) || versions.GreaterThan(minAPIVersion, api.DefaultVersion) {
41+
return nil, fmt.Errorf("invalid minimum API version (%s): must be between %s and %s", minAPIVersion, api.MinSupportedAPIVersion, api.DefaultVersion)
42+
}
43+
if versions.GreaterThan(minAPIVersion, defaultAPIVersion) {
44+
return nil, fmt.Errorf("invalid API version: the minimum API version (%s) is higher than the default version (%s)", minAPIVersion, defaultAPIVersion)
2845
}
46+
return &VersionMiddleware{
47+
serverVersion: serverVersion,
48+
defaultAPIVersion: defaultAPIVersion,
49+
minAPIVersion: minAPIVersion,
50+
}, nil
2951
}
3052

3153
type versionUnsupportedError struct {
@@ -45,18 +67,18 @@ func (e versionUnsupportedError) InvalidParameter() {}
4567
func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
4668
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
4769
w.Header().Set("Server", fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS))
48-
w.Header().Set("API-Version", v.defaultVersion)
70+
w.Header().Set("API-Version", v.defaultAPIVersion)
4971
w.Header().Set("OSType", runtime.GOOS)
5072

5173
apiVersion := vars["version"]
5274
if apiVersion == "" {
53-
apiVersion = v.defaultVersion
75+
apiVersion = v.defaultAPIVersion
5476
}
55-
if versions.LessThan(apiVersion, v.minVersion) {
56-
return versionUnsupportedError{version: apiVersion, minVersion: v.minVersion}
77+
if versions.LessThan(apiVersion, v.minAPIVersion) {
78+
return versionUnsupportedError{version: apiVersion, minVersion: v.minAPIVersion}
5779
}
58-
if versions.GreaterThan(apiVersion, v.defaultVersion) {
59-
return versionUnsupportedError{version: apiVersion, maxVersion: v.defaultVersion}
80+
if versions.GreaterThan(apiVersion, v.defaultAPIVersion) {
81+
return versionUnsupportedError{version: apiVersion, maxVersion: v.defaultAPIVersion}
6082
}
6183
ctx = context.WithValue(ctx, httputils.APIVersionKey{}, apiVersion)
6284
return handler(ctx, w, r, vars)

api/server/middleware/version_test.go

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,82 @@ package middleware // import "github.com/docker/docker/api/server/middleware"
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"net/http/httptest"
78
"runtime"
89
"testing"
910

11+
"github.com/docker/docker/api"
1012
"github.com/docker/docker/api/server/httputils"
1113
"gotest.tools/v3/assert"
1214
is "gotest.tools/v3/assert/cmp"
1315
)
1416

17+
func TestNewVersionMiddlewareValidation(t *testing.T) {
18+
tests := []struct {
19+
doc, defaultVersion, minVersion, expectedErr string
20+
}{
21+
{
22+
doc: "defaults",
23+
defaultVersion: api.DefaultVersion,
24+
minVersion: api.MinSupportedAPIVersion,
25+
},
26+
{
27+
doc: "invalid default lower than min",
28+
defaultVersion: api.MinSupportedAPIVersion,
29+
minVersion: api.DefaultVersion,
30+
expectedErr: fmt.Sprintf("invalid API version: the minimum API version (%s) is higher than the default version (%s)", api.DefaultVersion, api.MinSupportedAPIVersion),
31+
},
32+
{
33+
doc: "invalid default too low",
34+
defaultVersion: "0.1",
35+
minVersion: api.MinSupportedAPIVersion,
36+
expectedErr: fmt.Sprintf("invalid default API version (0.1): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
37+
},
38+
{
39+
doc: "invalid default too high",
40+
defaultVersion: "9999.9999",
41+
minVersion: api.DefaultVersion,
42+
expectedErr: fmt.Sprintf("invalid default API version (9999.9999): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
43+
},
44+
{
45+
doc: "invalid minimum too low",
46+
defaultVersion: api.MinSupportedAPIVersion,
47+
minVersion: "0.1",
48+
expectedErr: fmt.Sprintf("invalid minimum API version (0.1): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
49+
},
50+
{
51+
doc: "invalid minimum too high",
52+
defaultVersion: api.DefaultVersion,
53+
minVersion: "9999.9999",
54+
expectedErr: fmt.Sprintf("invalid minimum API version (9999.9999): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
55+
},
56+
}
57+
58+
for _, tc := range tests {
59+
tc := tc
60+
t.Run(tc.doc, func(t *testing.T) {
61+
_, err := NewVersionMiddleware("1.2.3", tc.defaultVersion, tc.minVersion)
62+
if tc.expectedErr == "" {
63+
assert.Check(t, err)
64+
} else {
65+
assert.Check(t, is.Error(err, tc.expectedErr))
66+
}
67+
})
68+
}
69+
}
70+
1571
func TestVersionMiddlewareVersion(t *testing.T) {
16-
defaultVersion := "1.10.0"
17-
minVersion := "1.2.0"
18-
expectedVersion := defaultVersion
72+
expectedVersion := "<not set>"
1973
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
2074
v := httputils.VersionFromContext(ctx)
2175
assert.Check(t, is.Equal(expectedVersion, v))
2276
return nil
2377
}
2478

25-
m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
79+
m, err := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
80+
assert.NilError(t, err)
2681
h := m.WrapHandler(handler)
2782

2883
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
@@ -35,19 +90,19 @@ func TestVersionMiddlewareVersion(t *testing.T) {
3590
errString string
3691
}{
3792
{
38-
expectedVersion: "1.10.0",
93+
expectedVersion: api.DefaultVersion,
3994
},
4095
{
41-
reqVersion: "1.9.0",
42-
expectedVersion: "1.9.0",
96+
reqVersion: api.MinSupportedAPIVersion,
97+
expectedVersion: api.MinSupportedAPIVersion,
4398
},
4499
{
45100
reqVersion: "0.1",
46-
errString: "client version 0.1 is too old. Minimum supported API version is 1.2.0, please upgrade your client to a newer version",
101+
errString: fmt.Sprintf("client version 0.1 is too old. Minimum supported API version is %s, please upgrade your client to a newer version", api.MinSupportedAPIVersion),
47102
},
48103
{
49104
reqVersion: "9999.9999",
50-
errString: "client version 9999.9999 is too new. Maximum supported API version is 1.10.0",
105+
errString: fmt.Sprintf("client version 9999.9999 is too new. Maximum supported API version is %s", api.DefaultVersion),
51106
},
52107
}
53108

@@ -71,22 +126,21 @@ func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
71126
return nil
72127
}
73128

74-
defaultVersion := "1.10.0"
75-
minVersion := "1.2.0"
76-
m := NewVersionMiddleware(defaultVersion, defaultVersion, minVersion)
129+
m, err := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
130+
assert.NilError(t, err)
77131
h := m.WrapHandler(handler)
78132

79133
req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
80134
resp := httptest.NewRecorder()
81135
ctx := context.Background()
82136

83137
vars := map[string]string{"version": "0.1"}
84-
err := h(ctx, resp, req, vars)
138+
err = h(ctx, resp, req, vars)
85139
assert.Check(t, is.ErrorContains(err, ""))
86140

87141
hdr := resp.Result().Header
88-
assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/"+defaultVersion))
142+
assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/1.2.3"))
89143
assert.Check(t, is.Contains(hdr.Get("Server"), runtime.GOOS))
90-
assert.Check(t, is.Equal(hdr.Get("API-Version"), defaultVersion))
144+
assert.Check(t, is.Equal(hdr.Get("API-Version"), api.DefaultVersion))
91145
assert.Check(t, is.Equal(hdr.Get("OSType"), runtime.GOOS))
92146
}

api/server/router/build/build_routes.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
4242
SuppressOutput: httputils.BoolValue(r, "q"),
4343
NoCache: httputils.BoolValue(r, "nocache"),
4444
ForceRemove: httputils.BoolValue(r, "forcerm"),
45+
PullParent: httputils.BoolValue(r, "pull"),
4546
MemorySwap: httputils.Int64ValueOrZero(r, "memswap"),
4647
Memory: httputils.Int64ValueOrZero(r, "memory"),
4748
CPUShares: httputils.Int64ValueOrZero(r, "cpushares"),
@@ -66,17 +67,14 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
6667
return nil, invalidParam{errors.New("security options are not supported on " + runtime.GOOS)}
6768
}
6869

69-
version := httputils.VersionFromContext(ctx)
70-
if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
70+
if httputils.BoolValue(r, "forcerm") {
7171
options.Remove = true
72-
} else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
72+
} else if r.FormValue("rm") == "" {
7373
options.Remove = true
7474
} else {
7575
options.Remove = httputils.BoolValue(r, "rm")
7676
}
77-
if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
78-
options.PullParent = true
79-
}
77+
version := httputils.VersionFromContext(ctx)
8078
if versions.GreaterThanOrEqualTo(version, "1.32") {
8179
options.Platform = r.FormValue("platform")
8280
}

api/server/router/container/backend.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type execBackend interface {
2424
// copyBackend includes functions to implement to provide container copy functionality.
2525
type copyBackend interface {
2626
ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error)
27-
ContainerCopy(name string, res string) (io.ReadCloser, error)
2827
ContainerExport(ctx context.Context, name string, out io.Writer) error
2928
ContainerExtractToDir(name, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error
3029
ContainerStatPath(name string, path string) (stat *types.ContainerPathStat, err error)
@@ -39,7 +38,7 @@ type stateBackend interface {
3938
ContainerResize(name string, height, width int) error
4039
ContainerRestart(ctx context.Context, name string, options container.StopOptions) error
4140
ContainerRm(name string, config *backend.ContainerRmConfig) error
42-
ContainerStart(ctx context.Context, name string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
41+
ContainerStart(ctx context.Context, name string, checkpoint string, checkpointDir string) error
4342
ContainerStop(ctx context.Context, name string, options container.StopOptions) error
4443
ContainerUnpause(name string) error
4544
ContainerUpdate(name string, hostConfig *container.HostConfig) (container.ContainerUpdateOKBody, error)

api/server/router/container/container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func (r *containerRouter) initRoutes() {
5656
router.NewPostRoute("/containers/{name:.*}/wait", r.postContainersWait),
5757
router.NewPostRoute("/containers/{name:.*}/resize", r.postContainersResize),
5858
router.NewPostRoute("/containers/{name:.*}/attach", r.postContainersAttach),
59-
router.NewPostRoute("/containers/{name:.*}/copy", r.postContainersCopy), // Deprecated since 1.8 (API v1.20), errors out since 1.12 (API v1.24)
6059
router.NewPostRoute("/containers/{name:.*}/exec", r.postContainerExecCreate),
6160
router.NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
6261
router.NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),

0 commit comments

Comments
 (0)