|
| 1 | +/* |
| 2 | +Copyright 2015 The Kubernetes Authors. |
| 3 | +
|
| 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 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package endpoints |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "crypto/md5" |
| 22 | + "hash" |
| 23 | + "sort" |
| 24 | + |
| 25 | + hashutil "github.com/argoproj/gitops-engine/internal/kubernetes_vendor/pkg/util/hash" |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | +) |
| 28 | + |
| 29 | +// LessEndpointAddress compares IP addresses lexicographically and returns true if first argument is lesser than second |
| 30 | +func LessEndpointAddress(a, b *v1.EndpointAddress) bool { |
| 31 | + ipComparison := bytes.Compare([]byte(a.IP), []byte(b.IP)) |
| 32 | + if ipComparison != 0 { |
| 33 | + return ipComparison < 0 |
| 34 | + } |
| 35 | + if b.TargetRef == nil { |
| 36 | + return false |
| 37 | + } |
| 38 | + if a.TargetRef == nil { |
| 39 | + return true |
| 40 | + } |
| 41 | + return a.TargetRef.UID < b.TargetRef.UID |
| 42 | +} |
| 43 | + |
| 44 | +// SortSubsets sorts an array of EndpointSubset objects in place. For ease of |
| 45 | +// use it returns the input slice. |
| 46 | +func SortSubsets(subsets []v1.EndpointSubset) []v1.EndpointSubset { |
| 47 | + for i := range subsets { |
| 48 | + ss := &subsets[i] |
| 49 | + sort.Sort(addrsByIPAndUID(ss.Addresses)) |
| 50 | + sort.Sort(addrsByIPAndUID(ss.NotReadyAddresses)) |
| 51 | + sort.Sort(portsByHash(ss.Ports)) |
| 52 | + } |
| 53 | + sort.Sort(subsetsByHash(subsets)) |
| 54 | + return subsets |
| 55 | +} |
| 56 | + |
| 57 | +func hashObject(hasher hash.Hash, obj interface{}) []byte { |
| 58 | + hashutil.DeepHashObject(hasher, obj) |
| 59 | + return hasher.Sum(nil) |
| 60 | +} |
| 61 | + |
| 62 | +type subsetsByHash []v1.EndpointSubset |
| 63 | + |
| 64 | +func (sl subsetsByHash) Len() int { return len(sl) } |
| 65 | +func (sl subsetsByHash) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] } |
| 66 | +func (sl subsetsByHash) Less(i, j int) bool { |
| 67 | + hasher := md5.New() |
| 68 | + h1 := hashObject(hasher, sl[i]) |
| 69 | + h2 := hashObject(hasher, sl[j]) |
| 70 | + return bytes.Compare(h1, h2) < 0 |
| 71 | +} |
| 72 | + |
| 73 | +type addrsByIPAndUID []v1.EndpointAddress |
| 74 | + |
| 75 | +func (sl addrsByIPAndUID) Len() int { return len(sl) } |
| 76 | +func (sl addrsByIPAndUID) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] } |
| 77 | +func (sl addrsByIPAndUID) Less(i, j int) bool { |
| 78 | + return LessEndpointAddress(&sl[i], &sl[j]) |
| 79 | +} |
| 80 | + |
| 81 | +type portsByHash []v1.EndpointPort |
| 82 | + |
| 83 | +func (sl portsByHash) Len() int { return len(sl) } |
| 84 | +func (sl portsByHash) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] } |
| 85 | +func (sl portsByHash) Less(i, j int) bool { |
| 86 | + hasher := md5.New() |
| 87 | + h1 := hashObject(hasher, sl[i]) |
| 88 | + h2 := hashObject(hasher, sl[j]) |
| 89 | + return bytes.Compare(h1, h2) < 0 |
| 90 | +} |
0 commit comments