internal_services.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "os"
  16. "testing"
  17. etcdtesting "k8s.io/apiserver/pkg/storage/etcd/testing"
  18. "k8s.io/apiserver/pkg/storage/storagebackend"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. "k8s.io/klog"
  21. )
  22. // e2eService manages e2e services in current process.
  23. type e2eServices struct {
  24. rmDirs []string
  25. // statically linked e2e services
  26. etcdServer *etcdtesting.EtcdTestServer
  27. etcdStorage *storagebackend.Config
  28. apiServer *APIServer
  29. nsController *NamespaceController
  30. }
  31. func newE2EServices() *e2eServices {
  32. return &e2eServices{}
  33. }
  34. // run starts all e2e services and wait for the termination signal. Once receives the
  35. // termination signal, it will stop the e2e services gracefully.
  36. func (es *e2eServices) run(t *testing.T) error {
  37. defer es.stop(t)
  38. if err := es.start(t); err != nil {
  39. return err
  40. }
  41. // Wait until receiving a termination signal.
  42. waitForTerminationSignal()
  43. return nil
  44. }
  45. // start starts the tests embedded services or returns an error.
  46. func (es *e2eServices) start(t *testing.T) error {
  47. klog.Info("Starting e2e services...")
  48. err := es.startEtcd(t)
  49. if err != nil {
  50. return err
  51. }
  52. err = es.startAPIServer(es.etcdStorage)
  53. if err != nil {
  54. return err
  55. }
  56. err = es.startNamespaceController()
  57. if err != nil {
  58. return nil
  59. }
  60. klog.Info("E2E services started.")
  61. return nil
  62. }
  63. // stop stops the embedded e2e services.
  64. func (es *e2eServices) stop(t *testing.T) {
  65. klog.Info("Stopping e2e services...")
  66. // TODO(random-liu): Use a loop to stop all services after introducing
  67. // service interface.
  68. klog.Info("Stopping namespace controller")
  69. if es.nsController != nil {
  70. if err := es.nsController.Stop(); err != nil {
  71. klog.Errorf("Failed to stop %q: %v", es.nsController.Name(), err)
  72. }
  73. }
  74. klog.Info("Stopping API server")
  75. if es.apiServer != nil {
  76. if err := es.apiServer.Stop(); err != nil {
  77. klog.Errorf("Failed to stop %q: %v", es.apiServer.Name(), err)
  78. }
  79. }
  80. klog.Info("Stopping etcd")
  81. if es.etcdServer != nil {
  82. es.etcdServer.Terminate(t)
  83. }
  84. for _, d := range es.rmDirs {
  85. klog.Infof("Deleting directory %v", d)
  86. err := os.RemoveAll(d)
  87. if err != nil {
  88. klog.Errorf("Failed to delete directory %s.\n%v", d, err)
  89. }
  90. }
  91. klog.Info("E2E services stopped.")
  92. }
  93. // startEtcd starts the embedded etcd instance or returns an error.
  94. func (es *e2eServices) startEtcd(t *testing.T) error {
  95. klog.Info("Starting etcd")
  96. server, etcdStorage := etcdtesting.NewUnsecuredEtcd3TestClientServer(t)
  97. es.etcdServer = server
  98. es.etcdStorage = etcdStorage
  99. return nil
  100. }
  101. // startAPIServer starts the embedded API server or returns an error.
  102. func (es *e2eServices) startAPIServer(etcdStorage *storagebackend.Config) error {
  103. klog.Info("Starting API server")
  104. es.apiServer = NewAPIServer(*etcdStorage)
  105. return es.apiServer.Start()
  106. }
  107. // startNamespaceController starts the embedded namespace controller or returns an error.
  108. func (es *e2eServices) startNamespaceController() error {
  109. klog.Info("Starting namespace controller")
  110. es.nsController = NewNamespaceController(framework.TestContext.Host)
  111. return es.nsController.Start()
  112. }
  113. // getServicesHealthCheckURLs returns the health check urls for the internal services.
  114. func getServicesHealthCheckURLs() []string {
  115. return []string{
  116. getAPIServerHealthCheckURL(),
  117. }
  118. }