dns.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2019 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 windows
  14. import (
  15. "context"
  16. "regexp"
  17. "strings"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  22. imageutils "k8s.io/kubernetes/test/utils/image"
  23. "github.com/onsi/ginkgo"
  24. )
  25. var _ = SIGDescribe("DNS", func() {
  26. ginkgo.BeforeEach(func() {
  27. e2eskipper.SkipUnlessNodeOSDistroIs("windows")
  28. })
  29. f := framework.NewDefaultFramework("dns")
  30. ginkgo.It("should support configurable pod DNS servers", func() {
  31. ginkgo.By("Preparing a test DNS service with injected DNS names...")
  32. testInjectedIP := "1.1.1.1"
  33. testSearchPath := "resolv.conf.local"
  34. ginkgo.By("Creating a pod with dnsPolicy=None and customized dnsConfig...")
  35. testUtilsPod := generateDNSUtilsPod()
  36. testUtilsPod.Spec.DNSPolicy = v1.DNSNone
  37. testUtilsPod.Spec.DNSConfig = &v1.PodDNSConfig{
  38. Nameservers: []string{testInjectedIP},
  39. Searches: []string{testSearchPath},
  40. }
  41. testUtilsPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), testUtilsPod, metav1.CreateOptions{})
  42. framework.ExpectNoError(err)
  43. framework.Logf("Created pod %v", testUtilsPod)
  44. defer func() {
  45. framework.Logf("Deleting pod %s...", testUtilsPod.Name)
  46. if err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), testUtilsPod.Name, metav1.NewDeleteOptions(0)); err != nil {
  47. framework.Failf("Failed to delete pod %s: %v", testUtilsPod.Name, err)
  48. }
  49. }()
  50. framework.ExpectNoError(f.WaitForPodRunning(testUtilsPod.Name), "failed to wait for pod %s to be running", testUtilsPod.Name)
  51. ginkgo.By("Verifying customized DNS option is configured on pod...")
  52. cmd := []string{"ipconfig", "/all"}
  53. stdout, _, err := f.ExecWithOptions(framework.ExecOptions{
  54. Command: cmd,
  55. Namespace: f.Namespace.Name,
  56. PodName: testUtilsPod.Name,
  57. ContainerName: "util",
  58. CaptureStdout: true,
  59. CaptureStderr: true,
  60. })
  61. framework.ExpectNoError(err)
  62. framework.Logf("ipconfig /all:\n%s", stdout)
  63. dnsRegex, err := regexp.Compile(`DNS Servers[\s*.]*:(\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})+`)
  64. framework.ExpectNoError(err)
  65. if dnsRegex.MatchString(stdout) {
  66. match := dnsRegex.FindString(stdout)
  67. if !strings.Contains(match, testInjectedIP) {
  68. framework.Failf("customized DNS options not found in ipconfig /all, got: %s", match)
  69. }
  70. } else {
  71. framework.Failf("cannot find DNS server info in ipconfig /all output: \n%s", stdout)
  72. }
  73. // TODO: Add more test cases for other DNSPolicies.
  74. })
  75. })
  76. func generateDNSUtilsPod() *v1.Pod {
  77. return &v1.Pod{
  78. TypeMeta: metav1.TypeMeta{
  79. Kind: "Pod",
  80. },
  81. ObjectMeta: metav1.ObjectMeta{
  82. GenerateName: "e2e-dns-utils-",
  83. },
  84. Spec: v1.PodSpec{
  85. Containers: []v1.Container{
  86. {
  87. Name: "util",
  88. Image: imageutils.GetE2EImage(imageutils.Dnsutils),
  89. Command: []string{"sleep", "10000"},
  90. },
  91. },
  92. },
  93. }
  94. }