Skip to content

Commit

Permalink
test/framework: expose CopyAndAmendClusterctlConfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Feb 2, 2024
1 parent 73cd253 commit 78bc3a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
8 changes: 4 additions & 4 deletions test/framework/clusterctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ type UpgradeInput struct {
func Upgrade(ctx context.Context, input UpgradeInput) {
if len(input.ClusterctlVariables) > 0 {
outputPath := filepath.Join(filepath.Dir(input.ClusterctlConfigPath), fmt.Sprintf("clusterctl-upgrade-config-%s.yaml", input.ClusterName))
copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{
Expect(CopyAndAmendClusterctlConfig(ctx, CopyAndAmendClusterctlConfigInput{
ClusterctlConfigPath: input.ClusterctlConfigPath,
OutputPath: outputPath,
Variables: input.ClusterctlVariables,
})
})).To(Succeed())
input.ClusterctlConfigPath = outputPath
}

Expand Down Expand Up @@ -288,11 +288,11 @@ func ConfigCluster(ctx context.Context, input ConfigClusterInput) []byte {

if len(input.ClusterctlVariables) > 0 {
outputPath := filepath.Join(filepath.Dir(input.ClusterctlConfigPath), fmt.Sprintf("clusterctl-upgrade-config-%s.yaml", input.ClusterName))
copyAndAmendClusterctlConfig(ctx, copyAndAmendClusterctlConfigInput{
Expect(CopyAndAmendClusterctlConfig(ctx, CopyAndAmendClusterctlConfigInput{
ClusterctlConfigPath: input.ClusterctlConfigPath,
OutputPath: outputPath,
Variables: input.ClusterctlVariables,
})
})).To(Succeed())
input.ClusterctlConfigPath = outputPath
}

Expand Down
25 changes: 19 additions & 6 deletions test/framework/clusterctl/clusterctl_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

. "github.com/onsi/gomega"
"github.com/pkg/errors"
"sigs.k8s.io/yaml"
)

Expand All @@ -40,18 +41,30 @@ type providerConfig struct {
}

// write writes a clusterctl config file to disk.
func (c *clusterctlConfig) write() {
func (c *clusterctlConfig) write() error {
data, err := yaml.Marshal(c.Values)
if err != nil {
return errors.Wrap(err, "Failed to marshal the clusterctl config file")
}
Expect(err).ToNot(HaveOccurred(), "Failed to marshal the clusterctl config file")

Expect(os.WriteFile(c.Path, data, 0600)).To(Succeed(), "Failed to write the clusterctl config file")
if err := os.WriteFile(c.Path, data, 0600); err != nil {
return errors.Wrap(err, "Failed to write the clusterctl config file")
}

return nil
}

// read reads a clusterctl config file from disk.
func (c *clusterctlConfig) read() {
func (c *clusterctlConfig) read() error {
data, err := os.ReadFile(c.Path)
Expect(err).ToNot(HaveOccurred())
if err != nil {
return errors.Wrapf(err, "Failed to read clusterctl config file %q", c.Path)
}

if err = yaml.Unmarshal(data, &c.Values); err != nil {
return errors.Wrap(err, "Failed to unmarshal the clusterctl config file")
}

err = yaml.Unmarshal(data, &c.Values)
Expect(err).ToNot(HaveOccurred(), "Failed to unmarshal the clusterctl config file")
return nil
}
14 changes: 8 additions & 6 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,23 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
return clusterctlConfigFile.Path
}

// copyAndAmendClusterctlConfigInput is the input for copyAndAmendClusterctlConfig.
type copyAndAmendClusterctlConfigInput struct {
// CopyAndAmendClusterctlConfigInput is the input for copyAndAmendClusterctlConfig.
type CopyAndAmendClusterctlConfigInput struct {
ClusterctlConfigPath string
OutputPath string
Variables map[string]string
}

// copyAndAmendClusterctlConfig copies the clusterctl-config from ClusterctlConfigPath to
// CopyAndAmendClusterctlConfig copies the clusterctl-config from ClusterctlConfigPath to
// OutputPath and adds the given Variables.
func copyAndAmendClusterctlConfig(_ context.Context, input copyAndAmendClusterctlConfigInput) {
func CopyAndAmendClusterctlConfig(_ context.Context, input CopyAndAmendClusterctlConfigInput) error {
// Read clusterctl config from ClusterctlConfigPath.
clusterctlConfigFile := &clusterctlConfig{
Path: input.ClusterctlConfigPath,
}
clusterctlConfigFile.read()
if err := clusterctlConfigFile.read(); err != nil {
return err
}

// Overwrite variables.
if clusterctlConfigFile.Values == nil {
Expand All @@ -185,7 +187,7 @@ func copyAndAmendClusterctlConfig(_ context.Context, input copyAndAmendClusterct

// Write clusterctl config to OutputPath.
clusterctlConfigFile.Path = input.OutputPath
clusterctlConfigFile.write()
return clusterctlConfigFile.write()
}

// AdjustConfigPathForBinary adjusts the clusterctlConfigPath in case the clusterctl version v1.3.
Expand Down

0 comments on commit 78bc3a5

Please sign in to comment.