-
Notifications
You must be signed in to change notification settings - Fork 303
/
types.go
327 lines (285 loc) · 11.2 KB
/
types.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Copyright 2018 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 types
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
istioV1alpha3 "istio.io/api/networking/v1alpha3"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/ingress-gce/pkg/annotations"
)
type NetworkEndpointType string
type EndpointsCalculatorMode string
const (
VmIpPortEndpointType = NetworkEndpointType("GCE_VM_IP_PORT")
VmIpEndpointType = NetworkEndpointType("GCE_VM_IP")
NonGCPPrivateEndpointType = NetworkEndpointType("NON_GCP_PRIVATE_IP_PORT")
L7Mode = EndpointsCalculatorMode("L7")
L4LocalMode = EndpointsCalculatorMode("L4, ExternalTrafficPolicy:Local")
L4ClusterMode = EndpointsCalculatorMode("L4, ExternalTrafficPolicy:Cluster")
// These keys are to be used as label keys for NEG CRs when enabled
NegCRManagedByKey = "networking.gke.io/managed-by"
NegCRServiceNameKey = "networking.gke.io/service-name"
NegCRServicePortKey = "networking.gke.io/service-port"
// NegCRControllerValue is used as the value for the managed-by label on NEG CRs when enabled.
NegCRControllerValue = "neg-controller"
// NEG CR Condition Reasons
NegSyncSuccessful = "NegSyncSuccessful"
NegSyncFailed = "NegSyncFailed"
NegInitializationSuccessful = "NegInitializationSuccessful"
NegInitializationFailed = "NegInitializationFailed"
)
// SvcPortTuple is the tuple representing one service port
type SvcPortTuple struct {
// Port is the service port number
Port int32
// Name is the service port name
Name string
// TargetPort is the service target port.
// This can be a port number or named port
TargetPort string
}
func (t SvcPortTuple) Empty() bool {
return t.Port == 0 && t.Name == "" && t.TargetPort == ""
}
// String returns the string representation of SvcPortTuple
func (t SvcPortTuple) String() string {
return fmt.Sprintf("%s/%v-%s", t.Name, t.Port, t.TargetPort)
}
// SvcPortTupleSet is a set of SvcPortTuple
type SvcPortTupleSet map[SvcPortTuple]struct{}
// NewSvcPortTupleSet returns SvcPortTupleSet with the input tuples
func NewSvcPortTupleSet(tuples ...SvcPortTuple) SvcPortTupleSet {
set := SvcPortTupleSet{}
set.Insert(tuples...)
return set
}
// Insert inserts a SvcPortTuple into SvcPortTupleSet
func (set SvcPortTupleSet) Insert(tuples ...SvcPortTuple) {
for _, tuple := range tuples {
set[tuple] = struct{}{}
}
}
// Get returns the SvcPortTuple with matching svc port if found
func (set SvcPortTupleSet) Get(svcPort int32) (SvcPortTuple, bool) {
for tuple := range set {
if svcPort == tuple.Port {
return tuple, true
}
}
return SvcPortTuple{}, false
}
// PortInfo contains information associated with service port
type PortInfo struct {
// PortTuple is port tuple of a service.
PortTuple SvcPortTuple
// Subset name, subset is defined in Istio:DestinationRule, this is only used
// when --enable-csm=true.
Subset string
// Subset label, should set together with Subset.
SubsetLabels string
// NegName is the name of the NEG
NegName string
// ReadinessGate indicates if the NEG associated with the port has NEG readiness gate enabled
// This is enabled with service port is reference by ingress.
// If the service port is only exposed as stand alone NEG, it should not be enbled.
ReadinessGate bool
// RandomizeEndpoints indicates if the endpoints for the NEG associated with this port need to
// be selected at random, rather than selecting the endpoints of this service. This is applicable
// in GCE_VM_IP NEGs where the endpoints are the nodes instead of pods.
RandomizeEndpoints bool
}
// PortInfoMapKey is the Key of PortInfoMap
type PortInfoMapKey struct {
// ServicePort is the service port
ServicePort int32
// Istio:DestinationRule Subset, only used when --enable-csm=true
Subset string
}
// PortInfoMap is a map of PortInfoMapKey:PortInfo
type PortInfoMap map[PortInfoMapKey]PortInfo
func NewPortInfoMap(namespace, name string, svcPortTupleSet SvcPortTupleSet, namer NetworkEndpointGroupNamer, readinessGate bool, customNegNames map[SvcPortTuple]string) PortInfoMap {
ret := PortInfoMap{}
for svcPortTuple := range svcPortTupleSet {
negName, ok := customNegNames[svcPortTuple]
if !ok {
negName = namer.NEG(namespace, name, svcPortTuple.Port)
}
ret[PortInfoMapKey{svcPortTuple.Port, ""}] = PortInfo{
PortTuple: svcPortTuple,
NegName: negName,
ReadinessGate: readinessGate,
}
}
return ret
}
// NewPortInfoMapForVMIPNEG creates PortInfoMap with empty port tuple. Since VM_IP NEGs target
// the node instead of the pod, there is no port info to be stored.
func NewPortInfoMapForVMIPNEG(namespace, name string, namer NetworkEndpointGroupNamer, randomize bool) PortInfoMap {
ret := PortInfoMap{}
svcPortSet := make(SvcPortTupleSet)
svcPortSet.Insert(
// Insert Empty PortTuple for VmIp NEGs.
SvcPortTuple{},
)
for svcPortTuple := range svcPortSet {
ret[PortInfoMapKey{svcPortTuple.Port, ""}] = PortInfo{
PortTuple: svcPortTuple,
NegName: namer.VMIPNEG(namespace, name),
RandomizeEndpoints: randomize,
}
}
return ret
}
// NewPortInfoMapWithDestinationRule create PortInfoMap based on a gaven DesinationRule.
// Return error message if the DestinationRule contains duplicated subsets.
func NewPortInfoMapWithDestinationRule(namespace, name string, svcPortTupleSet SvcPortTupleSet, namer NetworkEndpointGroupNamer, readinessGate bool,
destinationRule *istioV1alpha3.DestinationRule) (PortInfoMap, error) {
ret := PortInfoMap{}
var duplicateSubset []string
for _, subset := range destinationRule.Subsets {
for tuple := range svcPortTupleSet {
key := PortInfoMapKey{tuple.Port, subset.Name}
if _, ok := ret[key]; ok {
duplicateSubset = append(duplicateSubset, subset.Name)
}
ret[key] = PortInfo{
PortTuple: tuple,
NegName: namer.NEGWithSubset(namespace, name, subset.Name, tuple.Port),
ReadinessGate: readinessGate,
Subset: subset.Name,
SubsetLabels: labels.Set(subset.Labels).String(),
}
}
}
if len(duplicateSubset) != 0 {
return ret, fmt.Errorf("Duplicated subsets: %s", strings.Join(duplicateSubset, ", "))
}
return ret, nil
}
// Merge merges p2 into p1 PortInfoMap
// It assumes the same key (service port) will have the same target port and negName
// If not, it will throw error
// If a key in p1 or p2 has readiness gate enabled, the merged port info will also has readiness gate enabled
// If a key in p1 or p2 has randomize endpoints enabled, the merged port info will also has randomize endpoints enabled.
// This field is only applicable for VMPrimaryIP NEGs.
func (p1 PortInfoMap) Merge(p2 PortInfoMap) error {
var err error
for mapKey, portInfo := range p2 {
mergedInfo := PortInfo{}
if existingPortInfo, ok := p1[mapKey]; ok {
if existingPortInfo.PortTuple != portInfo.PortTuple {
return fmt.Errorf("for service port %v, port tuple in existing map is %q, but the merge map has %q", mapKey, existingPortInfo.PortTuple, portInfo.PortTuple)
}
if existingPortInfo.NegName != portInfo.NegName {
return fmt.Errorf("for service port %v, NEG name in existing map is %q, but the merge map has %q", mapKey, existingPortInfo.NegName, portInfo.NegName)
}
if existingPortInfo.Subset != portInfo.Subset {
return fmt.Errorf("for service port %v, Subset name in existing map is %q, but the merge map has %q", mapKey, existingPortInfo.Subset, portInfo.Subset)
}
if existingPortInfo.RandomizeEndpoints != portInfo.RandomizeEndpoints {
return fmt.Errorf("For service port %v, Existing map has RandomizeEndpoints %v, but the merge map has %v", mapKey, existingPortInfo.RandomizeEndpoints, portInfo.RandomizeEndpoints)
}
mergedInfo.ReadinessGate = existingPortInfo.ReadinessGate
}
mergedInfo.PortTuple = portInfo.PortTuple
mergedInfo.NegName = portInfo.NegName
// Turn on the readiness gate if one of them is on
mergedInfo.ReadinessGate = mergedInfo.ReadinessGate || portInfo.ReadinessGate
mergedInfo.RandomizeEndpoints = portInfo.RandomizeEndpoints
mergedInfo.Subset = portInfo.Subset
mergedInfo.SubsetLabels = portInfo.SubsetLabels
p1[mapKey] = mergedInfo
}
return err
}
// Difference returns the entries of PortInfoMap which satisfies one of the following 2 conditions:
// 1. portInfo entry is not the present in p1
// 2. or the portInfo entry is not the same in p1 and p2.
func (p1 PortInfoMap) Difference(p2 PortInfoMap) PortInfoMap {
result := make(PortInfoMap)
for mapKey, p1PortInfo := range p1 {
p2PortInfo, ok := p2[mapKey]
if ok && reflect.DeepEqual(p1[mapKey], p2PortInfo) {
continue
}
result[mapKey] = p1PortInfo
}
return result
}
func (p1 PortInfoMap) ToPortNegMap() annotations.PortNegMap {
ret := annotations.PortNegMap{}
for mapKey, portInfo := range p1 {
ret[strconv.Itoa(int(mapKey.ServicePort))] = portInfo.NegName
}
return ret
}
func (p1 PortInfoMap) ToPortSubsetNegMap() annotations.PortSubsetNegMap {
ret := annotations.PortSubsetNegMap{}
for mapKey, portInfo := range p1 {
if m, ok := ret[mapKey.Subset]; ok {
m[strconv.Itoa(int(mapKey.ServicePort))] = portInfo.NegName
} else {
ret[mapKey.Subset] = map[string]string{strconv.Itoa(int(mapKey.ServicePort)): portInfo.NegName}
}
}
return ret
}
// NegsWithReadinessGate returns the NegNames which has readiness gate enabled
func (p1 PortInfoMap) NegsWithReadinessGate() sets.String {
ret := sets.NewString()
for _, info := range p1 {
if info.ReadinessGate {
ret.Insert(info.NegName)
}
}
return ret
}
// NegSyncerKey includes information to uniquely identify a NEG syncer
type NegSyncerKey struct {
// Namespace of service
Namespace string
// Name of service
Name string
// PortTuple is the port tuple of the service backing the NEG
PortTuple SvcPortTuple
// Subset name, subset is defined in Istio:DestinationRule, this is only used
// when --enable-csm=true.
Subset string
// Subset label, should set together with Subset.
SubsetLabels string
// NegType is the type of the network endpoints in this NEG.
NegType NetworkEndpointType
}
func (key NegSyncerKey) String() string {
return fmt.Sprintf("%s/%s-%s-%s-%s", key.Namespace, key.Name, key.Subset, key.PortTuple.String(), string(key.NegType))
}
// GetAPIVersion returns the compute API version to be used in order
// to create the negType specified in the given NegSyncerKey.
func (key NegSyncerKey) GetAPIVersion() meta.Version {
switch key.NegType {
case VmIpEndpointType:
return meta.VersionAlpha
case NonGCPPrivateEndpointType:
return meta.VersionAlpha
default:
return meta.VersionGA
}
}
// EndpointPodMap is a map from network endpoint to a namespaced name of a pod
type EndpointPodMap map[NetworkEndpoint]types.NamespacedName