...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/main/testmain.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/main

     1  // Copyright 2022 Google LLC
     2  //
     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  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testmain
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"sync"
    22  	"time"
    23  
    24  	appsv1 "k8s.io/api/apps/v1"
    25  	corev1 "k8s.io/api/core/v1"
    26  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    27  	"k8s.io/client-go/kubernetes/scheme"
    28  	"k8s.io/client-go/rest"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    30  	"sigs.k8s.io/controller-runtime/pkg/manager"
    31  
    32  	customizev1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/customize/v1alpha1"
    33  	corev1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1"
    34  	"github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/test/util/paths"
    35  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager/nocache"
    36  )
    37  
    38  func init() {
    39  	s := scheme.Scheme
    40  	if err := apiextensions.SchemeBuilder.AddToScheme(s); err != nil {
    41  		log.Fatalf("error registering apiextensions v1beta1 scheme: %v", err)
    42  	}
    43  	if err := corev1beta1.SchemeBuilder.AddToScheme(s); err != nil {
    44  		log.Fatalf("error registering core kcc operator scheme: %v", err)
    45  	}
    46  	if err := corev1.SchemeBuilder.AddToScheme(s); err != nil {
    47  		log.Fatalf("error registering core v1 scheme: %v", err)
    48  	}
    49  	if err := appsv1.SchemeBuilder.AddToScheme(s); err != nil {
    50  		log.Fatalf("error registering apps v1 scheme: %v", err)
    51  	}
    52  	if err := customizev1alpha1.SchemeBuilder.AddToScheme(s); err != nil {
    53  		log.Fatalf("error registering kcc customization scheme: %v", err)
    54  	}
    55  }
    56  
    57  // This starts a local K8S API server to run tests against. These tests do
    58  // not require an external API server to execute.
    59  func StartTestEnv() (*rest.Config, func()) {
    60  	testEnv := &envtest.Environment{
    61  		CRDDirectoryPaths:        []string{paths.GetOperatorCRDsPath()},
    62  		ControlPlaneStartTimeout: time.Minute,
    63  	}
    64  	var err error
    65  	cfg, err := testEnv.Start()
    66  	if err != nil {
    67  		log.Fatal(err)
    68  	}
    69  	stop := func() {
    70  		if err := testEnv.Stop(); err != nil {
    71  			log.Printf("unable to stop the test environment: %v", err)
    72  		}
    73  	}
    74  	return cfg, stop
    75  }
    76  
    77  func StartTestManager(cfg *rest.Config) (manager.Manager, func(), error) {
    78  	mgr, err := manager.New(cfg, manager.Options{
    79  		// Supply a concrete client to disable the default behavior of caching
    80  		NewClient: nocache.NoCacheClientFunc,
    81  		// Prevent manager from binding to a port to serve prometheus metrics
    82  		// since creating multiple managers for tests will fail if more than
    83  		// one manager tries to bind to the same port.
    84  		MetricsBindAddress: "0",
    85  		// Prevent manager from binding to a port to serve health probes since
    86  		// creating multiple managers for tests will fail if more than one
    87  		// manager tries to bind to the same port.
    88  		HealthProbeBindAddress: "0",
    89  	})
    90  	if err != nil {
    91  		return nil, nil, fmt.Errorf("error creating manager: %v", err)
    92  	}
    93  	stopFunc := startMgr(mgr, log.Fatalf)
    94  	return mgr, stopFunc, nil
    95  }
    96  
    97  func startMgr(mgr manager.Manager, mgrStartErrHandler func(string, ...interface{})) func() {
    98  	ctx, cancel := context.WithCancel(context.TODO())
    99  	// it is important to wait for the below goroutine to terminate before attempting to exit the application because
   100  	// otherwise there can be panics and unexpected behavior while the manager is shutting down
   101  	wg := sync.WaitGroup{}
   102  	wg.Add(1)
   103  	go func() {
   104  		defer wg.Done()
   105  		if err := mgr.Start(ctx); err != nil {
   106  			mgrStartErrHandler("unable to start manager: %v", err)
   107  		}
   108  	}()
   109  	stop := func() {
   110  		// calling cancel() will cancel the context 'ctx', the mgr will stop all runnables and Start() will return and
   111  		// the above goroutine will exit
   112  		cancel()
   113  		// wait for the goroutine above to exit (it has a deferred wg.Done())
   114  		wg.Wait()
   115  	}
   116  	return stop
   117  }
   118  
   119  func StartTestManagerFromNewTestEnv() (manager.Manager, func()) {
   120  	cfg, stopEnv := StartTestEnv()
   121  	mgr, stopMgr, err := StartTestManager(cfg)
   122  	if err != nil {
   123  		log.Fatal(err)
   124  	}
   125  	stop := func() {
   126  		stopMgr()
   127  		stopEnv()
   128  	}
   129  	return mgr, stop
   130  }
   131  

View as plain text