convert.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/go-connections/nat"
  22. "github.com/docker/compose-cli/api/containers"
  23. )
  24. // ToRuntimeConfig convert into containers.RuntimeConfig
  25. func ToRuntimeConfig(m *types.ContainerJSON) *containers.RuntimeConfig {
  26. if m.Config == nil {
  27. return nil
  28. }
  29. var env map[string]string
  30. if m.Config.Env != nil {
  31. env = make(map[string]string)
  32. for _, e := range m.Config.Env {
  33. tokens := strings.Split(e, "=")
  34. if len(tokens) != 2 {
  35. continue
  36. }
  37. env[tokens[0]] = tokens[1]
  38. }
  39. }
  40. var labels []string
  41. if m.Config.Labels != nil {
  42. for k, v := range m.Config.Labels {
  43. labels = append(labels, fmt.Sprintf("%s=%s", k, v))
  44. }
  45. }
  46. sort.Strings(labels)
  47. if env == nil &&
  48. labels == nil {
  49. return nil
  50. }
  51. return &containers.RuntimeConfig{
  52. Env: env,
  53. Labels: labels,
  54. }
  55. }
  56. // ToHostConfig convert into containers.HostConfig
  57. func ToHostConfig(m *types.ContainerJSON) *containers.HostConfig {
  58. if m.HostConfig == nil {
  59. return nil
  60. }
  61. return &containers.HostConfig{
  62. AutoRemove: m.HostConfig.AutoRemove,
  63. RestartPolicy: fromRestartPolicyName(m.HostConfig.RestartPolicy.Name),
  64. CPULimit: float64(m.HostConfig.Resources.NanoCPUs) / 1e9,
  65. MemoryLimit: uint64(m.HostConfig.Resources.Memory),
  66. }
  67. }
  68. // ToPorts convert into containers.Port
  69. func ToPorts(ports []types.Port) []containers.Port {
  70. result := []containers.Port{}
  71. for _, port := range ports {
  72. result = append(result, containers.Port{
  73. ContainerPort: uint32(port.PrivatePort),
  74. HostPort: uint32(port.PublicPort),
  75. HostIP: port.IP,
  76. Protocol: port.Type,
  77. })
  78. }
  79. return result
  80. }
  81. // FromPorts convert to nat.Port / nat.PortBinding
  82. func FromPorts(ports []containers.Port) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
  83. var (
  84. exposedPorts = make(map[nat.Port]struct{}, len(ports))
  85. bindings = make(map[nat.Port][]nat.PortBinding)
  86. )
  87. for _, port := range ports {
  88. p, err := nat.NewPort(port.Protocol, strconv.Itoa(int(port.ContainerPort)))
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. if _, exists := exposedPorts[p]; !exists {
  93. exposedPorts[p] = struct{}{}
  94. }
  95. portBinding := nat.PortBinding{
  96. HostIP: port.HostIP,
  97. HostPort: strconv.Itoa(int(port.HostPort)),
  98. }
  99. bslice, exists := bindings[p]
  100. if !exists {
  101. bslice = []nat.PortBinding{}
  102. }
  103. bindings[p] = append(bslice, portBinding)
  104. }
  105. return exposedPorts, bindings, nil
  106. }
  107. func fromRestartPolicyName(m string) string {
  108. switch m {
  109. case "always":
  110. return containers.RestartPolicyAny
  111. case "on-failure":
  112. return containers.RestartPolicyOnFailure
  113. case "no", "":
  114. fallthrough
  115. default:
  116. return containers.RestartPolicyNone
  117. }
  118. }
  119. // ToRestartPolicy convert to container.RestartPolicy
  120. func ToRestartPolicy(p string) container.RestartPolicy {
  121. switch p {
  122. case containers.RestartPolicyAny:
  123. return container.RestartPolicy{Name: "always"}
  124. case containers.RestartPolicyOnFailure:
  125. return container.RestartPolicy{Name: "on-failure"}
  126. case containers.RestartPolicyNone:
  127. fallthrough
  128. default:
  129. return container.RestartPolicy{Name: "no"}
  130. }
  131. }