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

Make CloudConfigToString a struct receiver function called String #1505

Merged
merged 3 commits into from
Dec 6, 2022
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
2 changes: 1 addition & 1 deletion pkg/cloudprovider/provider/vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (p *provider) GetCloudConfig(spec clusterv1alpha1.MachineSpec) (config stri
},
}

s, err := vspheretypes.CloudConfigToString(cc)
s, err := cc.String()
if err != nil {
return "", "", fmt.Errorf("failed to convert the cloud-config to string: %w", err)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/cloudprovider/provider/vsphere/types/cloudconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ type CloudConfig struct {
VirtualCenter map[string]*VirtualCenterConfig
}

// String converts CloudConfig into its formatted string representation.
func (c *CloudConfig) String() (string, error) {
funcMap := sprig.TxtFuncMap()
funcMap["iniEscape"] = ini.Escape

tpl, err := template.New("cloud-config").Funcs(funcMap).Parse(cloudConfigTpl)
if err != nil {
return "", fmt.Errorf("failed to parse the cloud config template: %w", err)
}

buf := &bytes.Buffer{}
if err := tpl.Execute(buf, c); err != nil {
return "", fmt.Errorf("failed to execute cloud config template: %w", err)
}

return buf.String(), nil
}

// CloudConfigToString converts CloudConfig into its formatted string representation.
// Deprecated: use struct receiver function String() instead.
func CloudConfigToString(c *CloudConfig) (string, error) {
funcMap := sprig.TxtFuncMap()
funcMap["iniEscape"] = ini.Escape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestCloudConfigToString(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
s, err := CloudConfigToString(test.config)
s, err := test.config.String()
if err != nil {
t.Fatal(err)
}
Expand Down