-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
236 lines (203 loc) · 5.48 KB
/
common.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
package kubernetes
import (
"fmt"
"github.com/golang/glog"
"strconv"
"strings"
)
type ResourceType int
const (
SERVICE_TYPE ResourceType = 1
DEPLOYMENT_TYPE ResourceType = 2
POD_TYPE ResourceType = 3
INGRESS_TYPE ResourceType = 4
ENVOY_ENABLED = "traffic.envoy.enabled"
ENVOY_PROXY_ANNOTATION = "traffic.envoy.proxy"
DEFAULT_WEIGHT = 100
POD_SERVICE_PREFIX = "traffic.svc."
POD_DEPLOYMENT_PREFIX = "traffic.rs."
ENVOY_ENABLED_BY_DEPLOYMENT = POD_DEPLOYMENT_PREFIX + "envoy.enabled"
PROTO_HTTP = 2
PROTO_TCP = 1
PROTO_DIRECT = 0
)
func GetProtocol(value string) int {
value = strings.ToLower(value)
switch value {
case "":
return -1
case "http":
return PROTO_HTTP
case "tcp":
return PROTO_TCP
default:
return PROTO_DIRECT
}
}
func GetLabelValueUInt32(value string) uint32 {
if value == "" {
return 0
}
i, err := strconv.Atoi(value)
if err != nil {
return 0
}
return uint32(i)
}
func GetLabelValueUInt64(value string) uint64 {
if value == "" {
return 0
}
i, err := strconv.Atoi(value)
if err != nil {
return 0
}
return uint64(i)
}
func GetLabelValueBool(value string) bool {
return strings.EqualFold(value, "true")
}
func GetLabelValueFloat64(value string) float64 {
if value == "" {
return 0
}
i, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0
}
return i
}
func GetLabelValueInt64(value string) int64 {
if value == "" {
return 0
}
i, err := strconv.Atoi(value)
if err != nil {
return 0
}
return int64(i)
}
func ServicePortProtocol(port uint32) string {
return fmt.Sprintf("traffic.port.%d", port)
}
func IngressAttrLabel(port uint32, attr string) string {
return fmt.Sprintf("traffic.ingress.port.%d.%s", port, attr)
}
func PodPortProtcolByService(svc string, port uint32) string {
return fmt.Sprintf("%s%s.port.%d", POD_SERVICE_PREFIX, svc, port)
}
func PodTargetPortProtcolByService(svc string, port uint32) string {
return fmt.Sprintf("%s%s.target.port.%d", POD_SERVICE_PREFIX, svc, port)
}
func podKeyByService(svc string, key string) string {
return fmt.Sprintf("%s%s.%s", POD_SERVICE_PREFIX, svc, key)
}
func PodEnvoyByService(svc string) string {
return podKeyByService(svc, "envoy")
}
func AnnotationHasDeploymentLabel(label string) bool {
return strings.HasPrefix(label, POD_DEPLOYMENT_PREFIX)
}
func DeploymentLabelToPodAnnotation(label string) string {
return fmt.Sprintf("%s%s", POD_DEPLOYMENT_PREFIX, label[len("traffic."):])
}
func AnnotationHasServiceLabel(svc string, label string) bool {
return strings.HasPrefix(label, fmt.Sprintf("%s%s.", POD_SERVICE_PREFIX, svc))
}
func ServiceLabelToPodAnnotation(svc string, label string) string {
return podKeyByService(svc, label[len("traffic."):])
}
func (e ResourceType) String() string {
switch e {
case POD_TYPE:
return "Pod"
case DEPLOYMENT_TYPE:
return "Deployment"
case SERVICE_TYPE:
return "Service"
default:
return fmt.Sprintf("UNKNOWN(%d)", int(e))
}
}
type ResourceInfoPointer interface {
GetSelector() map[string]string
Namespace() string
Name() string
Type() ResourceType
String() string
}
func (manager *K8sResourceManager) addResource(resource ResourceInfoPointer) {
if glog.V(2) {
glog.Infof("add %s", resource.String())
}
for k, v := range resource.GetSelector() {
key := fmt.Sprintf("%s:%s:%s", resource.Namespace(), k, v)
typeResourceMap := manager.labelTypeResourceMap[key]
if typeResourceMap == nil {
typeResourceMap = make(ResourcesOnLabel)
}
typeResourceMap[resource.Type()] = append(typeResourceMap[resource.Type()], resource)
manager.labelTypeResourceMap[key] = typeResourceMap
}
}
func (manager *K8sResourceManager) removeResource(resource ResourceInfoPointer) {
if glog.V(2) {
glog.Infof("remove %s", resource.String())
}
for k, v := range resource.GetSelector() {
key := fmt.Sprintf("%s:%s:%s", resource.Namespace(), k, v)
typeResourceMap := manager.labelTypeResourceMap[key]
if typeResourceMap == nil {
continue
}
resources := typeResourceMap[resource.Type()]
var matched []ResourceInfoPointer
for _, existResource := range resources {
if existResource.Name() == resource.Name() {
continue
}
matched = append(matched, existResource)
}
typeResourceMap[resource.Type()] = matched
}
}
func (manager *K8sResourceManager) GetMatchedResources(resource ResourceInfoPointer, matchType ResourceType) []ResourceInfoPointer {
if !manager.IsLocked() {
panic("K8sResourceManager should be locked in GetMatchedResources()")
}
countMap := make(map[ResourceInfoPointer]*int)
for k, v := range resource.GetSelector() {
key := fmt.Sprintf("%s:%s:%s", resource.Namespace(), k, v)
typeResourceMap := manager.labelTypeResourceMap[key]
if typeResourceMap == nil {
return nil
}
resources := typeResourceMap[matchType]
for _, matchResource := range resources {
if countMap[matchResource] == nil {
count := 1
countMap[matchResource] = &count
} else {
*countMap[matchResource]++
}
}
}
//type service < deployment < pod
returnParent := resource.Type() > matchType
var result []ResourceInfoPointer
for matchResource, countPtr := range countMap {
if returnParent {
if *countPtr != len(matchResource.GetSelector()) {
//return services or deployments from pod
//count should be same with service or deployment selector
continue
}
} else if *countPtr != len(resource.GetSelector()) {
//return pods from service or deployment
//count should be same with pod labels
continue
}
result = append(result, matchResource)
}
return result
}