@@ -63,14 +63,6 @@ func (k Kubernetes) chaosDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
6363 return dns .RcodeServerFailure , fmt .Errorf ("dns chaos error" )
6464 }
6565
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-
7466 answers := []dns.RR {}
7567 qname := state .Name ()
7668
@@ -180,8 +172,14 @@ func (k Kubernetes) needChaos(podInfo *PodInfo, records []dns.RR, name string) b
180172 if podInfo .Scope == ScopeAll {
181173 return true
182174 }
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
185183 }
186184
187185 rules := podInfo .Selector .Match (name , "" )
@@ -212,21 +210,26 @@ func generateDNSRecords(state request.Request, domainAndIpMap map[string]string,
212210 if domainAndIpMap == nil {
213211 return dns .RcodeServerFailure , nil
214212 }
215- ip , ok := domainAndIpMap [qname ]
213+ ipStr , ok := domainAndIpMap [qname ]
216214 if ! ok {
217- //如果不存在则
218215 return dns .RcodeServerFailure , fmt .Errorf ("domain %s not found" , qname )
219216 }
217+ ip := net .ParseIP (ipStr )
220218 switch state .QType () {
221219 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 )
225226 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 })
230233 }
231234 m := new (dns.Msg )
232235 m .SetReply (r )
0 commit comments