1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

service update: Don't assume existing pointers in spec are valid

When updating values in the spec according to CLI flags, don't write
into the existing pointers. They may be nil. Instead, update them to
point to the new value we're writing.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
(cherry picked from commit f9c920a126)
Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
Aaron Lehmann 2016-07-25 23:53:20 -07:00 committed by Tibor Vass
parent 0e2d7c996c
commit e16a20d11a

View file

@ -98,7 +98,6 @@ func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, serviceID stri
} }
func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error { func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
updateString := func(flag string, field *string) { updateString := func(flag string, field *string) {
if flags.Changed(flag) { if flags.Changed(flag) {
*field, _ = flags.GetString(flag) *field, _ = flags.GetString(flag)
@ -117,9 +116,10 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
} }
} }
updateDurationOpt := func(flag string, field *time.Duration) { updateDurationOpt := func(flag string, field **time.Duration) {
if flags.Changed(flag) { if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*DurationOpt).Value() val := *flags.Lookup(flag).Value.(*DurationOpt).Value()
*field = &val
} }
} }
@ -129,9 +129,10 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
} }
} }
updateUint64Opt := func(flag string, field *uint64) { updateUint64Opt := func(flag string, field **uint64) {
if flags.Changed(flag) { if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*Uint64Opt).Value() val := *flags.Lookup(flag).Value.(*Uint64Opt).Value()
*field = &val
} }
} }
@ -159,7 +160,6 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
taskResources().Limits = &swarm.Resources{} taskResources().Limits = &swarm.Resources{}
updateInt64Value(flagLimitCPU, &task.Resources.Limits.NanoCPUs) updateInt64Value(flagLimitCPU, &task.Resources.Limits.NanoCPUs)
updateInt64Value(flagLimitMemory, &task.Resources.Limits.MemoryBytes) updateInt64Value(flagLimitMemory, &task.Resources.Limits.MemoryBytes)
} }
if flags.Changed(flagReserveCPU) || flags.Changed(flagReserveMemory) { if flags.Changed(flagReserveCPU) || flags.Changed(flagReserveMemory) {
taskResources().Reservations = &swarm.Resources{} taskResources().Reservations = &swarm.Resources{}
@ -167,7 +167,7 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
updateInt64Value(flagReserveMemory, &task.Resources.Reservations.MemoryBytes) updateInt64Value(flagReserveMemory, &task.Resources.Reservations.MemoryBytes)
} }
updateDurationOpt(flagStopGracePeriod, cspec.StopGracePeriod) updateDurationOpt(flagStopGracePeriod, &cspec.StopGracePeriod)
if anyChanged(flags, flagRestartCondition, flagRestartDelay, flagRestartMaxAttempts, flagRestartWindow) { if anyChanged(flags, flagRestartCondition, flagRestartDelay, flagRestartMaxAttempts, flagRestartWindow) {
if task.RestartPolicy == nil { if task.RestartPolicy == nil {
@ -178,9 +178,9 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
value, _ := flags.GetString(flagRestartCondition) value, _ := flags.GetString(flagRestartCondition)
task.RestartPolicy.Condition = swarm.RestartPolicyCondition(value) task.RestartPolicy.Condition = swarm.RestartPolicyCondition(value)
} }
updateDurationOpt(flagRestartDelay, task.RestartPolicy.Delay) updateDurationOpt(flagRestartDelay, &task.RestartPolicy.Delay)
updateUint64Opt(flagRestartMaxAttempts, task.RestartPolicy.MaxAttempts) updateUint64Opt(flagRestartMaxAttempts, &task.RestartPolicy.MaxAttempts)
updateDurationOpt((flagRestartWindow), task.RestartPolicy.Window) updateDurationOpt(flagRestartWindow, &task.RestartPolicy.Window)
} }
if anyChanged(flags, flagConstraintAdd, flagConstraintRemove) { if anyChanged(flags, flagConstraintAdd, flagConstraintRemove) {
@ -206,6 +206,9 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
updateNetworks(flags, &spec.Networks) updateNetworks(flags, &spec.Networks)
if flags.Changed(flagEndpointMode) { if flags.Changed(flagEndpointMode) {
value, _ := flags.GetString(flagEndpointMode) value, _ := flags.GetString(flagEndpointMode)
if spec.EndpointSpec == nil {
spec.EndpointSpec = &swarm.EndpointSpec{}
}
spec.EndpointSpec.Mode = swarm.ResolutionMode(value) spec.EndpointSpec.Mode = swarm.ResolutionMode(value)
} }
@ -409,7 +412,7 @@ func updateReplicas(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error
return nil return nil
} }
if serviceMode.Replicated == nil { if serviceMode == nil || serviceMode.Replicated == nil {
return fmt.Errorf("replicas can only be used with replicated mode") return fmt.Errorf("replicas can only be used with replicated mode")
} }
serviceMode.Replicated.Replicas = flags.Lookup(flagReplicas).Value.(*Uint64Opt).Value() serviceMode.Replicated.Replicas = flags.Lookup(flagReplicas).Value.(*Uint64Opt).Value()