Skip to content

Commit

Permalink
Merge pull request #10086 from chrischdi/pr-expose-copyandamendcluste…
Browse files Browse the repository at this point in the history
…rctlconfig

🌱 test/framework: expose CopyAndAmendClusterctlConfig function
  • Loading branch information
k8s-ci-robot committed Feb 5, 2024
2 parents ad9151c + da0ff64 commit 97782fe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 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(), "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,
}