service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package defaults
  2. import (
  3. "time"
  4. "github.com/docker/swarmkit/api"
  5. "github.com/docker/swarmkit/api/deepcopy"
  6. gogotypes "github.com/gogo/protobuf/types"
  7. )
  8. // Service is a ServiceSpec object with all fields filled in using default
  9. // values.
  10. var Service = api.ServiceSpec{
  11. Task: api.TaskSpec{
  12. Runtime: &api.TaskSpec_Container{
  13. Container: &api.ContainerSpec{
  14. StopGracePeriod: gogotypes.DurationProto(10 * time.Second),
  15. PullOptions: &api.ContainerSpec_PullOptions{},
  16. DNSConfig: &api.ContainerSpec_DNSConfig{},
  17. },
  18. },
  19. Resources: &api.ResourceRequirements{},
  20. Restart: &api.RestartPolicy{
  21. Condition: api.RestartOnAny,
  22. Delay: gogotypes.DurationProto(5 * time.Second),
  23. },
  24. Placement: &api.Placement{},
  25. },
  26. Update: &api.UpdateConfig{
  27. FailureAction: api.UpdateConfig_PAUSE,
  28. Monitor: gogotypes.DurationProto(5 * time.Second),
  29. Parallelism: 1,
  30. Order: api.UpdateConfig_STOP_FIRST,
  31. },
  32. Rollback: &api.UpdateConfig{
  33. FailureAction: api.UpdateConfig_PAUSE,
  34. Monitor: gogotypes.DurationProto(5 * time.Second),
  35. Parallelism: 1,
  36. Order: api.UpdateConfig_STOP_FIRST,
  37. },
  38. }
  39. // InterpolateService returns a ServiceSpec based on the provided spec, which
  40. // has all unspecified values filled in with default values.
  41. func InterpolateService(origSpec *api.ServiceSpec) *api.ServiceSpec {
  42. spec := origSpec.Copy()
  43. container := spec.Task.GetContainer()
  44. defaultContainer := Service.Task.GetContainer()
  45. if container != nil {
  46. if container.StopGracePeriod == nil {
  47. container.StopGracePeriod = &gogotypes.Duration{}
  48. deepcopy.Copy(container.StopGracePeriod, defaultContainer.StopGracePeriod)
  49. }
  50. if container.PullOptions == nil {
  51. container.PullOptions = defaultContainer.PullOptions.Copy()
  52. }
  53. if container.DNSConfig == nil {
  54. container.DNSConfig = defaultContainer.DNSConfig.Copy()
  55. }
  56. }
  57. if spec.Task.Resources == nil {
  58. spec.Task.Resources = Service.Task.Resources.Copy()
  59. }
  60. if spec.Task.Restart == nil {
  61. spec.Task.Restart = Service.Task.Restart.Copy()
  62. } else {
  63. if spec.Task.Restart.Delay == nil {
  64. spec.Task.Restart.Delay = &gogotypes.Duration{}
  65. deepcopy.Copy(spec.Task.Restart.Delay, Service.Task.Restart.Delay)
  66. }
  67. }
  68. if spec.Task.Placement == nil {
  69. spec.Task.Placement = Service.Task.Placement.Copy()
  70. }
  71. if spec.Update == nil {
  72. spec.Update = Service.Update.Copy()
  73. } else {
  74. if spec.Update.Monitor == nil {
  75. spec.Update.Monitor = &gogotypes.Duration{}
  76. deepcopy.Copy(spec.Update.Monitor, Service.Update.Monitor)
  77. }
  78. }
  79. if spec.Rollback == nil {
  80. spec.Rollback = Service.Rollback.Copy()
  81. } else {
  82. if spec.Rollback.Monitor == nil {
  83. spec.Rollback.Monitor = &gogotypes.Duration{}
  84. deepcopy.Copy(spec.Rollback.Monitor, Service.Rollback.Monitor)
  85. }
  86. }
  87. return spec
  88. }