utils.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // +build !windows
  2. /*
  3. Copyright 2019 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package main
  15. import (
  16. "strings"
  17. )
  18. const etcHostsFile = "/etc/hosts"
  19. // A /etc/resolv.conf file managed by kubelet looks like this:
  20. // nameserver DNS_CLUSTER_IP
  21. // search test-dns.svc.cluster.local svc.cluster.local cluster.local q53aahaikqaehcai3ylfqdtc5b.bx.internal.cloudapp.net
  22. // options ndots:5
  23. func getDNSSuffixList() []string {
  24. fileData := readFile("/etc/resolv.conf")
  25. lines := strings.Split(fileData, "\n")
  26. for _, line := range lines {
  27. if strings.HasPrefix(line, "search") {
  28. // omit the starting "search".
  29. return strings.Split(line, " ")[1:]
  30. }
  31. }
  32. panic("Could not find DNS search list!")
  33. }
  34. func getDNSServerList() []string {
  35. fileData := readFile("/etc/resolv.conf")
  36. lines := strings.Split(fileData, "\n")
  37. for _, line := range lines {
  38. if strings.HasPrefix(line, "nameserver") {
  39. // omit the starting "nameserver".
  40. return strings.Split(line, " ")[1:]
  41. }
  42. }
  43. panic("Could not find DNS search list!")
  44. }