forked from helmfile/helmfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.go
95 lines (73 loc) · 1.65 KB
/
temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package state
import (
"errors"
"fmt"
"hash/fnv"
"os"
"path/filepath"
"strings"
"github.com/davecgh/go-spew/spew"
"k8s.io/apimachinery/pkg/util/rand"
"github.com/helmfile/helmfile/pkg/envvar"
)
func createTempValuesFile(release *ReleaseSpec, data any) (*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 any) (*string, error) {
id, err := generateValuesID(release, data)
if err != nil {
return nil, err
}
workDir := os.Getenv(envvar.TempDir)
if workDir == "" {
workDir, err = os.MkdirTemp(os.TempDir(), "helmfile")
} else {
err = os.MkdirAll(workDir, os.FileMode(0700))
}
if err != nil {
return nil, err
}
d := filepath.Join(workDir, id)
_, err = os.Stat(d)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
return &d, nil
}
func generateValuesID(release *ReleaseSpec, data any) (string, error) {
var id []string
if release.Namespace != "" {
id = append(id, release.Namespace)
}
id = append(id, release.Name, "values")
hash, err := HashObject([]any{release, data})
if err != nil {
return "", err
}
id = append(id, hash)
return strings.Join(id, "-"), nil
}
func HashObject(obj any) (string, error) {
hash := fnv.New32a()
hash.Reset()
printer := spew.ConfigState{
Indent: " ",
SortKeys: true,
DisableMethods: true,
SpewKeys: true,
}
_, err := printer.Fprintf(hash, "%#v", obj)
if err != nil {
return "", err
}
sum := fmt.Sprint(hash.Sum32())
return rand.SafeEncodeString(sum), nil
}