Skip to content

Commit

Permalink
Merge pull request #3896 from arghya88/resolve-cni-resources
Browse files Browse the repository at this point in the history
⚠️ E2E framework resolves CNI_RESOURCES without using env vars
  • Loading branch information
k8s-ci-robot authored Nov 9, 2020
2 parents 08260e7 + 65ad6b3 commit defb99c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
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 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
14 changes: 0 additions & 14 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 @@ -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.
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

0 comments on commit defb99c

Please sign in to comment.