-
Notifications
You must be signed in to change notification settings - Fork 273
/
azure_utils.go
491 lines (418 loc) · 13.6 KB
/
azure_utils.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
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 provider
import (
"context"
"fmt"
"net"
"strings"
"sync"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
utilnet "k8s.io/utils/net"
"k8s.io/utils/pointer"
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
)
var strToExtendedLocationType = map[string]network.ExtendedLocationTypes{
"edgezone": network.EdgeZone,
}
// lockMap used to lock on entries
type lockMap struct {
sync.Mutex
mutexMap map[string]*sync.Mutex
}
// NewLockMap returns a new lock map
func newLockMap() *lockMap {
return &lockMap{
mutexMap: make(map[string]*sync.Mutex),
}
}
// LockEntry acquires a lock associated with the specific entry
func (lm *lockMap) LockEntry(entry string) {
lm.Lock()
// check if entry does not exists, then add entry
mutex, exists := lm.mutexMap[entry]
if !exists {
mutex = &sync.Mutex{}
lm.mutexMap[entry] = mutex
}
lm.Unlock()
mutex.Lock()
}
// UnlockEntry release the lock associated with the specific entry
func (lm *lockMap) UnlockEntry(entry string) {
lm.Lock()
defer lm.Unlock()
mutex, exists := lm.mutexMap[entry]
if !exists {
return
}
mutex.Unlock()
}
func getContextWithCancel() (context.Context, context.CancelFunc) {
return context.WithCancel(context.Background())
}
func convertMapToMapPointer(origin map[string]string) map[string]*string {
newly := make(map[string]*string)
for k, v := range origin {
value := v
newly[k] = &value
}
return newly
}
func parseTags(tags string, tagsMap map[string]string) map[string]*string {
formatted := make(map[string]*string)
if tags != "" {
kvs := strings.Split(tags, consts.TagsDelimiter)
for _, kv := range kvs {
res := strings.Split(kv, consts.TagKeyValueDelimiter)
if len(res) != 2 {
klog.Warningf("parseTags: error when parsing key-value pair %s, would ignore this one", kv)
continue
}
k, v := strings.TrimSpace(res[0]), strings.TrimSpace(res[1])
if k == "" {
klog.Warning("parseTags: empty key, ignoring this key-value pair")
continue
}
formatted[k] = pointer.String(v)
}
}
if len(tagsMap) > 0 {
for key, value := range tagsMap {
key, value := strings.TrimSpace(key), strings.TrimSpace(value)
if key == "" {
klog.Warningf("parseTags: empty key, ignoring this key-value pair")
continue
}
if found, k := findKeyInMapCaseInsensitive(formatted, key); found && k != key {
klog.V(4).Infof("parseTags: found identical keys: %s from tags and %s from tagsMap (case-insensitive), %s will replace %s", k, key, key, k)
delete(formatted, k)
}
formatted[key] = pointer.String(value)
}
}
return formatted
}
func findKeyInMapCaseInsensitive(targetMap map[string]*string, key string) (bool, string) {
for k := range targetMap {
if strings.EqualFold(k, key) {
return true, k
}
}
return false, ""
}
func (az *Cloud) reconcileTags(currentTagsOnResource, newTags map[string]*string) (reconciledTags map[string]*string, changed bool) {
var systemTags []string
systemTagsMap := make(map[string]*string)
if az.SystemTags != "" {
systemTags = strings.Split(az.SystemTags, consts.TagsDelimiter)
for i := 0; i < len(systemTags); i++ {
systemTags[i] = strings.TrimSpace(systemTags[i])
}
for _, systemTag := range systemTags {
systemTagsMap[systemTag] = pointer.String("")
}
}
// if the systemTags is not set, just add/update new currentTagsOnResource and not delete old currentTagsOnResource
for k, v := range newTags {
found, key := findKeyInMapCaseInsensitive(currentTagsOnResource, k)
if !found {
currentTagsOnResource[k] = v
changed = true
} else if !strings.EqualFold(pointer.StringDeref(v, ""), pointer.StringDeref(currentTagsOnResource[key], "")) {
currentTagsOnResource[key] = v
changed = true
}
}
// if the systemTags is set, delete the old currentTagsOnResource
if len(systemTagsMap) > 0 {
for k := range currentTagsOnResource {
if _, ok := newTags[k]; !ok {
if found, _ := findKeyInMapCaseInsensitive(systemTagsMap, k); !found {
delete(currentTagsOnResource, k)
changed = true
}
}
}
}
return currentTagsOnResource, changed
}
func (az *Cloud) getVMSetNamesSharingPrimarySLB() sets.Set[string] {
vmSetNames := make([]string, 0)
if az.NodePoolsWithoutDedicatedSLB != "" {
vmSetNames = strings.Split(az.Config.NodePoolsWithoutDedicatedSLB, consts.VMSetNamesSharingPrimarySLBDelimiter)
for i := 0; i < len(vmSetNames); i++ {
vmSetNames[i] = strings.ToLower(strings.TrimSpace(vmSetNames[i]))
}
}
return sets.New(vmSetNames...)
}
func getExtendedLocationTypeFromString(extendedLocationType string) network.ExtendedLocationTypes {
extendedLocationType = strings.ToLower(extendedLocationType)
if val, ok := strToExtendedLocationType[extendedLocationType]; ok {
return val
}
return network.EdgeZone
}
func getServiceAdditionalPublicIPs(service *v1.Service) ([]string, error) {
if service == nil {
return nil, nil
}
result := []string{}
if val, ok := service.Annotations[consts.ServiceAnnotationAdditionalPublicIPs]; ok {
pips := strings.Split(strings.TrimSpace(val), ",")
for _, pip := range pips {
ip := strings.TrimSpace(pip)
if ip == "" {
continue // skip empty string
}
if net.ParseIP(ip) == nil {
return nil, fmt.Errorf("%s is not a valid IP address", ip)
}
result = append(result, ip)
}
}
return result, nil
}
func getNodePrivateIPAddress(node *v1.Node, isIPv6 bool) string {
for _, nodeAddress := range node.Status.Addresses {
if strings.EqualFold(string(nodeAddress.Type), string(v1.NodeInternalIP)) &&
utilnet.IsIPv6String(nodeAddress.Address) == isIPv6 {
klog.V(6).Infof("getNodePrivateIPAddress: node %s, ip %s", node.Name, nodeAddress.Address)
return nodeAddress.Address
}
}
klog.Warningf("getNodePrivateIPAddress: empty ip found for node %s", node.Name)
return ""
}
func getNodePrivateIPAddresses(node *v1.Node) []string {
addresses := make([]string, 0)
for _, nodeAddress := range node.Status.Addresses {
if strings.EqualFold(string(nodeAddress.Type), string(v1.NodeInternalIP)) {
addresses = append(addresses, nodeAddress.Address)
}
}
return addresses
}
func getBoolValueFromServiceAnnotations(service *v1.Service, key string) bool {
if l, found := service.Annotations[key]; found {
return strings.EqualFold(strings.TrimSpace(l), consts.TrueAnnotationValue)
}
return false
}
func sameContentInSlices(s1 []string, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
map1 := make(map[string]int)
for _, s := range s1 {
map1[s]++
}
for _, s := range s2 {
if v, ok := map1[s]; !ok || v <= 0 {
return false
}
map1[s]--
}
return true
}
func removeDuplicatedSecurityRules(rules []network.SecurityRule) []network.SecurityRule {
ruleNames := make(map[string]bool)
for i := len(rules) - 1; i >= 0; i-- {
if _, ok := ruleNames[pointer.StringDeref(rules[i].Name, "")]; ok {
klog.Warningf("Found duplicated rule %s, will be removed.", pointer.StringDeref(rules[i].Name, ""))
rules = append(rules[:i], rules[i+1:]...)
}
ruleNames[pointer.StringDeref(rules[i].Name, "")] = true
}
return rules
}
func getVMSSVMCacheKey(resourceGroup, vmssName string) string {
cacheKey := strings.ToLower(fmt.Sprintf("%s/%s", resourceGroup, vmssName))
return cacheKey
}
// isNodeInVMSSVMCache check whether nodeName is in vmssVMCache
func isNodeInVMSSVMCache(nodeName string, vmssVMCache *azcache.TimedCache) bool {
if vmssVMCache == nil {
return false
}
var isInCache bool
vmssVMCache.Lock.Lock()
defer vmssVMCache.Lock.Unlock()
for _, entry := range vmssVMCache.Store.List() {
if entry != nil {
e := entry.(*azcache.AzureCacheEntry)
e.Lock.Lock()
data := e.Data
if data != nil {
data.(*sync.Map).Range(func(vmName, _ interface{}) bool {
if vmName != nil && vmName.(string) == nodeName {
isInCache = true
return false
}
return true
})
}
e.Lock.Unlock()
}
if isInCache {
break
}
}
return isInCache
}
func extractVmssVMName(name string) (string, string, error) {
split := strings.SplitAfter(name, consts.VMSSNameSeparator)
if len(split) < 2 {
klog.V(3).Infof("Failed to extract vmssVMName %q", name)
return "", "", ErrorNotVmssInstance
}
ssName := strings.Join(split[0:len(split)-1], "")
// removing the trailing `vmssNameSeparator` since we used SplitAfter
ssName = ssName[:len(ssName)-1]
instanceID := split[len(split)-1]
return ssName, instanceID, nil
}
// isServiceDualStack checks if a Service is dual-stack or not.
func isServiceDualStack(svc *v1.Service) bool {
return len(svc.Spec.IPFamilies) == 2
}
// getIPFamiliesEnabled checks if IPv4, IPv6 are enabled according to svc.Spec.IPFamilies.
func getIPFamiliesEnabled(svc *v1.Service) (v4Enabled bool, v6Enabled bool) {
for _, ipFamily := range svc.Spec.IPFamilies {
if ipFamily == v1.IPv4Protocol {
v4Enabled = true
} else if ipFamily == v1.IPv6Protocol {
v6Enabled = true
}
}
return
}
// getServiceLoadBalancerIP retrieves LB IP from IPv4 annotation, then IPv6 annotation, then service.Spec.LoadBalancerIP.
func getServiceLoadBalancerIP(service *v1.Service, isIPv6 bool) string {
if service == nil {
return ""
}
if ip, ok := service.Annotations[consts.ServiceAnnotationLoadBalancerIPDualStack[isIPv6]]; ok && ip != "" {
return ip
}
// Retrieve LB IP from service.Spec.LoadBalancerIP (will be deprecated)
svcLBIP := service.Spec.LoadBalancerIP
if (net.ParseIP(svcLBIP).To4() != nil && !isIPv6) ||
(net.ParseIP(svcLBIP).To4() == nil && isIPv6) {
return svcLBIP
}
return ""
}
func getServiceLoadBalancerIPs(service *v1.Service) []string {
if service == nil {
return []string{}
}
ips := []string{}
if ip, ok := service.Annotations[consts.ServiceAnnotationLoadBalancerIPDualStack[false]]; ok && ip != "" {
ips = append(ips, ip)
}
if ip, ok := service.Annotations[consts.ServiceAnnotationLoadBalancerIPDualStack[true]]; ok && ip != "" {
ips = append(ips, ip)
}
if len(ips) != 0 {
return ips
}
lbIP := service.Spec.LoadBalancerIP
if lbIP != "" {
ips = append(ips, lbIP)
}
return ips
}
// setServiceLoadBalancerIP sets LB IP to a Service
func setServiceLoadBalancerIP(service *v1.Service, ip string) {
if service.Annotations == nil {
service.Annotations = map[string]string{}
}
isIPv6 := net.ParseIP(ip).To4() == nil
service.Annotations[consts.ServiceAnnotationLoadBalancerIPDualStack[isIPv6]] = ip
}
func getServicePIPName(service *v1.Service, isIPv6 bool) string {
if service == nil {
return ""
}
if !isServiceDualStack(service) {
return service.Annotations[consts.ServiceAnnotationPIPNameDualStack[false]]
}
return service.Annotations[consts.ServiceAnnotationPIPNameDualStack[isIPv6]]
}
func getServicePIPPrefixID(service *v1.Service, isIPv6 bool) string {
if service == nil {
return ""
}
if !isServiceDualStack(service) {
return service.Annotations[consts.ServiceAnnotationPIPPrefixIDDualStack[false]]
}
return service.Annotations[consts.ServiceAnnotationPIPPrefixIDDualStack[isIPv6]]
}
func getResourceByIPFamily(resource string, isIPv6 bool) string {
if isIPv6 {
return fmt.Sprintf("%s-%s", resource, v6Suffix)
}
return resource
}
// isFIPIPv6 checks if the frontend IP configuration is of IPv6.
func (az *Cloud) isFIPIPv6(fip *network.FrontendIPConfiguration, pips *[]network.PublicIPAddress, isInternal bool) (isIPv6 bool, err error) {
if err := az.safeListPIP(az.ResourceGroup, pips); err != nil {
return false, fmt.Errorf("failed to ensure PIP is refreshed: %w", err)
}
if isInternal {
if fip.FrontendIPConfigurationPropertiesFormat != nil {
if fip.FrontendIPConfigurationPropertiesFormat.PrivateIPAddressVersion != "" {
return fip.FrontendIPConfigurationPropertiesFormat.PrivateIPAddressVersion == network.IPv6, nil
}
return net.ParseIP(pointer.StringDeref(fip.FrontendIPConfigurationPropertiesFormat.PrivateIPAddress, "")).To4() == nil, nil
}
klog.Errorf("Checking IP Family of frontend IP configuration %q of internal Service but its"+
" FrontendIPConfigurationPropertiesFormat is nil. It's considered to be IPv4",
pointer.StringDeref(fip.Name, ""))
return
}
var fipPIPID string
if fip.FrontendIPConfigurationPropertiesFormat != nil && fip.FrontendIPConfigurationPropertiesFormat.PublicIPAddress != nil {
fipPIPID = pointer.StringDeref(fip.FrontendIPConfigurationPropertiesFormat.PublicIPAddress.ID, "")
}
for _, pip := range *pips {
id := pointer.StringDeref(pip.ID, "")
if !strings.EqualFold(fipPIPID, id) {
continue
}
if pip.PublicIPAddressPropertiesFormat != nil {
// First check PublicIPAddressVersion, then IPAddress
if pip.PublicIPAddressPropertiesFormat.PublicIPAddressVersion == network.IPv6 ||
net.ParseIP(pointer.StringDeref(pip.PublicIPAddressPropertiesFormat.IPAddress, "")).To4() == nil {
isIPv6 = true
break
}
}
break
}
return isIPv6, nil
}
// getResourceIDPrefix returns a substring from the provided one between beginning and the last "/".
func getResourceIDPrefix(id string) string {
idx := strings.LastIndexByte(id, '/')
if idx == -1 {
return id // Should not happen
}
return id[:idx]
}