diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index 0c77876e4223..0b0159eb9334 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -159,18 +159,23 @@ func loadE2EConfig(configPath string) *clusterctl.E2EConfig { config := clusterctl.LoadE2EConfig(context.TODO(), clusterctl.LoadE2EConfigInput{ConfigPath: configPath}) Expect(config).ToNot(BeNil(), "Failed to load E2E config from %s", configPath) - // Read CNI file and set CNI_RESOURCES environmental variable - Expect(config.Variables).To(HaveKey(CNIPath), "Missing %s variable in the config", CNIPath) - clusterctl.SetCNIEnvVar(config.GetVariable(CNIPath), CNIResources) - return config } func createClusterctlLocalRepository(config *clusterctl.E2EConfig, repositoryFolder string) string { - clusterctlConfig := clusterctl.CreateRepository(context.TODO(), clusterctl.CreateRepositoryInput{ + createRepositoryInput := clusterctl.CreateRepositoryInput{ E2EConfig: config, RepositoryFolder: repositoryFolder, - }) + } + + // Ensuring a CNI file is defined in the config and register a FileTransformation to inject the referenced file in place of the CNI_RESOURCES envSubst variable. + Expect(config.Variables).To(HaveKey(CNIPath), "Missing %s variable in the config", CNIPath) + cniPath := config.GetVariable(CNIPath) + Expect(cniPath).To(BeAnExistingFile(), "The %s variable should resolve to an existing file", CNIPath) + + createRepositoryInput.RegisterClusterResourceSetConfigMapTransformation(cniPath, CNIResources) + + clusterctlConfig := clusterctl.CreateRepository(context.TODO(), createRepositoryInput) Expect(clusterctlConfig).To(BeAnExistingFile(), "The clusterctl config file does not exists in the local repository %s", repositoryFolder) return clusterctlConfig } diff --git a/test/framework/clusterctl/e2e_config.go b/test/framework/clusterctl/e2e_config.go index f8273ac326c0..bfbe516fd532 100644 --- a/test/framework/clusterctl/e2e_config.go +++ b/test/framework/clusterctl/e2e_config.go @@ -18,7 +18,6 @@ package clusterctl import ( "context" - "encoding/json" "fmt" "io/ioutil" "os" @@ -64,19 +63,6 @@ func LoadE2EConfig(ctx context.Context, input LoadE2EConfigInput) *E2EConfig { return config } -// SetCNIEnvVar read CNI from cniManifestPath and sets an environmental variable that keeps CNI resources. -// A ClusterResourceSet can be used to apply CNI using this environmental variable. -func SetCNIEnvVar(cniManifestPath string, cniEnvVar string) { - cniData, err := ioutil.ReadFile(cniManifestPath) - Expect(err).ToNot(HaveOccurred(), "Failed to read the e2e test CNI file") - Expect(cniData).ToNot(BeEmpty(), "CNI file should not be empty") - data := map[string]interface{}{} - data["resources"] = string(cniData) - marshalledData, err := json.Marshal(data) - Expect(err).NotTo(HaveOccurred()) - Expect(os.Setenv(cniEnvVar, string(marshalledData))).NotTo(HaveOccurred()) -} - // E2EConfig defines the configuration of an e2e test environment. type E2EConfig struct { // Name is the name of the Kind management cluster. diff --git a/test/framework/clusterctl/repository.go b/test/framework/clusterctl/repository.go index f30e809953cb..74c4e748ce24 100644 --- a/test/framework/clusterctl/repository.go +++ b/test/framework/clusterctl/repository.go @@ -17,11 +17,15 @@ limitations under the License. package clusterctl import ( + "bytes" "context" + "fmt" "io/ioutil" "os" "path/filepath" + "strings" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" @@ -29,11 +33,34 @@ import ( ) // Provides helpers for managing a clusterctl local repository to be used for running e2e tests in isolation. +type RepositoryFileTransformation func([]byte) ([]byte, error) // CreateRepositoryInput is the input for CreateRepository. type CreateRepositoryInput struct { - RepositoryFolder string - E2EConfig *E2EConfig + RepositoryFolder string + E2EConfig *E2EConfig + FileTransformations []RepositoryFileTransformation +} + +// RegisterClusterResourceSetConfigMapTransformation registers a FileTransformations that injects a CNI file into +// a ConfigMap that defines a ClusterResourceSet resource. +// +// NOTE: this transformation is specifically designed for replacing "data: ${envSubstVar}". +func (i *CreateRepositoryInput) RegisterClusterResourceSetConfigMapTransformation(cniManifestPath, envSubstVar string) { + By(fmt.Sprintf("Reading the CNI manifest %s", cniManifestPath)) + cniData, err := ioutil.ReadFile(cniManifestPath) + Expect(err).ToNot(HaveOccurred(), "Failed to read the e2e test CNI file") + Expect(cniData).ToNot(BeEmpty(), "CNI file should not be empty") + + i.FileTransformations = append(i.FileTransformations, func(template []byte) ([]byte, error) { + old := fmt.Sprintf("data: ${%s}", envSubstVar) + new := "data:\n" + new += " resources: |\n" + for _, l := range strings.Split(string(cniData), "\n") { + new += strings.Repeat(" ", 4) + l + "\n" + } + return bytes.Replace(template, []byte(old), []byte(new), -1), nil + }) } // CreateRepository creates a clusterctl local repository based on the e2e test config, and the returns the path @@ -72,6 +99,12 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string { data, err := ioutil.ReadFile(file.SourcePath) Expect(err).ToNot(HaveOccurred(), "Failed to read file %q / %q", provider.Name, file.SourcePath) + // Applies FileTransformations if defined + for _, t := range input.FileTransformations { + data, err = t(data) + Expect(err).ToNot(HaveOccurred(), "Failed to apply transformation func template %q", file) + } + destinationFile := filepath.Join(filepath.Dir(providerURL), file.TargetName) Expect(ioutil.WriteFile(destinationFile, data, 0600)).To(Succeed(), "Failed to write clusterctl local repository file %q / %q", provider.Name, file.TargetName) }