...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/environment/environment.go

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

     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 testenvironment
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"time"
    21  
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/repo"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook"
    26  
    27  	admissionregistration "k8s.io/api/admissionregistration/v1"
    28  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    31  )
    32  
    33  func init() {
    34  	s := scheme.Scheme
    35  	if err := apiextensions.SchemeBuilder.AddToScheme(s); err != nil {
    36  		log.Fatalf("error registering apiextensions v1beta1 scheme: %v", err)
    37  	}
    38  	if err := apis.AddToScheme(s); err != nil {
    39  		log.Fatalf("error registering schemes: %v", err)
    40  	}
    41  }
    42  
    43  func StartTestEnvironmentOrLogFatal(testType test.TestType, crds []*apiextensions.CustomResourceDefinition, whCfgs []webhook.WebhookConfig) *envtest.Environment {
    44  	env, err := startTestEnvironment(testType, crds, whCfgs)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  	return env
    49  }
    50  
    51  func startTestEnvironment(testType test.TestType, crds []*apiextensions.CustomResourceDefinition, whCfgs []webhook.WebhookConfig) (*envtest.Environment, error) {
    52  	env := &envtest.Environment{
    53  		ControlPlaneStartTimeout: time.Minute,
    54  	}
    55  	switch {
    56  	case len(crds) > 0:
    57  		env.CRDs = crds
    58  	case testType == test.UnitTestType:
    59  		env.CRDs = test.FakeCRDs()
    60  	case testType == test.IntegrationTestType:
    61  		env.CRDDirectoryPaths = []string{repo.GetCRDsPath()}
    62  	}
    63  	if testType == test.IntegrationTestType {
    64  		ConfigureWebhookInstallOptions(env, whCfgs)
    65  	}
    66  	_, err := env.Start()
    67  	if err != nil {
    68  		return nil, fmt.Errorf("error starting test environment: %v", err)
    69  	}
    70  	return env, nil
    71  }
    72  
    73  func ConfigureWebhookInstallOptions(env *envtest.Environment, whCfgs []webhook.WebhookConfig) {
    74  	validatingWebhookCfg, mutatingWebhookCfg := webhook.GenerateWebhookManifests(
    75  		webhook.ValidatingWebhookConfigurationName,
    76  		webhook.MutatingWebhookConfigurationName,
    77  		webhook.CommonWebhookServiceName,
    78  		whCfgs,
    79  	)
    80  	env.WebhookInstallOptions = envtest.WebhookInstallOptions{}
    81  	if validatingWebhookCfg != nil {
    82  		env.WebhookInstallOptions.ValidatingWebhooks = []*admissionregistration.ValidatingWebhookConfiguration{validatingWebhookCfg}
    83  	}
    84  	if mutatingWebhookCfg != nil {
    85  		env.WebhookInstallOptions.MutatingWebhooks = []*admissionregistration.MutatingWebhookConfiguration{mutatingWebhookCfg}
    86  	}
    87  }
    88  

View as plain text