endpointset.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2019 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 endpointslice
  14. import (
  15. "sort"
  16. discovery "k8s.io/api/discovery/v1beta1"
  17. endpointutil "k8s.io/kubernetes/pkg/controller/util/endpoint"
  18. )
  19. // endpointHash is used to uniquely identify endpoints. Only including addresses
  20. // and hostnames as unique identifiers allows us to do more in place updates
  21. // should attributes such as topology, conditions, or targetRef change.
  22. type endpointHash string
  23. type endpointHashObj struct {
  24. Addresses []string
  25. Hostname string
  26. }
  27. func hashEndpoint(endpoint *discovery.Endpoint) endpointHash {
  28. sort.Strings(endpoint.Addresses)
  29. hashObj := endpointHashObj{Addresses: endpoint.Addresses}
  30. if endpoint.Hostname != nil {
  31. hashObj.Hostname = *endpoint.Hostname
  32. }
  33. return endpointHash(endpointutil.DeepHashObjectToString(hashObj))
  34. }
  35. // endpointSet provides simple methods for comparing sets of Endpoints.
  36. type endpointSet map[endpointHash]*discovery.Endpoint
  37. // Insert adds items to the set.
  38. func (s endpointSet) Insert(items ...*discovery.Endpoint) endpointSet {
  39. for _, item := range items {
  40. s[hashEndpoint(item)] = item
  41. }
  42. return s
  43. }
  44. // Delete removes all items from the set.
  45. func (s endpointSet) Delete(items ...*discovery.Endpoint) endpointSet {
  46. for _, item := range items {
  47. delete(s, hashEndpoint(item))
  48. }
  49. return s
  50. }
  51. // Has returns true if and only if item is contained in the set.
  52. func (s endpointSet) Has(item *discovery.Endpoint) bool {
  53. _, contained := s[hashEndpoint(item)]
  54. return contained
  55. }
  56. // Returns an endpoint matching the hash if contained in the set.
  57. func (s endpointSet) Get(item *discovery.Endpoint) *discovery.Endpoint {
  58. return s[hashEndpoint(item)]
  59. }
  60. // UnsortedList returns the slice with contents in random order.
  61. func (s endpointSet) UnsortedList() []*discovery.Endpoint {
  62. endpoints := make([]*discovery.Endpoint, 0, len(s))
  63. for _, endpoint := range s {
  64. endpoints = append(endpoints, endpoint)
  65. }
  66. return endpoints
  67. }
  68. // Returns a single element from the set.
  69. func (s endpointSet) PopAny() (*discovery.Endpoint, bool) {
  70. for _, endpoint := range s {
  71. s.Delete(endpoint)
  72. return endpoint, true
  73. }
  74. return nil, false
  75. }
  76. // Len returns the size of the set.
  77. func (s endpointSet) Len() int {
  78. return len(s)
  79. }