Skip to content

Commit 649fd52

Browse files
update domain name match logic (#14)
1 parent 2d69b31 commit 649fd52

File tree

5 files changed

+89
-79
lines changed

5 files changed

+89
-79
lines changed

chaos.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/coredns/coredns/request"
1111
"github.com/miekg/dns"
12+
selector "github.com/pingcap/tidb-tools/pkg/table-rule-selector"
1213
api "k8s.io/api/core/v1"
1314
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
)
@@ -33,6 +34,7 @@ type PodInfo struct {
3334
Name string
3435
Action string
3536
Scope string
37+
Selector selector.Selector
3638
IP string
3739
LastUpdateTime time.Time
3840
}
@@ -156,7 +158,7 @@ func (k Kubernetes) getChaosPod(ip string) (*PodInfo, error) {
156158
}
157159

158160
// needChaos judges weather should do chaos for the request
159-
func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, err error) bool {
161+
func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, name string) bool {
160162
if podInfo == nil {
161163
return false
162164
}
@@ -165,33 +167,17 @@ func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, err error) boo
165167
return true
166168
}
167169

168-
if err != nil {
169-
// not found in cluster, is outer host
170-
if k.IsNameError(err) {
171-
if podInfo.Scope == ScopeOuter {
172-
return true
173-
}
174-
return false
175-
}
176-
177-
// can't judge the host is outer or inner host, ignore chaos
170+
rules := podInfo.Selector.Match(name, "")
171+
if len(rules) == 0 {
178172
return false
179173
}
180174

181-
if len(records) == 0 {
182-
// not found in cluster, is outer host {
183-
if podInfo.Scope == ScopeOuter {
184-
return true
185-
}
175+
match, ok := rules[0].(bool)
176+
if !ok {
186177
return false
187178
}
188179

189-
// is inner host
190-
if podInfo.Scope == ScopeInner {
191-
return true
192-
}
193-
194-
return false
180+
return match
195181
}
196182

197183
func (k Kubernetes) getPodFromCluster(namespace, name string) (*api.Pod, error) {

grpc_server.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"strings"
78
"time"
89

910
"github.com/chaos-mesh/k8s_dns_chaos/pb"
11+
trieselector "github.com/pingcap/tidb-tools/pkg/table-rule-selector"
1012
"google.golang.org/grpc"
1113
)
1214

@@ -45,6 +47,30 @@ func (k Kubernetes) SetDNSChaos(ctx context.Context, req *pb.SetDNSChaosRequest)
4547

4648
k.chaosMap[req.Name] = req
4749

50+
var scope string
51+
if len(req.Patterns) == 0 {
52+
scope = ScopeAll
53+
}
54+
55+
// build selector
56+
selector := trieselector.NewTrieSelector()
57+
for _, pattern := range req.Patterns {
58+
err := selector.Insert(pattern, "", true, trieselector.Insert)
59+
if err != nil {
60+
log.Errorf("fail to build selector %v", err)
61+
return nil, err
62+
}
63+
64+
if !strings.Contains(pattern, "*") {
65+
// when send dns request to the dns server, will add a '.' at the end of the domain name.
66+
err := selector.Insert(fmt.Sprintf("%s.", pattern), "", true, trieselector.Insert)
67+
if err != nil {
68+
log.Errorf("fail to build selector %v", err)
69+
return nil, err
70+
}
71+
}
72+
}
73+
4874
for _, pod := range req.Pods {
4975
v1Pod, err := k.getPodFromCluster(pod.Namespace, pod.Name)
5076
if err != nil {
@@ -66,7 +92,8 @@ func (k Kubernetes) SetDNSChaos(ctx context.Context, req *pb.SetDNSChaosRequest)
6692
Namespace: pod.Namespace,
6793
Name: pod.Name,
6894
Action: req.Action,
69-
Scope: req.Scope,
95+
Scope: scope,
96+
Selector: selector,
7097
IP: v1Pod.Status.PodIP,
7198
LastUpdateTime: time.Now(),
7299
}

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
2323
records, extra, zone, err := k.getRecords(ctx, state)
2424
log.Debugf("records: %v, err: %v", records, err)
2525

26-
if k.needChaos(chaosPod, records, err) {
26+
if k.needChaos(chaosPod, records, state.QName()) {
2727
return k.chaosDNS(ctx, w, r, state, chaosPod)
2828
}
2929

pb/dns.pb.go

Lines changed: 51 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pb/dns.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ message SetDNSChaosRequest {
2222
// "all": chaos works on all host
2323
string scope = 4;
2424
string selector = 5;
25+
repeated string patterns = 6;
2526
}
2627

2728
message Pod {

0 commit comments

Comments
 (0)