dns_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 dns
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "os"
  19. "strings"
  20. "testing"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/apimachinery/pkg/util/sets"
  25. "k8s.io/client-go/tools/record"
  26. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  27. "github.com/stretchr/testify/assert"
  28. "github.com/stretchr/testify/require"
  29. )
  30. var (
  31. fetchEvent = func(recorder *record.FakeRecorder) string {
  32. select {
  33. case event := <-recorder.Events:
  34. return event
  35. default:
  36. return ""
  37. }
  38. }
  39. )
  40. func TestParseResolvConf(t *testing.T) {
  41. testCases := []struct {
  42. data string
  43. nameservers []string
  44. searches []string
  45. options []string
  46. isErr bool
  47. }{
  48. {"", []string{}, []string{}, []string{}, false},
  49. {" ", []string{}, []string{}, []string{}, false},
  50. {"\n", []string{}, []string{}, []string{}, false},
  51. {"\t\n\t", []string{}, []string{}, []string{}, false},
  52. {"#comment\n", []string{}, []string{}, []string{}, false},
  53. {" #comment\n", []string{}, []string{}, []string{}, false},
  54. {"#comment\n#comment", []string{}, []string{}, []string{}, false},
  55. {"#comment\nnameserver", []string{}, []string{}, []string{}, true}, // nameserver empty
  56. {"#comment\nnameserver\nsearch", []string{}, []string{}, []string{}, true}, // nameserver and search empty
  57. {"#comment\nnameserver 1.2.3.4\nsearch", []string{"1.2.3.4"}, []string{}, []string{}, false}, // nameserver specified and search empty
  58. {"nameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}, []string{}, false},
  59. {" nameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}, []string{}, false},
  60. {"\tnameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}, []string{}, false},
  61. {"nameserver\t1.2.3.4", []string{"1.2.3.4"}, []string{}, []string{}, false},
  62. {"nameserver \t 1.2.3.4", []string{"1.2.3.4"}, []string{}, []string{}, false},
  63. {"nameserver 1.2.3.4\nnameserver 5.6.7.8", []string{"1.2.3.4", "5.6.7.8"}, []string{}, []string{}, false},
  64. {"nameserver 1.2.3.4 #comment", []string{"1.2.3.4"}, []string{}, []string{}, false},
  65. {"search ", []string{}, []string{}, []string{}, false}, // search empty
  66. {"search foo", []string{}, []string{"foo"}, []string{}, false},
  67. {"search foo bar", []string{}, []string{"foo", "bar"}, []string{}, false},
  68. {"search foo bar bat\n", []string{}, []string{"foo", "bar", "bat"}, []string{}, false},
  69. {"search foo\nsearch bar", []string{}, []string{"bar"}, []string{}, false},
  70. {"nameserver 1.2.3.4\nsearch foo bar", []string{"1.2.3.4"}, []string{"foo", "bar"}, []string{}, false},
  71. {"nameserver 1.2.3.4\nsearch foo\nnameserver 5.6.7.8\nsearch bar", []string{"1.2.3.4", "5.6.7.8"}, []string{"bar"}, []string{}, false},
  72. {"#comment\nnameserver 1.2.3.4\n#comment\nsearch foo\ncomment", []string{"1.2.3.4"}, []string{"foo"}, []string{}, false},
  73. {"options ", []string{}, []string{}, []string{}, false},
  74. {"options ndots:5 attempts:2", []string{}, []string{}, []string{"ndots:5", "attempts:2"}, false},
  75. {"options ndots:1\noptions ndots:5 attempts:3", []string{}, []string{}, []string{"ndots:5", "attempts:3"}, false},
  76. {"nameserver 1.2.3.4\nsearch foo\nnameserver 5.6.7.8\nsearch bar\noptions ndots:5 attempts:4", []string{"1.2.3.4", "5.6.7.8"}, []string{"bar"}, []string{"ndots:5", "attempts:4"}, false},
  77. }
  78. for i, tc := range testCases {
  79. ns, srch, opts, err := parseResolvConf(strings.NewReader(tc.data))
  80. if !tc.isErr {
  81. require.NoError(t, err)
  82. assert.EqualValues(t, tc.nameservers, ns, "test case [%d]: name servers", i)
  83. assert.EqualValues(t, tc.searches, srch, "test case [%d] searches", i)
  84. assert.EqualValues(t, tc.options, opts, "test case [%d] options", i)
  85. } else {
  86. require.Error(t, err, "tc.searches %v", tc.searches)
  87. }
  88. }
  89. }
  90. func TestFormDNSSearchFitsLimits(t *testing.T) {
  91. recorder := record.NewFakeRecorder(20)
  92. nodeRef := &v1.ObjectReference{
  93. Kind: "Node",
  94. Name: string("testNode"),
  95. UID: types.UID("testNode"),
  96. Namespace: "",
  97. }
  98. testClusterDNSDomain := "TEST"
  99. configurer := NewConfigurer(recorder, nodeRef, nil, nil, testClusterDNSDomain, "")
  100. pod := &v1.Pod{
  101. ObjectMeta: metav1.ObjectMeta{
  102. UID: "",
  103. Name: "test_pod",
  104. Namespace: "testNS",
  105. Annotations: map[string]string{},
  106. },
  107. }
  108. testCases := []struct {
  109. hostNames []string
  110. resultSearch []string
  111. events []string
  112. }{
  113. {
  114. []string{"testNS.svc.TEST", "svc.TEST", "TEST"},
  115. []string{"testNS.svc.TEST", "svc.TEST", "TEST"},
  116. []string{},
  117. },
  118. {
  119. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA", "BBB"},
  120. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA", "BBB"},
  121. []string{},
  122. },
  123. {
  124. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA", strings.Repeat("B", 256), "BBB"},
  125. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA"},
  126. []string{"Search Line limits were exceeded, some search paths have been omitted, the applied search line is: testNS.svc.TEST svc.TEST TEST AAA"},
  127. },
  128. {
  129. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA", "BBB", "CCC", "DDD"},
  130. []string{"testNS.svc.TEST", "svc.TEST", "TEST", "AAA", "BBB", "CCC"},
  131. []string{"Search Line limits were exceeded, some search paths have been omitted, the applied search line is: testNS.svc.TEST svc.TEST TEST AAA BBB CCC"},
  132. },
  133. }
  134. for i, tc := range testCases {
  135. dnsSearch := configurer.formDNSSearchFitsLimits(tc.hostNames, pod)
  136. assert.EqualValues(t, tc.resultSearch, dnsSearch, "test [%d]", i)
  137. for _, expectedEvent := range tc.events {
  138. expected := fmt.Sprintf("%s %s %s", v1.EventTypeWarning, "DNSConfigForming", expectedEvent)
  139. event := fetchEvent(recorder)
  140. assert.Equal(t, expected, event, "test [%d]", i)
  141. }
  142. }
  143. }
  144. func TestFormDNSNameserversFitsLimits(t *testing.T) {
  145. recorder := record.NewFakeRecorder(20)
  146. nodeRef := &v1.ObjectReference{
  147. Kind: "Node",
  148. Name: string("testNode"),
  149. UID: types.UID("testNode"),
  150. Namespace: "",
  151. }
  152. testClusterDNSDomain := "TEST"
  153. configurer := NewConfigurer(recorder, nodeRef, nil, nil, testClusterDNSDomain, "")
  154. pod := &v1.Pod{
  155. ObjectMeta: metav1.ObjectMeta{
  156. UID: "",
  157. Name: "test_pod",
  158. Namespace: "testNS",
  159. Annotations: map[string]string{},
  160. },
  161. }
  162. testCases := []struct {
  163. desc string
  164. nameservers []string
  165. expectedNameserver []string
  166. expectedEvent bool
  167. }{
  168. {
  169. desc: "valid: 1 nameserver",
  170. nameservers: []string{"127.0.0.1"},
  171. expectedNameserver: []string{"127.0.0.1"},
  172. expectedEvent: false,
  173. },
  174. {
  175. desc: "valid: 3 nameservers",
  176. nameservers: []string{"127.0.0.1", "10.0.0.10", "8.8.8.8"},
  177. expectedNameserver: []string{"127.0.0.1", "10.0.0.10", "8.8.8.8"},
  178. expectedEvent: false,
  179. },
  180. {
  181. desc: "invalid: 4 nameservers, trimmed to 3",
  182. nameservers: []string{"127.0.0.1", "10.0.0.10", "8.8.8.8", "1.2.3.4"},
  183. expectedNameserver: []string{"127.0.0.1", "10.0.0.10", "8.8.8.8"},
  184. expectedEvent: true,
  185. },
  186. }
  187. for _, tc := range testCases {
  188. appliedNameservers := configurer.formDNSNameserversFitsLimits(tc.nameservers, pod)
  189. assert.EqualValues(t, tc.expectedNameserver, appliedNameservers, tc.desc)
  190. event := fetchEvent(recorder)
  191. if tc.expectedEvent && len(event) == 0 {
  192. t.Errorf("%s: formDNSNameserversFitsLimits(%v) expected event, got no event.", tc.desc, tc.nameservers)
  193. } else if !tc.expectedEvent && len(event) > 0 {
  194. t.Errorf("%s: formDNSNameserversFitsLimits(%v) expected no event, got event: %v", tc.desc, tc.nameservers, event)
  195. }
  196. }
  197. }
  198. func TestMergeDNSOptions(t *testing.T) {
  199. testOptionValue := "3"
  200. testCases := []struct {
  201. desc string
  202. existingDNSConfigOptions []string
  203. dnsConfigOptions []v1.PodDNSConfigOption
  204. expectedOptions []string
  205. }{
  206. {
  207. desc: "Empty dnsConfigOptions",
  208. existingDNSConfigOptions: []string{"ndots:5", "debug"},
  209. dnsConfigOptions: nil,
  210. expectedOptions: []string{"ndots:5", "debug"},
  211. },
  212. {
  213. desc: "No duplicated entries",
  214. existingDNSConfigOptions: []string{"ndots:5", "debug"},
  215. dnsConfigOptions: []v1.PodDNSConfigOption{
  216. {Name: "single-request"},
  217. {Name: "attempts", Value: &testOptionValue},
  218. },
  219. expectedOptions: []string{"ndots:5", "debug", "single-request", "attempts:3"},
  220. },
  221. {
  222. desc: "Overwrite duplicated entries",
  223. existingDNSConfigOptions: []string{"ndots:5", "debug"},
  224. dnsConfigOptions: []v1.PodDNSConfigOption{
  225. {Name: "ndots", Value: &testOptionValue},
  226. {Name: "debug"},
  227. {Name: "single-request"},
  228. {Name: "attempts", Value: &testOptionValue},
  229. },
  230. expectedOptions: []string{"ndots:3", "debug", "single-request", "attempts:3"},
  231. },
  232. }
  233. for _, tc := range testCases {
  234. options := mergeDNSOptions(tc.existingDNSConfigOptions, tc.dnsConfigOptions)
  235. // Options order may be changed after conversion.
  236. if !sets.NewString(options...).Equal(sets.NewString(tc.expectedOptions...)) {
  237. t.Errorf("%s: mergeDNSOptions(%v, %v)=%v, want %v", tc.desc, tc.existingDNSConfigOptions, tc.dnsConfigOptions, options, tc.expectedOptions)
  238. }
  239. }
  240. }
  241. func TestGetPodDNSType(t *testing.T) {
  242. recorder := record.NewFakeRecorder(20)
  243. nodeRef := &v1.ObjectReference{
  244. Kind: "Node",
  245. Name: string("testNode"),
  246. UID: types.UID("testNode"),
  247. Namespace: "",
  248. }
  249. testClusterDNSDomain := "TEST"
  250. clusterNS := "203.0.113.1"
  251. testClusterDNS := []net.IP{net.ParseIP(clusterNS)}
  252. configurer := NewConfigurer(recorder, nodeRef, nil, nil, testClusterDNSDomain, "")
  253. pod := &v1.Pod{
  254. ObjectMeta: metav1.ObjectMeta{
  255. UID: "",
  256. Name: "test_pod",
  257. Namespace: "testNS",
  258. Annotations: map[string]string{},
  259. },
  260. }
  261. testCases := []struct {
  262. desc string
  263. hasClusterDNS bool
  264. hostNetwork bool
  265. dnsPolicy v1.DNSPolicy
  266. expectedDNSType podDNSType
  267. expectedError bool
  268. }{
  269. {
  270. desc: "valid DNSClusterFirst without hostnetwork",
  271. hasClusterDNS: true,
  272. dnsPolicy: v1.DNSClusterFirst,
  273. expectedDNSType: podDNSCluster,
  274. },
  275. {
  276. desc: "valid DNSClusterFirstWithHostNet with hostnetwork",
  277. hasClusterDNS: true,
  278. hostNetwork: true,
  279. dnsPolicy: v1.DNSClusterFirstWithHostNet,
  280. expectedDNSType: podDNSCluster,
  281. },
  282. {
  283. desc: "valid DNSClusterFirstWithHostNet without hostnetwork",
  284. hasClusterDNS: true,
  285. dnsPolicy: v1.DNSClusterFirstWithHostNet,
  286. expectedDNSType: podDNSCluster,
  287. },
  288. {
  289. desc: "valid DNSDefault without hostnetwork",
  290. dnsPolicy: v1.DNSDefault,
  291. expectedDNSType: podDNSHost,
  292. },
  293. {
  294. desc: "valid DNSDefault with hostnetwork",
  295. hostNetwork: true,
  296. dnsPolicy: v1.DNSDefault,
  297. expectedDNSType: podDNSHost,
  298. },
  299. {
  300. desc: "DNSClusterFirst with hostnetwork, fallback to DNSDefault",
  301. hasClusterDNS: true,
  302. hostNetwork: true,
  303. dnsPolicy: v1.DNSClusterFirst,
  304. expectedDNSType: podDNSHost,
  305. },
  306. {
  307. desc: "valid DNSNone",
  308. dnsPolicy: v1.DNSNone,
  309. expectedDNSType: podDNSNone,
  310. },
  311. {
  312. desc: "invalid DNS policy, should return error",
  313. dnsPolicy: "invalidPolicy",
  314. expectedError: true,
  315. },
  316. }
  317. for _, tc := range testCases {
  318. t.Run(tc.desc, func(t *testing.T) {
  319. if tc.hasClusterDNS {
  320. configurer.clusterDNS = testClusterDNS
  321. } else {
  322. configurer.clusterDNS = nil
  323. }
  324. pod.Spec.DNSPolicy = tc.dnsPolicy
  325. pod.Spec.HostNetwork = tc.hostNetwork
  326. resType, err := getPodDNSType(pod)
  327. if tc.expectedError {
  328. if err == nil {
  329. t.Errorf("%s: GetPodDNSType(%v) got no error, want error", tc.desc, pod)
  330. }
  331. return
  332. }
  333. if resType != tc.expectedDNSType {
  334. t.Errorf("%s: GetPodDNSType(%v)=%v, want %v", tc.desc, pod, resType, tc.expectedDNSType)
  335. }
  336. })
  337. }
  338. }
  339. func TestGetPodDNS(t *testing.T) {
  340. recorder := record.NewFakeRecorder(20)
  341. nodeRef := &v1.ObjectReference{
  342. Kind: "Node",
  343. Name: string("testNode"),
  344. UID: types.UID("testNode"),
  345. Namespace: "",
  346. }
  347. clusterNS := "203.0.113.1"
  348. testClusterDNSDomain := "kubernetes.io"
  349. testClusterDNS := []net.IP{net.ParseIP(clusterNS)}
  350. configurer := NewConfigurer(recorder, nodeRef, nil, testClusterDNS, testClusterDNSDomain, "")
  351. pods := newTestPods(4)
  352. pods[0].Spec.DNSPolicy = v1.DNSClusterFirstWithHostNet
  353. pods[1].Spec.DNSPolicy = v1.DNSClusterFirst
  354. pods[2].Spec.DNSPolicy = v1.DNSClusterFirst
  355. pods[2].Spec.HostNetwork = false
  356. pods[3].Spec.DNSPolicy = v1.DNSDefault
  357. options := make([]struct {
  358. DNS []string
  359. DNSSearch []string
  360. }, 4)
  361. for i, pod := range pods {
  362. var err error
  363. dnsConfig, err := configurer.GetPodDNS(pod)
  364. if err != nil {
  365. t.Fatalf("failed to generate container options: %v", err)
  366. }
  367. options[i].DNS, options[i].DNSSearch = dnsConfig.Servers, dnsConfig.Searches
  368. }
  369. if len(options[0].DNS) != 1 || options[0].DNS[0] != clusterNS {
  370. t.Errorf("expected nameserver %s, got %+v", clusterNS, options[0].DNS)
  371. }
  372. if len(options[0].DNSSearch) == 0 || options[0].DNSSearch[0] != ".svc."+configurer.ClusterDomain {
  373. t.Errorf("expected search %s, got %+v", ".svc."+configurer.ClusterDomain, options[0].DNSSearch)
  374. }
  375. if len(options[1].DNS) != 1 || options[1].DNS[0] != "127.0.0.1" {
  376. t.Errorf("expected nameserver 127.0.0.1, got %+v", options[1].DNS)
  377. }
  378. if len(options[1].DNSSearch) != 1 || options[1].DNSSearch[0] != "." {
  379. t.Errorf("expected search \".\", got %+v", options[1].DNSSearch)
  380. }
  381. if len(options[2].DNS) != 1 || options[2].DNS[0] != clusterNS {
  382. t.Errorf("expected nameserver %s, got %+v", clusterNS, options[2].DNS)
  383. }
  384. if len(options[2].DNSSearch) == 0 || options[2].DNSSearch[0] != ".svc."+configurer.ClusterDomain {
  385. t.Errorf("expected search %s, got %+v", ".svc."+configurer.ClusterDomain, options[2].DNSSearch)
  386. }
  387. if len(options[3].DNS) != 1 || options[3].DNS[0] != "127.0.0.1" {
  388. t.Errorf("expected nameserver 127.0.0.1, got %+v", options[3].DNS)
  389. }
  390. if len(options[3].DNSSearch) != 1 || options[3].DNSSearch[0] != "." {
  391. t.Errorf("expected search \".\", got %+v", options[3].DNSSearch)
  392. }
  393. testResolverConfig := "/etc/resolv.conf"
  394. configurer = NewConfigurer(recorder, nodeRef, nil, testClusterDNS, testClusterDNSDomain, testResolverConfig)
  395. for i, pod := range pods {
  396. var err error
  397. dnsConfig, err := configurer.GetPodDNS(pod)
  398. if err != nil {
  399. t.Fatalf("failed to generate container options: %v", err)
  400. }
  401. options[i].DNS, options[i].DNSSearch = dnsConfig.Servers, dnsConfig.Searches
  402. }
  403. t.Logf("nameservers %+v", options[1].DNS)
  404. if len(options[0].DNS) != 1 {
  405. t.Errorf("expected cluster nameserver only, got %+v", options[0].DNS)
  406. } else if options[0].DNS[0] != clusterNS {
  407. t.Errorf("expected nameserver %s, got %v", clusterNS, options[0].DNS[0])
  408. }
  409. expLength := len(options[1].DNSSearch) + 3
  410. if expLength > 6 {
  411. expLength = 6
  412. }
  413. if len(options[0].DNSSearch) != expLength {
  414. t.Errorf("expected prepend of cluster domain, got %+v", options[0].DNSSearch)
  415. } else if options[0].DNSSearch[0] != ".svc."+configurer.ClusterDomain {
  416. t.Errorf("expected domain %s, got %s", ".svc."+configurer.ClusterDomain, options[0].DNSSearch)
  417. }
  418. if len(options[2].DNS) != 1 {
  419. t.Errorf("expected cluster nameserver only, got %+v", options[2].DNS)
  420. } else if options[2].DNS[0] != clusterNS {
  421. t.Errorf("expected nameserver %s, got %v", clusterNS, options[2].DNS[0])
  422. }
  423. if len(options[2].DNSSearch) != expLength {
  424. t.Errorf("expected prepend of cluster domain, got %+v", options[2].DNSSearch)
  425. } else if options[2].DNSSearch[0] != ".svc."+configurer.ClusterDomain {
  426. t.Errorf("expected domain %s, got %s", ".svc."+configurer.ClusterDomain, options[0].DNSSearch)
  427. }
  428. }
  429. func TestGetPodDNSCustom(t *testing.T) {
  430. recorder := record.NewFakeRecorder(20)
  431. nodeRef := &v1.ObjectReference{
  432. Kind: "Node",
  433. Name: string("testNode"),
  434. UID: types.UID("testNode"),
  435. Namespace: "",
  436. }
  437. testPodNamespace := "testNS"
  438. testClusterNameserver := "10.0.0.10"
  439. testClusterDNSDomain := "kubernetes.io"
  440. testSvcDomain := fmt.Sprintf("svc.%s", testClusterDNSDomain)
  441. testNsSvcDomain := fmt.Sprintf("%s.svc.%s", testPodNamespace, testClusterDNSDomain)
  442. testNdotsOptionValue := "3"
  443. testHostNameserver := "8.8.8.8"
  444. testHostDomain := "host.domain"
  445. testPod := &v1.Pod{
  446. ObjectMeta: metav1.ObjectMeta{
  447. Name: "test_pod",
  448. Namespace: testPodNamespace,
  449. },
  450. }
  451. resolvConfContent := []byte(fmt.Sprintf("nameserver %s\nsearch %s\n", testHostNameserver, testHostDomain))
  452. tmpfile, err := ioutil.TempFile("", "tmpResolvConf")
  453. if err != nil {
  454. t.Fatal(err)
  455. }
  456. defer os.Remove(tmpfile.Name())
  457. if _, err := tmpfile.Write(resolvConfContent); err != nil {
  458. t.Fatal(err)
  459. }
  460. if err := tmpfile.Close(); err != nil {
  461. t.Fatal(err)
  462. }
  463. configurer := NewConfigurer(recorder, nodeRef, nil, []net.IP{net.ParseIP(testClusterNameserver)}, testClusterDNSDomain, tmpfile.Name())
  464. testCases := []struct {
  465. desc string
  466. hostnetwork bool
  467. dnsPolicy v1.DNSPolicy
  468. dnsConfig *v1.PodDNSConfig
  469. expectedDNSConfig *runtimeapi.DNSConfig
  470. }{
  471. {
  472. desc: "DNSNone without DNSConfig should have empty DNS settings",
  473. dnsPolicy: v1.DNSNone,
  474. expectedDNSConfig: &runtimeapi.DNSConfig{},
  475. },
  476. {
  477. desc: "DNSNone with DNSConfig should have a merged DNS settings",
  478. dnsPolicy: v1.DNSNone,
  479. dnsConfig: &v1.PodDNSConfig{
  480. Nameservers: []string{"203.0.113.1"},
  481. Searches: []string{"my.domain", "second.domain"},
  482. Options: []v1.PodDNSConfigOption{
  483. {Name: "ndots", Value: &testNdotsOptionValue},
  484. {Name: "debug"},
  485. },
  486. },
  487. expectedDNSConfig: &runtimeapi.DNSConfig{
  488. Servers: []string{"203.0.113.1"},
  489. Searches: []string{"my.domain", "second.domain"},
  490. Options: []string{"ndots:3", "debug"},
  491. },
  492. },
  493. {
  494. desc: "DNSClusterFirst with DNSConfig should have a merged DNS settings",
  495. dnsPolicy: v1.DNSClusterFirst,
  496. dnsConfig: &v1.PodDNSConfig{
  497. Nameservers: []string{"10.0.0.11"},
  498. Searches: []string{"my.domain"},
  499. Options: []v1.PodDNSConfigOption{
  500. {Name: "ndots", Value: &testNdotsOptionValue},
  501. {Name: "debug"},
  502. },
  503. },
  504. expectedDNSConfig: &runtimeapi.DNSConfig{
  505. Servers: []string{testClusterNameserver, "10.0.0.11"},
  506. Searches: []string{testNsSvcDomain, testSvcDomain, testClusterDNSDomain, testHostDomain, "my.domain"},
  507. Options: []string{"ndots:3", "debug"},
  508. },
  509. },
  510. {
  511. desc: "DNSClusterFirstWithHostNet with DNSConfig should have a merged DNS settings",
  512. hostnetwork: true,
  513. dnsPolicy: v1.DNSClusterFirstWithHostNet,
  514. dnsConfig: &v1.PodDNSConfig{
  515. Nameservers: []string{"10.0.0.11"},
  516. Searches: []string{"my.domain"},
  517. Options: []v1.PodDNSConfigOption{
  518. {Name: "ndots", Value: &testNdotsOptionValue},
  519. {Name: "debug"},
  520. },
  521. },
  522. expectedDNSConfig: &runtimeapi.DNSConfig{
  523. Servers: []string{testClusterNameserver, "10.0.0.11"},
  524. Searches: []string{testNsSvcDomain, testSvcDomain, testClusterDNSDomain, testHostDomain, "my.domain"},
  525. Options: []string{"ndots:3", "debug"},
  526. },
  527. },
  528. {
  529. desc: "DNSDefault with DNSConfig should have a merged DNS settings",
  530. dnsPolicy: v1.DNSDefault,
  531. dnsConfig: &v1.PodDNSConfig{
  532. Nameservers: []string{"10.0.0.11"},
  533. Searches: []string{"my.domain"},
  534. Options: []v1.PodDNSConfigOption{
  535. {Name: "ndots", Value: &testNdotsOptionValue},
  536. {Name: "debug"},
  537. },
  538. },
  539. expectedDNSConfig: &runtimeapi.DNSConfig{
  540. Servers: []string{testHostNameserver, "10.0.0.11"},
  541. Searches: []string{testHostDomain, "my.domain"},
  542. Options: []string{"ndots:3", "debug"},
  543. },
  544. },
  545. }
  546. for _, tc := range testCases {
  547. t.Run(tc.desc, func(t *testing.T) {
  548. testPod.Spec.HostNetwork = tc.hostnetwork
  549. testPod.Spec.DNSConfig = tc.dnsConfig
  550. testPod.Spec.DNSPolicy = tc.dnsPolicy
  551. resDNSConfig, err := configurer.GetPodDNS(testPod)
  552. if err != nil {
  553. t.Errorf("%s: GetPodDNS(%v), unexpected error: %v", tc.desc, testPod, err)
  554. }
  555. if !dnsConfigsAreEqual(resDNSConfig, tc.expectedDNSConfig) {
  556. t.Errorf("%s: GetPodDNS(%v)=%v, want %v", tc.desc, testPod, resDNSConfig, tc.expectedDNSConfig)
  557. }
  558. })
  559. }
  560. }
  561. func dnsConfigsAreEqual(resConfig, expectedConfig *runtimeapi.DNSConfig) bool {
  562. if len(resConfig.Servers) != len(expectedConfig.Servers) ||
  563. len(resConfig.Searches) != len(expectedConfig.Searches) ||
  564. len(resConfig.Options) != len(expectedConfig.Options) {
  565. return false
  566. }
  567. for i, server := range resConfig.Servers {
  568. if expectedConfig.Servers[i] != server {
  569. return false
  570. }
  571. }
  572. for i, search := range resConfig.Searches {
  573. if expectedConfig.Searches[i] != search {
  574. return false
  575. }
  576. }
  577. // Options order may be changed after conversion.
  578. return sets.NewString(resConfig.Options...).Equal(sets.NewString(expectedConfig.Options...))
  579. }
  580. func newTestPods(count int) []*v1.Pod {
  581. pods := make([]*v1.Pod, count)
  582. for i := 0; i < count; i++ {
  583. pods[i] = &v1.Pod{
  584. Spec: v1.PodSpec{
  585. HostNetwork: true,
  586. },
  587. ObjectMeta: metav1.ObjectMeta{
  588. UID: types.UID(10000 + i),
  589. Name: fmt.Sprintf("pod%d", i),
  590. },
  591. }
  592. }
  593. return pods
  594. }