dns_common.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package network
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "time"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/fields"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/util/intstr"
  24. "k8s.io/apimachinery/pkg/util/uuid"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. clientset "k8s.io/client-go/kubernetes"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  29. imageutils "k8s.io/kubernetes/test/utils/image"
  30. "github.com/onsi/ginkgo"
  31. "github.com/onsi/gomega"
  32. )
  33. type dnsTestCommon struct {
  34. f *framework.Framework
  35. c clientset.Interface
  36. ns string
  37. name string
  38. labels []string
  39. dnsPod *v1.Pod
  40. utilPod *v1.Pod
  41. utilService *v1.Service
  42. dnsServerPod *v1.Pod
  43. cm *v1.ConfigMap
  44. }
  45. func newDNSTestCommon() dnsTestCommon {
  46. return dnsTestCommon{
  47. f: framework.NewDefaultFramework("dns-config-map"),
  48. ns: "kube-system",
  49. }
  50. }
  51. func (t *dnsTestCommon) init() {
  52. ginkgo.By("Finding a DNS pod")
  53. label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": "kube-dns"}))
  54. options := metav1.ListOptions{LabelSelector: label.String()}
  55. namespace := "kube-system"
  56. pods, err := t.f.ClientSet.CoreV1().Pods(namespace).List(options)
  57. framework.ExpectNoError(err, "failed to list pods in namespace: %s", namespace)
  58. gomega.Expect(len(pods.Items)).Should(gomega.BeNumerically(">=", 1))
  59. t.dnsPod = &pods.Items[0]
  60. e2elog.Logf("Using DNS pod: %v", t.dnsPod.Name)
  61. if strings.Contains(t.dnsPod.Name, "coredns") {
  62. t.name = "coredns"
  63. } else {
  64. t.name = "kube-dns"
  65. }
  66. }
  67. func (t *dnsTestCommon) checkDNSRecord(name string, predicate func([]string) bool, timeout time.Duration) {
  68. t.checkDNSRecordFrom(name, predicate, "kube-dns", timeout)
  69. }
  70. func (t *dnsTestCommon) checkDNSRecordFrom(name string, predicate func([]string) bool, target string, timeout time.Duration) {
  71. var actual []string
  72. err := wait.PollImmediate(
  73. time.Duration(1)*time.Second,
  74. timeout,
  75. func() (bool, error) {
  76. actual = t.runDig(name, target)
  77. if predicate(actual) {
  78. return true, nil
  79. }
  80. return false, nil
  81. })
  82. if err != nil {
  83. framework.Failf("dig result did not match: %#v after %v",
  84. actual, timeout)
  85. }
  86. }
  87. // runDig queries for `dnsName`. Returns a list of responses.
  88. func (t *dnsTestCommon) runDig(dnsName, target string) []string {
  89. cmd := []string{"/usr/bin/dig", "+short"}
  90. switch target {
  91. case "coredns":
  92. cmd = append(cmd, "@"+t.dnsPod.Status.PodIP)
  93. case "kube-dns":
  94. cmd = append(cmd, "@"+t.dnsPod.Status.PodIP, "-p", "10053")
  95. case "ptr-record":
  96. cmd = append(cmd, "-x")
  97. case "cluster-dns":
  98. case "cluster-dns-ipv6":
  99. cmd = append(cmd, "AAAA")
  100. break
  101. default:
  102. panic(fmt.Errorf("invalid target: " + target))
  103. }
  104. cmd = append(cmd, dnsName)
  105. stdout, stderr, err := t.f.ExecWithOptions(framework.ExecOptions{
  106. Command: cmd,
  107. Namespace: t.f.Namespace.Name,
  108. PodName: t.utilPod.Name,
  109. ContainerName: "util",
  110. CaptureStdout: true,
  111. CaptureStderr: true,
  112. })
  113. e2elog.Logf("Running dig: %v, stdout: %q, stderr: %q, err: %v",
  114. cmd, stdout, stderr, err)
  115. if stdout == "" {
  116. return []string{}
  117. }
  118. return strings.Split(stdout, "\n")
  119. }
  120. func (t *dnsTestCommon) setConfigMap(cm *v1.ConfigMap) {
  121. if t.cm != nil {
  122. t.cm = cm
  123. }
  124. cm.ObjectMeta.Namespace = t.ns
  125. cm.ObjectMeta.Name = t.name
  126. options := metav1.ListOptions{
  127. FieldSelector: fields.Set{
  128. "metadata.namespace": t.ns,
  129. "metadata.name": t.name,
  130. }.AsSelector().String(),
  131. }
  132. cmList, err := t.c.CoreV1().ConfigMaps(t.ns).List(options)
  133. framework.ExpectNoError(err, "failed to list ConfigMaps in namespace: %s", t.ns)
  134. if len(cmList.Items) == 0 {
  135. ginkgo.By(fmt.Sprintf("Creating the ConfigMap (%s:%s) %+v", t.ns, t.name, *cm))
  136. _, err := t.c.CoreV1().ConfigMaps(t.ns).Create(cm)
  137. framework.ExpectNoError(err, "failed to create ConfigMap (%s:%s) %+v", t.ns, t.name, *cm)
  138. } else {
  139. ginkgo.By(fmt.Sprintf("Updating the ConfigMap (%s:%s) to %+v", t.ns, t.name, *cm))
  140. _, err := t.c.CoreV1().ConfigMaps(t.ns).Update(cm)
  141. framework.ExpectNoError(err, "failed to update ConfigMap (%s:%s) to %+v", t.ns, t.name, *cm)
  142. }
  143. }
  144. func (t *dnsTestCommon) fetchDNSConfigMapData() map[string]string {
  145. if t.name == "coredns" {
  146. pcm, err := t.c.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(t.name, metav1.GetOptions{})
  147. framework.ExpectNoError(err, "failed to get DNS ConfigMap: %s", t.name)
  148. return pcm.Data
  149. }
  150. return nil
  151. }
  152. func (t *dnsTestCommon) restoreDNSConfigMap(configMapData map[string]string) {
  153. if t.name == "coredns" {
  154. t.setConfigMap(&v1.ConfigMap{Data: configMapData})
  155. t.deleteCoreDNSPods()
  156. } else {
  157. t.c.CoreV1().ConfigMaps(t.ns).Delete(t.name, nil)
  158. }
  159. }
  160. func (t *dnsTestCommon) deleteConfigMap() {
  161. ginkgo.By(fmt.Sprintf("Deleting the ConfigMap (%s:%s)", t.ns, t.name))
  162. t.cm = nil
  163. err := t.c.CoreV1().ConfigMaps(t.ns).Delete(t.name, nil)
  164. framework.ExpectNoError(err, "failed to delete config map: %s", t.name)
  165. }
  166. func (t *dnsTestCommon) createUtilPodLabel(baseName string) {
  167. // Actual port # doesn't matter, just needs to exist.
  168. const servicePort = 10101
  169. t.utilPod = &v1.Pod{
  170. TypeMeta: metav1.TypeMeta{
  171. Kind: "Pod",
  172. },
  173. ObjectMeta: metav1.ObjectMeta{
  174. Namespace: t.f.Namespace.Name,
  175. Labels: map[string]string{"app": baseName},
  176. GenerateName: baseName + "-",
  177. },
  178. Spec: v1.PodSpec{
  179. Containers: []v1.Container{
  180. {
  181. Name: "util",
  182. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  183. Command: []string{"sleep", "10000"},
  184. Ports: []v1.ContainerPort{
  185. {ContainerPort: servicePort, Protocol: v1.ProtocolTCP},
  186. },
  187. },
  188. },
  189. },
  190. }
  191. var err error
  192. t.utilPod, err = t.c.CoreV1().Pods(t.f.Namespace.Name).Create(t.utilPod)
  193. framework.ExpectNoError(err, "failed to create pod: %v", t.utilPod)
  194. e2elog.Logf("Created pod %v", t.utilPod)
  195. err = t.f.WaitForPodRunning(t.utilPod.Name)
  196. framework.ExpectNoError(err, "pod failed to start running: %v", t.utilPod)
  197. t.utilService = &v1.Service{
  198. TypeMeta: metav1.TypeMeta{
  199. Kind: "Service",
  200. },
  201. ObjectMeta: metav1.ObjectMeta{
  202. Namespace: t.f.Namespace.Name,
  203. Name: baseName,
  204. },
  205. Spec: v1.ServiceSpec{
  206. Selector: map[string]string{"app": baseName},
  207. Ports: []v1.ServicePort{
  208. {
  209. Protocol: v1.ProtocolTCP,
  210. Port: servicePort,
  211. TargetPort: intstr.FromInt(servicePort),
  212. },
  213. },
  214. },
  215. }
  216. t.utilService, err = t.c.CoreV1().Services(t.f.Namespace.Name).Create(t.utilService)
  217. framework.ExpectNoError(err, "failed to create service: %s/%s", t.f.Namespace.Name, t.utilService.ObjectMeta.Name)
  218. e2elog.Logf("Created service %v", t.utilService)
  219. }
  220. func (t *dnsTestCommon) deleteUtilPod() {
  221. podClient := t.c.CoreV1().Pods(t.f.Namespace.Name)
  222. if err := podClient.Delete(t.utilPod.Name, metav1.NewDeleteOptions(0)); err != nil {
  223. e2elog.Logf("Delete of pod %v/%v failed: %v",
  224. t.utilPod.Namespace, t.utilPod.Name, err)
  225. }
  226. }
  227. // deleteCoreDNSPods manually deletes the CoreDNS pods to apply the changes to the ConfigMap.
  228. func (t *dnsTestCommon) deleteCoreDNSPods() {
  229. label := labels.SelectorFromSet(labels.Set(map[string]string{"k8s-app": "kube-dns"}))
  230. options := metav1.ListOptions{LabelSelector: label.String()}
  231. pods, err := t.f.ClientSet.CoreV1().Pods("kube-system").List(options)
  232. podClient := t.c.CoreV1().Pods(metav1.NamespaceSystem)
  233. for _, pod := range pods.Items {
  234. err = podClient.Delete(pod.Name, metav1.NewDeleteOptions(0))
  235. framework.ExpectNoError(err, "failed to delete pod: %s", pod.Name)
  236. }
  237. }
  238. func generateDNSServerPod(aRecords map[string]string) *v1.Pod {
  239. pod := &v1.Pod{
  240. TypeMeta: metav1.TypeMeta{
  241. Kind: "Pod",
  242. },
  243. ObjectMeta: metav1.ObjectMeta{
  244. GenerateName: "e2e-dns-configmap-dns-server-",
  245. },
  246. Spec: v1.PodSpec{
  247. Containers: []v1.Container{
  248. {
  249. Name: "dns",
  250. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  251. Command: []string{
  252. "/usr/sbin/dnsmasq",
  253. "-u", "root",
  254. "-k",
  255. "--log-facility", "-",
  256. "-q",
  257. },
  258. },
  259. },
  260. DNSPolicy: "Default",
  261. },
  262. }
  263. for name, ip := range aRecords {
  264. pod.Spec.Containers[0].Command = append(
  265. pod.Spec.Containers[0].Command,
  266. fmt.Sprintf("-A/%v/%v", name, ip))
  267. }
  268. return pod
  269. }
  270. func (t *dnsTestCommon) createDNSPodFromObj(pod *v1.Pod) {
  271. t.dnsServerPod = pod
  272. var err error
  273. t.dnsServerPod, err = t.c.CoreV1().Pods(t.f.Namespace.Name).Create(t.dnsServerPod)
  274. framework.ExpectNoError(err, "failed to create pod: %v", t.dnsServerPod)
  275. e2elog.Logf("Created pod %v", t.dnsServerPod)
  276. err = t.f.WaitForPodRunning(t.dnsServerPod.Name)
  277. framework.ExpectNoError(err, "pod failed to start running: %v", t.dnsServerPod)
  278. t.dnsServerPod, err = t.c.CoreV1().Pods(t.f.Namespace.Name).Get(
  279. t.dnsServerPod.Name, metav1.GetOptions{})
  280. framework.ExpectNoError(err, "failed to get pod: %s", t.dnsServerPod.Name)
  281. }
  282. func (t *dnsTestCommon) createDNSServer(aRecords map[string]string) {
  283. t.createDNSPodFromObj(generateDNSServerPod(aRecords))
  284. }
  285. func (t *dnsTestCommon) createDNSServerWithPtrRecord(isIPv6 bool) {
  286. pod := &v1.Pod{
  287. TypeMeta: metav1.TypeMeta{
  288. Kind: "Pod",
  289. },
  290. ObjectMeta: metav1.ObjectMeta{
  291. GenerateName: "e2e-dns-configmap-dns-server-",
  292. },
  293. Spec: v1.PodSpec{
  294. Containers: []v1.Container{
  295. {
  296. Name: "dns",
  297. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  298. Command: []string{
  299. "/usr/sbin/dnsmasq",
  300. "-u", "root",
  301. "-k",
  302. "--log-facility", "-",
  303. "-q",
  304. },
  305. },
  306. },
  307. DNSPolicy: "Default",
  308. },
  309. }
  310. if isIPv6 {
  311. pod.Spec.Containers[0].Command = append(
  312. pod.Spec.Containers[0].Command,
  313. fmt.Sprintf("--host-record=my.test,2001:db8::29"))
  314. } else {
  315. pod.Spec.Containers[0].Command = append(
  316. pod.Spec.Containers[0].Command,
  317. fmt.Sprintf("--host-record=my.test,192.0.2.123"))
  318. }
  319. t.createDNSPodFromObj(pod)
  320. }
  321. func (t *dnsTestCommon) deleteDNSServerPod() {
  322. podClient := t.c.CoreV1().Pods(t.f.Namespace.Name)
  323. if err := podClient.Delete(t.dnsServerPod.Name, metav1.NewDeleteOptions(0)); err != nil {
  324. e2elog.Logf("Delete of pod %v/%v failed: %v",
  325. t.utilPod.Namespace, t.dnsServerPod.Name, err)
  326. }
  327. }
  328. func createDNSPod(namespace, wheezyProbeCmd, jessieProbeCmd, podHostName, serviceName string) *v1.Pod {
  329. dnsPod := &v1.Pod{
  330. TypeMeta: metav1.TypeMeta{
  331. Kind: "Pod",
  332. APIVersion: "v1",
  333. },
  334. ObjectMeta: metav1.ObjectMeta{
  335. Name: "dns-test-" + string(uuid.NewUUID()),
  336. Namespace: namespace,
  337. },
  338. Spec: v1.PodSpec{
  339. Volumes: []v1.Volume{
  340. {
  341. Name: "results",
  342. VolumeSource: v1.VolumeSource{
  343. EmptyDir: &v1.EmptyDirVolumeSource{},
  344. },
  345. },
  346. },
  347. Containers: []v1.Container{
  348. // TODO: Consider scraping logs instead of running a webserver.
  349. {
  350. Name: "webserver",
  351. Image: imageutils.GetE2EImage(imageutils.TestWebserver),
  352. Ports: []v1.ContainerPort{
  353. {
  354. Name: "http",
  355. ContainerPort: 80,
  356. },
  357. },
  358. VolumeMounts: []v1.VolumeMount{
  359. {
  360. Name: "results",
  361. MountPath: "/results",
  362. },
  363. },
  364. },
  365. {
  366. Name: "querier",
  367. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  368. Command: []string{"sh", "-c", wheezyProbeCmd},
  369. VolumeMounts: []v1.VolumeMount{
  370. {
  371. Name: "results",
  372. MountPath: "/results",
  373. },
  374. },
  375. },
  376. {
  377. Name: "jessie-querier",
  378. Image: imageutils.GetE2EImage(imageutils.JessieDnsutils),
  379. Command: []string{"sh", "-c", jessieProbeCmd},
  380. VolumeMounts: []v1.VolumeMount{
  381. {
  382. Name: "results",
  383. MountPath: "/results",
  384. },
  385. },
  386. },
  387. },
  388. },
  389. }
  390. dnsPod.Spec.Hostname = podHostName
  391. dnsPod.Spec.Subdomain = serviceName
  392. return dnsPod
  393. }
  394. func createProbeCommand(namesToResolve []string, hostEntries []string, ptrLookupIP string, fileNamePrefix, namespace, dnsDomain string) (string, []string) {
  395. fileNames := make([]string, 0, len(namesToResolve)*2)
  396. probeCmd := "for i in `seq 1 600`; do "
  397. for _, name := range namesToResolve {
  398. // Resolve by TCP and UDP DNS. Use $$(...) because $(...) is
  399. // expanded by kubernetes (though this won't expand so should
  400. // remain a literal, safe > sorry).
  401. lookup := "A"
  402. if strings.HasPrefix(name, "_") {
  403. lookup = "SRV"
  404. }
  405. fileName := fmt.Sprintf("%s_udp@%s", fileNamePrefix, name)
  406. fileNames = append(fileNames, fileName)
  407. probeCmd += fmt.Sprintf(`check="$$(dig +notcp +noall +answer +search %s %s)" && test -n "$$check" && echo OK > /results/%s;`, name, lookup, fileName)
  408. fileName = fmt.Sprintf("%s_tcp@%s", fileNamePrefix, name)
  409. fileNames = append(fileNames, fileName)
  410. probeCmd += fmt.Sprintf(`check="$$(dig +tcp +noall +answer +search %s %s)" && test -n "$$check" && echo OK > /results/%s;`, name, lookup, fileName)
  411. }
  412. for _, name := range hostEntries {
  413. fileName := fmt.Sprintf("%s_hosts@%s", fileNamePrefix, name)
  414. fileNames = append(fileNames, fileName)
  415. probeCmd += fmt.Sprintf(`test -n "$$(getent hosts %s)" && echo OK > /results/%s;`, name, fileName)
  416. }
  417. podARecByUDPFileName := fmt.Sprintf("%s_udp@PodARecord", fileNamePrefix)
  418. podARecByTCPFileName := fmt.Sprintf("%s_tcp@PodARecord", fileNamePrefix)
  419. probeCmd += fmt.Sprintf(`podARec=$$(hostname -i| awk -F. '{print $$1"-"$$2"-"$$3"-"$$4".%s.pod.%s"}');`, namespace, dnsDomain)
  420. probeCmd += fmt.Sprintf(`check="$$(dig +notcp +noall +answer +search $${podARec} A)" && test -n "$$check" && echo OK > /results/%s;`, podARecByUDPFileName)
  421. probeCmd += fmt.Sprintf(`check="$$(dig +tcp +noall +answer +search $${podARec} A)" && test -n "$$check" && echo OK > /results/%s;`, podARecByTCPFileName)
  422. fileNames = append(fileNames, podARecByUDPFileName)
  423. fileNames = append(fileNames, podARecByTCPFileName)
  424. if len(ptrLookupIP) > 0 {
  425. ptrLookup := fmt.Sprintf("%s.in-addr.arpa.", strings.Join(reverseArray(strings.Split(ptrLookupIP, ".")), "."))
  426. ptrRecByUDPFileName := fmt.Sprintf("%s_udp@PTR", ptrLookupIP)
  427. ptrRecByTCPFileName := fmt.Sprintf("%s_tcp@PTR", ptrLookupIP)
  428. probeCmd += fmt.Sprintf(`check="$$(dig +notcp +noall +answer +search %s PTR)" && test -n "$$check" && echo OK > /results/%s;`, ptrLookup, ptrRecByUDPFileName)
  429. probeCmd += fmt.Sprintf(`check="$$(dig +tcp +noall +answer +search %s PTR)" && test -n "$$check" && echo OK > /results/%s;`, ptrLookup, ptrRecByTCPFileName)
  430. fileNames = append(fileNames, ptrRecByUDPFileName)
  431. fileNames = append(fileNames, ptrRecByTCPFileName)
  432. }
  433. probeCmd += "sleep 1; done"
  434. return probeCmd, fileNames
  435. }
  436. // createTargetedProbeCommand returns a command line that performs a DNS lookup for a specific record type
  437. func createTargetedProbeCommand(nameToResolve string, lookup string, fileNamePrefix string) (string, string) {
  438. fileName := fmt.Sprintf("%s_udp@%s", fileNamePrefix, nameToResolve)
  439. probeCmd := fmt.Sprintf("for i in `seq 1 30`; do dig +short %s %s > /results/%s; sleep 1; done", nameToResolve, lookup, fileName)
  440. return probeCmd, fileName
  441. }
  442. func assertFilesExist(fileNames []string, fileDir string, pod *v1.Pod, client clientset.Interface) {
  443. assertFilesContain(fileNames, fileDir, pod, client, false, "")
  444. }
  445. func assertFilesContain(fileNames []string, fileDir string, pod *v1.Pod, client clientset.Interface, check bool, expected string) {
  446. var failed []string
  447. framework.ExpectNoError(wait.PollImmediate(time.Second*5, time.Second*600, func() (bool, error) {
  448. failed = []string{}
  449. ctx, cancel := context.WithTimeout(context.Background(), framework.SingleCallTimeout)
  450. defer cancel()
  451. for _, fileName := range fileNames {
  452. contents, err := client.CoreV1().RESTClient().Get().
  453. Context(ctx).
  454. Namespace(pod.Namespace).
  455. Resource("pods").
  456. SubResource("proxy").
  457. Name(pod.Name).
  458. Suffix(fileDir, fileName).
  459. Do().Raw()
  460. if err != nil {
  461. if ctx.Err() != nil {
  462. framework.Failf("Unable to read %s from pod %s/%s: %v", fileName, pod.Namespace, pod.Name, err)
  463. } else {
  464. e2elog.Logf("Unable to read %s from pod %s/%s: %v", fileName, pod.Namespace, pod.Name, err)
  465. }
  466. failed = append(failed, fileName)
  467. } else if check && strings.TrimSpace(string(contents)) != expected {
  468. e2elog.Logf("File %s from pod %s/%s contains '%s' instead of '%s'", fileName, pod.Namespace, pod.Name, string(contents), expected)
  469. failed = append(failed, fileName)
  470. }
  471. }
  472. if len(failed) == 0 {
  473. return true, nil
  474. }
  475. e2elog.Logf("Lookups using %s/%s failed for: %v\n", pod.Namespace, pod.Name, failed)
  476. return false, nil
  477. }))
  478. gomega.Expect(len(failed)).To(gomega.Equal(0))
  479. }
  480. func validateDNSResults(f *framework.Framework, pod *v1.Pod, fileNames []string) {
  481. ginkgo.By("submitting the pod to kubernetes")
  482. podClient := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
  483. defer func() {
  484. ginkgo.By("deleting the pod")
  485. defer ginkgo.GinkgoRecover()
  486. podClient.Delete(pod.Name, metav1.NewDeleteOptions(0))
  487. }()
  488. if _, err := podClient.Create(pod); err != nil {
  489. framework.Failf("ginkgo.Failed to create pod %s/%s: %v", pod.Namespace, pod.Name, err)
  490. }
  491. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  492. ginkgo.By("retrieving the pod")
  493. pod, err := podClient.Get(pod.Name, metav1.GetOptions{})
  494. if err != nil {
  495. framework.Failf("ginkgo.Failed to get pod %s/%s: %v", pod.Namespace, pod.Name, err)
  496. }
  497. // Try to find results for each expected name.
  498. ginkgo.By("looking for the results for each expected name from probers")
  499. assertFilesExist(fileNames, "results", pod, f.ClientSet)
  500. // TODO: probe from the host, too.
  501. e2elog.Logf("DNS probes using %s/%s succeeded\n", pod.Namespace, pod.Name)
  502. }
  503. func validateTargetedProbeOutput(f *framework.Framework, pod *v1.Pod, fileNames []string, value string) {
  504. ginkgo.By("submitting the pod to kubernetes")
  505. podClient := f.ClientSet.CoreV1().Pods(f.Namespace.Name)
  506. defer func() {
  507. ginkgo.By("deleting the pod")
  508. defer ginkgo.GinkgoRecover()
  509. podClient.Delete(pod.Name, metav1.NewDeleteOptions(0))
  510. }()
  511. if _, err := podClient.Create(pod); err != nil {
  512. framework.Failf("ginkgo.Failed to create pod %s/%s: %v", pod.Namespace, pod.Name, err)
  513. }
  514. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  515. ginkgo.By("retrieving the pod")
  516. pod, err := podClient.Get(pod.Name, metav1.GetOptions{})
  517. if err != nil {
  518. framework.Failf("ginkgo.Failed to get pod %s/%s: %v", pod.Namespace, pod.Name, err)
  519. }
  520. // Try to find the expected value for each expected name.
  521. ginkgo.By("looking for the results for each expected name from probers")
  522. assertFilesContain(fileNames, "results", pod, f.ClientSet, true, value)
  523. e2elog.Logf("DNS probes using %s succeeded\n", pod.Name)
  524. }
  525. func reverseArray(arr []string) []string {
  526. for i := 0; i < len(arr)/2; i++ {
  527. j := len(arr) - i - 1
  528. arr[i], arr[j] = arr[j], arr[i]
  529. }
  530. return arr
  531. }
  532. func generateDNSUtilsPod() *v1.Pod {
  533. return &v1.Pod{
  534. TypeMeta: metav1.TypeMeta{
  535. Kind: "Pod",
  536. },
  537. ObjectMeta: metav1.ObjectMeta{
  538. GenerateName: "e2e-dns-utils-",
  539. },
  540. Spec: v1.PodSpec{
  541. Containers: []v1.Container{
  542. {
  543. Name: "util",
  544. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  545. Command: []string{"sleep", "10000"},
  546. },
  547. },
  548. },
  549. }
  550. }