Skip to content

Commit b5403ad

Browse files
authored
tag configMap data nodes as 'string' (#3229)
1 parent d8acc54 commit b5403ad

File tree

6 files changed

+48
-36
lines changed

6 files changed

+48
-36
lines changed

internal/fnruntime/runner.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,7 @@ func newFnConfig(fsys filesys.FileSystem, f *kptfilev1.Function, pkgPath types.U
508508
// directly use the config from file
509509
return node, nil
510510
case len(f.ConfigMap) != 0:
511-
node = yaml.NewMapRNode(&f.ConfigMap)
512-
if node == nil {
513-
return nil, nil
514-
}
515-
// create a ConfigMap only for configMap config
516-
configNode := yaml.MustParse(`
517-
apiVersion: v1
518-
kind: ConfigMap
519-
metadata:
520-
name: function-input
521-
data: {}
522-
`)
523-
err := configNode.PipeE(yaml.SetField("data", node))
511+
configNode, err := NewConfigMap(f.ConfigMap)
524512
if err != nil {
525513
return nil, errors.E(op, fn, err)
526514
}

internal/fnruntime/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,30 @@ func annoMatch(node *yaml.RNode, selector kptfilev1.Selector) bool {
221221
}
222222
return true
223223
}
224+
225+
func NewConfigMap(data map[string]string) (*yaml.RNode, error) {
226+
node := yaml.NewMapRNode(&data)
227+
if node == nil {
228+
return nil, nil
229+
}
230+
// create a ConfigMap only for configMap config
231+
configMap := yaml.MustParse(`
232+
apiVersion: v1
233+
kind: ConfigMap
234+
metadata:
235+
name: function-input
236+
data: {}
237+
`)
238+
if err := node.VisitFields(func(node *yaml.MapNode) error {
239+
v := node.Value.YNode()
240+
v.Tag = yaml.NodeTagString
241+
node.Value.SetYNode(v)
242+
return nil
243+
}); err != nil {
244+
return nil, err
245+
}
246+
if err := configMap.PipeE(yaml.SetField("data", node)); err != nil {
247+
return nil, err
248+
}
249+
return configMap, nil
250+
}

internal/fnruntime/utils_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,19 @@ spec:
140140
})
141141
}
142142
}
143+
144+
func TestNewConfigMap(t *testing.T) {
145+
data := map[string]string{
146+
"normal string": "abc",
147+
"integer": "8081",
148+
"float": "1.23",
149+
"bool": "true",
150+
}
151+
m, err := NewConfigMap(data)
152+
assert.NoError(t, err)
153+
mapAsString := m.MustString()
154+
assert.Contains(t, mapAsString, `bool: "true"`)
155+
assert.Contains(t, mapAsString, `normal string: abc`)
156+
assert.Contains(t, mapAsString, `integer: "8081"`)
157+
assert.Contains(t, mapAsString, `float: "1.23"`)
158+
}

porch/pkg/engine/eval.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
"context"
1919
"fmt"
2020

21+
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
2122
v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
2223
"github.com/GoogleContainerTools/kpt/pkg/fn"
2324
api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
24-
"github.com/GoogleContainerTools/kpt/porch/pkg/kpt"
2525
"github.com/GoogleContainerTools/kpt/porch/pkg/repository"
2626
"go.opentelemetry.io/otel/trace"
2727
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
@@ -51,7 +51,7 @@ func (m *evalFunctionMutation) Apply(ctx context.Context, resources repository.P
5151

5252
var functionConfig *yaml.RNode
5353
if m.task.Eval.ConfigMap != nil {
54-
if cm, err := kpt.NewConfigMap(m.task.Eval.ConfigMap); err != nil {
54+
if cm, err := fnruntime.NewConfigMap(m.task.Eval.ConfigMap); err != nil {
5555
return repository.PackageResources{}, nil, fmt.Errorf("failed to create function config: %w", err)
5656
} else {
5757
functionConfig = cm

porch/pkg/kpt/eval.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/GoogleContainerTools/kpt/porch/pkg/kpt/internal"
2424
"sigs.k8s.io/kustomize/kyaml/fn/framework"
2525
"sigs.k8s.io/kustomize/kyaml/kio"
26-
"sigs.k8s.io/kustomize/kyaml/yaml"
2726
)
2827

2928
func NewSimpleFunctionRuntime() FunctionRuntime {
@@ -67,22 +66,3 @@ func (fr *runner) Run(r io.Reader, w io.Writer) error {
6766

6867
return framework.Execute(fr.processor, rw)
6968
}
70-
71-
func NewConfigMap(data map[string]string) (*yaml.RNode, error) {
72-
node := yaml.NewMapRNode(&data)
73-
if node == nil {
74-
return nil, nil
75-
}
76-
// create a ConfigMap only for configMap config
77-
configMap := yaml.MustParse(`
78-
apiVersion: v1
79-
kind: ConfigMap
80-
metadata:
81-
name: function-input
82-
data: {}
83-
`)
84-
if err := configMap.PipeE(yaml.SetField("data", node)); err != nil {
85-
return nil, err
86-
}
87-
return configMap, nil
88-
}

porch/pkg/kpt/eval_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"path"
2121
"testing"
2222

23+
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
2324
v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
2425
"sigs.k8s.io/kustomize/kyaml/kio"
2526
"sigs.k8s.io/kustomize/kyaml/yaml"
@@ -56,7 +57,7 @@ func TestSetLabels(t *testing.T) {
5657
t.Errorf("GetRunner failed: %v", err)
5758
}
5859

59-
config, err := NewConfigMap(map[string]string{
60+
config, err := fnruntime.NewConfigMap(map[string]string{
6061
"label-key": "label-value",
6162
})
6263
if err != nil {

0 commit comments

Comments
 (0)