convert.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/moby/moby/api/types/container"
  21. )
  22. // ToMobyEnv convert into []string
  23. func ToMobyEnv(environment compose.MappingWithEquals) []string {
  24. var env []string
  25. for k, v := range environment {
  26. if v == nil {
  27. env = append(env, k)
  28. } else {
  29. env = append(env, fmt.Sprintf("%s=%s", k, *v))
  30. }
  31. }
  32. return env
  33. }
  34. // ToMobyHealthCheck convert into container.HealthConfig
  35. func (s *composeService) ToMobyHealthCheck(ctx context.Context, check *compose.HealthCheckConfig) (*container.HealthConfig, error) {
  36. if check == nil {
  37. return nil, nil
  38. }
  39. var (
  40. interval time.Duration
  41. timeout time.Duration
  42. period time.Duration
  43. retries int
  44. )
  45. if check.Interval != nil {
  46. interval = time.Duration(*check.Interval)
  47. }
  48. if check.Timeout != nil {
  49. timeout = time.Duration(*check.Timeout)
  50. }
  51. if check.StartPeriod != nil {
  52. period = time.Duration(*check.StartPeriod)
  53. }
  54. if check.Retries != nil {
  55. retries = int(*check.Retries)
  56. }
  57. test := check.Test
  58. if check.Disable {
  59. test = []string{"NONE"}
  60. }
  61. var startInterval time.Duration
  62. if check.StartInterval != nil {
  63. startInterval = time.Duration(*check.StartInterval)
  64. if check.StartPeriod == nil {
  65. // see https://github.com/moby/moby/issues/48874
  66. return nil, errors.New("healthcheck.start_interval requires healthcheck.start_period to be set")
  67. }
  68. }
  69. return &container.HealthConfig{
  70. Test: test,
  71. Interval: interval,
  72. Timeout: timeout,
  73. StartPeriod: period,
  74. StartInterval: startInterval,
  75. Retries: retries,
  76. }, nil
  77. }
  78. // ToSeconds convert into seconds
  79. func ToSeconds(d *compose.Duration) *int {
  80. if d == nil {
  81. return nil
  82. }
  83. s := int(time.Duration(*d).Seconds())
  84. return &s
  85. }