Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 as 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
}
Expand Down
13 changes: 4 additions & 9 deletions test/framework/clusterctl/e2e_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package clusterctl

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -66,15 +65,11 @@ func LoadE2EConfig(ctx context.Context, input LoadE2EConfigInput) *E2EConfig {

// 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.
//
// Deprecated: Use FileTransformations in the CreateRepositoryInput to embedded CNI into cluster templates during create repository.
// The new approach does not uses env variables so we can avoid https://github.com/kubernetes-sigs/cluster-api/issues/3797;
// This func is preserved for avoiding to break users in the v0.3 series, but it is now a no-op.
func SetCNIEnvVar(cniManifestPath string, cniEnvVar string) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make this PR has "breaking" since this is now a no-op?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, done + updated PR comment

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.
Expand Down
37 changes: 35 additions & 2 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,50 @@ 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"
"sigs.k8s.io/cluster-api/test/framework"
)

// 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
Expand Down Expand Up @@ -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)
}
Expand Down