mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Opt in to use schema
This commit is contained in:
@@ -31,6 +31,8 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace'
|
|||||||
`if true, keep index and filename annotations set on Resources.`)
|
`if true, keep index and filename annotations set on Resources.`)
|
||||||
c.Flags().BoolVar(&r.Override, "override", false,
|
c.Flags().BoolVar(&r.Override, "override", false,
|
||||||
`if true, override existing filepath annotations.`)
|
`if true, override existing filepath annotations.`)
|
||||||
|
c.Flags().BoolVar(&r.UseSchema, "use-schema", false,
|
||||||
|
`if true, uses openapi resource schema to format resources.`)
|
||||||
r.Command = c
|
r.Command = c
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@@ -46,6 +48,7 @@ type FmtRunner struct {
|
|||||||
SetFilenames bool
|
SetFilenames bool
|
||||||
KeepAnnotations bool
|
KeepAnnotations bool
|
||||||
Override bool
|
Override bool
|
||||||
|
UseSchema bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
||||||
@@ -56,7 +59,9 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
||||||
f := []kio.Filter{filters.FormatFilter{}}
|
f := []kio.Filter{filters.FormatFilter{
|
||||||
|
UseSchema: r.UseSchema,
|
||||||
|
}}
|
||||||
|
|
||||||
// format with file names
|
// format with file names
|
||||||
if r.SetFilenames {
|
if r.SetFilenames {
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ func FormatFileOrDirectory(path string) error {
|
|||||||
|
|
||||||
type FormatFilter struct {
|
type FormatFilter struct {
|
||||||
Process func(n *yaml.Node) error
|
Process func(n *yaml.Node) error
|
||||||
|
UseSchema bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ kio.Filter = FormatFilter{}
|
var _ kio.Filter = FormatFilter{}
|
||||||
@@ -78,7 +79,12 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value
|
kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value
|
||||||
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind})
|
var s *openapi.ResourceSchema
|
||||||
|
if f.UseSchema {
|
||||||
|
s = openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind})
|
||||||
|
} else {
|
||||||
|
s = nil
|
||||||
|
}
|
||||||
err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}).
|
err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}).
|
||||||
fmtNode(slice[i].YNode(), "", s)
|
fmtNode(slice[i].YNode(), "", s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -151,7 +157,6 @@ func (f *formatter) fmtNode(n *yaml.Node, path string, schema *openapi.ResourceS
|
|||||||
s = schema.Elements()
|
s = schema.Elements()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// format the node using the schema
|
// format the node using the schema
|
||||||
err := f.fmtNode(n.Content[i], p, s)
|
err := f.fmtNode(n.Content[i], p, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -74,6 +74,73 @@ spec:
|
|||||||
containerPort: 80
|
containerPort: 80
|
||||||
`
|
`
|
||||||
|
|
||||||
|
buff := &bytes.Buffer{}
|
||||||
|
err := kio.Pipeline{
|
||||||
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
|
Filters: []kio.Filter{FormatFilter{
|
||||||
|
UseSchema: true,
|
||||||
|
}},
|
||||||
|
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
|
||||||
|
}.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, buff.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormat_UnsortedInput_No_Schema(t *testing.T) {
|
||||||
|
y := `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.0.0
|
||||||
|
args:
|
||||||
|
- on
|
||||||
|
- 1
|
||||||
|
- hello
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
targetPort: 80
|
||||||
|
containerPort: 80
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
labels:
|
||||||
|
foo: on
|
||||||
|
foo2: hello1
|
||||||
|
annotations:
|
||||||
|
bar: 1
|
||||||
|
bar2: hello2
|
||||||
|
`
|
||||||
|
|
||||||
|
// keep the style on values that parse as non-string types
|
||||||
|
expected := `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: foo
|
||||||
|
labels:
|
||||||
|
foo: on
|
||||||
|
foo2: hello1
|
||||||
|
annotations:
|
||||||
|
bar: 1
|
||||||
|
bar2: hello2
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.0.0
|
||||||
|
args:
|
||||||
|
- on
|
||||||
|
- 1
|
||||||
|
- hello
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
targetPort: 80
|
||||||
|
containerPort: 80
|
||||||
|
`
|
||||||
|
|
||||||
buff := &bytes.Buffer{}
|
buff := &bytes.Buffer{}
|
||||||
err := kio.Pipeline{
|
err := kio.Pipeline{
|
||||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
@@ -120,7 +187,9 @@ spec:
|
|||||||
buff := &bytes.Buffer{}
|
buff := &bytes.Buffer{}
|
||||||
err := kio.Pipeline{
|
err := kio.Pipeline{
|
||||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
|
Filters: []kio.Filter{FormatFilter{
|
||||||
|
UseSchema: true,
|
||||||
|
Process: func(n *yaml.Node) error {
|
||||||
if yaml.IsYaml1_1NonString(n) {
|
if yaml.IsYaml1_1NonString(n) {
|
||||||
// don't change these styles, they are important for backwards compatibility
|
// don't change these styles, they are important for backwards compatibility
|
||||||
// e.g. "on" must remain quoted, on must remain unquoted
|
// e.g. "on" must remain quoted, on must remain unquoted
|
||||||
@@ -186,7 +255,9 @@ spec:
|
|||||||
buff = &bytes.Buffer{}
|
buff = &bytes.Buffer{}
|
||||||
err = kio.Pipeline{
|
err = kio.Pipeline{
|
||||||
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
|
||||||
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
|
Filters: []kio.Filter{FormatFilter{
|
||||||
|
UseSchema: true,
|
||||||
|
Process: func(n *yaml.Node) error {
|
||||||
if yaml.IsYaml1_1NonString(n) {
|
if yaml.IsYaml1_1NonString(n) {
|
||||||
// don't change these styles, they are important for backwards compatibility
|
// don't change these styles, they are important for backwards compatibility
|
||||||
// e.g. "on" must remain quoted, on must remain unquoted
|
// e.g. "on" must remain quoted, on must remain unquoted
|
||||||
|
|||||||
Reference in New Issue
Block a user