Skip to content

Commit

Permalink
[e2e] add kubernetes_version var and some config getter and setter fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
Sedef committed Apr 13, 2020
1 parent bfc95bf commit c329dcd
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 25 deletions.
5 changes: 4 additions & 1 deletion test/e2e/config/aws-dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/config/docker-dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/quick_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
111 changes: 99 additions & 12 deletions test/framework/clusterctl/e2e_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions test/framework/management/existing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down

0 comments on commit c329dcd

Please sign in to comment.