New 🌊 wave for @helm

Commit 61f86b22

61f86b22128aMerged

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

Description

feat: allow to skip missing values and rendering values

Details

Commit
61f86b22128a
Parent
b8feb5ab4d2f

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,22 @@
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+ type raw ValuesReference
2832 var err error
2933 switch node.Kind {
3034 // single value or reference to another value
3135 case yaml.ScalarNode, yaml.AliasNode:
3236 err = node.Decode(&v.Src)
3337 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"]
38+ err = node.Decode((*raw)(v))
3939 default:
4040 err = fmt.Errorf("unknown format")
4141 }
@@ -54,7 +54,7 @@
5454 Dst string
5555 }{
5656 Src: v.Src,
57- Dst: v.dst,
57+ Dst: v.Dst,
5858 }, nil
5959 }
6060
@@ -64,8 +64,8 @@
6464
6565 // Download downloads values by source URL and places to destination path.
6666 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)
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)
6969 }
7070
7171 return nil
@@ -73,7 +73,7 @@
7373
7474 // Get returns destination path of values.
7575 func (v *ValuesReference) Get() string {
76- return v.dst
76+ return v.Dst
7777 }
7878
7979 // SetUniq generates unique file path based on provided base directory, release uniqname and sha1 of source path.
@@ -83,7 +83,7 @@
8383 hash := h.Sum(nil)
8484 s := hex.EncodeToString(hash)
8585
86- v.dst = filepath.Join(dir, "values", string(name), s+".yml")
86+ v.Dst = filepath.Join(dir, "values", string(name), s+".yml")
8787
8888 return v
8989 }
@@ -96,9 +96,13 @@
9696 // SetViaRelease downloads and templates values file.
9797 // Returns ErrSkipValues if values cannot be downloaded or doesn't exist in local FS.
9898 func (v *ValuesReference) SetViaRelease(rel Config, dir, templater string) error {
99+ if !v.Render {
100+ templater = "copy"
101+ }
102+
99103 v.SetUniq(dir, rel.Uniq())
100104
101- l := rel.Logger().WithField("values src", v.Src).WithField("values dst", v.dst)
105+ l := rel.Logger().WithField("values src", v.Src).WithField("values dst", v.Dst)
102106
103107 l.Trace("Building values reference")
104108
@@ -108,27 +112,39 @@
108112 Release: rel,
109113 }
110114
115+ err := v.fetch(l)
116+ if err != nil {
117+ return err
118+ }
119+
120+ if v.isURL() {
121+ err = template.Tpl2yml(v.Dst, v.Dst, data, templater)
122+ } else {
123+ err = template.Tpl2yml(v.Src, v.Dst, data, templater)
124+ }
125+
126+ if err != nil {
127+ return fmt.Errorf("failed to render %q values: %w", v.Src, err)
128+ }
129+
130+ return nil
131+}
132+
133+//nolint:nestif // it is still pretty easy to understand
134+func (v *ValuesReference) fetch(l *log.Entry) error {
111135 if v.isURL() {
112136 err := v.Download()
113137 if err != nil {
114138 l.WithError(err).Warnf("%q skipping: cant download", v.Src)
115139
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)
140+ if v.Strict {
141+ return ErrSkipValues
142+ }
121143 }
122-
123- return nil
124144 } else if !helper.IsExists(v.Src) {
125145 l.Warn("skipping: local file not found")
126146
127147 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)
132148 }
133149
134150 return nil