@@ -4,9 +4,11 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"net"
7
+ "strings"
7
8
"time"
8
9
9
10
"github.com/chaos-mesh/k8s_dns_chaos/pb"
11
+ trieselector "github.com/pingcap/tidb-tools/pkg/table-rule-selector"
10
12
"google.golang.org/grpc"
11
13
)
12
14
@@ -45,6 +47,30 @@ func (k Kubernetes) SetDNSChaos(ctx context.Context, req *pb.SetDNSChaosRequest)
45
47
46
48
k .chaosMap [req .Name ] = req
47
49
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
+
48
74
for _ , pod := range req .Pods {
49
75
v1Pod , err := k .getPodFromCluster (pod .Namespace , pod .Name )
50
76
if err != nil {
@@ -66,7 +92,8 @@ func (k Kubernetes) SetDNSChaos(ctx context.Context, req *pb.SetDNSChaosRequest)
66
92
Namespace : pod .Namespace ,
67
93
Name : pod .Name ,
68
94
Action : req .Action ,
69
- Scope : req .Scope ,
95
+ Scope : scope ,
96
+ Selector : selector ,
70
97
IP : v1Pod .Status .PodIP ,
71
98
LastUpdateTime : time .Now (),
72
99
}
0 commit comments