Skip to content

Commit 88508a2

Browse files
authored
Add a WithValueTranslator option to Reconciller. (#6)
This lets the user inject code which produces helm values based on the fetched object itself (unlike `Mapper` which can only see `Values`). This way the custom code can convert the object to a typed one and work with proper structs rather than a tree of maps from `string` to `interface{}`.
1 parent 22a7408 commit 88508a2

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

pkg/reconciler/internal/values/values.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,11 @@ func (v *Values) ApplyOverrides(in map[string]string) error {
6868
}
6969

7070
var DefaultMapper = values.MapperFunc(func(v chartutil.Values) chartutil.Values { return v })
71+
72+
var DefaultTranslator = values.TranslatorFunc(func(u *unstructured.Unstructured) (chartutil.Values, error) {
73+
internalValues, err := FromUnstructured(u)
74+
if err != nil {
75+
return chartutil.Values{}, err
76+
}
77+
return internalValues.Map(), err
78+
})

pkg/reconciler/reconciler.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const uninstallFinalizer = "uninstall-helm-release"
6161
type Reconciler struct {
6262
client client.Client
6363
actionClientGetter helmclient.ActionClientGetter
64+
valueTranslator values.Translator
6465
valueMapper values.Mapper
6566
eventRecorder record.EventRecorder
6667
preHooks []hook.PreHook
@@ -362,8 +363,20 @@ func WithPostHook(h hook.PostHook) Option {
362363
}
363364
}
364365

366+
// WithValueTranslator is an Option that configures a function that translates a
367+
// custom resource to the values passed to Helm.
368+
// Use this if you need to customize the logic that translates your custom resource to Helm values.
369+
func WithValueTranslator(t values.Translator) Option {
370+
return func(r *Reconciler) error {
371+
r.valueTranslator = t
372+
return nil
373+
}
374+
}
375+
365376
// WithValueMapper is an Option that configures a function that maps values
366-
// from a custom resource spec to the values passed to Helm
377+
// from a custom resource spec to the values passed to Helm.
378+
// Use this if you want to apply a transformation on the values obtained from your custom resource, before
379+
// they are passed to Helm.
367380
func WithValueMapper(m values.Mapper) Option {
368381
return func(r *Reconciler) error {
369382
r.valueMapper = m
@@ -522,14 +535,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.
522535
}
523536

524537
func (r *Reconciler) getValues(obj *unstructured.Unstructured) (chartutil.Values, error) {
525-
crVals, err := internalvalues.FromUnstructured(obj)
538+
vals, err := r.valueTranslator.Translate(obj)
526539
if err != nil {
527540
return chartutil.Values{}, err
528541
}
542+
crVals := internalvalues.New(vals)
529543
if err := crVals.ApplyOverrides(r.overrideValues); err != nil {
530544
return chartutil.Values{}, err
531545
}
532-
vals := r.valueMapper.Map(crVals.Map())
546+
vals = r.valueMapper.Map(crVals.Map())
533547
vals, err = chartutil.CoalesceValues(r.chrt, vals)
534548
if err != nil {
535549
return chartutil.Values{}, err
@@ -736,6 +750,9 @@ func (r *Reconciler) addDefaults(mgr ctrl.Manager, controllerName string) {
736750
if r.eventRecorder == nil {
737751
r.eventRecorder = mgr.GetEventRecorderFor(controllerName)
738752
}
753+
if r.valueTranslator == nil {
754+
r.valueTranslator = internalvalues.DefaultTranslator
755+
}
739756
if r.valueMapper == nil {
740757
r.valueMapper = internalvalues.DefaultMapper
741758
}

pkg/values/values.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package values
1818

1919
import (
2020
"helm.sh/helm/v3/pkg/chartutil"
21+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2122
)
2223

24+
// TODO: Consider deprecating Mapper and overrides in favour of Translator.
25+
2326
type Mapper interface {
2427
Map(chartutil.Values) chartutil.Values
2528
}
@@ -29,3 +32,13 @@ type MapperFunc func(chartutil.Values) chartutil.Values
2932
func (m MapperFunc) Map(v chartutil.Values) chartutil.Values {
3033
return m(v)
3134
}
35+
36+
type Translator interface {
37+
Translate(unstructured *unstructured.Unstructured) (chartutil.Values, error)
38+
}
39+
40+
type TranslatorFunc func(*unstructured.Unstructured) (chartutil.Values, error)
41+
42+
func (t TranslatorFunc) Translate(u *unstructured.Unstructured) (chartutil.Values, error) {
43+
return t(u)
44+
}

0 commit comments

Comments
 (0)