/
events.go
196 lines (177 loc) · 6.25 KB
/
events.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 bcsscheduler
import (
"log"
"reflect"
"strings"
"time"
bcstypes "github.com/Tencent/bk-bcs/bcs-common/common/types"
"github.com/Tencent/bk-bcs/bcs-services/bcs-dns/plugin/bcsscheduler/metrics"
"github.com/coredns/coredns/plugin/etcd/msg"
)
func (bcs *BcsScheduler) svcOnAdd(obj interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
}
func (bcs *BcsScheduler) svcOnUpdate(old, cur interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
//Service update, cluster ip list may be changed
//update remote storage
}
func (bcs *BcsScheduler) svcOnDelete(obj interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
}
/*
* EventFuncs for BcsEndpoint event funcs
*/
func (bcs *BcsScheduler) endpointOnAdd(obj interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
start := time.Now()
endpoint, ok := obj.(*bcstypes.BcsEndpoint)
if !ok {
log.Printf("[ERROR] scheduler endpoint ADD event get error data type")
return
}
domain := endpoint.GetName() + "." + endpoint.GetNamespace() + ".svc." + bcs.PrimaryZone()
svc := bcs.endpoint2Message(endpoint)
if err := bcs.storage.AddService(domain, svc); err != nil {
//todo(developer): DELETE error, try again?
log.Printf("[ERROR] scheduler ADD endpoint %s to storage failed, %s", domain, err.Error())
return
}
log.Printf("[WARN] scheduler ADD domain %s in sotrage success.", domain)
metrics.StorageOperatorTotal.WithLabelValues(metrics.AddOperation).Inc()
metrics.StorageOperatorLatency.WithLabelValues(metrics.AddOperation).Observe(time.Since(start).Seconds())
}
func (bcs *BcsScheduler) endpointOnUpdate(old, cur interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
start := time.Now()
if reflect.DeepEqual(old, cur) {
return
}
oldEndpoint, ok := old.(*bcstypes.BcsEndpoint)
if !ok {
log.Printf("[ERROR] scheduler endpoint UPDATE get error data type in oldData.")
return
}
curEndpoint, ok := cur.(*bcstypes.BcsEndpoint)
if !ok {
log.Printf("[ERROR] scheduler endpoint UPDATE get error data type in curData")
return
}
domain := curEndpoint.GetName() + "." + curEndpoint.GetNamespace() + ".svc." + bcs.PrimaryZone()
//change to endpoints
oldSvc := bcs.endpoint2Message(oldEndpoint)
curSvc := bcs.endpoint2Message(curEndpoint)
if err := bcs.storage.UpdateService(domain, oldSvc, curSvc); err != nil {
//todo(developer): update error, try again?
log.Printf("[ERROR] scheduler update endpoint [%s] to storage failed, %s", domain, err.Error())
return
}
log.Printf("[WARN] scheduler update %s to storage success.", domain)
metrics.StorageOperatorTotal.WithLabelValues(metrics.UpdateOperation).Inc()
metrics.StorageOperatorLatency.WithLabelValues(metrics.UpdateOperation).Observe(time.Since(start).Seconds())
}
func (bcs *BcsScheduler) endpointOnDelete(obj interface{}) {
if bcs.storage == nil || !bcs.registery.IsMaster() {
return
}
start := time.Now()
endpoint, ok := obj.(*bcstypes.BcsEndpoint)
if !ok {
log.Printf("[ERROR] scheduler endpoint DELETE event get error data type")
return
}
domain := endpoint.GetName() + "." + endpoint.GetNamespace() + ".svc." + bcs.PrimaryZone()
svc := bcs.endpoint2Message(endpoint)
if err := bcs.storage.DeleteService(domain, svc); err != nil {
//todo(developer): DELETE error, try again?
log.Printf("[ERROR] scheduler delete endpoint %s to storage failed, %s", domain, err.Error())
return
}
log.Printf("[WARN] scheduler delete domain %s in sotrage success.", domain)
metrics.StorageOperatorTotal.WithLabelValues(metrics.DeleteOperation).Inc()
metrics.StorageOperatorLatency.WithLabelValues(metrics.DeleteOperation).Observe(time.Since(start).Seconds())
}
//endpoint2Message create etcd message with BcsEndpoint
//now only create etcd service for TypeA
//todo(developer): PTR/SRV message will support future
func (bcs *BcsScheduler) endpoint2Message(endpoint *bcstypes.BcsEndpoint) (svcList []msg.Service) {
if endpoint == nil {
return svcList
}
if endpoint.Endpoints == nil || len(endpoint.Endpoints) == 0 {
return svcList
}
//check cluster ip address list
var clusterIPs []string
service := bcs.svcCache.GetServiceByEndpoint(endpoint)
if service == nil {
//todo(developer): lost bcs service info in cache. how to fix
log.Printf("[ERROR] %s/%s endpoint lost bcs service in cache", endpoint.GetNamespace(), endpoint.GetName())
} else {
if service.Spec.ClusterIP != nil && len(service.Spec.ClusterIP) != 0 {
clusterIPs = service.Spec.ClusterIP
}
}
if len(clusterIPs) != 0 {
//get cluster ip address, push to cluster storage
//no matter what network mode is
for _, ip := range clusterIPs {
svc := createDefaultMessage(ip)
svcList = append(svcList, svc)
}
return svcList
}
//no cluster ip, construct etcd service with endpoints
for _, ep := range endpoint.Endpoints {
mode := strings.ToLower(ep.NetworkMode)
if mode == "none" {
//filter none network mode
continue
}
var svc msg.Service
if mode == "bridge" || mode == "host" {
//in mesos: no solution for `bridge`, `host` interconnection.
//handle it with node ip address
svc = createDefaultMessage(ep.NodeIP)
} else {
//importance: `default` mode is bridge in k8s.
//user mode also setting to container ip address
svc = createDefaultMessage(ep.ContainerIP)
}
svcList = append(svcList, svc)
}
return svcList
//todo(developer): SRV SUPPORT
}
//createDefaultMessage default etcd service message
func createDefaultMessage(ip string) msg.Service {
return msg.Service{
Host: ip,
Port: 0,
Priority: 10,
Weight: 10,
TTL: 5,
}
}