From a9d20916c3d1f5e3c3ddada79af19aee4ec64629 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 14 Apr 2020 21:05:49 +0200 Subject: [PATCH] swarm: fix API version detection While the docker cli may be sending a "version" header, this header is not part of the API, or at least should not determin what API version is used. This code was added in c0afd9c873183604e57282e1cf47605c1f1e4d43, to adjust the handling of requests when an older version of the API was used, but because the code relied on the "version" header set by the CLI, it didn't work with other clients (e.g. when using cURL to make an API request). Signed-off-by: Sebastiaan van Stijn --- api/server/router/swarm/cluster_routes.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/api/server/router/swarm/cluster_routes.go b/api/server/router/swarm/cluster_routes.go index 2be44518d4..6ab097b4fd 100644 --- a/api/server/router/swarm/cluster_routes.go +++ b/api/server/router/swarm/cluster_routes.go @@ -225,15 +225,13 @@ func (sr *swarmRouter) createService(ctx context.Context, w http.ResponseWriter, // Get returns "" if the header does not exist encodedAuth := r.Header.Get("X-Registry-Auth") - cliVersion := r.Header.Get("version") queryRegistry := false - if cliVersion != "" { - if versions.LessThan(cliVersion, "1.30") { + if v := httputils.VersionFromContext(ctx); v != "" { + if versions.LessThan(v, "1.30") { queryRegistry = true } - adjustForAPIVersion(cliVersion, &service) + adjustForAPIVersion(v, &service) } - resp, err := sr.backend.CreateService(service, encodedAuth, queryRegistry) if err != nil { logrus.Errorf("Error creating service %s: %v", service.Name, err) @@ -265,13 +263,12 @@ func (sr *swarmRouter) updateService(ctx context.Context, w http.ResponseWriter, flags.EncodedRegistryAuth = r.Header.Get("X-Registry-Auth") flags.RegistryAuthFrom = r.URL.Query().Get("registryAuthFrom") flags.Rollback = r.URL.Query().Get("rollback") - cliVersion := r.Header.Get("version") queryRegistry := false - if cliVersion != "" { - if versions.LessThan(cliVersion, "1.30") { + if v := httputils.VersionFromContext(ctx); v != "" { + if versions.LessThan(v, "1.30") { queryRegistry = true } - adjustForAPIVersion(cliVersion, &service) + adjustForAPIVersion(v, &service) } resp, err := sr.backend.UpdateService(vars["id"], version, service, flags, queryRegistry)