host_ports.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2018 The Kubernetes 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 nodeinfo
  14. import (
  15. "k8s.io/api/core/v1"
  16. )
  17. // DefaultBindAllHostIP defines the default ip address used to bind to all host.
  18. const DefaultBindAllHostIP = "0.0.0.0"
  19. // ProtocolPort represents a protocol port pair, e.g. tcp:80.
  20. type ProtocolPort struct {
  21. Protocol string
  22. Port int32
  23. }
  24. // NewProtocolPort creates a ProtocolPort instance.
  25. func NewProtocolPort(protocol string, port int32) *ProtocolPort {
  26. pp := &ProtocolPort{
  27. Protocol: protocol,
  28. Port: port,
  29. }
  30. if len(pp.Protocol) == 0 {
  31. pp.Protocol = string(v1.ProtocolTCP)
  32. }
  33. return pp
  34. }
  35. // HostPortInfo stores mapping from ip to a set of ProtocolPort
  36. type HostPortInfo map[string]map[ProtocolPort]struct{}
  37. // Add adds (ip, protocol, port) to HostPortInfo
  38. func (h HostPortInfo) Add(ip, protocol string, port int32) {
  39. if port <= 0 {
  40. return
  41. }
  42. h.sanitize(&ip, &protocol)
  43. pp := NewProtocolPort(protocol, port)
  44. if _, ok := h[ip]; !ok {
  45. h[ip] = map[ProtocolPort]struct{}{
  46. *pp: {},
  47. }
  48. return
  49. }
  50. h[ip][*pp] = struct{}{}
  51. }
  52. // Remove removes (ip, protocol, port) from HostPortInfo
  53. func (h HostPortInfo) Remove(ip, protocol string, port int32) {
  54. if port <= 0 {
  55. return
  56. }
  57. h.sanitize(&ip, &protocol)
  58. pp := NewProtocolPort(protocol, port)
  59. if m, ok := h[ip]; ok {
  60. delete(m, *pp)
  61. if len(h[ip]) == 0 {
  62. delete(h, ip)
  63. }
  64. }
  65. }
  66. // Len returns the total number of (ip, protocol, port) tuple in HostPortInfo
  67. func (h HostPortInfo) Len() int {
  68. length := 0
  69. for _, m := range h {
  70. length += len(m)
  71. }
  72. return length
  73. }
  74. // CheckConflict checks if the input (ip, protocol, port) conflicts with the existing
  75. // ones in HostPortInfo.
  76. func (h HostPortInfo) CheckConflict(ip, protocol string, port int32) bool {
  77. if port <= 0 {
  78. return false
  79. }
  80. h.sanitize(&ip, &protocol)
  81. pp := NewProtocolPort(protocol, port)
  82. // If ip is 0.0.0.0 check all IP's (protocol, port) pair
  83. if ip == DefaultBindAllHostIP {
  84. for _, m := range h {
  85. if _, ok := m[*pp]; ok {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. // If ip isn't 0.0.0.0, only check IP and 0.0.0.0's (protocol, port) pair
  92. for _, key := range []string{DefaultBindAllHostIP, ip} {
  93. if m, ok := h[key]; ok {
  94. if _, ok2 := m[*pp]; ok2 {
  95. return true
  96. }
  97. }
  98. }
  99. return false
  100. }
  101. // sanitize the parameters
  102. func (h HostPortInfo) sanitize(ip, protocol *string) {
  103. if len(*ip) == 0 {
  104. *ip = DefaultBindAllHostIP
  105. }
  106. if len(*protocol) == 0 {
  107. *protocol = string(v1.ProtocolTCP)
  108. }
  109. }