4
4
"fmt"
5
5
"strings"
6
6
7
+ "github.com/golang/glog"
8
+
7
9
kapi "k8s.io/kubernetes/pkg/api"
8
10
"k8s.io/kubernetes/pkg/api/errors"
9
11
kclient "k8s.io/kubernetes/pkg/client/unversioned"
@@ -60,20 +62,20 @@ func NewServiceResolver(config *server.Config, accessor ServiceAccessor, endpoin
60
62
// * endpoint_id is "portal" when portalIP is set
61
63
// * endpoints always returns each individual endpoint as A records
62
64
//
63
- func (b * ServiceResolver ) Records (name string , exact bool ) ([]msg.Service , error ) {
64
- if ! strings .HasSuffix (name , b .base ) {
65
+ func (b * ServiceResolver ) Records (dnsName string , exact bool ) ([]msg.Service , error ) {
66
+ if ! strings .HasSuffix (dnsName , b .base ) {
65
67
return nil , nil
66
68
}
67
- prefix := strings .Trim (strings .TrimSuffix (name , b .base ), "." )
69
+ prefix := strings .Trim (strings .TrimSuffix (dnsName , b .base ), "." )
68
70
segments := strings .Split (prefix , "." )
69
71
for i , j := 0 , len (segments )- 1 ; i < j ; i , j = i + 1 , j - 1 {
70
72
segments [i ], segments [j ] = segments [j ], segments [i ]
71
73
}
72
74
if len (segments ) == 0 {
73
75
return nil , nil
74
76
}
75
-
76
- switch segments [0 ] {
77
+ glog . V ( 4 ). Infof ( "Answering query %s:%t" , dnsName , exact )
78
+ switch base := segments [0 ]; base {
77
79
case "svc" , "endpoints" :
78
80
if len (segments ) < 3 {
79
81
return nil , nil
@@ -94,7 +96,8 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
94
96
return nil , nil
95
97
}
96
98
97
- retrieveEndpoints := segments [0 ] == "endpoints" || (len (segments ) > 3 && segments [3 ] == "_endpoints" )
99
+ subdomain := buildDNSName (b .base , base , namespace , name )
100
+ retrieveEndpoints := base == "endpoints" || (len (segments ) > 3 && segments [3 ] == "_endpoints" )
98
101
99
102
// if has a portal IP and looking at svc
100
103
if svc .Spec .ClusterIP != kapi .ClusterIPNone && ! retrieveEndpoints {
@@ -117,8 +120,8 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
117
120
if len (portName ) == 0 {
118
121
portName = fmt .Sprintf ("unknown-port-%d" , port )
119
122
}
120
- srvName := fmt . Sprintf ( "%s. portal.%s " , portName , name )
121
- keyName := fmt . Sprintf ( "_%s._%s.%s" , portName , p .Protocol , name )
123
+ srvName := buildDNSName ( subdomain , " portal" , portName )
124
+ keyName := buildDNSName ( subdomain , "_" + strings . ToLower ( string ( p .Protocol )), "_" + portName )
122
125
services = append (services ,
123
126
msg.Service {
124
127
Host : svc .Spec .ClusterIP ,
@@ -144,6 +147,7 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
144
147
},
145
148
)
146
149
}
150
+ glog .V (4 ).Infof ("Answered %s:%t with %#v" , dnsName , exact , services )
147
151
return services , nil
148
152
}
149
153
@@ -181,11 +185,11 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
181
185
if len (p .Protocol ) == 0 {
182
186
p .Protocol = kapi .ProtocolTCP
183
187
}
184
- portName := p .Name
188
+ portName := strings . ToLower ( p .Name )
185
189
if len (portName ) == 0 {
186
190
portName = fmt .Sprintf ("unknown-port-%d" , port )
187
191
}
188
- srvName := fmt . Sprintf ( "%s.%s.%s" , portName , shortName , name )
192
+ srvName := buildDNSName ( subdomain , shortName , portName )
189
193
services = append (services , msg.Service {
190
194
Host : a .IP ,
191
195
Port : port ,
@@ -197,7 +201,7 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
197
201
Text : "" ,
198
202
Key : msg .Path (srvName ),
199
203
})
200
- keyName := fmt . Sprintf ( "_%s._%s.%s" , portName , p .Protocol , name )
204
+ keyName := buildDNSName ( subdomain , "_" + strings . ToLower ( string ( p .Protocol )), "_" + portName )
201
205
services = append (services , msg.Service {
202
206
Host : srvName ,
203
207
Port : port ,
@@ -225,6 +229,7 @@ func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error
225
229
}
226
230
}
227
231
}
232
+ glog .V (4 ).Infof ("Answered %s:%t with %#v" , dnsName , exact , services )
228
233
return services , nil
229
234
}
230
235
return nil , nil
@@ -246,8 +251,9 @@ func (b *ServiceResolver) ReverseRecord(name string) (*msg.Service, error) {
246
251
if len (svc .Spec .Ports ) > 0 {
247
252
port = svc .Spec .Ports [0 ].Port
248
253
}
254
+ hostName := buildDNSName (b .base , "svc" , svc .Namespace , svc .Name )
249
255
return & msg.Service {
250
- Host : fmt . Sprintf ( "%s.%s.svc.%s" , svc . Name , svc . Namespace , b . base ) ,
256
+ Host : hostName ,
251
257
Port : port ,
252
258
253
259
Priority : 10 ,
@@ -278,3 +284,16 @@ func extractIP(reverseName string) (string, bool) {
278
284
}
279
285
return strings .Join (segments , "." ), true
280
286
}
287
+
288
+ // buildDNSName reverses the labels order and joins them with dots.
289
+ func buildDNSName (labels ... string ) string {
290
+ var res string
291
+ for _ , label := range labels {
292
+ if res == "" {
293
+ res = label
294
+ } else {
295
+ res = fmt .Sprintf ("%s.%s" , label , res )
296
+ }
297
+ }
298
+ return res
299
+ }
0 commit comments