services.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 services
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "os/exec"
  19. "path"
  20. "testing"
  21. "k8s.io/klog"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. )
  25. // E2EServices starts and stops e2e services in a separate process. The test
  26. // uses it to start and stop all e2e services.
  27. type E2EServices struct {
  28. // monitorParent determines whether the sub-processes should watch and die with the current
  29. // process.
  30. rmDirs []string
  31. monitorParent bool
  32. services *server
  33. kubelet *server
  34. logs logFiles
  35. }
  36. // NewE2EServices returns a new E2EServices instance.
  37. func NewE2EServices(monitorParent bool) *E2EServices {
  38. return &E2EServices{
  39. monitorParent: monitorParent,
  40. // Special log files that need to be collected for additional debugging.
  41. logs: getLogFiles(),
  42. }
  43. }
  44. // Start starts the e2e services in another process by calling back into the
  45. // test binary. Returns when all e2e services are ready or an error.
  46. //
  47. // We want to statically link e2e services into the test binary, but we don't
  48. // want their glog output to pollute the test result. So we run the binary in
  49. // run-services-mode to start e2e services in another process.
  50. // The function starts 2 processes:
  51. // * internal e2e services: services which statically linked in the test binary - apiserver, etcd and
  52. // namespace controller.
  53. // * kubelet: kubelet binary is outside. (We plan to move main kubelet start logic out when we have
  54. // standard kubelet launcher)
  55. func (e *E2EServices) Start() error {
  56. var err error
  57. if !framework.TestContext.NodeConformance {
  58. // Start kubelet
  59. e.kubelet, err = e.startKubelet()
  60. if err != nil {
  61. return fmt.Errorf("failed to start kubelet: %v", err)
  62. }
  63. }
  64. e.services, err = e.startInternalServices()
  65. return err
  66. }
  67. // Stop stops the e2e services.
  68. func (e *E2EServices) Stop() {
  69. defer func() {
  70. if !framework.TestContext.NodeConformance {
  71. // Collect log files.
  72. e.collectLogFiles()
  73. }
  74. }()
  75. if e.services != nil {
  76. if err := e.services.kill(); err != nil {
  77. klog.Errorf("Failed to stop services: %v", err)
  78. }
  79. }
  80. if e.kubelet != nil {
  81. if err := e.kubelet.kill(); err != nil {
  82. klog.Errorf("Failed to stop kubelet: %v", err)
  83. }
  84. }
  85. if e.rmDirs != nil {
  86. for _, d := range e.rmDirs {
  87. err := os.RemoveAll(d)
  88. if err != nil {
  89. klog.Errorf("Failed to delete directory %s: %v", d, err)
  90. }
  91. }
  92. }
  93. }
  94. // RunE2EServices actually start the e2e services. This function is used to
  95. // start e2e services in current process. This is only used in run-services-mode.
  96. func RunE2EServices(t *testing.T) {
  97. // Populate global DefaultFeatureGate with value from TestContext.FeatureGates.
  98. // This way, statically-linked components see the same feature gate config as the test context.
  99. if err := utilfeature.DefaultMutableFeatureGate.SetFromMap(framework.TestContext.FeatureGates); err != nil {
  100. t.Fatal(err)
  101. }
  102. e := newE2EServices()
  103. if err := e.run(t); err != nil {
  104. klog.Fatalf("Failed to run e2e services: %v", err)
  105. }
  106. }
  107. const (
  108. // services.log is the combined log of all services
  109. servicesLogFile = "services.log"
  110. // LogVerbosityLevel is consistent with the level used in a cluster e2e test.
  111. LogVerbosityLevel = "4"
  112. )
  113. // startInternalServices starts the internal services in a separate process.
  114. func (e *E2EServices) startInternalServices() (*server, error) {
  115. testBin, err := os.Executable()
  116. if err != nil {
  117. return nil, fmt.Errorf("can't get current binary: %v", err)
  118. }
  119. // Pass all flags into the child process, so that it will see the same flag set.
  120. startCmd := exec.Command(testBin, append([]string{"--run-services-mode"}, os.Args[1:]...)...)
  121. server := newServer("services", startCmd, nil, nil, getServicesHealthCheckURLs(), servicesLogFile, e.monitorParent, false)
  122. return server, server.start()
  123. }
  124. // collectLogFiles collects logs of interest either via journalctl or by creating sym
  125. // links. Since we scp files from the remote directory, symlinks will be
  126. // treated as normal files and file contents will be copied over.
  127. func (e *E2EServices) collectLogFiles() {
  128. // Nothing to do if report dir is not specified.
  129. if framework.TestContext.ReportDir == "" {
  130. return
  131. }
  132. klog.Info("Fetching log files...")
  133. journaldFound := isJournaldAvailable()
  134. for targetFileName, log := range e.logs {
  135. targetLink := path.Join(framework.TestContext.ReportDir, targetFileName)
  136. if journaldFound {
  137. // Skip log files that do not have an equivalent in journald-based machines.
  138. if len(log.JournalctlCommand) == 0 {
  139. continue
  140. }
  141. klog.Infof("Get log file %q with journalctl command %v.", targetFileName, log.JournalctlCommand)
  142. out, err := exec.Command("journalctl", log.JournalctlCommand...).CombinedOutput()
  143. if err != nil {
  144. klog.Errorf("failed to get %q from journald: %v, %v", targetFileName, string(out), err)
  145. } else {
  146. if err = ioutil.WriteFile(targetLink, out, 0644); err != nil {
  147. klog.Errorf("failed to write logs to %q: %v", targetLink, err)
  148. }
  149. }
  150. continue
  151. }
  152. for _, file := range log.Files {
  153. if _, err := os.Stat(file); err != nil {
  154. // Expected file not found on this distro.
  155. continue
  156. }
  157. if err := copyLogFile(file, targetLink); err != nil {
  158. klog.Error(err)
  159. } else {
  160. break
  161. }
  162. }
  163. }
  164. }
  165. // isJournaldAvailable returns whether the system executing the tests uses
  166. // journald.
  167. func isJournaldAvailable() bool {
  168. _, err := exec.LookPath("journalctl")
  169. return err == nil
  170. }
  171. func copyLogFile(src, target string) error {
  172. // If not a journald based distro, then just symlink files.
  173. if out, err := exec.Command("cp", src, target).CombinedOutput(); err != nil {
  174. return fmt.Errorf("failed to copy %q to %q: %v, %v", src, target, out, err)
  175. }
  176. if out, err := exec.Command("chmod", "a+r", target).CombinedOutput(); err != nil {
  177. return fmt.Errorf("failed to make log file %q world readable: %v, %v", target, out, err)
  178. }
  179. return nil
  180. }