sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/shared/cluster.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2020 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11  	http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package shared
    21  
    22  import (
    23  	"context"
    24  	"path/filepath"
    25  
    26  	. "github.com/onsi/gomega"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  
    29  	capi_e2e "sigs.k8s.io/cluster-api/test/e2e"
    30  	"sigs.k8s.io/cluster-api/test/framework"
    31  	"sigs.k8s.io/cluster-api/test/framework/bootstrap"
    32  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    33  )
    34  
    35  // createClusterctlLocalRepository generates a clusterctl repository.
    36  // Must always be run after kubetest.NewConfiguration.
    37  func createClusterctlLocalRepository(e2eCtx *E2EContext, repositoryFolder string) string {
    38  	createRepositoryInput := clusterctl.CreateRepositoryInput{
    39  		E2EConfig:        e2eCtx.E2EConfig,
    40  		RepositoryFolder: repositoryFolder,
    41  	}
    42  
    43  	if !e2eCtx.IsManaged {
    44  		// Ensuring a CNI file is defined in the config and register a FileTransformation to inject the referenced file as in place of the CNI_RESOURCES envSubst variable.
    45  		Expect(e2eCtx.E2EConfig.Variables).To(HaveKey(capi_e2e.CNIPath), "Missing %s variable in the config", capi_e2e.CNIPath)
    46  		cniPath := e2eCtx.E2EConfig.GetVariable(capi_e2e.CNIPath)
    47  		Expect(cniPath).To(BeAnExistingFile(), "The %s variable should resolve to an existing file", capi_e2e.CNIPath)
    48  		createRepositoryInput.RegisterClusterResourceSetConfigMapTransformation(cniPath, capi_e2e.CNIResources)
    49  	}
    50  
    51  	clusterctlConfig := clusterctl.CreateRepository(context.TODO(), createRepositoryInput)
    52  	Expect(clusterctlConfig).To(BeAnExistingFile(), "The clusterctl generate file does not exists in the local repository %s", repositoryFolder)
    53  	return clusterctlConfig
    54  }
    55  
    56  // setupBootstrapCluster installs Cluster API components via clusterctl.
    57  func setupBootstrapCluster(config *clusterctl.E2EConfig, scheme *runtime.Scheme, useExistingCluster bool) (bootstrap.ClusterProvider, framework.ClusterProxy) {
    58  	var clusterProvider bootstrap.ClusterProvider
    59  	kubeconfigPath := ""
    60  	if !useExistingCluster {
    61  		clusterProvider = bootstrap.CreateKindBootstrapClusterAndLoadImages(context.TODO(), bootstrap.CreateKindBootstrapClusterAndLoadImagesInput{
    62  			Name:               config.ManagementClusterName,
    63  			KubernetesVersion:  config.GetVariable(KubernetesVersionManagement),
    64  			RequiresDockerSock: config.HasDockerProvider(),
    65  			Images:             config.Images,
    66  		})
    67  		Expect(clusterProvider).ToNot(BeNil(), "Failed to create a bootstrap cluster")
    68  
    69  		kubeconfigPath = clusterProvider.GetKubeconfigPath()
    70  		Expect(kubeconfigPath).To(BeAnExistingFile(), "Failed to get the kubeconfig file for the bootstrap cluster")
    71  	}
    72  
    73  	clusterProxy := framework.NewClusterProxy("bootstrap", kubeconfigPath, scheme)
    74  	Expect(clusterProxy).ToNot(BeNil(), "Failed to get a bootstrap cluster proxy")
    75  
    76  	return clusterProvider, clusterProxy
    77  }
    78  
    79  // initBootstrapCluster uses kind to create a cluster.
    80  func initBootstrapCluster(e2eCtx *E2EContext) {
    81  	// NOTE: the following originally used clusterctl.InitManagementClusterAndWatchControllerLogs.
    82  	// This can be used again when https://github.com/kubernetes-sigs/cluster-api/issues/3983 is completed
    83  	InitManagementClusterAndWatchControllerLogs(context.TODO(), InitManagementClusterAndWatchControllerLogsInput{
    84  		ClusterProxy:            e2eCtx.Environment.BootstrapClusterProxy,
    85  		ClusterctlConfigPath:    e2eCtx.Environment.ClusterctlConfigPath,
    86  		InfrastructureProviders: e2eCtx.E2EConfig.InfrastructureProviders(),
    87  		BootstrapProviders:      e2eCtx.BootstrapProviders(),
    88  		ControlPlaneProviders:   e2eCtx.ControlPlaneProviders(),
    89  		LogFolder:               filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", e2eCtx.Environment.BootstrapClusterProxy.GetName()),
    90  	}, e2eCtx.E2EConfig.GetIntervals(e2eCtx.Environment.BootstrapClusterProxy.GetName(), "wait-controllers")...)
    91  }
    92  
    93  // tearDown the bootstrap kind cluster.
    94  func tearDown(bootstrapClusterProvider bootstrap.ClusterProvider, bootstrapClusterProxy framework.ClusterProxy) {
    95  	if bootstrapClusterProxy != nil {
    96  		bootstrapClusterProxy.Dispose(context.TODO())
    97  	}
    98  	if bootstrapClusterProvider != nil {
    99  		bootstrapClusterProvider.Dispose(context.TODO())
   100  	}
   101  }