Skip to content

Commit 00635a5

Browse files
author
xiejunqiao
committed
feat:Complete the domain name resolution of the static type
1 parent 7aa14ef commit 00635a5

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

chaos.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

handler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
2424
if k.needChaos(chaosPod, records, state.QName()) && chaosPod.Action != ActionStatic {
2525
return k.chaosDNS(ctx, w, r, state, chaosPod)
2626
}
27+
// Check if chaos testing is needed and the action type is static IP.
2728
if k.needChaos(chaosPod, records, state.QName()) && chaosPod.Action == ActionStatic {
28-
log.Infof("need chaos, but action is static")
29-
return generateDNSRecords(state, k.domainAndIPMap[chaosPod.Namespace][chaosPod.Name], r, w)
29+
log.Debugf("need chaos, but action is static")
30+
// Get the domain-IP mapping for the specific namespace and pod name.
31+
domainMap := k.domainAndIPMap[chaosPod.Namespace][chaosPod.Name]
32+
// Check if the domain-IP mapping exists.
33+
if domainMap != nil {
34+
// Check if the requested domain exists in the mapping.
35+
if _, ok := domainMap[state.Name()]; ok {
36+
// Generate DNS records using the domain-IP mapping and return the result.
37+
return generateDNSRecords(state, domainMap, r, w)
38+
}
39+
}
3040
}
3141

3242
if k.IsNameError(err) {

setup.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ import (
1919
"github.com/caddyserver/caddy"
2020
"github.com/miekg/dns"
2121
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
22-
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
23-
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
24-
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
22+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
23+
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // pull this in here, because we want it excluded if plugin.cfg doesn't have k8s
2524
"k8s.io/client-go/tools/clientcmd"
2625
"k8s.io/klog"
2726
)

0 commit comments

Comments
 (0)