Skip to content

Commit 822fde7

Browse files
authored
Merge pull request #3337 from sbueringer/pr-cleanup-clusterctl-func
🌱 Cleanup copy of CopyAndAmendClusterctlConfig
2 parents 38f6b67 + 4250f60 commit 822fde7

File tree

1 file changed

+5
-59
lines changed

1 file changed

+5
-59
lines changed

test/e2e/e2e_setup_test.go

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package e2e
1818

1919
import (
20-
"context"
2120
"fmt"
2221
"net"
2322
"os"
@@ -32,9 +31,9 @@ import (
3231
"k8s.io/apimachinery/pkg/util/sets"
3332
"k8s.io/klog/v2"
3433
"sigs.k8s.io/cluster-api/test/framework"
34+
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
3535
. "sigs.k8s.io/cluster-api/test/framework/ginkgoextensions"
3636
crclient "sigs.k8s.io/controller-runtime/pkg/client"
37-
"sigs.k8s.io/yaml"
3837

3938
vsphereip "sigs.k8s.io/cluster-api-provider-vsphere/test/framework/ip"
4039
vspherevcsim "sigs.k8s.io/cluster-api-provider-vsphere/test/framework/vcsim"
@@ -145,11 +144,11 @@ func Setup(specName string, f func(testSpecificSettings func() testSettings), op
145144
// may re-write the file to add some variables, but it needs to exist already before that.
146145
testSpecificClusterctlConfigPath = fmt.Sprintf("%s-%s.yaml", strings.TrimSuffix(clusterctlConfigPath, ".yaml"), specName)
147146
Byf("Writing a new clusterctl config to %s", testSpecificClusterctlConfigPath)
148-
copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{
147+
Expect(clusterctl.CopyAndAmendClusterctlConfig(ctx, clusterctl.CopyAndAmendClusterctlConfigInput{
149148
ClusterctlConfigPath: clusterctlConfigPath,
150149
OutputPath: testSpecificClusterctlConfigPath,
151150
Variables: testSpecificVariables,
152-
})
151+
})).To(Succeed())
153152

154153
// The setup done in `postNamespaceCreatedFunc` does
155154
// 1. Create a VCSim Server (only for additional VCSim at separate management cluster)
@@ -176,11 +175,11 @@ func Setup(specName string, f func(testSpecificSettings func() testSettings), op
176175

177176
// Re-write the clusterctl config file and add the new variables created above (ip addresses, VCSim variables).
178177
Byf("Writing a new clusterctl config to %s", testSpecificClusterctlConfigPath)
179-
copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{
178+
Expect(clusterctl.CopyAndAmendClusterctlConfig(ctx, clusterctl.CopyAndAmendClusterctlConfigInput{
180179
ClusterctlConfigPath: testSpecificClusterctlConfigPath,
181180
OutputPath: testSpecificClusterctlConfigPath,
182181
Variables: testSpecificVariables,
183-
})
182+
})).To(Succeed())
184183

185184
// Run additional initialization required for supervisor.
186185
if testMode == SupervisorTestMode {
@@ -429,56 +428,3 @@ func setupNamespaceWithVMOperatorDependenciesVCenter(managementClusterProxy fram
429428
err := vmoperator.ReconcileDependencies(ctx, c, dependenciesConfig)
430429
Expect(err).ToNot(HaveOccurred(), "Failed to reconcile VMOperatorDependencies")
431430
}
432-
433-
// Note: Copy-paste from CAPI below.
434-
435-
// copyAndAmendClusterctlConfigInput is the input for copyAndAmendClusterctlConfig.
436-
type copyAndAmendClusterctlConfigInput struct {
437-
ClusterctlConfigPath string
438-
OutputPath string
439-
Variables map[string]string
440-
}
441-
442-
// copyAndAmendClusterctlConfig copies the clusterctl-config from ClusterctlConfigPath to
443-
// OutputPath and adds the given Variables.
444-
func copyAndAmendClusterctlConfig(_ context.Context, input copyAndAmendClusterctlConfigInput) {
445-
// Read clusterctl config from ClusterctlConfigPath.
446-
clusterctlConfigFile := &clusterctlConfig{
447-
Path: input.ClusterctlConfigPath,
448-
}
449-
clusterctlConfigFile.read()
450-
451-
// Overwrite variables.
452-
if clusterctlConfigFile.Values == nil {
453-
clusterctlConfigFile.Values = map[string]interface{}{}
454-
}
455-
for key, value := range input.Variables {
456-
clusterctlConfigFile.Values[key] = value
457-
}
458-
459-
// Write clusterctl config to OutputPath.
460-
clusterctlConfigFile.Path = input.OutputPath
461-
clusterctlConfigFile.write()
462-
}
463-
464-
type clusterctlConfig struct {
465-
Path string
466-
Values map[string]interface{}
467-
}
468-
469-
// write writes a clusterctl config file to disk.
470-
func (c *clusterctlConfig) write() {
471-
data, err := yaml.Marshal(c.Values)
472-
Expect(err).ToNot(HaveOccurred(), "Failed to marshal the clusterctl config file")
473-
474-
Expect(os.WriteFile(c.Path, data, 0600)).To(Succeed(), "Failed to write the clusterctl config file")
475-
}
476-
477-
// read reads a clusterctl config file from disk.
478-
func (c *clusterctlConfig) read() {
479-
data, err := os.ReadFile(c.Path)
480-
Expect(err).ToNot(HaveOccurred())
481-
482-
err = yaml.Unmarshal(data, &c.Values)
483-
Expect(err).ToNot(HaveOccurred(), "Failed to unmarshal the clusterctl config file")
484-
}

0 commit comments

Comments
 (0)