Skip to content

Commit

Permalink
Merge pull request #2892 from fabriziopandini/e2e-clusterctl-improvem…
Browse files Browse the repository at this point in the history
…ents

🏃[e2e]: clusterctl improvements
  • Loading branch information
k8s-ci-robot authored Apr 10, 2020
2 parents 19121e7 + 163bf79 commit ea22b7f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
32 changes: 24 additions & 8 deletions test/framework/clusterctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (

// Provide E2E friendly wrappers for the clusterctl client library.

const (
// DefaultFlavor for ConfigClusterInput; use it for getting the cluster-template.yaml file.
DefaultFlavor = ""
)

// InitInput is the input for Init.
type InitInput struct {
LogPath string
Expand All @@ -46,12 +51,12 @@ type InitInput struct {

// Init calls clusterctl init with the list of providers defined in the local repository
func Init(ctx context.Context, input InitInput) {
By(fmt.Sprintf("clusterctl init --core %s --bootstrap %s --control-plane %s --infrastructure %s",
fmt.Fprintf(GinkgoWriter, "clusterctl init --core %s --bootstrap %s --control-plane %s --infrastructure %s\n",
input.CoreProvider,
strings.Join(input.BootstrapProviders, ", "),
strings.Join(input.ControlPlaneProviders, ", "),
strings.Join(input.InfrastructureProviders, ", "),
))
)

initOpt := clusterctlclient.InitOptions{
Kubeconfig: input.KubeconfigPath,
Expand All @@ -75,32 +80,36 @@ type ConfigClusterInput struct {
ClusterctlConfigPath string
KubeconfigPath string
InfrastructureProvider string
Namespace string
ClusterName string
KubernetesVersion string
ControlPlaneMachineCount *int64
WorkerMachineCount *int64
Flavor string
}

// ConfigCluster gets a workload cluster based on a template.
func ConfigCluster(ctx context.Context, input ConfigClusterInput) []byte {
By(fmt.Sprintf("clusterctl config cluster %s --infrastructure %s --kubernetes-version %s --control-plane-machine-count %d --worker-machine-count %s",
fmt.Fprintf(GinkgoWriter, "clusterctl config cluster %s --infrastructure %s --kubernetes-version %s --control-plane-machine-count %d --worker-machine-count %d --flavor %s\n",
input.ClusterName,
input.InfrastructureProvider,
valueOrDefault(input.InfrastructureProvider),
input.KubernetesVersion,
input.ControlPlaneMachineCount,
input.WorkerMachineCount,
))
*input.ControlPlaneMachineCount,
*input.WorkerMachineCount,
valueOrDefault(input.Flavor),
)

templateOptions := clusterctlclient.GetClusterTemplateOptions{
Kubeconfig: input.KubeconfigPath,
ProviderRepositorySource: &clusterctlclient.ProviderRepositorySourceOptions{
InfrastructureProvider: input.InfrastructureProvider,
Flavor: "",
Flavor: input.Flavor,
},
ClusterName: input.ClusterName,
KubernetesVersion: input.KubernetesVersion,
ControlPlaneMachineCount: input.ControlPlaneMachineCount,
WorkerMachineCount: input.WorkerMachineCount,
TargetNamespace: input.Namespace,
}

clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-config-cluster.log", input.LogPath)
Expand Down Expand Up @@ -153,3 +162,10 @@ func getClusterctlClientWithLogger(configPath, logName, logPath string) (cluster
Expect(err).ToNot(HaveOccurred(), "Failed to create the clusterctl client library")
return c, log
}

func valueOrDefault(v string) string {
if v != "" {
return v
}
return "(default)"
}
29 changes: 16 additions & 13 deletions test/framework/clusterctl/e2e_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ import (
type LoadE2EConfigInput struct {
// ConfigPath for the e2e test.
ConfigPath string

// BasePath to be used as a base for relative paths in the e2e config file.
BasePath string
}

// LoadE2EConfig loads the configuration for the e2e test environment.
Expand All @@ -58,7 +55,7 @@ func LoadE2EConfig(ctx context.Context, input LoadE2EConfigInput) *E2EConfig {
Expect(yaml.Unmarshal(configData, config)).To(Succeed(), "Failed to convert the e2e test config file to yaml")

config.Defaults()
config.AbsPaths(input.BasePath)
config.AbsPaths(filepath.Dir(input.ConfigPath))

Expect(config.Validate()).To(Succeed(), "The e2e test config file is not valid")

Expand Down Expand Up @@ -325,20 +322,26 @@ func fileExists(filename string) bool {
}

// InfraProvider returns the infrastructure provider selected for running this E2E test.
func (c *E2EConfig) InfraProvider() string {
for _, providerConfig := range c.Providers {
if providerConfig.Type == string(clusterctlv1.InfrastructureProviderType) {
return providerConfig.Name
func (c *E2EConfig) InfraProviders() []string {
InfraProviders := []string{}
for _, provider := range c.Providers {
if provider.Type == string(clusterctlv1.InfrastructureProviderType) {
InfraProviders = append(InfraProviders, provider.Name)
}
}
panic("it is required to have an infra provider in the config")
return InfraProviders
}

// IntervalsOrDefault returns the intervals to be applied to a Eventually operation.
func (c *E2EConfig) IntervalsOrDefault(key string, defaults ...interface{}) []interface{} {
intervals, ok := c.Intervals[key]
// GetIntervals returns the intervals to be applied to a Eventually operation.
// It searches for [spec]/[key] intervals first, and if it is not found, it searches
// for default/[key]. If also the default/[key] intervals are not found,
// ginkgo DefaultEventuallyTimeout and DefaultEventuallyPollingInterval are used.
func (c *E2EConfig) GetIntervals(spec, key string) []interface{} {
intervals, ok := c.Intervals[fmt.Sprintf("%s/%s", spec, key)]
if !ok {
return defaults
if intervals, ok = c.Intervals[fmt.Sprintf("default/%s", key)]; !ok {
return nil
}
}
intervalsInterfaces := make([]interface{}, len(intervals))
for i := range intervals {
Expand Down
12 changes: 6 additions & 6 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ import (

// CreateRepositoryInput is the input for CreateRepository.
type CreateRepositoryInput struct {
ArtifactsPath string
E2EConfig *E2EConfig
RepositoryFolder string
E2EConfig *E2EConfig
}

// CreateRepository creates a clusterctl local repository based on the e2e test config, and the returns the path
// to a clusterctl config file to be used for working with such repository.
func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling CreateRepository")
Expect(os.MkdirAll(input.RepositoryFolder, 0755)).To(Succeed(), "Failed to create the clusterctl local repository folder %s", input.RepositoryFolder)

providers := []providerConfig{}
repositoryPath := filepath.Join(input.ArtifactsPath, "repository")
for _, provider := range input.E2EConfig.Providers {
providerUrl := ""
for _, version := range provider.Versions {
Expand All @@ -54,7 +54,7 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
manifest, err := generator.Manifests(ctx)
Expect(err).ToNot(HaveOccurred(), "Failed to generate the manifest for %q / %q", providerLabel, version.Name)

sourcePath := filepath.Join(repositoryPath, providerLabel, version.Name)
sourcePath := filepath.Join(input.RepositoryFolder, providerLabel, version.Name)
Expect(os.MkdirAll(sourcePath, 0755)).To(Succeed(), "Failed to create the clusterctl local repository folder for %q / %q", providerLabel, version.Name)

filePath := filepath.Join(sourcePath, "components.yaml")
Expand All @@ -80,12 +80,12 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
}

// set this path to an empty file under the repository path, so test can run in isolation without user's overrides kicking in
overridePath := filepath.Join(repositoryPath, "overrides")
overridePath := filepath.Join(input.RepositoryFolder, "overrides")
Expect(os.MkdirAll(overridePath, 0755)).To(Succeed(), "Failed to create the clusterctl overrides folder %q", overridePath)

// creates a clusterctl config file to be used for working with such repository
clusterctlConfigFile := &clusterctlConfig{
Path: filepath.Join(repositoryPath, "clusterctl-config.yaml"),
Path: filepath.Join(input.RepositoryFolder, "clusterctl-config.yaml"),
Values: map[string]interface{}{
"providers": providers,
"overridesFolder": overridePath,
Expand Down

0 comments on commit ea22b7f

Please sign in to comment.