-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathazure_utils.go
169 lines (139 loc) · 4.54 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
/*
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"
"strings"
"sync"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/klog/v2"
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
)
var strToExtendedLocationType = map[string]network.ExtendedLocationTypes{
"edgezone": network.ExtendedLocationTypesEdgeZone,
}
// 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
if _, exists := lm.mutexMap[entry]; !exists {
lm.addEntry(entry)
}
lm.Unlock()
lm.lockEntry(entry)
}
// UnlockEntry release the lock associated with the specific entry
func (lm *lockMap) UnlockEntry(entry string) {
lm.Lock()
defer lm.Unlock()
if _, exists := lm.mutexMap[entry]; !exists {
return
}
lm.unlockEntry(entry)
}
func (lm *lockMap) addEntry(entry string) {
lm.mutexMap[entry] = &sync.Mutex{}
}
func (lm *lockMap) lockEntry(entry string) {
lm.mutexMap[entry].Lock()
}
func (lm *lockMap) unlockEntry(entry string) {
lm.mutexMap[entry].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) map[string]*string {
kvs := strings.Split(tags, consts.TagsDelimiter)
formatted := make(map[string]*string)
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 == "" || v == "" {
klog.Warningf("parseTags: error when parsing key-value pair %s-%s, would ignore this one", k, v)
continue
}
formatted[k] = to.StringPtr(v)
}
return formatted
}
func (az *Cloud) reconcileTags(currentTagsOnResource, newTags map[string]*string) (reconciledTags map[string]*string, changed bool) {
var systemTags []string
var systemTagsMap sets.String
if az.SystemTags != "" {
systemTags = strings.Split(az.SystemTags, consts.TagsDelimiter)
for i := 0; i < len(systemTags); i++ {
systemTags[i] = strings.TrimSpace(systemTags[i])
}
systemTagsMap = sets.NewString(systemTags...)
}
// if the systemTags is not set, just add/update new currentTagsOnResource and not delete old currentTagsOnResource
for k, v := range newTags {
if vv, ok := currentTagsOnResource[k]; !ok || !strings.EqualFold(to.String(v), to.String(vv)) {
currentTagsOnResource[k] = 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 && !systemTagsMap.Has(k) {
delete(currentTagsOnResource, k)
changed = true
}
}
}
return currentTagsOnResource, changed
}
func (az *Cloud) getVMSetNamesSharingPrimarySLB() sets.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.NewString(vmSetNames...)
}
func getExtendedLocationTypeFromString(extendedLocationType string) network.ExtendedLocationTypes {
extendedLocationType = strings.ToLower(extendedLocationType)
if val, ok := strToExtendedLocationType[extendedLocationType]; ok {
return val
}
return network.ExtendedLocationTypesEdgeZone
}