mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-04 17:43:52 +02:00
This finishes the work started at #1619 by introducing the following changes: - You can now set `HELMFILE_TEMPDIR` and `CHARTIFY_TEMPDIR` as the directory to persist temporary values files rendered by helmfile and temporary charts generated by chartify. Note that chartify is used internally when you use kustomize integration or raw K8s manifests as a chart. - Helmfile uses the hash sum of the release config and the values file content in the file name of the temporary values file. In combination with `HELMFILE_TEMPDIR` and `CHARTIFY_TEMPDIR`, this enables helmfile to use the stable file names for temporary values files, which contribute to stabilize log messages like `Comparing release=appset, chart=PATH/TO/TEMPORARY/CHART`, where the `PATH/TO/TEMPORARY/CHART` had been randomized due to formerly random helmfile tempdir and temporary values file names, and random chartify tempdir. You can try this feature with a script like: ``` tempdir=$(mktemp -d -t helmfile) HELMFILE_TEMPDIR=${tempdir} CHARTIFY_TEMPDIR=${tempdir} helmfile diff rm -rf ${tempdir} ```
91 lines
1.5 KiB
Go
91 lines
1.5 KiB
Go
package state
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
func createTempValuesFile(release *ReleaseSpec, data interface{}) (*os.File, error) {
|
|
p, err := tempValuesFilePath(release, data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
f, err := os.Create(*p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return f, nil
|
|
}
|
|
|
|
func tempValuesFilePath(release *ReleaseSpec, data interface{}) (*string, error) {
|
|
id, err := generateValuesID(release, data)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
workDir := os.Getenv("HELMFILE_TEMPDIR")
|
|
if workDir == "" {
|
|
workDir, err = ioutil.TempDir(os.TempDir(), "helmfile")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
d := filepath.Join(workDir, id)
|
|
|
|
info, err := os.Stat(d)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
panic(err)
|
|
} else if info == nil {
|
|
|
|
}
|
|
|
|
return &d, nil
|
|
}
|
|
|
|
func generateValuesID(release *ReleaseSpec, data interface{}) (string, error) {
|
|
var id []string
|
|
|
|
if release.Namespace != "" {
|
|
id = append(id, release.Namespace)
|
|
}
|
|
|
|
id = append(id, release.Name, "values")
|
|
|
|
hash, err := HashObject([]interface{}{release, data})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
id = append(id, hash)
|
|
|
|
return strings.Join(id, "-"), nil
|
|
}
|
|
|
|
func HashObject(obj interface{}) (string, error) {
|
|
hash := fnv.New32a()
|
|
|
|
hash.Reset()
|
|
|
|
printer := spew.ConfigState{
|
|
Indent: " ",
|
|
SortKeys: true,
|
|
DisableMethods: true,
|
|
SpewKeys: true,
|
|
}
|
|
printer.Fprintf(hash, "%#v", obj)
|
|
|
|
sum := fmt.Sprint(hash.Sum32())
|
|
|
|
return SafeEncodeString(sum), nil
|
|
}
|