container.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 formatter
  14. import (
  15. "fmt"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "github.com/docker/compose-cli/api/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, fqdn string) []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 fqdn != "" {
  43. hostIP = fqdn
  44. }
  45. if port.HostPort != port.ContainerPort {
  46. hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", hostIP, port.HostPort, port.ContainerPort, port.Protocol))
  47. continue
  48. }
  49. current := port.ContainerPort
  50. portKey := fmt.Sprintf("%s/%s", hostIP, port.Protocol)
  51. group := groupMap[portKey]
  52. if group == nil {
  53. groupMap[portKey] = &portGroup{first: current, last: current}
  54. // record order that groupMap keys are created
  55. groupMapKeys = append(groupMapKeys, portKey)
  56. continue
  57. }
  58. if current == (group.last + 1) {
  59. group.last = current
  60. continue
  61. }
  62. result = append(result, formGroup(portKey, group.first, group.last))
  63. groupMap[portKey] = &portGroup{first: current, last: current}
  64. }
  65. for _, portKey := range groupMapKeys {
  66. g := groupMap[portKey]
  67. result = append(result, formGroup(portKey, g.first, g.last))
  68. }
  69. result = append(result, hostMappings...)
  70. return result
  71. }
  72. func formGroup(key string, start uint32, last uint32) string {
  73. parts := strings.Split(key, "/")
  74. protocol := parts[0]
  75. var ip string
  76. if len(parts) > 1 {
  77. ip = parts[0]
  78. protocol = parts[1]
  79. }
  80. group := strconv.Itoa(int(start))
  81. // add range
  82. if start != last {
  83. group = fmt.Sprintf("%s-%d", group, last)
  84. }
  85. // add host ip
  86. if ip != "" {
  87. group = fmt.Sprintf("%s:%s->%s", ip, group, group)
  88. }
  89. // add protocol
  90. return fmt.Sprintf("%s/%s", group, protocol)
  91. }
  92. func comparePorts(i containers.Port, j containers.Port) bool {
  93. if i.ContainerPort != j.ContainerPort {
  94. return i.ContainerPort < j.ContainerPort
  95. }
  96. if i.HostIP != j.HostIP {
  97. return i.HostIP < j.HostIP
  98. }
  99. if i.HostPort != j.HostPort {
  100. return i.HostPort < j.HostPort
  101. }
  102. return i.Protocol < j.Protocol
  103. }