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

refactor(helmraiser): Abstract template code #363

Merged
merged 2 commits into from
Sep 1, 2020
Merged

Conversation

sh0rez
Copy link
Member

@sh0rez sh0rez commented Aug 27, 2020

Abstracts the code that calls helm template into a struct method, so
it can be used beyond the Jsonnet interface.
This enables better testing, is more readable and also leverages more of
the existing code in this project.

This is extracted from #355

Abstracts the code that calls `helm template` into a struct method, so
it can be used beyond the Jsonnet interface.
This enables better testing, is more readable and also leverages more of
the existing code in this project.
@sh0rez sh0rez added the kind/enhancement Improve something existing label Aug 27, 2020
pkg/helmraiser/helm.go Outdated Show resolved Hide resolved
Comment on lines +90 to +96
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = os.Stderr

// snake_case string
normalizeName := func(s string) string {
s = strings.ReplaceAll(s, "-", "_")
s = strings.ReplaceAll(s, ":", "_")
s = strings.ToLower(s)
return s
}
if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, "Expanding Helm Chart")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could all be wrapped in the Helm.run() function, I would even think of returning func (c *Cmd) Output() ([]byte, error) for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bit awkward that github diff included the normalizeName, just ignore that)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing the same pattern that proved working well in pkg/kubernetes/client:

func (k Kubectl) ctl(action string, args ...string) *exec.Cmd {

Returning the raw exec.Cmd allows consuming functions to also modify the commands environment, stdin, capture stderr if required, etc.

I think we will need this in the future here.

Comment on lines +98 to +110
var list manifest.List
d := yaml.NewDecoder(&buf)
for {
var m manifest.Manifest
if err := d.Decode(&m); err != nil {
if err == io.EOF {
break
}
return nil, errors.Wrap(err, "Parsing Helm output")
}
list = append(list, m)
}
return files, nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this replaces parseYamlToMap, could you move it into a function and add a unit test for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo the implementation of yaml unmarshalling doesn't require testing here, because go-yaml is heavily unit tested itself, and unmarshalling yaml output into a map[string]interface{} pretty standard Go stuff.

What I instead would like to test here is that the output of helm correctly ends up in Jsonnet, regardless of our actual Go implementation.
I will add such a test in a subsequent pull request, which this one is the preparation for.

@sh0rez sh0rez requested a review from Duologic August 30, 2020 14:35
@sh0rez sh0rez merged commit d985b00 into master Sep 1, 2020
@sh0rez sh0rez deleted the helm-refactor branch September 1, 2020 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improve something existing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants