From c329dcde9f1b9bd52bdef70ea15197bfab0c9b49 Mon Sep 17 00:00:00 2001 From: Sedef Date: Sun, 12 Apr 2020 20:36:22 -0700 Subject: [PATCH] [e2e] add kubernetes_version var and some config getter and setter functions --- test/e2e/config/aws-dev.conf | 5 +- test/e2e/config/docker-dev.conf | 6 +- test/e2e/quick_start_test.go | 8 +- test/framework/clusterctl/e2e_config.go | 111 +++++++++++++++++++++--- test/framework/clusterctl/repository.go | 6 +- test/framework/management/existing.go | 8 +- 6 files changed, 119 insertions(+), 25 deletions(-) diff --git a/test/e2e/config/aws-dev.conf b/test/e2e/config/aws-dev.conf index 7fd0f2d4e8b7..5be38e66d3b9 100644 --- a/test/e2e/config/aws-dev.conf +++ b/test/e2e/config/aws-dev.conf @@ -82,13 +82,16 @@ providers: # Add a cluster template - sourcePath: "../../../test/e2e/data/infrastructure-aws/cluster-template.yaml" -variables: +clusterctlVariables: KUBERNETES_VERSION: "v1.17.0" CONTROL_PLANE_MACHINE_COUNT: 1 WORKER_MACHINE_COUNT: 1 AWS_CONTROL_PLANE_MACHINE_TYPE: t3.large AWS_NODE_MACHINE_TYPE: t3.large +testVariables: + KUBERNETES_UPGRADE_VERSION: "v1.17.0" + intervals: default/wait-controllers: ["3m", "10s"] default/wait-cluster: ["10m", "10s"] diff --git a/test/e2e/config/docker-dev.conf b/test/e2e/config/docker-dev.conf index 27c65bbe011a..cac09f5cbc56 100644 --- a/test/e2e/config/docker-dev.conf +++ b/test/e2e/config/docker-dev.conf @@ -79,10 +79,14 @@ providers: # Add a cluster template - sourcePath: "../data/infrastructure-docker/cluster-template.yaml" -variables: +clusterctlVariables: DOCKER_SERVICE_DOMAIN: "cluster.local" DOCKER_SERVICE_CIDRS: "10.128.0.0/12" DOCKER_POD_CIDRS: "192.168.0.0/16" + KUBERNETES_VERSION: "v1.16.2" + +testVariables: + KUBERNETES_UPGRADE_VERSION: "v1.17.2" intervals: default/wait-controllers: ["3m", "10s"] diff --git a/test/e2e/quick_start_test.go b/test/e2e/quick_start_test.go index e06d98ddc920..d4aca9317106 100644 --- a/test/e2e/quick_start_test.go +++ b/test/e2e/quick_start_test.go @@ -44,14 +44,14 @@ var _ = Describe("When following the Cluster API quick-start", func() { }) }) + It("Should create a workload cluster", func() { settings := createClusterTemplateInput{getClusterTemplateInput{ flavor: clusterctl.DefaultFlavor, clusterName: fmt.Sprintf("cluster-%s", util.RandomString(6)), - //TODO: read from variables - kubernetesVersion: "v1.17.0", - controlPlaneMachineCount: 1, - workerMachineCount: 1, + kubernetesVersion: e2eConfig.GetKubernetesVersion(), + controlPlaneMachineCount: e2eConfig.GetControlPlaneMachineCount(), + workerMachineCount: e2eConfig.GetWorkerMachineCount(), }} Byf("Creating the a cluster name %s using %s template (%s, %d control-planes, %d workers)", diff --git a/test/framework/clusterctl/e2e_config.go b/test/framework/clusterctl/e2e_config.go index 3f24b2161ae4..22aa3b518942 100644 --- a/test/framework/clusterctl/e2e_config.go +++ b/test/framework/clusterctl/e2e_config.go @@ -25,11 +25,13 @@ import ( "os" "path/filepath" "regexp" + "strconv" "time" . "github.com/onsi/gomega" "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/util/version" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" clusterctlconfig "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" "sigs.k8s.io/cluster-api/test/framework" @@ -39,6 +41,13 @@ import ( // Provides access to the configuration for an e2e test. +const ( + kubernetesVersion = "KUBERNETES_VERSION" + kubernetesUpgradeVersion = "KUBERNETES_UPGRADE_VERSION" + controlPlaneMachineCount = "CONTROL_PLANE_MACHINE_COUNT" + workerMachineCount = "WORKER_MACHINE_COUNT" +) + // LoadE2EConfigInput is the input for LoadE2EConfig. type LoadE2EConfigInput struct { // ConfigPath for the e2e test. @@ -80,10 +89,13 @@ type E2EConfig struct { // The test will adapt to the selected infrastructure provider Providers []ProviderConfig `json:"providers,omitempty"` - // Variables to be added to the clusterctl config file + // ClusterctlVariables to be added to the clusterctl config file // Please note that clusterctl read variables from OS environment variables as well, so you can avoid to hard code // sensitive data in the config file. - Variables map[string]string `json:"variables,omitempty"` + ClusterctlVariables map[string]string `json:"clusterctlVariables,omitempty"` + + // TestVariables are used in e2e tests + TestVariables map[string]string `json:"testVariables,omitempty"` // Intervals to be used for long operations during tests Intervals map[string][]string `json:"intervals,omitempty"` @@ -145,6 +157,9 @@ func (c *E2EConfig) Defaults() { containerImage.LoadBehavior = framework.MustLoadImage } } + if c.GetControlPlaneMachineCount() == 0 { + c.ClusterctlVariables[controlPlaneMachineCount] = "1" + } } // AbsPaths makes relative paths absolute using the give base path. @@ -217,23 +232,25 @@ func (c *E2EConfig) Validate() error { return errInvalidArg("Providers[%d].Type=%q", i, providerConfig.Type) } - // Providers version should have a name. - // Providers version.type should be one of [url, kustomize]. - // Providers version.replacements.old should be a valid regex. - for j, version := range providerConfig.Versions { - if version.Name == "" { + // Providers providerVersion should have a name. + // Providers providerVersion.type should be one of [url, kustomize]. + // Providers providerVersion.replacements.old should be a valid regex. + for j, providerVersion := range providerConfig.Versions { + if providerVersion.Name == "" { return errEmptyArg(fmt.Sprintf("Providers[%d].Sources[%d].Name", i, j)) } - //TODO: check if name is a valid semantic version - switch version.Type { + if _, err := version.ParseSemantic(providerVersion.Name); err != nil { + return errInvalidArg("Providers[%d].Sources[%d].Name=%q", i, j, providerVersion.Name) + } + switch providerVersion.Type { case framework.URLSource, framework.KustomizeSource: - if version.Value == "" { + if providerVersion.Value == "" { return errEmptyArg(fmt.Sprintf("Providers[%d].Sources[%d].Value", i, j)) } default: - return errInvalidArg("Providers[%d].Sources[%d].Type=%q", i, j, version.Type) + return errInvalidArg("Providers[%d].Sources[%d].Type=%q", i, j, providerVersion.Type) } - for k, replacement := range version.Replacements { + for k, replacement := range providerVersion.Replacements { if _, err := regexp.Compile(replacement.Old); err != nil { return errInvalidArg("Providers[%d].Sources[%d].Replacements[%d].Old=%q: %v", i, j, k, replacement.Old, err) } @@ -310,6 +327,23 @@ func (c *E2EConfig) Validate() error { } } + // If kubernetesVersion is nil or not valid, return error. + k8sVersion := c.GetKubernetesVersion() + if k8sVersion == "" { + return errEmptyArg(fmt.Sprintf("ClusterctlVariables[%s]", kubernetesVersion)) + } else { + if _, err := version.ParseSemantic(k8sVersion); err != nil { + return errInvalidArg("ClusterctlVariables[%s]=%q", kubernetesVersion, k8sVersion) + } + } + + // If kubernetesUpgradeVersion is not valid, return error. + k8sUpgradeVersion := c.GetKubernetesUpgradeVersion() + if k8sVersion != "" { + if _, err := version.ParseSemantic(k8sUpgradeVersion); err != nil { + return errInvalidArg("TestVariables[%s]=%q", kubernetesUpgradeVersion, k8sUpgradeVersion) + } + } return nil } @@ -349,3 +383,56 @@ func (c *E2EConfig) GetIntervals(spec, key string) []interface{} { } return intervalsInterfaces } + +// GetKubernetesVersion returns the kubernetes version provided in e2e config. +func (c *E2EConfig) GetVariable(varName string) string { + version, ok := c.ClusterctlVariables[varName] + if !ok { + return "" + } + return version +} + +// GetControlPlaneMachineCount returns controlPlaneMachineCount provided in e2e config. +// If it is nil, return 0. +func (c *E2EConfig) GetControlPlaneMachineCount() int64 { + cpCountStr := c.GetVariable(controlPlaneMachineCount) + if cpCountStr == "" { + return 0 + } + + cpCount, err := strconv.ParseInt(cpCountStr, 10, 64) + Expect(err).NotTo(HaveOccurred()) + return cpCount +} + +// GetWorkerMachineCount returns workerMachineCount provided in e2e config. +// If it is nil, return 0. +func (c *E2EConfig) GetWorkerMachineCount() int64 { + wCountStr := c.GetVariable(workerMachineCount) + if wCountStr == "" { + return 0 + } + + wCount, err := strconv.ParseInt(wCountStr, 10, 64) + Expect(err).NotTo(HaveOccurred()) + return wCount +} + +// GetKubernetesVersion returns the kubernetes version provided in e2e config. +func (c *E2EConfig) GetKubernetesVersion() string { + version, ok := c.ClusterctlVariables[kubernetesVersion] + if !ok { + return "" + } + return version +} + +// GetKubernetesUpgradeVersion returns the kubernetes version to be used during upgrade provided in e2e config. +func (c *E2EConfig) GetKubernetesUpgradeVersion() string { + version, ok := c.TestVariables[kubernetesUpgradeVersion] + if !ok { + return "" + } + return version +} diff --git a/test/framework/clusterctl/repository.go b/test/framework/clusterctl/repository.go index 2d949563227f..b05c422b240b 100644 --- a/test/framework/clusterctl/repository.go +++ b/test/framework/clusterctl/repository.go @@ -38,8 +38,8 @@ type CreateRepositoryInput struct { 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. +// CreateRepository creates a clusterctl local repository based on the e2e test config and returns the path +// to a clusterctl config file to be used for working with that 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) @@ -91,7 +91,7 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string { "overridesFolder": overridePath, }, } - for key, value := range input.E2EConfig.Variables { + for key, value := range input.E2EConfig.ClusterctlVariables { clusterctlConfigFile.Values[key] = value } clusterctlConfigFile.write() diff --git a/test/framework/management/existing.go b/test/framework/management/existing.go index 5c47ec84e42a..329306bd65a4 100644 --- a/test/framework/management/existing.go +++ b/test/framework/management/existing.go @@ -37,13 +37,13 @@ type BaseCluster struct { } // NewBaseCluster returns a BaseCluster given a KubeconfigPath and the scheme defining the types hosted in the cluster. -func NewBaseCluster(KubeconfigPath string, scheme *runtime.Scheme) *BaseCluster { +func NewBaseCluster(kubeconfigPath string, scheme *runtime.Scheme) *BaseCluster { // If a kubeconfig file isn't provided, find one in the standard locations. - if KubeconfigPath == "" { - KubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename() + if kubeconfigPath == "" { + kubeconfigPath = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename() } return &BaseCluster{ - kubeconfigPath: KubeconfigPath, + kubeconfigPath: kubeconfigPath, scheme: scheme, } }