convert.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // +build local
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package local
  15. import (
  16. "fmt"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/container"
  22. "github.com/docker/go-connections/nat"
  23. "github.com/docker/compose-cli/api/containers"
  24. )
  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. func toHostConfig(m *types.ContainerJSON) *containers.HostConfig {
  57. if m.HostConfig == nil {
  58. return nil
  59. }
  60. return &containers.HostConfig{
  61. AutoRemove: m.HostConfig.AutoRemove,
  62. RestartPolicy: fromRestartPolicyName(m.HostConfig.RestartPolicy.Name),
  63. CPULimit: float64(m.HostConfig.Resources.NanoCPUs) / 1e9,
  64. MemoryLimit: uint64(m.HostConfig.Resources.Memory),
  65. }
  66. }
  67. func toPorts(ports []types.Port) []containers.Port {
  68. result := []containers.Port{}
  69. for _, port := range ports {
  70. result = append(result, containers.Port{
  71. ContainerPort: uint32(port.PrivatePort),
  72. HostPort: uint32(port.PublicPort),
  73. HostIP: port.IP,
  74. Protocol: port.Type,
  75. })
  76. }
  77. return result
  78. }
  79. func fromPorts(ports []containers.Port) (map[nat.Port]struct{}, map[nat.Port][]nat.PortBinding, error) {
  80. var (
  81. exposedPorts = make(map[nat.Port]struct{}, len(ports))
  82. bindings = make(map[nat.Port][]nat.PortBinding)
  83. )
  84. for _, port := range ports {
  85. p, err := nat.NewPort(port.Protocol, strconv.Itoa(int(port.ContainerPort)))
  86. if err != nil {
  87. return nil, nil, err
  88. }
  89. if _, exists := exposedPorts[p]; !exists {
  90. exposedPorts[p] = struct{}{}
  91. }
  92. portBinding := nat.PortBinding{
  93. HostIP: port.HostIP,
  94. HostPort: strconv.Itoa(int(port.HostPort)),
  95. }
  96. bslice, exists := bindings[p]
  97. if !exists {
  98. bslice = []nat.PortBinding{}
  99. }
  100. bindings[p] = append(bslice, portBinding)
  101. }
  102. return exposedPorts, bindings, nil
  103. }
  104. func fromRestartPolicyName(m string) string {
  105. switch m {
  106. case "always":
  107. return containers.RestartPolicyAny
  108. case "on-failure":
  109. return containers.RestartPolicyOnFailure
  110. case "no", "":
  111. fallthrough
  112. default:
  113. return containers.RestartPolicyNone
  114. }
  115. }
  116. func toRestartPolicy(p string) container.RestartPolicy {
  117. switch p {
  118. case containers.RestartPolicyAny:
  119. return container.RestartPolicy{Name: "always"}
  120. case containers.RestartPolicyOnFailure:
  121. return container.RestartPolicy{Name: "on-failure"}
  122. case containers.RestartPolicyNone:
  123. fallthrough
  124. default:
  125. return container.RestartPolicy{Name: "no"}
  126. }
  127. }