container.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 formatter
  14. import (
  15. "fmt"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "github.com/docker/compose-cli/containers"
  20. )
  21. type portGroup struct {
  22. first uint32
  23. last uint32
  24. }
  25. // PortsToStrings returns a human readable published ports
  26. func PortsToStrings(ports []containers.Port) []string {
  27. groupMap := make(map[string]*portGroup)
  28. var (
  29. result []string
  30. hostMappings []string
  31. groupMapKeys []string
  32. )
  33. sort.Slice(ports, func(i int, j int) bool {
  34. return comparePorts(ports[i], ports[j])
  35. })
  36. for _, port := range ports {
  37. // Simple case: HOST_IP:PORT1:PORT2
  38. hostIP := "0.0.0.0"
  39. if port.HostIP != "" {
  40. hostIP = port.HostIP
  41. }
  42. if port.HostPort != port.ContainerPort {
  43. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", hostIP, port.HostPort, port.ContainerPort, port.Protocol))
  44. continue
  45. }
  46. current := port.ContainerPort
  47. portKey := fmt.Sprintf("%s/%s", hostIP, port.Protocol)
  48. group := groupMap[portKey]
  49. if group == nil {
  50. groupMap[portKey] = &portGroup{first: current, last: current}
  51. // record order that groupMap keys are created
  52. groupMapKeys = append(groupMapKeys, portKey)
  53. continue
  54. }
  55. if current == (group.last + 1) {
  56. group.last = current
  57. continue
  58. }
  59. result = append(result, formGroup(portKey, group.first, group.last))
  60. groupMap[portKey] = &portGroup{first: current, last: current}
  61. }
  62. for _, portKey := range groupMapKeys {
  63. g := groupMap[portKey]
  64. result = append(result, formGroup(portKey, g.first, g.last))
  65. }
  66. result = append(result, hostMappings...)
  67. return result
  68. }
  69. func formGroup(key string, start uint32, last uint32) string {
  70. parts := strings.Split(key, "/")
  71. protocol := parts[0]
  72. var ip string
  73. if len(parts) > 1 {
  74. ip = parts[0]
  75. protocol = parts[1]
  76. }
  77. group := strconv.Itoa(int(start))
  78. // add range
  79. if start != last {
  80. group = fmt.Sprintf("%s-%d", group, last)
  81. }
  82. // add host ip
  83. if ip != "" {
  84. group = fmt.Sprintf("%s:%s->%s", ip, group, group)
  85. }
  86. // add protocol
  87. return fmt.Sprintf("%s/%s", group, protocol)
  88. }
  89. func comparePorts(i containers.Port, j containers.Port) bool {
  90. if i.ContainerPort != j.ContainerPort {
  91. return i.ContainerPort < j.ContainerPort
  92. }
  93. if i.HostIP != j.HostIP {
  94. return i.HostIP < j.HostIP
  95. }
  96. if i.HostPort != j.HostPort {
  97. return i.HostPort < j.HostPort
  98. }
  99. return i.Protocol < j.Protocol
  100. }