Skip to content

Commit 676d424

Browse files
Disable unwrapping to read fnconfig (#2582)
* Disable wrapping to read fnconfig * Update index annotation
1 parent ce2a3f4 commit 676d424

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

pkg/api/kptfile/v1/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func GetValidatedFnConfigFromPath(pkgPath types.UniquePath, configPath string) (
162162
if err != nil {
163163
return nil, fmt.Errorf("functionConfig must exist in the current package")
164164
}
165-
reader := kio.ByteReader{Reader: file, PreserveSeqIndent: true, WrapBareSeqNode: true}
165+
reader := kio.ByteReader{Reader: file, PreserveSeqIndent: true, WrapBareSeqNode: true, DisableUnwrapping: true}
166166
nodes, err := reader.Read()
167167
if err != nil {
168168
return nil, fmt.Errorf("failed to read functionConfig %q: %w", configPath, err)

pkg/api/kptfile/v1/validation_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
package v1
1515

1616
import (
17+
"io/ioutil"
18+
"path/filepath"
1719
"testing"
1820

21+
"github.com/GoogleContainerTools/kpt/internal/types"
22+
"github.com/stretchr/testify/assert"
1923
"sigs.k8s.io/kustomize/kyaml/yaml"
2024
)
2125

@@ -397,3 +401,123 @@ kind: Kustomization
397401
})
398402
}
399403
}
404+
405+
func TestGetValidatedFnConfigFromPath(t *testing.T) {
406+
testcases := []struct {
407+
name string
408+
input string
409+
exp string
410+
errMsg string
411+
}{
412+
{
413+
name: "normal resource",
414+
input: `
415+
apiVersion: v1
416+
kind: Service
417+
metadata:
418+
name: myService
419+
spec:
420+
selector:
421+
app: bar
422+
`,
423+
exp: `apiVersion: v1
424+
kind: Service
425+
metadata:
426+
name: myService
427+
annotations:
428+
config.kubernetes.io/index: '0'
429+
internal.config.kubernetes.io/index: '0'
430+
internal.config.kubernetes.io/seqindent: 'compact'
431+
spec:
432+
selector:
433+
app: bar
434+
`,
435+
},
436+
{
437+
name: "multiple resources wrapped in List",
438+
input: `
439+
apiVersion: v1
440+
kind: List
441+
metadata:
442+
name: upsert-multiple-resources-config
443+
items:
444+
- apiVersion: v1
445+
kind: Service
446+
metadata:
447+
name: myService
448+
namespace: mySpace
449+
spec:
450+
selector:
451+
app: bar
452+
- apiVersion: apps/v1
453+
kind: Deployment
454+
metadata:
455+
name: myDeployment2
456+
namespace: mySpace
457+
spec:
458+
replicas: 10
459+
`,
460+
exp: `apiVersion: v1
461+
kind: List
462+
metadata:
463+
name: upsert-multiple-resources-config
464+
annotations:
465+
config.kubernetes.io/index: '0'
466+
internal.config.kubernetes.io/index: '0'
467+
internal.config.kubernetes.io/seqindent: 'compact'
468+
items:
469+
- apiVersion: v1
470+
kind: Service
471+
metadata:
472+
name: myService
473+
namespace: mySpace
474+
spec:
475+
selector:
476+
app: bar
477+
- apiVersion: apps/v1
478+
kind: Deployment
479+
metadata:
480+
name: myDeployment2
481+
namespace: mySpace
482+
spec:
483+
replicas: 10
484+
`,
485+
},
486+
{
487+
name: "error for multiple resources",
488+
input: `
489+
apiVersion: v1
490+
kind: Service
491+
metadata:
492+
name: myService
493+
namespace: mySpace
494+
---
495+
apiVersion: v1
496+
kind: Service
497+
metadata:
498+
name: myService2
499+
namespace: mySpace
500+
`,
501+
errMsg: `functionConfig "f1.yaml" must not contain more than one config, got 2`,
502+
},
503+
}
504+
for _, tc := range testcases {
505+
tc := tc
506+
t.Run(tc.name, func(t *testing.T) {
507+
d, err := ioutil.TempDir("", "")
508+
assert.NoError(t, err)
509+
err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(tc.input), 0700)
510+
assert.NoError(t, err)
511+
got, err := GetValidatedFnConfigFromPath(types.UniquePath(d), "f1.yaml")
512+
if tc.errMsg != "" {
513+
assert.Error(t, err)
514+
assert.Equal(t, tc.errMsg, err.Error())
515+
return
516+
}
517+
assert.NoError(t, err)
518+
actual, err := got.String()
519+
assert.NoError(t, err)
520+
assert.Equal(t, tc.exp, actual)
521+
})
522+
}
523+
}

0 commit comments

Comments
 (0)