convert.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 moby
  14. import (
  15. "fmt"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. compose "github.com/compose-spec/compose-go/types"
  21. "github.com/docker/docker/api/types"
  22. "github.com/docker/docker/api/types/container"
  23. "github.com/docker/go-connections/nat"
  24. "github.com/docker/compose-cli/api/containers"
  25. )
  26. // ToRuntimeConfig convert into containers.RuntimeConfig
  27. func ToRuntimeConfig(m *types.ContainerJSON) *containers.RuntimeConfig {
  28. if m.Config == nil {
  29. return nil
  30. }
  31. var env map[string]string
  32. if m.Config.Env != nil {
  33. env = make(map[string]string)
  34. for _, e := range m.Config.Env {
  35. tokens := strings.Split(e, "=")
  36. if len(tokens) != 2 {
  37. continue
  38. }
  39. env[tokens[0]] = tokens[1]
  40. }
  41. }
  42. var labels []string
  43. if m.Config.Labels != nil {
  44. for k, v := range m.Config.Labels {
  45. labels = append(labels, fmt.Sprintf("%s=%s", k, v))
  46. }
  47. }
  48. sort.Strings(labels)
  49. if env == nil &&
  50. labels == nil {
  51. return nil
  52. }
  53. return &containers.RuntimeConfig{
  54. Env: env,
  55. Labels: labels,
  56. }
  57. }
  58. // ToHostConfig convert into containers.HostConfig
  59. func ToHostConfig(m *types.ContainerJSON) *containers.HostConfig {
  60. if m.HostConfig == nil {
  61. return nil
  62. }
  63. return &containers.HostConfig{
  64. AutoRemove: m.HostConfig.AutoRemove,
  65. RestartPolicy: fromRestartPolicyName(m.HostConfig.RestartPolicy.Name),
  66. CPULimit: float64(m.HostConfig.Resources.NanoCPUs) / 1e9,
  67. MemoryLimit: uint64(m.HostConfig.Resources.Memory),
  68. }
  69. }
  70. // ToPorts convert into containers.Port
  71. func ToPorts(ports []types.Port) []containers.Port {
  72. result := []containers.Port{}
  73. for _, port := range ports {
  74. result = append(result, containers.Port{
  75. ContainerPort: uint32(port.PrivatePort),
  76. HostPort: uint32(port.PublicPort),
  77. HostIP: port.IP,
  78. Protocol: port.Type,
  79. })
  80. }
  81. return result
  82. }
  83. // ToMobyEnv convert into []string
  84. func ToMobyEnv(environment compose.MappingWithEquals) []string {
  85. var env []string
  86. for k, v := range environment {
  87. if v == nil {
  88. env = append(env, k)
  89. } else {
  90. env = append(env, fmt.Sprintf("%s=%s", k, *v))
  91. }
  92. }
  93. return env
  94. }
  95. // ToMobyHealthCheck convert into container.HealthConfig
  96. func ToMobyHealthCheck(check *compose.HealthCheckConfig) *container.HealthConfig {
  97. if check == nil {
  98. return nil
  99. }
  100. var (
  101. interval time.Duration
  102. timeout time.Duration
  103. period time.Duration
  104. retries int
  105. )
  106. if check.Interval != nil {
  107. interval = time.Duration(*check.Interval)
  108. }
  109. if check.Timeout != nil {
  110. timeout = time.Duration(*check.Timeout)
  111. }
  112. if check.StartPeriod != nil {
  113. period = time.Duration(*check.StartPeriod)
  114. }
  115. if check.Retries != nil {
  116. retries = int(*check.Retries)
  117. }
  118. test := check.Test
  119. if check.Disable {
  120. test = []string{"NONE"}
  121. }
  122. return &container.HealthConfig{
  123. Test: test,
  124. Interval: interval,
  125. Timeout: timeout,
  126. StartPeriod: period,
  127. Retries: retries,
  128. }
  129. }
  130. // ToSeconds convert into seconds
  131. func ToSeconds(d *compose.Duration) *int {
  132. if d == nil {
  133. return nil
  134. }
  135. s := int(time.Duration(*d).Seconds())
  136. return &s
  137. }
  138. // FromPorts convert to nat.Port / nat.PortBinding
  139. func FromPorts(ports []containers.Port) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
  140. var (
  141. exposedPorts = make(map[nat.Port]struct{}, len(ports))
  142. bindings = make(map[nat.Port][]nat.PortBinding)
  143. )
  144. for _, port := range ports {
  145. p, err := nat.NewPort(port.Protocol, strconv.Itoa(int(port.ContainerPort)))
  146. if err != nil {
  147. return nil, nil, err
  148. }
  149. if _, exists := exposedPorts[p]; !exists {
  150. exposedPorts[p] = struct{}{}
  151. }
  152. portBinding := nat.PortBinding{
  153. HostIP: port.HostIP,
  154. HostPort: strconv.Itoa(int(port.HostPort)),
  155. }
  156. bslice, exists := bindings[p]
  157. if !exists {
  158. bslice = []nat.PortBinding{}
  159. }
  160. bindings[p] = append(bslice, portBinding)
  161. }
  162. return exposedPorts, bindings, nil
  163. }
  164. func fromRestartPolicyName(m string) string {
  165. switch m {
  166. case "always":
  167. return containers.RestartPolicyAny
  168. case "on-failure":
  169. return containers.RestartPolicyOnFailure
  170. case "no", "":
  171. fallthrough
  172. default:
  173. return containers.RestartPolicyNone
  174. }
  175. }
  176. // ToRestartPolicy convert to container.RestartPolicy
  177. func ToRestartPolicy(p string) container.RestartPolicy {
  178. switch p {
  179. case containers.RestartPolicyAny:
  180. return container.RestartPolicy{Name: "always"}
  181. case containers.RestartPolicyOnFailure:
  182. return container.RestartPolicy{Name: "on-failure"}
  183. case containers.RestartPolicyNone:
  184. fallthrough
  185. default:
  186. return container.RestartPolicy{Name: "no"}
  187. }
  188. }