forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
379 lines (342 loc) · 12.5 KB
/
dns.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package dns
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
. "github.com/onsi/ginkgo"
kapiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/kubernetes/pkg/api"
kapi "k8s.io/kubernetes/pkg/api"
e2e "k8s.io/kubernetes/test/e2e/framework"
)
func createDNSPod(namespace, probeCmd string) *kapiv1.Pod {
pod := &kapiv1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: kapi.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "dns-test-" + string(uuid.NewUUID()),
Namespace: namespace,
},
Spec: kapiv1.PodSpec{
RestartPolicy: kapiv1.RestartPolicyNever,
Containers: []kapiv1.Container{
{
Name: "querier",
Image: "gcr.io/google_containers/dnsutils:e2e",
Command: []string{"sh", "-c", probeCmd},
},
},
},
}
return pod
}
func digForNames(namesToResolve []string, expect sets.String) string {
fileNamePrefix := "test"
var probeCmd string
for _, name := range namesToResolve {
// Resolve by TCP and UDP DNS. Use $$(...) because $(...) is
// expanded by kubernetes (though this won't expand so should
// remain a literal, safe > sorry).
lookup := "A"
if strings.HasPrefix(name, "_") {
lookup = "SRV"
}
fileName := fmt.Sprintf("%s_udp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +notcp +noall +answer +search %s %s)" && echo %q;`, name, lookup, fileName)
fileName = fmt.Sprintf("%s_tcp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +tcp +noall +answer +search %s %s)" && echo %q;`, name, lookup, fileName)
}
return probeCmd
}
func digForCNAMEs(namesToResolve []string, expect sets.String) string {
fileNamePrefix := "test"
var probeCmd string
for _, name := range namesToResolve {
// Resolve by TCP and UDP DNS. Use $$(...) because $(...) is
// expanded by kubernetes (though this won't expand so should
// remain a literal, safe > sorry).
lookup := "CNAME"
fileName := fmt.Sprintf("%s_udp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +notcp +noall +answer +search %s %s)" && echo %q;`, name, lookup, fileName)
fileName = fmt.Sprintf("%s_tcp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +tcp +noall +answer +search %s %s)" && echo %q;`, name, lookup, fileName)
}
return probeCmd
}
func digForSRVs(namesToResolve []string, expect sets.String) string {
fileNamePrefix := "test"
var probeCmd string
for _, name := range namesToResolve {
// Resolve by TCP and UDP DNS. Use $$(...) because $(...) is
// expanded by kubernetes (though this won't expand so should
// remain a literal, safe > sorry).
lookup := "SRV"
fileName := fmt.Sprintf("%s_udp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +notcp +noall +additional +search %s %s)" && echo %q;`, name, lookup, fileName)
fileName = fmt.Sprintf("%s_tcp@%s", fileNamePrefix, name)
expect.Insert(fileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +tcp +noall +additional +search %s %s)" && echo %q;`, name, lookup, fileName)
}
return probeCmd
}
func digForARecords(records map[string][]string, expect sets.String) string {
var probeCmd string
fileNamePrefix := "test"
for name, ips := range records {
fileName := fmt.Sprintf("%s_endpoints@%s", fileNamePrefix, name)
probeCmd += fmt.Sprintf(`[ "$$(dig +short +notcp +noall +answer +search %s A | sort | xargs echo)" = "%s" ] && echo %q;`, name, strings.Join(ips, " "), fileName)
expect.Insert(fileName)
}
return probeCmd
}
func reverseIP(ip string) string {
a := strings.Split(ip, ".")
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
return strings.Join(a, ".")
}
func digForPTRRecords(records map[string]string, expect sets.String) string {
var probeCmd string
fileNamePrefix := "test"
for ip, name := range records {
fileName := fmt.Sprintf("%s_ptr@%s", fileNamePrefix, ip)
probeCmd += fmt.Sprintf(`[ "$(dig +short +notcp +noall +answer +search %s.in-addr.arpa PTR)" = "%s" ] && echo %q;`, reverseIP(ip), name, fileName)
//probeCmd += fmt.Sprintf(`echo "$(dig +short +notcp +noall +answer +search %s.in-addr.arpa PTR)" "# %s %s" %q;`, reverseIP(ip), reverseIP(ip), name, fileName)
expect.Insert(fileName)
}
return probeCmd
}
func digForPod(namespace string, expect sets.String) string {
var probeCmd string
fileNamePrefix := "test"
podARecByUDPFileName := fmt.Sprintf("%s_udp@PodARecord", fileNamePrefix)
podARecByTCPFileName := fmt.Sprintf("%s_tcp@PodARecord", fileNamePrefix)
probeCmd += fmt.Sprintf(`podARec=$$(hostname -i| awk -F. '{print $$1"-"$$2"-"$$3"-"$$4".%s.pod.cluster.local"}');`, namespace)
probeCmd += fmt.Sprintf(`test -n "$$(dig +notcp +noall +answer +search $${podARec} A)" && echo %q;`, podARecByUDPFileName)
probeCmd += fmt.Sprintf(`test -n "$$(dig +tcp +noall +answer +search $${podARec} A)" && echo %q;`, podARecByTCPFileName)
expect.Insert(podARecByUDPFileName, podARecByTCPFileName)
return probeCmd
}
func repeatCommand(times int, cmd ...string) string {
probeCmd := fmt.Sprintf("for i in `seq 1 %d`; do ", times)
probeCmd += strings.Join(cmd, " ")
probeCmd += "sleep 1; done"
return probeCmd
}
func assertLinesExist(lines sets.String, expect int, r io.Reader) error {
count := make(map[string]int)
unrecognized := sets.NewString()
scan := bufio.NewScanner(r)
for scan.Scan() {
line := scan.Text()
if lines.Has(line) {
count[line]++
} else {
unrecognized.Insert(line)
}
}
for k := range lines {
if count[k] != expect {
return fmt.Errorf("unexpected count %d/%d for %q: %v", count[k], expect, k, unrecognized)
}
}
if unrecognized.Len() > 0 {
return fmt.Errorf("unexpected matches from output: %v", unrecognized)
}
return nil
}
// PodSucceeded returns true if the pod has succeeded, false if the pod has not yet
// reached running state, or an error in any other case.
func PodSucceeded(event watch.Event) (bool, error) {
switch event.Type {
case watch.Deleted:
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
}
switch t := event.Object.(type) {
case *kapiv1.Pod:
switch t.Status.Phase {
case kapiv1.PodSucceeded:
return true, nil
case kapiv1.PodFailed:
return false, fmt.Errorf("pod failed: %#v", t)
}
}
return false, nil
}
func validateDNSResults(f *e2e.Framework, pod *kapiv1.Pod, fileNames sets.String, expect int) {
By("submitting the pod to kubernetes")
podClient := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
defer func() {
By("deleting the pod")
defer GinkgoRecover()
podClient.Delete(pod.Name, metav1.NewDeleteOptions(0))
}()
updated, err := podClient.Create(pod)
if err != nil {
e2e.Failf("Failed to create %s pod: %v", pod.Name, err)
}
w, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Watch(metav1.SingleObject(metav1.ObjectMeta{Name: pod.Name, ResourceVersion: updated.ResourceVersion}))
if err != nil {
e2e.Failf("Failed: %v", err)
}
if _, err = watch.Until(e2e.PodStartTimeout, w, PodSucceeded); err != nil {
e2e.Failf("Failed: %v", err)
}
By("retrieving the pod logs")
r, err := podClient.GetLogs(pod.Name, &kapiv1.PodLogOptions{Container: "querier"}).Stream()
if err != nil {
e2e.Failf("Failed to get pod logs %s: %v", pod.Name, err)
}
out, err := ioutil.ReadAll(r)
if err != nil {
e2e.Failf("Failed to read pod logs %s: %v", pod.Name, err)
}
// Try to find results for each expected name.
By("looking for the results for each expected name from probiers")
if err := assertLinesExist(fileNames, expect, bytes.NewBuffer(out)); err != nil {
e2e.Logf("Got results from pod:\n%s", out)
e2e.Failf("Unexpected results: %v", err)
}
e2e.Logf("DNS probes using %s succeeded\n", pod.Name)
}
func createServiceSpec(serviceName string, isHeadless bool, externalName string, selector map[string]string) *kapiv1.Service {
s := &kapiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceName,
},
Spec: kapiv1.ServiceSpec{
Ports: []kapiv1.ServicePort{
{Port: 80, Name: "http", Protocol: "TCP"},
},
Selector: selector,
},
}
if isHeadless {
s.Spec.ClusterIP = "None"
}
if len(externalName) > 0 {
s.Spec.Type = kapiv1.ServiceTypeExternalName
s.Spec.ExternalName = externalName
s.Spec.ClusterIP = ""
}
return s
}
func createEndpointSpec(name string) *kapiv1.Endpoints {
return &kapiv1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Subsets: []kapiv1.EndpointSubset{
{
Addresses: []kapiv1.EndpointAddress{
{IP: "1.1.1.1", Hostname: "endpoint1"},
{IP: "1.1.1.2"},
},
NotReadyAddresses: []kapiv1.EndpointAddress{
{IP: "2.1.1.1"},
{IP: "2.1.1.2"},
},
Ports: []kapiv1.EndpointPort{
{Port: 80},
},
},
},
}
}
func ipsForEndpoints(ep *kapiv1.Endpoints) []string {
ips := sets.NewString()
for _, sub := range ep.Subsets {
for _, addr := range sub.Addresses {
ips.Insert(addr.IP)
}
}
return ips.List()
}
var _ = Describe("DNS", func() {
f := e2e.NewDefaultFramework("dns")
It("should answer endpoint and wildcard queries for the cluster [Conformance]", func() {
if _, err := f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(createServiceSpec("headless", true, "", nil)); err != nil {
e2e.Failf("unable to create headless service: %v", err)
}
if _, err := f.ClientSet.CoreV1().Endpoints(f.Namespace.Name).Create(createEndpointSpec("headless")); err != nil {
e2e.Failf("unable to create clusterip endpoints: %v", err)
}
if _, err := f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(createServiceSpec("clusterip", false, "", nil)); err != nil {
e2e.Failf("unable to create clusterip service: %v", err)
}
if _, err := f.ClientSet.CoreV1().Endpoints(f.Namespace.Name).Create(createEndpointSpec("clusterip")); err != nil {
e2e.Failf("unable to create clusterip endpoints: %v", err)
}
if _, err := f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(createServiceSpec("externalname", true, "www.google.com", nil)); err != nil {
e2e.Failf("unable to create externalName service: %v", err)
}
ep, err := f.ClientSet.CoreV1().Endpoints("default").Get("kubernetes", metav1.GetOptions{})
if err != nil {
e2e.Failf("unable to find endpoints for kubernetes.default: %v", err)
}
kubeEndpoints := ipsForEndpoints(ep)
readyEndpoints := ipsForEndpoints(createEndpointSpec(""))
// All the names we need to be able to resolve.
expect := sets.NewString()
times := 10
cmd := repeatCommand(
times,
// the DNS pod should be able to resolve these names
digForNames([]string{
// answer wildcards on default service
"prefix.kubernetes.default",
"prefix.kubernetes.default.svc",
"prefix.kubernetes.default.svc.cluster.local",
// answer wildcards on clusterIP services
fmt.Sprintf("prefix.clusterip.%s", f.Namespace.Name),
}, expect),
// the DNS pod should be able to get additional A records for this service
digForSRVs([]string{
fmt.Sprintf("_http._tcp.externalname.%s.svc", f.Namespace.Name),
}, expect),
// the DNS pod should be able to get a CNAME for this service
digForCNAMEs([]string{
fmt.Sprintf("externalname.%s.svc", f.Namespace.Name),
}, expect),
// the DNS pod should be able to look up endpoints for names and wildcards
digForARecords(map[string][]string{
"kubernetes.default.endpoints": kubeEndpoints,
fmt.Sprintf("headless.%s.svc", f.Namespace.Name): readyEndpoints,
fmt.Sprintf("headless.%s.endpoints", f.Namespace.Name): readyEndpoints,
fmt.Sprintf("clusterip.%s.endpoints", f.Namespace.Name): readyEndpoints,
fmt.Sprintf("endpoint1.headless.%s.endpoints", f.Namespace.Name): {"1.1.1.1"},
fmt.Sprintf("endpoint1.clusterip.%s.endpoints", f.Namespace.Name): {"1.1.1.1"},
}, expect),
// the DNS pod should be able to find an endpoint hostname via a PTR record for the IP
digForPTRRecords(map[string]string{
"1.1.1.1": fmt.Sprintf("endpoint1.headless.%s.svc.cluster.local.", f.Namespace.Name),
"1.1.1.2": "", // has no hostname
"2.1.1.1": "", // has no hostname
}, expect),
// the DNS pod should respond to its own request
digForPod(f.Namespace.Name, expect),
)
By("Running these commands:" + cmd + "\n")
// Run a pod which probes DNS and exposes the results by HTTP.
By("creating a pod to probe DNS")
pod := createDNSPod(f.Namespace.Name, cmd)
validateDNSResults(f, pod, expect, times)
})
})