@@ -13,27 +13,60 @@ import (
13
13
func (k Kubernetes ) ServeDNS (ctx context.Context , w dns.ResponseWriter , r * dns.Msg ) (int , error ) {
14
14
state := request.Request {W : w , Req : r }
15
15
sourceIP := state .IP ()
16
- log .Infof ("k8s ServeDNS, source IP: %s" , sourceIP )
16
+ log .Infof ("k8s ServeDNS, source IP: %s, state: %v " , sourceIP , state )
17
17
18
18
chaosPod , err := k .getChaosPod (sourceIP )
19
19
if err != nil {
20
20
log .Infof ("fail to get pod information from cluster, IP: %s, error: %v" , sourceIP , err )
21
21
}
22
- if k .needChaos (chaosPod , state ) {
22
+
23
+ records , extra , zone , err := k .getRecords (ctx , state )
24
+ log .Infof ("records: %v, err: %v" , records , err )
25
+
26
+ if k .needChaos (chaosPod , records , err ) {
23
27
return k .chaosDNS (ctx , w , r , state , chaosPod )
24
28
}
25
29
30
+ if k .IsNameError (err ) {
31
+ if k .Fall .Through (state .Name ()) {
32
+ return plugin .NextOrFailure (k .Name (), k .Next , ctx , w , r )
33
+ }
34
+ if ! k .APIConn .HasSynced () {
35
+ // If we haven't synchronized with the kubernetes cluster, return server failure
36
+ return plugin .BackendError (ctx , & k , zone , dns .RcodeServerFailure , state , nil /* err */ , plugin.Options {})
37
+ }
38
+ return plugin .BackendError (ctx , & k , zone , dns .RcodeNameError , state , nil /* err */ , plugin.Options {})
39
+ }
40
+ if err != nil {
41
+ return dns .RcodeServerFailure , err
42
+ }
43
+
44
+ if len (records ) == 0 {
45
+ return plugin .BackendError (ctx , & k , zone , dns .RcodeSuccess , state , nil , plugin.Options {})
46
+ }
47
+
48
+ m := new (dns.Msg )
49
+ m .SetReply (r )
50
+ m .Authoritative = true
51
+ m .Answer = append (m .Answer , records ... )
52
+ m .Extra = append (m .Extra , extra ... )
53
+
54
+ w .WriteMsg (m )
55
+ return dns .RcodeSuccess , nil
56
+ }
57
+
58
+ // get records from cache
59
+ func (k Kubernetes ) getRecords (ctx context.Context , state request.Request ) ([]dns.RR , []dns.RR , string , error ) {
26
60
qname := state .QName ()
27
61
zone := plugin .Zones (k .Zones ).Matches (qname )
28
- if zone == "" {
29
- return plugin .NextOrFailure (k .Name (), k .Next , ctx , w , r )
30
- }
62
+
31
63
zone = qname [len (qname )- len (zone ):] // maintain case of original query
32
64
state .Zone = zone
33
65
34
66
var (
35
67
records []dns.RR
36
68
extra []dns.RR
69
+ err error
37
70
)
38
71
39
72
switch state .QType () {
@@ -68,34 +101,7 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
68
101
_ , err = plugin .A (ctx , & k , zone , fake , nil , plugin.Options {})
69
102
}
70
103
71
- if k .IsNameError (err ) {
72
- if k .Fall .Through (state .Name ()) {
73
- return plugin .NextOrFailure (k .Name (), k .Next , ctx , w , r )
74
- }
75
- if ! k .APIConn .HasSynced () {
76
- // If we haven't synchronized with the kubernetes cluster, return server failure
77
- return plugin .BackendError (ctx , & k , zone , dns .RcodeServerFailure , state , nil /* err */ , plugin.Options {})
78
- }
79
- return plugin .BackendError (ctx , & k , zone , dns .RcodeNameError , state , nil /* err */ , plugin.Options {})
80
- }
81
- if err != nil {
82
- return dns .RcodeServerFailure , err
83
- }
84
-
85
- if len (records ) == 0 {
86
- return plugin .BackendError (ctx , & k , zone , dns .RcodeSuccess , state , nil , plugin.Options {})
87
- }
88
-
89
- log .Infof ("records %v" , records )
90
-
91
- m := new (dns.Msg )
92
- m .SetReply (r )
93
- m .Authoritative = true
94
- m .Answer = append (m .Answer , records ... )
95
- m .Extra = append (m .Extra , extra ... )
96
-
97
- w .WriteMsg (m )
98
- return dns .RcodeSuccess , nil
104
+ return records , extra , zone , err
99
105
}
100
106
101
107
// Name implements the Handler interface.
0 commit comments