diff --git a/api/types/swarm/service.go b/api/types/swarm/service.go index b5a6b4f89a..0b678dea33 100644 --- a/api/types/swarm/service.go +++ b/api/types/swarm/service.go @@ -106,18 +106,27 @@ type ReplicatedJob struct { // This type is deliberately empty. type GlobalJob struct{} +// FailureAction is the action to perform when updating a service fails. +type FailureAction string + const ( // UpdateFailureActionPause PAUSE - UpdateFailureActionPause = "pause" + UpdateFailureActionPause FailureAction = "pause" // UpdateFailureActionContinue CONTINUE - UpdateFailureActionContinue = "continue" + UpdateFailureActionContinue FailureAction = "continue" // UpdateFailureActionRollback ROLLBACK - UpdateFailureActionRollback = "rollback" + UpdateFailureActionRollback FailureAction = "rollback" +) +// UpdateOrder is the order of operations when rolling out or rolling back +// an updated tasks for a service. +type UpdateOrder string + +const ( // UpdateOrderStopFirst STOP_FIRST - UpdateOrderStopFirst = "stop-first" + UpdateOrderStopFirst UpdateOrder = "stop-first" // UpdateOrderStartFirst START_FIRST - UpdateOrderStartFirst = "start-first" + UpdateOrderStartFirst UpdateOrder = "start-first" ) // UpdateConfig represents the update configuration. @@ -130,7 +139,7 @@ type UpdateConfig struct { Delay time.Duration `json:",omitempty"` // FailureAction is the action to take when an update failures. - FailureAction string `json:",omitempty"` + FailureAction FailureAction `json:",omitempty"` // Monitor indicates how long to monitor a task for failure after it is // created. If the task fails by ending up in one of the states @@ -156,7 +165,7 @@ type UpdateConfig struct { // Order indicates the order of operations when rolling out an updated // task. Either the old task is shut down before the new task is // started, or the new task is started before the old task is shut down. - Order string + Order UpdateOrder } // ServiceStatus represents the number of running tasks in a service and the @@ -198,8 +207,12 @@ type JobStatus struct { LastExecution time.Time `json:",omitempty"` } +// RegistryAuthSource defines options for the "registryAuthFrom" query parameter +// on service update. +type RegistryAuthSource string + // Values for RegistryAuthFrom in ServiceUpdateOptions const ( - RegistryAuthFromSpec = "spec" - RegistryAuthFromPreviousSpec = "previous-spec" + RegistryAuthFromSpec RegistryAuthSource = "spec" + RegistryAuthFromPreviousSpec RegistryAuthSource = "previous-spec" ) diff --git a/client/service_update.go b/client/service_update.go index 36061c9d16..9e6b52781e 100644 --- a/client/service_update.go +++ b/client/service_update.go @@ -28,7 +28,7 @@ type ServiceUpdateOptions struct { // RegistryAuthFrom specifies where to find the registry authorization // credentials if they are not given in EncodedRegistryAuth. Valid // values are "spec" and "previous-spec". - RegistryAuthFrom string + RegistryAuthFrom swarm.RegistryAuthSource // Rollback indicates whether a server-side rollback should be // performed. When this is set, the provided spec will be ignored. @@ -65,7 +65,7 @@ func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, options query := url.Values{} if options.RegistryAuthFrom != "" { - query.Set("registryAuthFrom", options.RegistryAuthFrom) + query.Set("registryAuthFrom", string(options.RegistryAuthFrom)) } if options.Rollback != "" { diff --git a/daemon/server/router/swarm/cluster_routes.go b/daemon/server/router/swarm/cluster_routes.go index 7b47198c99..e4ec2d9757 100644 --- a/daemon/server/router/swarm/cluster_routes.go +++ b/daemon/server/router/swarm/cluster_routes.go @@ -276,7 +276,14 @@ func (sr *swarmRouter) updateService(ctx context.Context, w http.ResponseWriter, // Get returns "" if the header does not exist flags.EncodedRegistryAuth = r.Header.Get(registry.AuthHeader) - flags.RegistryAuthFrom = r.URL.Query().Get("registryAuthFrom") + if v := r.URL.Query().Get("registryAuthFrom"); v != "" { + switch val := types.RegistryAuthSource(v); val { + case types.RegistryAuthFromSpec, types.RegistryAuthFromPreviousSpec: + flags.RegistryAuthFrom = val + default: + return errdefs.InvalidParameter(fmt.Errorf("invalid registryAuthFrom '%s'", v)) + } + } flags.Rollback = r.URL.Query().Get("rollback") queryRegistry := false if v := httputils.VersionFromContext(ctx); v != "" { diff --git a/daemon/server/swarmbackend/swarm.go b/daemon/server/swarmbackend/swarm.go index e18cbe27c4..7419da0162 100644 --- a/daemon/server/swarmbackend/swarm.go +++ b/daemon/server/swarmbackend/swarm.go @@ -1,6 +1,9 @@ package swarmbackend -import "github.com/moby/moby/v2/daemon/internal/filters" +import ( + "github.com/moby/moby/api/types/swarm" + "github.com/moby/moby/v2/daemon/internal/filters" +) type ConfigListOptions struct { Filters filters.Args @@ -34,7 +37,7 @@ type ServiceUpdateOptions struct { // RegistryAuthFrom specifies where to find the registry authorization // credentials if they are not given in EncodedRegistryAuth. Valid // values are "spec" and "previous-spec". - RegistryAuthFrom string + RegistryAuthFrom swarm.RegistryAuthSource // Rollback indicates whether a server-side rollback should be // performed. When this is set, the provided spec will be ignored. diff --git a/integration-cli/docker_api_swarm_test.go b/integration-cli/docker_api_swarm_test.go index d8243af167..0721935f22 100644 --- a/integration-cli/docker_api_swarm_test.go +++ b/integration-cli/docker_api_swarm_test.go @@ -678,7 +678,7 @@ func setInstances(replicas int) testdaemon.ServiceConstructor { } } -func setUpdateOrder(order string) testdaemon.ServiceConstructor { +func setUpdateOrder(order swarm.UpdateOrder) testdaemon.ServiceConstructor { return func(s *swarm.Service) { if s.Spec.UpdateConfig == nil { s.Spec.UpdateConfig = &swarm.UpdateConfig{} @@ -687,7 +687,7 @@ func setUpdateOrder(order string) testdaemon.ServiceConstructor { } } -func setRollbackOrder(order string) testdaemon.ServiceConstructor { +func setRollbackOrder(order swarm.UpdateOrder) testdaemon.ServiceConstructor { return func(s *swarm.Service) { if s.Spec.RollbackConfig == nil { s.Spec.RollbackConfig = &swarm.UpdateConfig{} @@ -705,7 +705,7 @@ func setImage(image string) testdaemon.ServiceConstructor { } } -func setFailureAction(failureAction string) testdaemon.ServiceConstructor { +func setFailureAction(failureAction swarm.FailureAction) testdaemon.ServiceConstructor { return func(s *swarm.Service) { s.Spec.UpdateConfig.FailureAction = failureAction } diff --git a/vendor/github.com/moby/moby/api/types/swarm/service.go b/vendor/github.com/moby/moby/api/types/swarm/service.go index b5a6b4f89a..0b678dea33 100644 --- a/vendor/github.com/moby/moby/api/types/swarm/service.go +++ b/vendor/github.com/moby/moby/api/types/swarm/service.go @@ -106,18 +106,27 @@ type ReplicatedJob struct { // This type is deliberately empty. type GlobalJob struct{} +// FailureAction is the action to perform when updating a service fails. +type FailureAction string + const ( // UpdateFailureActionPause PAUSE - UpdateFailureActionPause = "pause" + UpdateFailureActionPause FailureAction = "pause" // UpdateFailureActionContinue CONTINUE - UpdateFailureActionContinue = "continue" + UpdateFailureActionContinue FailureAction = "continue" // UpdateFailureActionRollback ROLLBACK - UpdateFailureActionRollback = "rollback" + UpdateFailureActionRollback FailureAction = "rollback" +) +// UpdateOrder is the order of operations when rolling out or rolling back +// an updated tasks for a service. +type UpdateOrder string + +const ( // UpdateOrderStopFirst STOP_FIRST - UpdateOrderStopFirst = "stop-first" + UpdateOrderStopFirst UpdateOrder = "stop-first" // UpdateOrderStartFirst START_FIRST - UpdateOrderStartFirst = "start-first" + UpdateOrderStartFirst UpdateOrder = "start-first" ) // UpdateConfig represents the update configuration. @@ -130,7 +139,7 @@ type UpdateConfig struct { Delay time.Duration `json:",omitempty"` // FailureAction is the action to take when an update failures. - FailureAction string `json:",omitempty"` + FailureAction FailureAction `json:",omitempty"` // Monitor indicates how long to monitor a task for failure after it is // created. If the task fails by ending up in one of the states @@ -156,7 +165,7 @@ type UpdateConfig struct { // Order indicates the order of operations when rolling out an updated // task. Either the old task is shut down before the new task is // started, or the new task is started before the old task is shut down. - Order string + Order UpdateOrder } // ServiceStatus represents the number of running tasks in a service and the @@ -198,8 +207,12 @@ type JobStatus struct { LastExecution time.Time `json:",omitempty"` } +// RegistryAuthSource defines options for the "registryAuthFrom" query parameter +// on service update. +type RegistryAuthSource string + // Values for RegistryAuthFrom in ServiceUpdateOptions const ( - RegistryAuthFromSpec = "spec" - RegistryAuthFromPreviousSpec = "previous-spec" + RegistryAuthFromSpec RegistryAuthSource = "spec" + RegistryAuthFromPreviousSpec RegistryAuthSource = "previous-spec" ) diff --git a/vendor/github.com/moby/moby/client/service_update.go b/vendor/github.com/moby/moby/client/service_update.go index 36061c9d16..9e6b52781e 100644 --- a/vendor/github.com/moby/moby/client/service_update.go +++ b/vendor/github.com/moby/moby/client/service_update.go @@ -28,7 +28,7 @@ type ServiceUpdateOptions struct { // RegistryAuthFrom specifies where to find the registry authorization // credentials if they are not given in EncodedRegistryAuth. Valid // values are "spec" and "previous-spec". - RegistryAuthFrom string + RegistryAuthFrom swarm.RegistryAuthSource // Rollback indicates whether a server-side rollback should be // performed. When this is set, the provided spec will be ignored. @@ -65,7 +65,7 @@ func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, options query := url.Values{} if options.RegistryAuthFrom != "" { - query.Set("registryAuthFrom", options.RegistryAuthFrom) + query.Set("registryAuthFrom", string(options.RegistryAuthFrom)) } if options.Rollback != "" {