node_conformance.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Copyright 2016 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 remote
  14. import (
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "runtime"
  20. "strings"
  21. "time"
  22. "k8s.io/klog"
  23. "k8s.io/kubernetes/test/e2e_node/builder"
  24. "k8s.io/kubernetes/test/utils"
  25. )
  26. // ConformanceRemote contains the specific functions in the node conformance test suite.
  27. type ConformanceRemote struct{}
  28. // InitConformanceRemote initializes the node conformance test suite.
  29. func InitConformanceRemote() TestSuite {
  30. return &ConformanceRemote{}
  31. }
  32. // getConformanceDirectory gets node conformance test build directory.
  33. func getConformanceDirectory() (string, error) {
  34. k8sRoot, err := utils.GetK8sRootDir()
  35. if err != nil {
  36. return "", err
  37. }
  38. return filepath.Join(k8sRoot, "test", "e2e_node", "conformance", "build"), nil
  39. }
  40. // commandToString is a helper function which formats command to string.
  41. func commandToString(c *exec.Cmd) string {
  42. return strings.Join(append([]string{c.Path}, c.Args[1:]...), " ")
  43. }
  44. // Image path constants.
  45. const (
  46. conformanceRegistry = "k8s.gcr.io"
  47. conformanceArch = runtime.GOARCH
  48. conformanceTarfile = "node_conformance.tar"
  49. conformanceTestBinary = "e2e_node.test"
  50. conformanceImageLoadTimeout = time.Duration(30) * time.Second
  51. )
  52. // timestamp is used as an unique id of current test.
  53. var timestamp = getTimestamp()
  54. // getConformanceTestImageName returns name of the conformance test image given the system spec name.
  55. func getConformanceTestImageName(systemSpecName string) string {
  56. if systemSpecName == "" {
  57. return fmt.Sprintf("%s/node-test-%s:%s", conformanceRegistry, conformanceArch, timestamp)
  58. }
  59. return fmt.Sprintf("%s/node-test-%s-%s:%s", conformanceRegistry, systemSpecName, conformanceArch, timestamp)
  60. }
  61. // buildConformanceTest builds node conformance test image tarball into binDir.
  62. func buildConformanceTest(binDir, systemSpecName string) error {
  63. // Get node conformance directory.
  64. conformancePath, err := getConformanceDirectory()
  65. if err != nil {
  66. return fmt.Errorf("failed to get node conformance directory: %v", err)
  67. }
  68. // Build docker image.
  69. cmd := exec.Command("make", "-C", conformancePath, "BIN_DIR="+binDir,
  70. "REGISTRY="+conformanceRegistry,
  71. "ARCH="+conformanceArch,
  72. "VERSION="+timestamp,
  73. "SYSTEM_SPEC_NAME="+systemSpecName)
  74. if output, err := cmd.CombinedOutput(); err != nil {
  75. return fmt.Errorf("failed to build node conformance docker image: command - %q, error - %v, output - %q",
  76. commandToString(cmd), err, output)
  77. }
  78. // Save docker image into tar file.
  79. cmd = exec.Command("docker", "save", "-o", filepath.Join(binDir, conformanceTarfile), getConformanceTestImageName(systemSpecName))
  80. if output, err := cmd.CombinedOutput(); err != nil {
  81. return fmt.Errorf("failed to save node conformance docker image into tar file: command - %q, error - %v, output - %q",
  82. commandToString(cmd), err, output)
  83. }
  84. return nil
  85. }
  86. // SetupTestPackage sets up the test package with binaries k8s required for node conformance test
  87. func (c *ConformanceRemote) SetupTestPackage(tardir, systemSpecName string) error {
  88. // Build the executables
  89. if err := builder.BuildGo(); err != nil {
  90. return fmt.Errorf("failed to build the dependencies: %v", err)
  91. }
  92. // Make sure we can find the newly built binaries
  93. buildOutputDir, err := utils.GetK8sBuildOutputDir()
  94. if err != nil {
  95. return fmt.Errorf("failed to locate kubernetes build output directory %v", err)
  96. }
  97. // Build node conformance tarball.
  98. if err := buildConformanceTest(buildOutputDir, systemSpecName); err != nil {
  99. return fmt.Errorf("failed to build node conformance test: %v", err)
  100. }
  101. // Copy files
  102. requiredFiles := []string{"kubelet", conformanceTestBinary, conformanceTarfile}
  103. for _, file := range requiredFiles {
  104. source := filepath.Join(buildOutputDir, file)
  105. if _, err := os.Stat(source); err != nil {
  106. return fmt.Errorf("failed to locate test file %s: %v", file, err)
  107. }
  108. output, err := exec.Command("cp", source, filepath.Join(tardir, file)).CombinedOutput()
  109. if err != nil {
  110. return fmt.Errorf("failed to copy %q: error - %v output - %q", file, err, output)
  111. }
  112. }
  113. return nil
  114. }
  115. // loadConformanceImage loads node conformance image from tar file.
  116. func loadConformanceImage(host, workspace string) error {
  117. tarfile := filepath.Join(workspace, conformanceTarfile)
  118. if output, err := SSH(host, "timeout", conformanceImageLoadTimeout.String(),
  119. "docker", "load", "-i", tarfile); err != nil {
  120. return fmt.Errorf("failed to load node conformance image from tar file %q: error - %v output - %q",
  121. tarfile, err, output)
  122. }
  123. return nil
  124. }
  125. // kubeletLauncherLog is the log of kubelet launcher.
  126. const kubeletLauncherLog = "kubelet-launcher.log"
  127. // kubeletPodPath is a fixed known pod specification path. We can not use the random pod
  128. // manifest directory generated in e2e_node.test because we need to mount the directory into
  129. // the conformance test container, it's easier if it's a known directory.
  130. // TODO(random-liu): Get rid of this once we switch to cluster e2e node bootstrap script.
  131. var kubeletPodPath = "conformance-pod-manifest-" + timestamp
  132. // getPodPath returns pod manifest full path.
  133. func getPodPath(workspace string) string {
  134. return filepath.Join(workspace, kubeletPodPath)
  135. }
  136. // isSystemd returns whether the node is a systemd node.
  137. func isSystemd(host string) (bool, error) {
  138. // Returns "systemd" if /run/systemd/system is found, empty string otherwise.
  139. output, err := SSH(host, "test", "-e", "/run/systemd/system", "&&", "echo", "systemd", "||", "true")
  140. if err != nil {
  141. return false, fmt.Errorf("failed to check systemd: error - %v output - %q", err, output)
  142. }
  143. return strings.TrimSpace(output) != "", nil
  144. }
  145. // launchKubelet launches kubelet by running e2e_node.test binary in run-kubelet-mode.
  146. // This is a temporary solution, we should change node e2e to use the same node bootstrap
  147. // with cluster e2e and launch kubelet outside of the test for both regular node e2e and
  148. // node conformance test.
  149. // TODO(random-liu): Switch to use standard node bootstrap script.
  150. func launchKubelet(host, workspace, results, testArgs string) error {
  151. podManifestPath := getPodPath(workspace)
  152. if output, err := SSH(host, "mkdir", podManifestPath); err != nil {
  153. return fmt.Errorf("failed to create kubelet pod manifest path %q: error - %v output - %q",
  154. podManifestPath, err, output)
  155. }
  156. startKubeletCmd := fmt.Sprintf("./%s --run-kubelet-mode --logtostderr --node-name=%s"+
  157. " --report-dir=%s %s --kubelet-flags=--pod-manifest-path=%s > %s 2>&1",
  158. conformanceTestBinary, host, results, testArgs, podManifestPath, filepath.Join(results, kubeletLauncherLog))
  159. var cmd []string
  160. systemd, err := isSystemd(host)
  161. if err != nil {
  162. return fmt.Errorf("failed to check systemd: %v", err)
  163. }
  164. if systemd {
  165. cmd = []string{
  166. "systemd-run", "sh", "-c", getSSHCommand(" && ",
  167. // Switch to workspace.
  168. fmt.Sprintf("cd %s", workspace),
  169. // Launch kubelet by running e2e_node.test in run-kubelet-mode.
  170. startKubeletCmd,
  171. ),
  172. }
  173. } else {
  174. cmd = []string{
  175. "sh", "-c", getSSHCommand(" && ",
  176. // Switch to workspace.
  177. fmt.Sprintf("cd %s", workspace),
  178. // Launch kubelet by running e2e_node.test in run-kubelet-mode with nohup.
  179. fmt.Sprintf("(nohup %s &)", startKubeletCmd),
  180. ),
  181. }
  182. }
  183. klog.V(2).Infof("Launch kubelet with command: %v", cmd)
  184. output, err := SSH(host, cmd...)
  185. if err != nil {
  186. return fmt.Errorf("failed to launch kubelet with command %v: error - %v output - %q",
  187. cmd, err, output)
  188. }
  189. klog.Info("Successfully launch kubelet")
  190. return nil
  191. }
  192. // kubeletStopGracePeriod is the grace period to wait before forcibly killing kubelet.
  193. const kubeletStopGracePeriod = 10 * time.Second
  194. // stopKubelet stops kubelet launcher and kubelet gracefully.
  195. func stopKubelet(host, workspace string) error {
  196. klog.Info("Gracefully stop kubelet launcher")
  197. if output, err := SSH(host, "pkill", conformanceTestBinary); err != nil {
  198. return fmt.Errorf("failed to gracefully stop kubelet launcher: error - %v output - %q",
  199. err, output)
  200. }
  201. klog.Info("Wait for kubelet launcher to stop")
  202. stopped := false
  203. for start := time.Now(); time.Since(start) < kubeletStopGracePeriod; time.Sleep(time.Second) {
  204. // Check whether the process is still running.
  205. output, err := SSH(host, "pidof", conformanceTestBinary, "||", "true")
  206. if err != nil {
  207. return fmt.Errorf("failed to check kubelet stopping: error - %v output -%q",
  208. err, output)
  209. }
  210. // Kubelet is stopped
  211. if strings.TrimSpace(output) == "" {
  212. stopped = true
  213. break
  214. }
  215. }
  216. if !stopped {
  217. klog.Info("Forcibly stop kubelet")
  218. if output, err := SSH(host, "pkill", "-SIGKILL", conformanceTestBinary); err != nil {
  219. return fmt.Errorf("failed to forcibly stop kubelet: error - %v output - %q",
  220. err, output)
  221. }
  222. }
  223. klog.Info("Successfully stop kubelet")
  224. // Clean up the pod manifest path
  225. podManifestPath := getPodPath(workspace)
  226. if output, err := SSH(host, "rm", "-f", filepath.Join(workspace, podManifestPath)); err != nil {
  227. return fmt.Errorf("failed to cleanup pod manifest directory %q: error - %v, output - %q",
  228. podManifestPath, err, output)
  229. }
  230. return nil
  231. }
  232. // RunTest runs test on the node.
  233. func (c *ConformanceRemote) RunTest(host, workspace, results, imageDesc, junitFilePrefix, testArgs, _, systemSpecName, extraEnvs string, timeout time.Duration) (string, error) {
  234. // Install the cni plugins and add a basic CNI configuration.
  235. if err := setupCNI(host, workspace); err != nil {
  236. return "", err
  237. }
  238. // Configure iptables firewall rules.
  239. if err := configureFirewall(host); err != nil {
  240. return "", err
  241. }
  242. // Kill any running node processes.
  243. cleanupNodeProcesses(host)
  244. // Load node conformance image.
  245. if err := loadConformanceImage(host, workspace); err != nil {
  246. return "", err
  247. }
  248. // Launch kubelet.
  249. if err := launchKubelet(host, workspace, results, testArgs); err != nil {
  250. return "", err
  251. }
  252. // Stop kubelet.
  253. defer func() {
  254. if err := stopKubelet(host, workspace); err != nil {
  255. // Only log an error if failed to stop kubelet because it is not critical.
  256. klog.Errorf("failed to stop kubelet: %v", err)
  257. }
  258. }()
  259. // Run the tests
  260. klog.V(2).Infof("Starting tests on %q", host)
  261. podManifestPath := getPodPath(workspace)
  262. cmd := fmt.Sprintf("'timeout -k 30s %fs docker run --rm --privileged=true --net=host -v /:/rootfs -v %s:%s -v %s:/var/result -e TEST_ARGS=--report-prefix=%s -e EXTRA_ENVS=%s %s'",
  263. timeout.Seconds(), podManifestPath, podManifestPath, results, junitFilePrefix, extraEnvs, getConformanceTestImageName(systemSpecName))
  264. testOutput, err := SSH(host, "sh", "-c", cmd)
  265. if err != nil {
  266. return testOutput, err
  267. }
  268. return testOutput, nil
  269. }