@@ -63,14 +63,6 @@ func (k Kubernetes) chaosDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
63
63
return dns .RcodeServerFailure , fmt .Errorf ("dns chaos error" )
64
64
}
65
65
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
- }
73
-
74
66
answers := []dns.RR {}
75
67
qname := state .Name ()
76
68
@@ -180,8 +172,14 @@ func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, name string) b
180
172
if podInfo .Scope == ScopeAll {
181
173
return true
182
174
}
183
- if podInfo .Action == ActionStatic && k.domainAndIPMap [podInfo.Namespace ][podInfo.Name ] != nil {
184
- return true
175
+ if podInfo .Action == ActionStatic {
176
+ domainMap := k.domainAndIPMap [podInfo.Namespace ][podInfo.Name ]
177
+ if domainMap != nil {
178
+ if _ , ok := domainMap [name ]; ok {
179
+ return true
180
+ }
181
+ }
182
+ return false
185
183
}
186
184
187
185
rules := podInfo .Selector .Match (name , "" )
@@ -212,21 +210,26 @@ func generateDNSRecords(state request.Request, domainAndIpMap map[string]string,
212
210
if domainAndIpMap == nil {
213
211
return dns .RcodeServerFailure , nil
214
212
}
215
- ip , ok := domainAndIpMap [qname ]
213
+ ipStr , ok := domainAndIpMap [qname ]
216
214
if ! ok {
217
- //如果不存在则
218
215
return dns .RcodeServerFailure , fmt .Errorf ("domain %s not found" , qname )
219
216
}
217
+ ip := net .ParseIP (ipStr )
220
218
switch state .QType () {
221
219
case dns .TypeA :
222
- ips := []net.IP {net .ParseIP (ip )}
223
- log .Debugf ("dns.TypeA %v" , ips )
224
- answers = a (qname , 10 , ips )
220
+ ipv4 := ip .To4 ()
221
+ if ipv4 == nil {
222
+ return dns .RcodeServerFailure , fmt .Errorf ("not a valid IPv4 address: %s" , ipStr )
223
+ }
224
+ answers = a (qname , 10 , []net.IP {ipv4 })
225
+ log .Debugf ("dns.TypeA %v" , ipv4 )
225
226
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 )
227
+ ipv6 := ip .To16 ()
228
+ if ip .To4 () != nil {
229
+ return dns .RcodeServerFailure , fmt .Errorf ("not a valid IPv6 address: %s" , ipStr )
230
+ }
231
+ log .Debugf ("dns.TypeAAAA %v" , ipv6 )
232
+ answers = aaaa (qname , 10 , []net.IP {ipv6 })
230
233
}
231
234
m := new (dns.Msg )
232
235
m .SetReply (r )
0 commit comments