Merge pull request #2176 from phanimarupaka/OptInToFormatSchema

Opt in to use schema
This commit is contained in:
Kubernetes Prow Robot
2020-02-04 11:27:27 -08:00
committed by GitHub
3 changed files with 103 additions and 22 deletions

View File

@@ -31,6 +31,8 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace'
`if true, keep index and filename annotations set on Resources.`)
c.Flags().BoolVar(&r.Override, "override", false,
`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
return r
}
@@ -46,6 +48,7 @@ type FmtRunner struct {
SetFilenames bool
KeepAnnotations bool
Override bool
UseSchema bool
}
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 {
f := []kio.Filter{filters.FormatFilter{}}
f := []kio.Filter{filters.FormatFilter{
UseSchema: r.UseSchema,
}}
// format with file names
if r.SetFilenames {

View File

@@ -56,7 +56,8 @@ func FormatFileOrDirectory(path string) error {
}
type FormatFilter struct {
Process func(n *yaml.Node) error
Process func(n *yaml.Node) error
UseSchema bool
}
var _ kio.Filter = FormatFilter{}
@@ -78,7 +79,12 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
continue
}
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}).
fmtNode(slice[i].YNode(), "", s)
if err != nil {
@@ -151,7 +157,6 @@ func (f *formatter) fmtNode(n *yaml.Node, path string, schema *openapi.ResourceS
s = schema.Elements()
}
}
// format the node using the schema
err := f.fmtNode(n.Content[i], p, s)
if err != nil {

View File

@@ -74,6 +74,73 @@ spec:
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{}
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
@@ -120,16 +187,18 @@ spec:
buff := &bytes.Buffer{}
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
Filters: []kio.Filter{FormatFilter{
UseSchema: true,
Process: func(n *yaml.Node) error {
if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
return nil
}
// style does not have semantic meaning
n.Style = 0
return nil
}
// style does not have semantic meaning
n.Style = 0
return nil
}}},
}}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
}.Execute()
assert.NoError(t, err)
@@ -186,16 +255,18 @@ spec:
buff = &bytes.Buffer{}
err = kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error {
if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
Filters: []kio.Filter{FormatFilter{
UseSchema: true,
Process: func(n *yaml.Node) error {
if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
return nil
}
// style does not have semantic meaning
n.Style = yaml.SingleQuotedStyle
return nil
}
// style does not have semantic meaning
n.Style = yaml.SingleQuotedStyle
return nil
}}},
}}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
}.Execute()
assert.NoError(t, err)