Skip to content

Commit 035db28

Browse files
authored
Merge pull request #3846 from fabriziopandini/investigate-3797
⚠️ E2E test now resolve CNI_RESOURCES without using env variables
2 parents 912de5a + 14dba99 commit 035db28

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

test/e2e/e2e_suite_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,23 @@ func loadE2EConfig(configPath string) *clusterctl.E2EConfig {
159159
config := clusterctl.LoadE2EConfig(context.TODO(), clusterctl.LoadE2EConfigInput{ConfigPath: configPath})
160160
Expect(config).ToNot(BeNil(), "Failed to load E2E config from %s", configPath)
161161

162-
// Read CNI file and set CNI_RESOURCES environmental variable
163-
Expect(config.Variables).To(HaveKey(CNIPath), "Missing %s variable in the config", CNIPath)
164-
clusterctl.SetCNIEnvVar(config.GetVariable(CNIPath), CNIResources)
165-
166162
return config
167163
}
168164

169165
func createClusterctlLocalRepository(config *clusterctl.E2EConfig, repositoryFolder string) string {
170-
clusterctlConfig := clusterctl.CreateRepository(context.TODO(), clusterctl.CreateRepositoryInput{
166+
createRepositoryInput := clusterctl.CreateRepositoryInput{
171167
E2EConfig: config,
172168
RepositoryFolder: repositoryFolder,
173-
})
169+
}
170+
171+
// 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.
172+
Expect(config.Variables).To(HaveKey(CNIPath), "Missing %s variable in the config", CNIPath)
173+
cniPath := config.GetVariable(CNIPath)
174+
Expect(cniPath).To(BeAnExistingFile(), "The %s variable should resolve to an existing file", CNIPath)
175+
176+
createRepositoryInput.RegisterClusterResourceSetConfigMapTransformation(cniPath, CNIResources)
177+
178+
clusterctlConfig := clusterctl.CreateRepository(context.TODO(), createRepositoryInput)
174179
Expect(clusterctlConfig).To(BeAnExistingFile(), "The clusterctl config file does not exists in the local repository %s", repositoryFolder)
175180
return clusterctlConfig
176181
}

test/framework/clusterctl/e2e_config.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package clusterctl
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"io/ioutil"
2423
"os"
@@ -66,15 +65,11 @@ func LoadE2EConfig(ctx context.Context, input LoadE2EConfigInput) *E2EConfig {
6665

6766
// SetCNIEnvVar read CNI from cniManifestPath and sets an environmental variable that keeps CNI resources.
6867
// A ClusterResourceSet can be used to apply CNI using this environmental variable.
68+
//
69+
// Deprecated: Use FileTransformations in the CreateRepositoryInput to embedded CNI into cluster templates during create repository.
70+
// The new approach does not uses env variables so we can avoid https://github.com/kubernetes-sigs/cluster-api/issues/3797;
71+
// This func is preserved for avoiding to break users in the v0.3 series, but it is now a no-op.
6972
func SetCNIEnvVar(cniManifestPath string, cniEnvVar string) {
70-
cniData, err := ioutil.ReadFile(cniManifestPath)
71-
Expect(err).ToNot(HaveOccurred(), "Failed to read the e2e test CNI file")
72-
Expect(cniData).ToNot(BeEmpty(), "CNI file should not be empty")
73-
data := map[string]interface{}{}
74-
data["resources"] = string(cniData)
75-
marshalledData, err := json.Marshal(data)
76-
Expect(err).NotTo(HaveOccurred())
77-
Expect(os.Setenv(cniEnvVar, string(marshalledData))).NotTo(HaveOccurred())
7873
}
7974

8075
// E2EConfig defines the configuration of an e2e test environment.

test/framework/clusterctl/repository.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,50 @@ limitations under the License.
1717
package clusterctl
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"fmt"
2123
"io/ioutil"
2224
"os"
2325
"path/filepath"
26+
"strings"
2427

28+
. "github.com/onsi/ginkgo"
2529
. "github.com/onsi/gomega"
2630

2731
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
2832
"sigs.k8s.io/cluster-api/test/framework"
2933
)
3034

3135
// Provides helpers for managing a clusterctl local repository to be used for running e2e tests in isolation.
36+
type RepositoryFileTransformation func([]byte) ([]byte, error)
3237

3338
// CreateRepositoryInput is the input for CreateRepository.
3439
type CreateRepositoryInput struct {
35-
RepositoryFolder string
36-
E2EConfig *E2EConfig
40+
RepositoryFolder string
41+
E2EConfig *E2EConfig
42+
FileTransformations []RepositoryFileTransformation
43+
}
44+
45+
// RegisterClusterResourceSetConfigMapTransformation registers a FileTransformations that injects a CNI file into
46+
// a ConfigMap that defines a ClusterResourceSet resource.
47+
//
48+
// NOTE: this transformation is specifically designed for replacing "data: ${envSubstVar}".
49+
func (i *CreateRepositoryInput) RegisterClusterResourceSetConfigMapTransformation(cniManifestPath, envSubstVar string) {
50+
By(fmt.Sprintf("Reading the CNI manifest %s", cniManifestPath))
51+
cniData, err := ioutil.ReadFile(cniManifestPath)
52+
Expect(err).ToNot(HaveOccurred(), "Failed to read the e2e test CNI file")
53+
Expect(cniData).ToNot(BeEmpty(), "CNI file should not be empty")
54+
55+
i.FileTransformations = append(i.FileTransformations, func(template []byte) ([]byte, error) {
56+
old := fmt.Sprintf("data: ${%s}", envSubstVar)
57+
new := "data:\n"
58+
new += " resources: |\n"
59+
for _, l := range strings.Split(string(cniData), "\n") {
60+
new += strings.Repeat(" ", 4) + l + "\n"
61+
}
62+
return bytes.Replace(template, []byte(old), []byte(new), -1), nil
63+
})
3764
}
3865

3966
// 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 {
7299
data, err := ioutil.ReadFile(file.SourcePath)
73100
Expect(err).ToNot(HaveOccurred(), "Failed to read file %q / %q", provider.Name, file.SourcePath)
74101

102+
// Applies FileTransformations if defined
103+
for _, t := range input.FileTransformations {
104+
data, err = t(data)
105+
Expect(err).ToNot(HaveOccurred(), "Failed to apply transformation func template %q", file)
106+
}
107+
75108
destinationFile := filepath.Join(filepath.Dir(providerURL), file.TargetName)
76109
Expect(ioutil.WriteFile(destinationFile, data, 0600)).To(Succeed(), "Failed to write clusterctl local repository file %q / %q", provider.Name, file.TargetName)
77110
}

0 commit comments

Comments
 (0)