@@ -17,23 +17,50 @@ limitations under the License.
17
17
package clusterctl
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
22
+ "fmt"
21
23
"io/ioutil"
22
24
"os"
23
25
"path/filepath"
26
+ "strings"
24
27
28
+ . "github.com/onsi/ginkgo"
25
29
. "github.com/onsi/gomega"
26
30
27
31
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
28
32
"sigs.k8s.io/cluster-api/test/framework"
29
33
)
30
34
31
35
// Provides helpers for managing a clusterctl local repository to be used for running e2e tests in isolation.
36
+ type RepositoryFileTransformation func ([]byte ) ([]byte , error )
32
37
33
38
// CreateRepositoryInput is the input for CreateRepository.
34
39
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
+ })
37
64
}
38
65
39
66
// 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 {
72
99
data , err := ioutil .ReadFile (file .SourcePath )
73
100
Expect (err ).ToNot (HaveOccurred (), "Failed to read file %q / %q" , provider .Name , file .SourcePath )
74
101
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
+
75
108
destinationFile := filepath .Join (filepath .Dir (providerURL ), file .TargetName )
76
109
Expect (ioutil .WriteFile (destinationFile , data , 0600 )).To (Succeed (), "Failed to write clusterctl local repository file %q / %q" , provider .Name , file .TargetName )
77
110
}
0 commit comments