conformance.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // Build the binary with `go build conformance.go`, then run the conformance binary on a node candidate. If compiled
  14. // on a non-linux machine, must be cross compiled for the host.
  15. package main
  16. import (
  17. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "os/exec"
  22. "regexp"
  23. "strings"
  24. "errors"
  25. "os"
  26. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  27. )
  28. const success = "\033[0;32mSUCESS\033[0m"
  29. const failed = "\033[0;31mFAILED\033[0m"
  30. const notConfigured = "\033[0;34mNOT CONFIGURED\033[0m"
  31. const skipped = "\033[0;34mSKIPPED\033[0m"
  32. var checkFlag = flag.String(
  33. "check", "all", "what to check for conformance. One or more of all,container-runtime,daemons,dns,firewall,kernel")
  34. func init() {
  35. // Set this to false to undo util/logs.go settings it to true. Prevents cadvisor log spam.
  36. // Remove this once util/logs.go stops setting the flag to true.
  37. flag.Set("logtostderr", "false")
  38. }
  39. // TODO: Should we write an e2e test for this?
  40. func main() {
  41. flag.Parse()
  42. o := strings.Split(*checkFlag, ",")
  43. errs := check(o...)
  44. if len(errs) > 0 {
  45. os.Exit(1)
  46. } else {
  47. os.Exit(0)
  48. }
  49. }
  50. // check returns errors found while checking the provided components. Will prevent errors to stdout.
  51. func check(options ...string) []error {
  52. errs := []error{}
  53. for _, c := range options {
  54. switch c {
  55. case "all":
  56. errs = appendNotNil(errs, kernel())
  57. errs = appendNotNil(errs, containerRuntime())
  58. errs = appendNotNil(errs, daemons())
  59. errs = appendNotNil(errs, firewall())
  60. errs = appendNotNil(errs, dns())
  61. case "containerruntime":
  62. errs = appendNotNil(errs, containerRuntime())
  63. case "daemons":
  64. errs = appendNotNil(errs, daemons())
  65. case "dns":
  66. errs = appendNotNil(errs, dns())
  67. case "firewall":
  68. errs = appendNotNil(errs, firewall())
  69. case "kernel":
  70. errs = appendNotNil(errs, kernel())
  71. default:
  72. fmt.Printf("Unrecognized option %s\n", c)
  73. errs = append(errs, fmt.Errorf("Unrecognized option %s", c))
  74. }
  75. }
  76. return errs
  77. }
  78. const dockerVersionRegex = `1\.[7-9]\.[0-9]+`
  79. // containerRuntime checks that a suitable container runtime is installed and recognized by cadvisor: docker 1.7-1.9
  80. func containerRuntime() error {
  81. dockerRegex, err := regexp.Compile(dockerVersionRegex)
  82. if err != nil {
  83. // This should never happen and can only be fixed by changing the code
  84. panic(err)
  85. }
  86. // Setup cadvisor to check the container environment
  87. c, err := cadvisor.New(cadvisor.NewImageFsInfoProvider("docker", ""), "/var/lib/kubelet", []string{"/"}, false)
  88. if err != nil {
  89. return printError("Container Runtime Check: %s Could not start cadvisor %v", failed, err)
  90. }
  91. vi, err := c.VersionInfo()
  92. if err != nil {
  93. return printError("Container Runtime Check: %s Could not get VersionInfo %v", failed, err)
  94. }
  95. d := vi.DockerVersion
  96. if !dockerRegex.Match([]byte(d)) {
  97. return printError(
  98. "Container Runtime Check: %s Docker version %s does not matching %s. You may need to run as root or the "+
  99. "user the kubelet will run under.", failed, d, dockerVersionRegex)
  100. }
  101. return printSuccess("Container Runtime Check: %s", success)
  102. }
  103. const kubeletClusterDNSRegexStr = `\/kubelet.*--cluster-dns=(\S+) `
  104. const kubeletClusterDomainRegexStr = `\/kubelet.*--cluster-domain=(\S+)`
  105. // dns checks that cluster dns has been properly configured and can resolve the kubernetes.default service
  106. func dns() error {
  107. dnsRegex, err := regexp.Compile(kubeletClusterDNSRegexStr)
  108. if err != nil {
  109. // This should never happen and can only be fixed by changing the code
  110. panic(err)
  111. }
  112. domainRegex, err := regexp.Compile(kubeletClusterDomainRegexStr)
  113. if err != nil {
  114. // This should never happen and can only be fixed by changing the code
  115. panic(err)
  116. }
  117. h, err := net.LookupHost("kubernetes.default")
  118. if err == nil {
  119. return printSuccess("Dns Check (Optional): %s", success)
  120. }
  121. if len(h) > 0 {
  122. return printSuccess("Dns Check (Optional): %s", success)
  123. }
  124. kubecmd, err := exec.Command("ps", "aux").CombinedOutput()
  125. if err != nil {
  126. // Executing ps aux shouldn't have failed
  127. panic(err)
  128. }
  129. // look for the dns flag and parse the value
  130. dns := dnsRegex.FindStringSubmatch(string(kubecmd))
  131. if len(dns) < 2 {
  132. return printSuccess(
  133. "Dns Check (Optional): %s No hosts resolve to kubernetes.default. kubelet will need to set "+
  134. "--cluster-dns and --cluster-domain when run", notConfigured)
  135. }
  136. // look for the domain flag and parse the value
  137. domain := domainRegex.FindStringSubmatch(string(kubecmd))
  138. if len(domain) < 2 {
  139. return printSuccess(
  140. "Dns Check (Optional): %s No hosts resolve to kubernetes.default. kubelet will need to set "+
  141. "--cluster-dns and --cluster-domain when run", notConfigured)
  142. }
  143. // do a lookup with the flags the kubelet is running with
  144. nsArgs := []string{"-q=a", fmt.Sprintf("kubernetes.default.%s", domain[1]), dns[1]}
  145. if err = exec.Command("nslookup", nsArgs...).Run(); err != nil {
  146. // Mark this as failed since there was a clear intention to set it up, but it is done so improperly
  147. return printError(
  148. "Dns Check (Optional): %s No hosts resolve to kubernetes.default kubelet found, but cannot resolve "+
  149. "kubernetes.default using nslookup %s error: %v", failed, strings.Join(nsArgs, " "), err)
  150. }
  151. // Can resolve kubernetes.default using the kubelete dns and domain values
  152. return printSuccess("Dns Check (Optional): %s", success)
  153. }
  154. const cmdlineCGroupMemory = `cgroup_enable=memory`
  155. // kernel checks that the kernel has been configured correctly to support the required cgroup features
  156. func kernel() error {
  157. cmdline, err := ioutil.ReadFile("/proc/cmdline")
  158. if err != nil {
  159. return printError("Kernel Command Line Check %s: Could not check /proc/cmdline", failed)
  160. }
  161. if !strings.Contains(string(cmdline), cmdlineCGroupMemory) {
  162. return printError("Kernel Command Line Check %s: cgroup_enable=memory not enabled in /proc/cmdline", failed)
  163. }
  164. return printSuccess("Kernel Command Line %s", success)
  165. }
  166. const iptablesInputRegexStr = `Chain INPUT \(policy DROP\)`
  167. const iptablesForwardRegexStr = `Chain FORWARD \(policy DROP\)`
  168. // firewall checks that iptables does not have common firewall rules setup that would disrupt traffic
  169. func firewall() error {
  170. out, err := exec.Command("iptables", "-L", "INPUT").CombinedOutput()
  171. if err != nil {
  172. return printSuccess("Firewall IPTables Check %s: Could not run iptables", skipped)
  173. }
  174. inputRegex, err := regexp.Compile(iptablesInputRegexStr)
  175. if err != nil {
  176. // This should never happen and can only be fixed by changing the code
  177. panic(err)
  178. }
  179. if inputRegex.Match(out) {
  180. return printError("Firewall IPTables Check %s: Found INPUT rule matching %s", failed, iptablesInputRegexStr)
  181. }
  182. // Check GCE forward rules
  183. out, err = exec.Command("iptables", "-L", "FORWARD").CombinedOutput()
  184. if err != nil {
  185. return printSuccess("Firewall IPTables Check %s: Could not run iptables", skipped)
  186. }
  187. forwardRegex, err := regexp.Compile(iptablesForwardRegexStr)
  188. if err != nil {
  189. // This should never happen and can only be fixed by changing the code
  190. panic(err)
  191. }
  192. if forwardRegex.Match(out) {
  193. return printError("Firewall IPTables Check %s: Found FORWARD rule matching %s", failed, iptablesInputRegexStr)
  194. }
  195. return printSuccess("Firewall IPTables Check %s", success)
  196. }
  197. // daemons checks that the required node programs are running: kubelet, kube-proxy, and docker
  198. func daemons() error {
  199. if exec.Command("pgrep", "-f", "kubelet").Run() != nil {
  200. return printError("Daemon Check %s: kubelet process not found", failed)
  201. }
  202. if exec.Command("pgrep", "-f", "kube-proxy").Run() != nil {
  203. return printError("Daemon Check %s: kube-proxy process not found", failed)
  204. }
  205. return printSuccess("Daemon Check %s", success)
  206. }
  207. // printError provides its arguments to print a format string to the console (newline terminated) and returns an
  208. // error with the same string
  209. func printError(s string, args ...interface{}) error {
  210. es := fmt.Sprintf(s, args...)
  211. fmt.Println(es)
  212. return errors.New(es)
  213. }
  214. // printSuccess provides its arguments to print a format string to the console (newline terminated) and returns nil
  215. func printSuccess(s string, args ...interface{}) error {
  216. fmt.Println(fmt.Sprintf(s, args...))
  217. return nil
  218. }
  219. // appendNotNil appends err to errs iff err is not nil
  220. func appendNotNil(errs []error, err error) []error {
  221. if err != nil {
  222. return append(errs, err)
  223. }
  224. return errs
  225. }