New 🌊 wave for @helm

Commit d7a2f320

d7a2f3201bacMerged

Dmitriy Zhilyaev <[email protected]> · 4 years ago · 6 files · +1 added · ~3 modified

Description

Merge pull request #381 from helmwave/values

feat: allow to skip missing values and rendering values

Details

Commit
d7a2f3201bac
Parent
b8feb5ab4d2f
Merge parent
d4a1c9e59e8b

pkg/release/values.gomodifiedView file

@@ -11,6 +11,7 @@
1111 "github.com/helmwave/helmwave/pkg/helper"
1212 "github.com/helmwave/helmwave/pkg/release/uniqname"
1313 "github.com/helmwave/helmwave/pkg/template"
14+ log "github.com/sirupsen/logrus"
1415 "gopkg.in/yaml.v3"
1516 )
1617
@@ -19,23 +20,24 @@
1920
2021 // ValuesReference is used to match source values file path and temporary.
2122 type ValuesReference struct {
22- Src string `yaml:"src"`
23- dst string `yaml:"dst"`
23+ Src string `yaml:"src"`
24+ Dst string `yaml:"dst"`
25+ Strict bool `yaml:"strict"`
26+ Render bool `yaml:"render"`
2427 }
2528
2629 // UnmarshalYAML is used to implement Unmarshaler interface of gopkg.in/yaml.v3.
2730 func (v *ValuesReference) UnmarshalYAML(node *yaml.Node) error {
31+ v.Render = true // we render values by default
32+
33+ type raw ValuesReference
2834 var err error
2935 switch node.Kind {
3036 // single value or reference to another value
3137 case yaml.ScalarNode, yaml.AliasNode:
3238 err = node.Decode(&v.Src)
3339 case yaml.MappingNode:
34- var m map[string]string
35- err = node.Decode(&m)
36-
37- v.Src = m["src"]
38- v.dst = m["dst"]
40+ err = node.Decode((*raw)(v))
3941 default:
4042 err = fmt.Errorf("unknown format")
4143 }
@@ -54,7 +56,7 @@
5456 Dst string
5557 }{
5658 Src: v.Src,
57- Dst: v.dst,
59+ Dst: v.Dst,
5860 }, nil
5961 }
6062
@@ -64,8 +66,8 @@
6466
6567 // Download downloads values by source URL and places to destination path.
6668 func (v *ValuesReference) Download() error {
67- if err := helper.Download(v.dst, v.Src); err != nil {
68- return fmt.Errorf("failed to download values %s -> %s: %w", v.Src, v.dst, err)
69+ if err := helper.Download(v.Dst, v.Src); err != nil {
70+ return fmt.Errorf("failed to download values %s -> %s: %w", v.Src, v.Dst, err)
6971 }
7072
7173 return nil
@@ -73,7 +75,7 @@
7375
7476 // Get returns destination path of values.
7577 func (v *ValuesReference) Get() string {
76- return v.dst
78+ return v.Dst
7779 }
7880
7981 // SetUniq generates unique file path based on provided base directory, release uniqname and sha1 of source path.
@@ -83,7 +85,7 @@
8385 hash := h.Sum(nil)
8486 s := hex.EncodeToString(hash)
8587
86- v.dst = filepath.Join(dir, "values", string(name), s+".yml")
88+ v.Dst = filepath.Join(dir, "values", string(name), s+".yml")
8789
8890 return v
8991 }
@@ -96,9 +98,13 @@
9698 // SetViaRelease downloads and templates values file.
9799 // Returns ErrSkipValues if values cannot be downloaded or doesn't exist in local FS.
98100 func (v *ValuesReference) SetViaRelease(rel Config, dir, templater string) error {
101+ if !v.Render {
102+ templater = "copy"
103+ }
104+
99105 v.SetUniq(dir, rel.Uniq())
100106
101- l := rel.Logger().WithField("values src", v.Src).WithField("values dst", v.dst)
107+ l := rel.Logger().WithField("values src", v.Src).WithField("values dst", v.Dst)
102108
103109 l.Trace("Building values reference")
104110
@@ -108,27 +114,39 @@
108114 Release: rel,
109115 }
110116
117+ err := v.fetch(l)
118+ if err != nil {
119+ return err
120+ }
121+
111122 if v.isURL() {
123+ err = template.Tpl2yml(v.Dst, v.Dst, data, templater)
124+ } else {
125+ err = template.Tpl2yml(v.Src, v.Dst, data, templater)
126+ }
127+
128+ if err != nil {
129+ return fmt.Errorf("failed to render %q values: %w", v.Src, err)
130+ }
131+
132+ return nil
133+}
134+
135+//nolint:nestif // it is still pretty easy to understand
136+func (v *ValuesReference) fetch(l *log.Entry) error {
137+ if v.isURL() {
112138 err := v.Download()
113139 if err != nil {
114140 l.WithError(err).Warnf("%q skipping: cant download", v.Src)
115141
116- return ErrSkipValues
117- }
118-
119- if err := template.Tpl2yml(v.dst, v.dst, data, templater); err != nil {
120- return fmt.Errorf("failed to render %q values: %w", v.Src, err)
142+ if v.Strict {
143+ return ErrSkipValues
144+ }
121145 }
122-
123- return nil
124146 } else if !helper.IsExists(v.Src) {
125147 l.Warn("skipping: local file not found")
126148
127149 return ErrSkipValues
128- }
129-
130- if err := template.Tpl2yml(v.Src, v.dst, data, templater); err != nil {
131- return fmt.Errorf("failed to render %q values: %w", v.Src, err)
132150 }
133151
134152 return nil