Skip to content

Commit 293b50c

Browse files
committed
Update to func handling
Signed-off-by: Matt Farina <[email protected]> (cherry picked from commit 863bc74)
1 parent 472c573 commit 293b50c

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

cmd/helm/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func addInstallFlags(cmd *cobra.Command, f *pflag.FlagSet, client *action.Instal
170170
f.BoolVar(&client.Atomic, "atomic", false, "if set, the installation process deletes the installation on failure. The --wait flag will be set automatically if --atomic is used")
171171
f.BoolVar(&client.SkipCRDs, "skip-crds", false, "if set, no CRDs will be installed. By default, CRDs are installed if not already present")
172172
f.BoolVar(&client.SubNotes, "render-subchart-notes", false, "if set, render subchart notes along with the parent")
173+
f.BoolVar(&client.EnableDNS, "enable-dns", false, "enable DNS lookups when rendering templates")
173174
addValueOptionsFlags(f, valueOpts)
174175
addChartPathOptionsFlags(f, &client.ChartPathOptions)
175176

cmd/helm/upgrade.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
119119
instClient.SubNotes = client.SubNotes
120120
instClient.Description = client.Description
121121
instClient.DependencyUpdate = client.DependencyUpdate
122+
instClient.EnableDNS = client.EnableDNS
122123

123124
rel, err := runInstall(args, instClient, valueOpts, out)
124125
if err != nil {
@@ -232,6 +233,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
232233
f.BoolVar(&client.SubNotes, "render-subchart-notes", false, "if set, render subchart notes along with the parent")
233234
f.StringVar(&client.Description, "description", "", "add a custom description")
234235
f.BoolVar(&client.DependencyUpdate, "dependency-update", false, "update dependencies if they are missing before installing the chart")
236+
f.BoolVar(&client.EnableDNS, "enable-dns", false, "enable DNS lookups when rendering templates")
235237
addChartPathOptionsFlags(f, &client.ChartPathOptions)
236238
addValueOptionsFlags(f, valueOpts)
237239
bindOutputFlag(cmd, &outfmt)

pkg/action/action.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ type Configuration struct {
101101
//
102102
// TODO: This function is badly in need of a refactor.
103103
// TODO: As part of the refactor the duplicate code in cmd/helm/template.go should be removed
104-
// This code has to do with writing files to disk.
105-
func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) {
104+
//
105+
// This code has to do with writing files to disk.
106+
func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun, enableDNS bool) ([]*release.Hook, *bytes.Buffer, string, error) {
106107
hs := []*release.Hook{}
107108
b := bytes.NewBuffer(nil)
108109

@@ -130,9 +131,13 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Valu
130131
if err != nil {
131132
return hs, b, "", err
132133
}
133-
files, err2 = engine.RenderWithClient(ch, values, restConfig)
134+
e := engine.New(restConfig)
135+
e.EnableDNS = enableDNS
136+
files, err2 = e.Render(ch, values)
134137
} else {
135-
files, err2 = engine.Render(ch, values)
138+
var e engine.Engine
139+
e.EnableDNS = enableDNS
140+
files, err2 = e.Render(ch, values)
136141
}
137142

138143
if err2 != nil {

pkg/action/install.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ type Install struct {
9797
APIVersions chartutil.VersionSet
9898
// Used by helm template to render charts with .Release.IsUpgrade. Ignored if Dry-Run is false
9999
IsUpgrade bool
100+
// Enable DNS lookups when rendering templates
101+
EnableDNS bool
100102
// Used by helm template to add the release as part of OutputDir path
101103
// OutputDir/<ReleaseName>
102104
UseReleaseName bool
@@ -257,7 +259,7 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma
257259
rel := i.createRelease(chrt, vals)
258260

259261
var manifestDoc *bytes.Buffer
260-
rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, i.DryRun)
262+
rel.Hooks, manifestDoc, rel.Info.Notes, err = i.cfg.renderResources(chrt, valuesToRender, i.ReleaseName, i.OutputDir, i.SubNotes, i.UseReleaseName, i.IncludeCRDs, i.PostRenderer, i.DryRun, i.EnableDNS)
261263
// Even for errors, attach this if available
262264
if manifestDoc != nil {
263265
rel.Manifest = manifestDoc.String()
@@ -457,10 +459,10 @@ func (i *Install) failRelease(rel *release.Release, err error) (*release.Release
457459
//
458460
// Roughly, this will return an error if name is
459461
//
460-
// - empty
461-
// - too long
462-
// - already in use, and not deleted
463-
// - used by a deleted release, and i.Replace is false
462+
// - empty
463+
// - too long
464+
// - already in use, and not deleted
465+
// - used by a deleted release, and i.Replace is false
464466
func (i *Install) availableName() error {
465467
start := i.ReleaseName
466468

pkg/action/upgrade.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ type Upgrade struct {
103103
DependencyUpdate bool
104104
// Lock to control raceconditions when the process receives a SIGTERM
105105
Lock sync.Mutex
106+
// Enable DNS lookups when rendering templates
107+
EnableDNS bool
106108
}
107109

108110
type resultMessage struct {
@@ -231,7 +233,7 @@ func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart, vals map[strin
231233
return nil, nil, err
232234
}
233235

234-
hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, u.DryRun)
236+
hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender, "", "", u.SubNotes, false, false, u.PostRenderer, u.DryRun, u.EnableDNS)
235237
if err != nil {
236238
return nil, nil, err
237239
}

pkg/engine/engine.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ type Engine struct {
4242
LintMode bool
4343
// the rest config to connect to the kubernetes api
4444
config *rest.Config
45+
// EnableDNS tells the engine to allow DNS lookups when rendering templates
46+
EnableDNS bool
47+
}
48+
49+
// New creates a new instance of Engine using the passed in rest config.
50+
func New(config *rest.Config) Engine {
51+
return Engine{
52+
config: config,
53+
}
4554
}
4655

4756
// Render takes a chart, optional values, and value overrides, and attempts to render the Go templates.
@@ -189,6 +198,14 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
189198
funcMap["lookup"] = NewLookupFunction(e.config)
190199
}
191200

201+
// When DNS lookups are not enabled override the sprig function and return
202+
// an empty string.
203+
if !e.EnableDNS {
204+
funcMap["getHostByName"] = func(name string) string {
205+
return ""
206+
}
207+
}
208+
192209
t.Funcs(funcMap)
193210
}
194211

pkg/engine/engine_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package engine
1818

1919
import (
2020
"fmt"
21+
"path"
2122
"strings"
2223
"sync"
2324
"testing"
@@ -89,6 +90,7 @@ func TestRender(t *testing.T) {
8990
{Name: "templates/test2", Data: []byte("{{.Values.global.callme | lower }}")},
9091
{Name: "templates/test3", Data: []byte("{{.noValue}}")},
9192
{Name: "templates/test4", Data: []byte("{{toJson .Values}}")},
93+
{Name: "templates/test5", Data: []byte("{{getHostByName \"helm.sh\"}}")},
9294
},
9395
Values: map[string]interface{}{"outer": "DEFAULT", "inner": "DEFAULT"},
9496
}
@@ -117,6 +119,7 @@ func TestRender(t *testing.T) {
117119
"moby/templates/test2": "ishmael",
118120
"moby/templates/test3": "",
119121
"moby/templates/test4": `{"global":{"callme":"Ishmael"},"inner":"inn","outer":"spouter"}`,
122+
"moby/templates/test5": "",
120123
}
121124

122125
for name, data := range expect {
@@ -200,6 +203,42 @@ func TestRenderInternals(t *testing.T) {
200203
}
201204
}
202205

206+
func TestRenderWIthDNS(t *testing.T) {
207+
c := &chart.Chart{
208+
Metadata: &chart.Metadata{
209+
Name: "moby",
210+
Version: "1.2.3",
211+
},
212+
Templates: []*chart.File{
213+
{Name: "templates/test1", Data: []byte("{{getHostByName \"helm.sh\"}}")},
214+
},
215+
Values: map[string]interface{}{},
216+
}
217+
218+
vals := map[string]interface{}{
219+
"Values": map[string]interface{}{},
220+
}
221+
222+
v, err := chartutil.CoalesceValues(c, vals)
223+
if err != nil {
224+
t.Fatalf("Failed to coalesce values: %s", err)
225+
}
226+
227+
var e Engine
228+
e.EnableDNS = true
229+
out, err := e.Render(c, v)
230+
if err != nil {
231+
t.Errorf("Failed to render templates: %s", err)
232+
}
233+
234+
for _, val := range c.Templates {
235+
fp := path.Join("moby", val.Name)
236+
if out[fp] == "" {
237+
t.Errorf("Expected IP address, got %q", out[fp])
238+
}
239+
}
240+
}
241+
203242
func TestParallelRenderInternals(t *testing.T) {
204243
// Make sure that we can use one Engine to run parallel template renders.
205244
e := new(Engine)

0 commit comments

Comments
 (0)