normalize.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package compose
  2. import (
  3. "fmt"
  4. "github.com/compose-spec/compose-go/types"
  5. "github.com/sirupsen/logrus"
  6. )
  7. // Normalize a compose-go model to move deprecated attributes to canonical position, and introduce implicit defaults
  8. // FIXME move this to compose-go
  9. func Normalize(model *types.Config) error {
  10. if len(model.Networks) == 0 {
  11. // Compose application model implies a default network if none is explicitly set.
  12. model.Networks["default"] = types.NetworkConfig{
  13. Name: "default",
  14. }
  15. }
  16. for i, s := range model.Services {
  17. if len(s.Networks) == 0 {
  18. // Service without explicit network attachment are implicitly exposed on default network
  19. s.Networks = map[string]*types.ServiceNetworkConfig{"default": nil}
  20. }
  21. for i, p := range s.Ports {
  22. if p.Published == 0 {
  23. p.Published = p.Target
  24. s.Ports[i] = p
  25. }
  26. }
  27. if s.LogDriver != "" {
  28. logrus.Warn("`log_driver` is deprecated. Use the `logging` attribute")
  29. if s.Logging == nil {
  30. s.Logging = &types.LoggingConfig{}
  31. }
  32. if s.Logging.Driver == "" {
  33. s.Logging.Driver = s.LogDriver
  34. } else {
  35. return fmt.Errorf("can't use both 'log_driver' (deprecated) and 'logging.driver'")
  36. }
  37. }
  38. if len(s.LogOpt) != 0 {
  39. logrus.Warn("`log_opts` is deprecated. Use the `logging` attribute")
  40. if s.Logging == nil {
  41. s.Logging = &types.LoggingConfig{}
  42. }
  43. for k, v := range s.LogOpt {
  44. if _, ok := s.Logging.Options[k]; !ok {
  45. s.Logging.Options[k] = v
  46. } else {
  47. return fmt.Errorf("can't use both 'log_opt' (deprecated) and 'logging.options'")
  48. }
  49. }
  50. }
  51. model.Services[i] = s
  52. }
  53. for i, n := range model.Networks {
  54. if n.Name == "" {
  55. n.Name = i
  56. model.Networks[i] = n
  57. }
  58. }
  59. for i, v := range model.Volumes {
  60. if v.Name == "" {
  61. v.Name = i
  62. model.Volumes[i] = v
  63. }
  64. }
  65. for i, c := range model.Configs {
  66. if c.Name == "" {
  67. c.Name = i
  68. model.Configs[i] = c
  69. }
  70. }
  71. for i, s := range model.Secrets {
  72. if s.Name == "" {
  73. s.Name = i
  74. model.Secrets[i] = s
  75. }
  76. }
  77. return nil
  78. }