-
Notifications
You must be signed in to change notification settings - Fork 248
/
handler.go
289 lines (263 loc) · 10.8 KB
/
handler.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
/*
* 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 (
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-dns/plugin/bcsscheduler/metrics"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
func init() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
}
func (bcs *BcsScheduler) tryReviseRequest(originalName string, req *request.Request) (revised bool) {
segs := dns.SplitDomainName(originalName)
if (req.Req.Question[0].Qtype == dns.TypeA || req.Req.Question[0].Qtype == dns.TypeAAAA) && len(segs) == 2 {
// A record
segs = append(segs, "svc", bcs.conf.Cluster, bcs.conf.Zones[0])
req.Clear()
req.Req.Question[0].Name = strings.Join(segs, ".")
req.Name()
req.Req.Question[0].Name = originalName
revised = true
} else if (req.Req.Question[0].Qtype == dns.TypeA ||
req.Req.Question[0].Qtype == dns.TypeAAAA) &&
// two scenario:
// svcname.namespace.svc
// podname.svcname.namespace.svc
(len(segs) == 3 || len(segs) == 4) &&
strings.HasSuffix(strings.TrimSuffix(originalName, "."), ".svc") {
segs = append(segs, bcs.conf.Cluster, bcs.conf.Zones[0])
req.Clear()
req.Req.Question[0].Name = strings.Join(segs, ".")
req.Name()
req.Req.Question[0].Name = originalName
revised = true
} else if req.Req.Question[0].Qtype == dns.TypeSRV &&
(strings.HasPrefix(originalName, "_") || strings.HasPrefix(originalName, "*.")) &&
len(segs) == 4 {
// srv record.
segs = append(segs, "svc", bcs.conf.Cluster, bcs.conf.Zones[0])
req.Clear()
req.Req.Question[0].Name = strings.Join(segs, ".")
req.Name()
req.Req.Question[0].Name = originalName
revised = true
}
return
}
func (bcs *BcsScheduler) getQualifiedQuestionName(originalName string, qType uint16) (string, bool) {
segs := dns.SplitDomainName(originalName)
qualified := false
if (qType == dns.TypeA || qType == dns.TypeAAAA) && len(segs) == 2 {
// A record, svcname.namespace
segs = append(segs, "svc", bcs.conf.Cluster, bcs.conf.Zones[0])
qualified = true
} else if (qType == dns.TypeA || qType == dns.TypeAAAA) && (len(segs) == 3 || len(segs) == 4) &&
strings.HasSuffix(strings.TrimSuffix(originalName, "."), ".svc") {
// two scenario:
// svcname.namespace.svc
// podname.svcname.namespace.svc
segs = append(segs, bcs.conf.Cluster, bcs.conf.Zones[0])
qualified = true
} else if qType == dns.TypeSRV && (strings.HasPrefix(originalName, "_") || strings.HasPrefix(originalName, "*.")) &&
len(segs) == 5 && strings.HasSuffix(strings.TrimSuffix(originalName, "."), ".svc") {
// srv record: _port._protocol.svcname.namespace.svc
segs = append(segs, bcs.conf.Cluster, bcs.conf.Zones[0])
qualified = true
}
return strings.Join(segs, "."), qualified
}
func (bcs *BcsScheduler) tryWithOutterDns(ctx context.Context, req *request.Request) (yes bool, code int, err error) {
zone := plugin.Zones(bcs.conf.Zones).Matches(req.Name())
switch {
case req.Req.Question[0].Qtype == dns.TypePTR:
fallthrough
case req.Req.Question[0].Qtype != dns.TypePTR && zone == "":
// not belongs to bcs zone.
if bcs.conf.Fallthrough {
code, err = plugin.NextOrFailure(bcs.Name(), bcs.Next, ctx, req.W, req.Req)
if err != nil {
log.Printf("[ERROR] proxy for %s err: %+v", req.Req.Question[0].Name, err)
return true, code, err
}
return true, code, nil
}
code, err = plugin.BackendError(bcs, zone, dns.RcodeNameError, *req, nil, plugin.Options{})
return true, code, err
}
return
}
func (bcs *BcsScheduler) inBcsZone(zoneName string) bool {
return dns.IsSubDomain("bcs.com.", zoneName)
}
func (bcs *BcsScheduler) inCurrentZone(zoneName string) bool {
return dns.IsSubDomain(bcs.PrimaryZone(), zoneName)
}
func (bcs *BcsScheduler) dealCommonResolve(zone string, state request.Request) (records, extra []dns.RR, err error) {
switch state.Type() {
case "A":
records, err = plugin.A(bcs, zone, state, nil, plugin.Options{})
case "AAAA":
records, err = plugin.AAAA(bcs, zone, state, nil, plugin.Options{})
case "TXT":
records, err = plugin.TXT(bcs, zone, state, plugin.Options{})
case "CNAME":
records, err = plugin.CNAME(bcs, zone, state, plugin.Options{})
case "PTR":
records, err = plugin.PTR(bcs, zone, state, plugin.Options{})
case "MX":
records, extra, err = plugin.MX(bcs, zone, state, plugin.Options{})
case "SRV":
records, extra, err = plugin.SRV(bcs, zone, state, plugin.Options{})
case "SOA":
records, err = plugin.SOA(bcs, zone, state, plugin.Options{})
case "NS":
if state.Name() == zone {
records, extra, err = plugin.NS(bcs, zone, state, plugin.Options{})
break
}
fallthrough
default:
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
_, err = plugin.A(bcs, zone, state, nil, plugin.Options{})
}
return
}
// validateRequest xxx
// this function must be called after tryReviseRequest() is called.
func (bcs *BcsScheduler) validateRequest(state request.Request) error {
segs := dns.SplitDomainName(state.Name())
segments := segs[:len(segs)-dns.CountLabel(bcs.PrimaryZone())]
switch state.QType() {
case dns.TypeSRV:
if len(segments) != 5 {
return fmt.Errorf("invalid type SRV request, name: %s", state.Name())
}
if segments[0] == "_" || segments[1] == "_" {
return fmt.Errorf("invalid type SRV request, name: %s", state.Name())
}
case dns.TypeA:
if len(segments) != 3 && len(segments) != 4 {
return fmt.Errorf("invalid type A request, name: %s", state.Name())
}
}
return nil
}
// ServeDNS implements the plugin.Handler interface.
func (bcs *BcsScheduler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
start := time.Now()
originalName := strings.ToLower(dns.Name(r.Question[0].Name).String())
state := request.Request{W: w, Req: r}
if state.QClass() != dns.ClassINET {
metrics.RequestCount.WithLabelValues(metrics.Failure).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Failure).Observe(time.Since(start).Seconds())
return dns.RcodeServerFailure, plugin.Error(bcs.Name(), errors.New("can only deal with ClassINET"))
}
// handle the special scenes.
bcs.tryReviseRequest(originalName, &state)
yes, code, zerr := bcs.tryWithOutterDns(ctx, &state)
if yes {
return code, zerr
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
writeBackMsg := func(rr []dns.RR, ext []dns.RR) {
m.Answer = append(m.Answer, rr...)
m.Extra = append(m.Extra, ext...)
state.SizeAndDo(m)
newMsg := state.Scrub(m)
w.WriteMsg(newMsg)
}
zone := plugin.Zones(bcs.conf.Zones).Matches(state.Name())
// log.Printf("[DEBUG] zones: %v, zone: %s, name: %s", bcs.conf.Zones, zone, state.Name())
if zone != "" && bcs.inBcsZone(zone) {
if bcs.inCurrentZone(state.Name()) {
// request is in current zone.
if e := bcs.validateRequest(state); e != nil {
log.Printf("[ERROR] validate request[%s] failed, err: %v", state.Name(), e)
metrics.RequestCount.WithLabelValues(metrics.Failure).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Failure).Observe(time.Since(start).Seconds())
return plugin.BackendError(bcs, zone, dns.RcodeNameError, state, e, plugin.Options{})
}
var records, extra []dns.RR
var err error
records, extra, err = bcs.dealCommonResolve(zone, state)
recursiveRecords, yes := bcs.dealRecursiveDNSWithClusterIP(state)
if yes && len(recursiveRecords) != 0 {
// add additional recursive records
records = append(records, recursiveRecords...)
if err != nil {
// do response with these records
writeBackMsg(records, extra)
metrics.RequestCount.WithLabelValues(metrics.Success).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Success).Observe(time.Since(start).Seconds())
return dns.RcodeSuccess, nil
}
}
if err != nil {
log.Printf("[ERROR] scheduler get current request failed err: %s", err.Error())
metrics.RequestCount.WithLabelValues(metrics.Failure).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Failure).Observe(time.Since(start).Seconds())
return dns.RcodeServerFailure, err
}
if len(records) == 0 {
if state.QType() != dns.TypeAAAA {
log.Printf("[ERROR] scheduler get no endpoint for %s, type: %s", state.Name(), state.Type())
}
metrics.RequestCount.WithLabelValues(metrics.Failure).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Failure).Observe(time.Since(start).Seconds())
return plugin.BackendError(bcs, zone, dns.RcodeServerFailure, state, fmt.Errorf("got no endpoints"),
plugin.Options{})
}
writeBackMsg(records, extra)
metrics.RequestCount.WithLabelValues(metrics.Success).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Success).Observe(time.Since(start).Seconds())
return dns.RcodeSuccess, nil
}
// request is in other bcs zone. reoute to upper bcs-dns server.
msg, err := bcs.Lookup(state, state.Name(), state.QType())
if err != nil {
log.Printf("[ERROR] request %s post to bcs upper cluster failed, err: %s", state.Name(), err.Error())
metrics.RequestOutProxyCount.WithLabelValues(metrics.Failure).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Failure).Observe(time.Since(start).Seconds())
return dns.RcodeServerFailure, err
}
writeBackMsg(msg.Answer, msg.Extra)
metrics.RequestOutProxyCount.WithLabelValues(metrics.Success).Inc()
metrics.RequestLatency.WithLabelValues(metrics.Success).Observe(time.Since(start).Seconds())
return dns.RcodeSuccess, nil
}
// this request is not belong to bcs zone.
return bcs.routeToNextOrFailure(ctx, w, state)
}
func (bcs *BcsScheduler) routeToNextOrFailure(ctx context.Context, w dns.ResponseWriter, req request.Request) (int,
error) {
// clear request name first, in case the question name is not same with name.
req.Clear()
if bcs.conf.Fallthrough {
return plugin.NextOrFailure(bcs.Name(), bcs.Next, ctx, w, req.Req)
}
log.Printf("[WARN] scheduler do not forward request to next plugin. request name:%v ", req.Name())
// Make err nil when returning here, so we don't log spam for NXDOMAIN.
return plugin.BackendError(bcs, req.Name(), dns.RcodeNameError, req, nil, plugin.Options{})
}
// Name implements the Handler interface.
func (bcs *BcsScheduler) Name() string { return "bcsscheduler" }