convert.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "time"
  19. compose "github.com/compose-spec/compose-go/v2/types"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/versions"
  22. )
  23. // ToMobyEnv convert into []string
  24. func ToMobyEnv(environment compose.MappingWithEquals) []string {
  25. var env []string
  26. for k, v := range environment {
  27. if v == nil {
  28. env = append(env, k)
  29. } else {
  30. env = append(env, fmt.Sprintf("%s=%s", k, *v))
  31. }
  32. }
  33. return env
  34. }
  35. // ToMobyHealthCheck convert into container.HealthConfig
  36. func (s *composeService) ToMobyHealthCheck(ctx context.Context, check *compose.HealthCheckConfig) (*container.HealthConfig, error) {
  37. if check == nil {
  38. return nil, nil
  39. }
  40. var (
  41. interval time.Duration
  42. timeout time.Duration
  43. period time.Duration
  44. retries int
  45. )
  46. if check.Interval != nil {
  47. interval = time.Duration(*check.Interval)
  48. }
  49. if check.Timeout != nil {
  50. timeout = time.Duration(*check.Timeout)
  51. }
  52. if check.StartPeriod != nil {
  53. period = time.Duration(*check.StartPeriod)
  54. }
  55. if check.Retries != nil {
  56. retries = int(*check.Retries)
  57. }
  58. test := check.Test
  59. if check.Disable {
  60. test = []string{"NONE"}
  61. }
  62. var startInterval time.Duration
  63. if check.StartInterval != nil {
  64. version, err := s.RuntimeVersion(ctx)
  65. if err != nil {
  66. return nil, err
  67. }
  68. if versions.LessThan(version, "1.44") {
  69. return nil, errors.New("can't set healthcheck.start_interval as feature require Docker Engine v25 or later")
  70. } else {
  71. startInterval = time.Duration(*check.StartInterval)
  72. }
  73. }
  74. return &container.HealthConfig{
  75. Test: test,
  76. Interval: interval,
  77. Timeout: timeout,
  78. StartPeriod: period,
  79. StartInterval: startInterval,
  80. Retries: retries,
  81. }, nil
  82. }
  83. // ToSeconds convert into seconds
  84. func ToSeconds(d *compose.Duration) *int {
  85. if d == nil {
  86. return nil
  87. }
  88. s := int(time.Duration(*d).Seconds())
  89. return &s
  90. }