Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 test/framework: expose CopyAndAmendClusterctlConfig function #10086

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), "Failed to CopyAndAmendClusterctlConfig")
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(), "Failed to CopyAndAmendClusterctlConfig")
input.ClusterctlConfigPath = outputPath
}

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

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

Expand All @@ -40,18 +40,29 @@ 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)
Expect(err).ToNot(HaveOccurred(), "Failed to marshal the clusterctl config file")
if err != nil {
return errors.Wrap(err, "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
}
18 changes: 10 additions & 8 deletions test/framework/clusterctl/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
for key := range input.E2EConfig.Variables {
clusterctlConfigFile.Values[key] = input.E2EConfig.GetVariable(key)
}
clusterctlConfigFile.write()
Expect(clusterctlConfigFile.write()).To(Succeed(), "Failed to write clusterctlConfigFile")

// creates a clusterctl config file to be used for working with such repository with only the providers supported in clusterctl < v1.3
clusterctlConfigFileV1_2 := &clusterctlConfig{
Expand All @@ -154,26 +154,28 @@ func CreateRepository(ctx context.Context, input CreateRepositoryInput) string {
for key := range input.E2EConfig.Variables {
clusterctlConfigFileV1_2.Values[key] = input.E2EConfig.GetVariable(key)
}
clusterctlConfigFileV1_2.write()
Expect(clusterctlConfigFileV1_2.write()).To(Succeed(), "Failed to write v1.2 clusterctlConfigFile")

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
Loading