k8s.io/kubernetes@v1.29.3/test/e2e_node/services/internal_services.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     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  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package services
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
    25  	"k8s.io/apiserver/pkg/storage/storagebackend"
    26  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    27  	"k8s.io/klog/v2/ktesting"
    28  	"k8s.io/kubernetes/test/e2e/framework"
    29  
    30  	"k8s.io/klog/v2"
    31  )
    32  
    33  // e2eService manages e2e services in current process.
    34  type e2eServices struct {
    35  	rmDirs []string
    36  	// statically linked e2e services
    37  	etcdServer   *etcd3testing.EtcdTestServer
    38  	etcdStorage  *storagebackend.Config
    39  	apiServer    *APIServer
    40  	nsController *NamespaceController
    41  }
    42  
    43  func newE2EServices() *e2eServices {
    44  	return &e2eServices{}
    45  }
    46  
    47  // run starts all e2e services and wait for the termination signal. Once receives the
    48  // termination signal, it will stop the e2e services gracefully.
    49  func (es *e2eServices) run(t *testing.T) error {
    50  	defer es.stop(t)
    51  	if err := es.start(t); err != nil {
    52  		return err
    53  	}
    54  	// Wait until receiving a termination signal.
    55  	waitForTerminationSignal()
    56  	return nil
    57  }
    58  
    59  // start starts the tests embedded services or returns an error.
    60  func (es *e2eServices) start(t *testing.T) error {
    61  	_, ctx := ktesting.NewTestContext(t)
    62  	klog.Info("Starting e2e services...")
    63  	err := es.startEtcd(t)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	err = es.startAPIServer(es.etcdStorage)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	err = es.startNamespaceController(ctx)
    72  	if err != nil {
    73  		return nil
    74  	}
    75  	klog.Info("E2E services started.")
    76  	return nil
    77  }
    78  
    79  // stop stops the embedded e2e services.
    80  func (es *e2eServices) stop(t *testing.T) {
    81  	klog.Info("Stopping e2e services...")
    82  	// TODO(random-liu): Use a loop to stop all services after introducing
    83  	// service interface.
    84  	klog.Info("Stopping namespace controller")
    85  	if es.nsController != nil {
    86  		if err := es.nsController.Stop(); err != nil {
    87  			klog.Errorf("Failed to stop %q: %v", es.nsController.Name(), err)
    88  		}
    89  	}
    90  
    91  	klog.Info("Stopping API server")
    92  	if es.apiServer != nil {
    93  		if err := es.apiServer.Stop(); err != nil {
    94  			klog.Errorf("Failed to stop %q: %v", es.apiServer.Name(), err)
    95  		}
    96  	}
    97  
    98  	klog.Info("Stopping etcd")
    99  	if es.etcdServer != nil {
   100  		es.etcdServer.Terminate(t)
   101  	}
   102  
   103  	for _, d := range es.rmDirs {
   104  		klog.Infof("Deleting directory %v", d)
   105  		err := os.RemoveAll(d)
   106  		if err != nil {
   107  			klog.Errorf("Failed to delete directory %s.\n%v", d, err)
   108  		}
   109  	}
   110  
   111  	klog.Info("E2E services stopped.")
   112  }
   113  
   114  // startEtcd starts the embedded etcd instance or returns an error.
   115  func (es *e2eServices) startEtcd(t *testing.T) error {
   116  	klog.Info("Starting etcd")
   117  	server, etcdStorage := etcd3testing.NewUnsecuredEtcd3TestClientServer(t)
   118  	es.etcdServer = server
   119  	es.etcdStorage = etcdStorage
   120  	return nil
   121  }
   122  
   123  // startAPIServer starts the embedded API server or returns an error.
   124  func (es *e2eServices) startAPIServer(etcdStorage *storagebackend.Config) error {
   125  	klog.Info("Starting API server")
   126  	es.apiServer = NewAPIServer(*etcdStorage)
   127  	return es.apiServer.Start()
   128  }
   129  
   130  // startNamespaceController starts the embedded namespace controller or returns an error.
   131  func (es *e2eServices) startNamespaceController(ctx context.Context) error {
   132  	klog.FromContext(ctx).Info("Starting namespace controller")
   133  	es.nsController = NewNamespaceController(framework.TestContext.Host)
   134  	return es.nsController.Start(ctx)
   135  }
   136  
   137  // getServicesHealthCheckURLs returns the health check urls for the internal services.
   138  func getServicesHealthCheckURLs() []string {
   139  	return []string{
   140  		getAPIServerHealthCheckURL(),
   141  	}
   142  }
   143  
   144  func SetFeatureGatesForInProcessComponents(featureGates map[string]bool) error {
   145  	return utilfeature.DefaultMutableFeatureGate.SetFromMap(featureGates)
   146  }