@@ -26,6 +26,8 @@ const (
26
26
ActionError = "error"
27
27
// ActionRandom means return random IP for DNS request
28
28
ActionRandom = "random"
29
+ // ActionChaos means return chaos IP for DNS request
30
+ ActionStatic = "static"
29
31
)
30
32
31
33
// PodInfo saves some information for pod
@@ -39,6 +41,12 @@ type PodInfo struct {
39
41
LastUpdateTime time.Time
40
42
}
41
43
44
+ // DomainIP Domain and ip mapping
45
+ type DomainIP struct {
46
+ Domain string
47
+ IP string
48
+ }
49
+
42
50
// IsOverdue ...
43
51
func (p * PodInfo ) IsOverdue () bool {
44
52
// if the pod's IP is not updated greater than 10 seconds, will treate it as overdue
@@ -55,7 +63,13 @@ func (k Kubernetes) chaosDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
55
63
return dns .RcodeServerFailure , fmt .Errorf ("dns chaos error" )
56
64
}
57
65
58
- // return random IP
66
+ //return static IP
67
+ if podInfo .Action == ActionStatic {
68
+ //return staticIP(ctx, w, r, state, podInfo)
69
+ //k.chaosMap
70
+ domainAndIPMap := k.domainAndIPMap [podInfo.Namespace ][podInfo.Name ]
71
+ return generateDNSRecords (state , domainAndIPMap , r , w )
72
+ }
59
73
60
74
answers := []dns.RR {}
61
75
qname := state .Name ()
@@ -166,6 +180,9 @@ func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, name string) b
166
180
if podInfo .Scope == ScopeAll {
167
181
return true
168
182
}
183
+ if podInfo .Action == ActionStatic && k.domainAndIPMap [podInfo.Namespace ][podInfo.Name ] != nil {
184
+ return true
185
+ }
169
186
170
187
rules := podInfo .Selector .Match (name , "" )
171
188
if len (rules ) == 0 {
@@ -188,3 +205,34 @@ func (k Kubernetes) getPodFromCluster(namespace, name string) (*api.Pod, error)
188
205
}
189
206
return pods .Get (context .Background (), name , meta.GetOptions {})
190
207
}
208
+
209
+ func generateDNSRecords (state request.Request , domainAndIpMap map [string ]string , r * dns.Msg , w dns.ResponseWriter ) (int , error ) {
210
+ answers := []dns.RR {}
211
+ qname := state .Name ()
212
+ if domainAndIpMap == nil {
213
+ return dns .RcodeServerFailure , nil
214
+ }
215
+ ip , ok := domainAndIpMap [qname ]
216
+ if ! ok {
217
+ //如果不存在则
218
+ return dns .RcodeServerFailure , fmt .Errorf ("domain %s not found" , qname )
219
+ }
220
+ switch state .QType () {
221
+ case dns .TypeA :
222
+ ips := []net.IP {net .ParseIP (ip )}
223
+ log .Debugf ("dns.TypeA %v" , ips )
224
+ answers = a (qname , 10 , ips )
225
+ case dns .TypeAAAA :
226
+ // TODO: return random IP
227
+ ips := []net.IP {net .ParseIP (ip )}
228
+ log .Debugf ("dns.TypeAAAA %v" , ips )
229
+ answers = aaaa (qname , 10 , ips )
230
+ }
231
+ m := new (dns.Msg )
232
+ m .SetReply (r )
233
+ m .Authoritative = true
234
+ m .Answer = answers
235
+
236
+ w .WriteMsg (m )
237
+ return dns .RcodeSuccess , nil
238
+ }
0 commit comments