example_cluster_dns.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2015 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 e2e
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. client "k8s.io/kubernetes/pkg/client/unversioned"
  20. "k8s.io/kubernetes/pkg/labels"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. const (
  26. dnsReadyTimeout = time.Minute
  27. )
  28. const queryDnsPythonTemplate string = `
  29. import socket
  30. try:
  31. socket.gethostbyname('%s')
  32. print 'ok'
  33. except:
  34. print 'err'`
  35. var _ = framework.KubeDescribe("ClusterDns [Feature:Example]", func() {
  36. f := framework.NewDefaultFramework("cluster-dns")
  37. var c *client.Client
  38. BeforeEach(func() {
  39. c = f.Client
  40. })
  41. It("should create pod that uses dns [Conformance]", func() {
  42. mkpath := func(file string) string {
  43. return filepath.Join(framework.TestContext.RepoRoot, "examples/cluster-dns", file)
  44. }
  45. // contrary to the example, this test does not use contexts, for simplicity
  46. // namespaces are passed directly.
  47. // Also, for simplicity, we don't use yamls with namespaces, but we
  48. // create testing namespaces instead.
  49. backendRcYaml := mkpath("dns-backend-rc.yaml")
  50. backendRcName := "dns-backend"
  51. backendSvcYaml := mkpath("dns-backend-service.yaml")
  52. backendSvcName := "dns-backend"
  53. backendPodName := "dns-backend"
  54. frontendPodYaml := mkpath("dns-frontend-pod.yaml")
  55. frontendPodName := "dns-frontend"
  56. frontendPodContainerName := "dns-frontend"
  57. podOutput := "Hello World!"
  58. // we need two namespaces anyway, so let's forget about
  59. // the one created in BeforeEach and create two new ones.
  60. namespaces := []*api.Namespace{nil, nil}
  61. for i := range namespaces {
  62. var err error
  63. namespaces[i], err = f.CreateNamespace(fmt.Sprintf("dnsexample%d", i), nil)
  64. Expect(err).NotTo(HaveOccurred())
  65. }
  66. for _, ns := range namespaces {
  67. framework.RunKubectlOrDie("create", "-f", backendRcYaml, getNsCmdFlag(ns))
  68. }
  69. for _, ns := range namespaces {
  70. framework.RunKubectlOrDie("create", "-f", backendSvcYaml, getNsCmdFlag(ns))
  71. }
  72. // wait for objects
  73. for _, ns := range namespaces {
  74. framework.WaitForRCPodsRunning(c, ns.Name, backendRcName)
  75. framework.WaitForService(c, ns.Name, backendSvcName, true, framework.Poll, framework.ServiceStartTimeout)
  76. }
  77. // it is not enough that pods are running because they may be set to running, but
  78. // the application itself may have not been initialized. Just query the application.
  79. for _, ns := range namespaces {
  80. label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendRcName}))
  81. options := api.ListOptions{LabelSelector: label}
  82. pods, err := c.Pods(ns.Name).List(options)
  83. Expect(err).NotTo(HaveOccurred())
  84. err = framework.PodsResponding(c, ns.Name, backendPodName, false, pods)
  85. Expect(err).NotTo(HaveOccurred(), "waiting for all pods to respond")
  86. framework.Logf("found %d backend pods responding in namespace %s", len(pods.Items), ns.Name)
  87. err = framework.ServiceResponding(c, ns.Name, backendSvcName)
  88. Expect(err).NotTo(HaveOccurred(), "waiting for the service to respond")
  89. }
  90. // Now another tricky part:
  91. // It may happen that the service name is not yet in DNS.
  92. // So if we start our pod, it will fail. We must make sure
  93. // the name is already resolvable. So let's try to query DNS from
  94. // the pod we have, until we find our service name.
  95. // This complicated code may be removed if the pod itself retried after
  96. // dns error or timeout.
  97. // This code is probably unnecessary, but let's stay on the safe side.
  98. label := labels.SelectorFromSet(labels.Set(map[string]string{"name": backendPodName}))
  99. options := api.ListOptions{LabelSelector: label}
  100. pods, err := c.Pods(namespaces[0].Name).List(options)
  101. if err != nil || pods == nil || len(pods.Items) == 0 {
  102. framework.Failf("no running pods found")
  103. }
  104. podName := pods.Items[0].Name
  105. queryDns := fmt.Sprintf(queryDnsPythonTemplate, backendSvcName+"."+namespaces[0].Name)
  106. _, err = framework.LookForStringInPodExec(namespaces[0].Name, podName, []string{"python", "-c", queryDns}, "ok", dnsReadyTimeout)
  107. Expect(err).NotTo(HaveOccurred(), "waiting for output from pod exec")
  108. updatedPodYaml := prepareResourceWithReplacedString(frontendPodYaml, "dns-backend.development.cluster.local", fmt.Sprintf("dns-backend.%s.svc.cluster.local", namespaces[0].Name))
  109. // create a pod in each namespace
  110. for _, ns := range namespaces {
  111. framework.NewKubectlCommand("create", "-f", "-", getNsCmdFlag(ns)).WithStdinData(updatedPodYaml).ExecOrDie()
  112. }
  113. // wait until the pods have been scheduler, i.e. are not Pending anymore. Remember
  114. // that we cannot wait for the pods to be running because our pods terminate by themselves.
  115. for _, ns := range namespaces {
  116. err := framework.WaitForPodNotPending(c, ns.Name, frontendPodName, "")
  117. framework.ExpectNoError(err)
  118. }
  119. // wait for pods to print their result
  120. for _, ns := range namespaces {
  121. _, err := framework.LookForStringInLog(ns.Name, frontendPodName, frontendPodContainerName, podOutput, framework.PodStartTimeout)
  122. Expect(err).NotTo(HaveOccurred())
  123. }
  124. })
  125. })
  126. func getNsCmdFlag(ns *api.Namespace) string {
  127. return fmt.Sprintf("--namespace=%v", ns.Name)
  128. }