forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceresolver.go
280 lines (249 loc) · 7.18 KB
/
serviceresolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package dns
import (
"fmt"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/skynetservices/skydns/msg"
"github.com/skynetservices/skydns/server"
)
// ServiceResolver is a SkyDNS backend that will serve lookups for DNS entries
// based on Kubernetes service entries. The default DNS name for each service
// will be `<name>.<namespace>.<base>` where base can be an arbitrary depth
// DNS suffix. Queries not recognized within this base will return an error.
type ServiceResolver struct {
config *server.Config
accessor ServiceAccessor
endpoints kclient.EndpointsNamespacer
base string
fallback FallbackFunc
}
// ServiceResolver implements server.Backend
var _ server.Backend = &ServiceResolver{}
type FallbackFunc func(name string, exact bool) (string, bool)
// NewServiceResolver creates an object that will return DNS record entries for
// SkyDNS based on service names.
func NewServiceResolver(config *server.Config, accessor ServiceAccessor, endpoints kclient.EndpointsNamespacer, fn FallbackFunc) *ServiceResolver {
domain := config.Domain
if !strings.HasSuffix(domain, ".") {
domain = domain + "."
}
return &ServiceResolver{
config: config,
accessor: accessor,
endpoints: endpoints,
base: domain,
fallback: fn,
}
}
// Records implements the SkyDNS Backend interface and returns standard records for
// a name.
//
// The standard pattern is <prefix>.<service_name>.<namespace>.(svc|endpoints).<base>
//
// * prefix may be any series of prefix values
// * service_name and namespace must locate a real service
// * svc indicates standard service rules apply (portalIP or endpoints as A records)
// * reverse lookup of IP is only possible for portalIP
// * SRV records are returned for each host+port combination as:
// _<port_name>._<port_protocol>.<dns>
// _<port_name>.<endpoint_id>.<dns>
// * endpoint_id is "portal" when portalIP is set
// * endpoints always returns each individual endpoint as A records
//
func (b *ServiceResolver) Records(name string, exact bool) ([]msg.Service, error) {
if !strings.HasSuffix(name, b.base) {
return nil, nil
}
prefix := strings.Trim(strings.TrimSuffix(name, b.base), ".")
segments := strings.Split(prefix, ".")
for i, j := 0, len(segments)-1; i < j; i, j = i+1, j-1 {
segments[i], segments[j] = segments[j], segments[i]
}
if len(segments) == 0 {
return nil, nil
}
switch segments[0] {
case "svc", "endpoints":
if len(segments) < 3 {
return nil, nil
}
namespace, name := segments[1], segments[2]
svc, err := b.accessor.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) && b.fallback != nil {
if fallback, ok := b.fallback(prefix, exact); ok {
return b.Records(fallback+b.base, exact)
}
}
return nil, err
}
// no portalIP and not headless, no DNS
if len(svc.Spec.PortalIP) == 0 {
return nil, nil
}
retrieveEndpoints := segments[0] == "endpoints" || (len(segments) > 3 && segments[3] == "_endpoints")
// if has a portal IP and looking at svc
if svc.Spec.PortalIP != kapi.PortalIPNone && !retrieveEndpoints {
if len(svc.Spec.Ports) == 0 {
return nil, nil
}
services := []msg.Service{}
for _, p := range svc.Spec.Ports {
port := p.Port
if port == 0 {
port = p.TargetPort.IntVal
}
if port == 0 {
continue
}
if len(p.Protocol) == 0 {
p.Protocol = kapi.ProtocolTCP
}
portName := p.Name
if len(portName) == 0 {
portName = fmt.Sprintf("unknown-port-%d", port)
}
srvName := fmt.Sprintf("%s.portal.%s", portName, name)
keyName := fmt.Sprintf("_%s._%s.%s", portName, p.Protocol, name)
services = append(services,
msg.Service{
Host: svc.Spec.PortalIP,
Port: port,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(srvName),
},
msg.Service{
Host: srvName,
Port: port,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(keyName),
},
)
}
return services, nil
}
// return endpoints
endpoints, err := b.endpoints.Endpoints(namespace).Get(name)
if err != nil {
return nil, err
}
targets := make(map[string]int)
services := make([]msg.Service, 0, len(endpoints.Subsets)*4)
count := 1
for _, s := range endpoints.Subsets {
for _, a := range s.Addresses {
shortName := ""
if a.TargetRef != nil {
name := fmt.Sprintf("%s-%s", a.TargetRef.Name, a.TargetRef.Namespace)
if c, ok := targets[name]; ok {
shortName = fmt.Sprintf("e%d", c)
} else {
shortName = fmt.Sprintf("e%d", count)
targets[name] = count
count++
}
} else {
shortName = fmt.Sprintf("e%d", count)
count++
}
hadPort := false
for _, p := range s.Ports {
port := p.Port
if port == 0 {
continue
}
hadPort = true
if len(p.Protocol) == 0 {
p.Protocol = kapi.ProtocolTCP
}
portName := p.Name
if len(portName) == 0 {
portName = fmt.Sprintf("unknown-port-%d", port)
}
srvName := fmt.Sprintf("%s.%s.%s", portName, shortName, name)
services = append(services, msg.Service{
Host: a.IP,
Port: port,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(srvName),
})
keyName := fmt.Sprintf("_%s._%s.%s", portName, p.Protocol, name)
services = append(services, msg.Service{
Host: srvName,
Port: port,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(keyName),
})
}
if !hadPort {
services = append(services, msg.Service{
Host: a.IP,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(name),
})
}
}
}
return services, nil
}
return nil, nil
}
// ReverseRecord implements the SkyDNS Backend interface and returns standard records for
// a name.
func (b *ServiceResolver) ReverseRecord(name string) (*msg.Service, error) {
portalIP, ok := extractIP(name)
if !ok {
return nil, fmt.Errorf("does not support reverse lookup with %s", name)
}
svc, err := b.accessor.ServiceByPortalIP(portalIP)
if err != nil {
return nil, err
}
port := 0
if len(svc.Spec.Ports) > 0 {
port = svc.Spec.Ports[0].Port
}
return &msg.Service{
Host: fmt.Sprintf("%s.%s.svc.%s", svc.Name, svc.Namespace, b.base),
Port: port,
Priority: 10,
Weight: 10,
Ttl: 30,
Text: "",
Key: msg.Path(name),
}, nil
}
// arpaSuffix is the standard suffix for PTR IP reverse lookups.
const arpaSuffix = ".in-addr.arpa."
// extractIP turns a standard PTR reverse record lookup name
// into an IP address
func extractIP(reverseName string) (string, bool) {
if !strings.HasSuffix(reverseName, arpaSuffix) {
return "", false
}
search := strings.TrimSuffix(reverseName, arpaSuffix)
// reverse the segments and then combine them
segments := strings.Split(search, ".")
for i := 0; i < len(segments)/2; i++ {
j := len(segments) - i - 1
segments[i], segments[j] = segments[j], segments[i]
}
return strings.Join(segments, "."), true
}