/
reconciler_helpers.go
137 lines (115 loc) · 4.65 KB
/
reconciler_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package endpointslicemirroring
import (
"k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1beta1"
)
// slicesByAction includes lists of slices to create, update, or delete.
type slicesByAction struct {
toCreate, toUpdate, toDelete []*discovery.EndpointSlice
}
// append appends slices from another slicesByAction struct.
func (s *slicesByAction) append(slices slicesByAction) {
s.toCreate = append(s.toCreate, slices.toCreate...)
s.toUpdate = append(s.toUpdate, slices.toUpdate...)
s.toDelete = append(s.toDelete, slices.toDelete...)
}
// totalsByAction includes total numbers for added and removed.
type totalsByAction struct {
added, updated, removed int
}
// add adds totals from another totalsByAction struct.
func (t *totalsByAction) add(totals totalsByAction) {
t.added += totals.added
t.updated += totals.updated
t.removed += totals.removed
}
// newDesiredCalc initializes and returns a new desiredCalc.
func newDesiredCalc() *desiredCalc {
return &desiredCalc{
portsByKey: map[addrTypePortMapKey][]discovery.EndpointPort{},
endpointsByKey: map[addrTypePortMapKey]endpointSet{},
numDesiredEndpoints: 0,
}
}
// desiredCalc helps calculate desired endpoints and ports.
type desiredCalc struct {
portsByKey map[addrTypePortMapKey][]discovery.EndpointPort
endpointsByKey map[addrTypePortMapKey]endpointSet
numDesiredEndpoints int
}
// multiAddrTypePortMapKey stores addrTypePortMapKey for different address
// types.
type multiAddrTypePortMapKey map[discovery.AddressType]addrTypePortMapKey
// initPorts initializes ports for a subset and address type and returns the
// corresponding addrTypePortMapKey.
func (d *desiredCalc) initPorts(subsetPorts []v1.EndpointPort) multiAddrTypePortMapKey {
endpointPorts := epPortsToEpsPorts(subsetPorts)
addrTypes := []discovery.AddressType{discovery.AddressTypeIPv4, discovery.AddressTypeIPv6}
multiKey := multiAddrTypePortMapKey{}
for _, addrType := range addrTypes {
multiKey[addrType] = newAddrTypePortMapKey(endpointPorts, addrType)
if _, ok := d.endpointsByKey[multiKey[addrType]]; !ok {
d.endpointsByKey[multiKey[addrType]] = endpointSet{}
}
d.portsByKey[multiKey[addrType]] = endpointPorts
}
return multiKey
}
// addAddress adds an EndpointAddress to the desired state if it is valid. It
// returns false if the address was invalid.
func (d *desiredCalc) addAddress(address v1.EndpointAddress, multiKey multiAddrTypePortMapKey, ready bool) bool {
endpoint := addressToEndpoint(address, ready)
addrType := getAddressType(address.IP)
if addrType == nil {
return false
}
d.endpointsByKey[multiKey[*addrType]].Insert(endpoint)
d.numDesiredEndpoints++
return true
}
type slicesByAddrType map[discovery.AddressType][]*discovery.EndpointSlice
// recycleSlices will recycle the slices marked for deletion by replacing
// creates with updates of slices that would otherwise be deleted.
func recycleSlices(slices *slicesByAction) {
toCreateByAddrType := toSlicesByAddrType(slices.toCreate)
toDeleteByAddrType := toSlicesByAddrType(slices.toDelete)
for addrType, slicesToCreate := range toCreateByAddrType {
slicesToDelete := toDeleteByAddrType[addrType]
for i := 0; len(slicesToCreate) > i && len(slicesToDelete) > i; i++ {
slices.toCreate = removeSlice(slices.toCreate, slicesToCreate[i])
slices.toDelete = removeSlice(slices.toDelete, slicesToDelete[i])
slice := slicesToCreate[i]
slice.Name = slicesToDelete[i].Name
slices.toUpdate = append(slices.toUpdate, slice)
}
}
}
// removeSlice removes an EndpointSlice from a list of EndpointSlices.
func removeSlice(slices []*discovery.EndpointSlice, sliceToRemove *discovery.EndpointSlice) []*discovery.EndpointSlice {
for i, slice := range slices {
if slice.Name == sliceToRemove.Name {
return append(slices[:i], slices[i+1:]...)
}
}
return slices
}
// toSliceByAddrType returns lists of EndpointSlices grouped by address.
func toSlicesByAddrType(slices []*discovery.EndpointSlice) slicesByAddrType {
byAddrType := slicesByAddrType{}
for _, slice := range slices {
byAddrType[slice.AddressType] = append(byAddrType[slice.AddressType], slice)
}
return byAddrType
}