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

feat: helmfile build --embed-values to embed release values and secrets into the output #1436

Merged
merged 1 commit into from
Aug 29, 2020
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
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ func main() {
{
Name: "build",
Usage: "output compiled helmfile state(s) as YAML",
Flags: []cli.Flag{},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "embed-values",
Usage: "Read all the values files for every release and embed into the output helmfile.yaml",
},
},
Action: action(func(run *app.App, c configImpl) error {
return run.PrintState(c)
}),
Expand Down Expand Up @@ -674,6 +679,10 @@ func (c configImpl) Context() int {
return c.c.Int("context")
}

func (c configImpl) EmbedValues() bool {
return c.c.Bool("embed-values")
}

func (c configImpl) Logger() *zap.SugaredLogger {
return c.c.App.Metadata["logger"].(*zap.SugaredLogger)
}
Expand Down
27 changes: 25 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,35 @@ func (a *App) PrintState(c StateConfigProvider) error {
err := run.withPreparedCharts("build", state.ChartPrepareOptions{
SkipRepos: true,
}, func() {
state, err := run.state.ToYaml()
if c.EmbedValues() {
for i := range run.state.Releases {
r := run.state.Releases[i]

values, err := run.state.LoadYAMLForEmbedding(r.Values, r.MissingFileHandler, r.ValuesPathPrefix)
if err != nil {
errs = []error{err}
return
}

run.state.Releases[i].Values = values

secrets, err := run.state.LoadYAMLForEmbedding(r.Secrets, r.MissingFileHandler, r.ValuesPathPrefix)
if err != nil {
errs = []error{err}
return
}

run.state.Releases[i].Secrets = secrets
}
}

stateYaml, err := run.state.ToYaml()
if err != nil {
errs = []error{err}
return
}
fmt.Printf("---\n# Source: %s\n\n%+v", run.state.FilePath, state)

fmt.Printf("---\n# Source: %s\n\n%+v", run.state.FilePath, stateYaml)

errs = []error{}
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2271,6 +2271,10 @@ func (c configImpl) Concurrency() int {
return 1
}

func (c configImpl) EmbedValues() bool {
return false
}

func (c configImpl) Output() string {
return c.output
}
Expand Down
1 change: 1 addition & 0 deletions pkg/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type StatusesConfigProvider interface {
}

type StateConfigProvider interface {
EmbedValues() bool
}

type concurrencyConfig interface {
Expand Down
13 changes: 8 additions & 5 deletions pkg/state/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,15 @@ func (r ReleaseSpec) ExecuteTemplateExpressions(renderer *tmpl.FileRenderer) (*R
}
}

for i, ts := range result.Secrets {
s, err := renderer.RenderTemplateContentToBuffer([]byte(ts))
if err != nil {
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".secrets[%d] = \"%s\": %v", r.Name, i, ts, err)
for i, t := range result.Secrets {
switch ts := t.(type) {
case string:
s, err := renderer.RenderTemplateContentToBuffer([]byte(ts))
if err != nil {
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".secrets[%d] = \"%s\": %v", r.Name, i, ts, err)
}
result.Secrets[i] = s.String()
}
result.Secrets[i] = s.String()
}

if len(result.SetValuesTemplate) > 0 {
Expand Down
79 changes: 74 additions & 5 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ type ReleaseSpec struct {
Namespace string `yaml:"namespace,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Values []interface{} `yaml:"values,omitempty"`
Secrets []string `yaml:"secrets,omitempty"`
Secrets []interface{} `yaml:"secrets,omitempty"`
SetValues []SetValue `yaml:"set,omitempty"`

ValuesTemplate []interface{} `yaml:"valuesTemplate,omitempty"`
Expand Down Expand Up @@ -2163,11 +2163,41 @@ func (st *HelmState) generateVanillaValuesFiles(release *ReleaseSpec) ([]string,
func (st *HelmState) generateSecretValuesFiles(helm helmexec.Interface, release *ReleaseSpec, workerIndex int) ([]string, error) {
var generatedFiles []string

for _, value := range release.Secrets {
paths, skip, err := st.storage().resolveFile(release.MissingFileHandler, "secrets", release.ValuesPathPrefix+value)
if err != nil {
return nil, err
for _, v := range release.Secrets {
var (
paths []string
skip bool
err error
)

switch value := v.(type) {
case string:
paths, skip, err = st.storage().resolveFile(release.MissingFileHandler, "secrets", release.ValuesPathPrefix+value)
if err != nil {
return nil, err
}
default:
bs, err := yaml.Marshal(value)
if err != nil {
return nil, err
}

path, err := ioutil.TempFile(os.TempDir(), "helmfile-embdedded-secrets-*.yaml.enc")
if err != nil {
return nil, err
}
_ = path.Close()
defer func() {
_ = os.Remove(path.Name())
}()

if err := ioutil.WriteFile(path.Name(), bs, 0644); err != nil {
return nil, err
}

paths = []string{path.Name()}
}

if skip {
continue
}
Expand Down Expand Up @@ -2447,3 +2477,42 @@ func (st *HelmState) ToYaml() (string, error) {
return string(result), nil
}
}

func (st *HelmState) LoadYAMLForEmbedding(entries []interface{}, missingFileHandler *string, pathPrefix string) ([]interface{}, error) {
var result []interface{}

for _, v := range entries {
switch t := v.(type) {
case string:
var values map[string]interface{}

paths, skip, err := st.storage().resolveFile(missingFileHandler, "values", pathPrefix+t)
if err != nil {
return nil, err
}
if skip {
continue
}

if len(paths) > 1 {
return nil, fmt.Errorf("glob patterns in release values and secrets is not supported yet. please submit a feature request if necessary")
Copy link

Choose a reason for hiding this comment

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

I like your error handling 😄

}
yamlOrTemplatePath := paths[0]

yamlBytes, err := st.RenderValuesFileToBytes(yamlOrTemplatePath)
if err != nil {
return nil, fmt.Errorf("failed to render values files \"%s\": %v", t, err)
}

if err := yaml.Unmarshal(yamlBytes, &values); err != nil {
return nil, err
}

result = append(result, values)
default:
result = append(result, v)
}
}

return result, nil
}
4 changes: 2 additions & 2 deletions pkg/state/state_exec_tmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestHelmState_executeTemplates(t *testing.T) {
Name: "test-app",
Namespace: "test-namespace-{{ .Release.Name }}",
ValuesTemplate: []interface{}{"config/{{ .Environment.Name }}/{{ .Release.Name }}/values.yaml"},
Secrets: []string{"config/{{ .Environment.Name }}/{{ .Release.Name }}/secrets.yaml"},
Secrets: []interface{}{"config/{{ .Environment.Name }}/{{ .Release.Name }}/secrets.yaml"},
Labels: map[string]string{"id": "{{ .Release.Name }}"},
},
want: ReleaseSpec{
Expand All @@ -45,7 +45,7 @@ func TestHelmState_executeTemplates(t *testing.T) {
Name: "test-app",
Namespace: "test-namespace-test-app",
Values: []interface{}{"config/test_env/test-app/values.yaml"},
Secrets: []string{"config/test_env/test-app/secrets.yaml"},
Secrets: []interface{}{"config/test_env/test-app/secrets.yaml"},
Labels: map[string]string{"id": "test-app"},
},
},
Expand Down